Parents and Children

Similar documents
OS Lab Tutorial 1. Spawning processes Shared memory

CS240: Programming in C

Lesson 2. process id = 1000 text data i = 5 pid = 1200

System Calls. Library Functions Vs. System Calls. Library Functions Vs. System Calls

Operating systems and concurrency - B03

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Processes. Operating System CS 217. Supports virtual machines. Provides services: User Process. User Process. OS Kernel. Hardware

Lecture 5. Systems Programming: Unix Processes Creation: fork & exec Process Communication: Pipes

Unix-Linux 2. Unix is supposed to leave room in the process table for a superuser process that could be used to kill errant processes.

One of the most profound ideas in computer science Not the same as program or processor

Operating Systems. Lecture 05

UNIX System Calls. Sys Calls versus Library Func

CSci 4061 Introduction to Operating Systems. Processes in C/Unix

Process Creation in UNIX

PROCESS CONTROL BLOCK TWO-STATE MODEL (CONT D)

CSC209H Lecture 6. Dan Zingaro. February 11, 2015

Operating System Structure

COMP 2355 Introduction to Systems Programming

Dept. of Computer Science & Engineering 1 Knowledge & Data Engineering Lab.

System Programming. Process Control III

CSC 1600 Unix Processes. Goals of This Lecture

Unix Processes 1 / 31

Princeton University Computer Science 217: Introduction to Programming Systems. Process Management

fork System-Level Function

Princeton University Computer Science 217: Introduction to Programming Systems. Process Management

Process management 1

Princeton University. Computer Science 217: Introduction to Programming Systems. Process Management

Week 2 Intro to the Shell with Fork, Exec, Wait. Sarah Diesburg Operating Systems CS 3430

CSC209 Fall Karen Reid 1

Process Management 1

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 20

Process Management! Goals of this Lecture!

CS240: Programming in C

Operating Systems. Processes

CPSC 341 OS & Networks. Processes. Dr. Yingwu Zhu

Processes. Today. Next Time. ! Process concept! Process model! Implementing processes! Multiprocessing once again. ! Scheduling processes

CSI 402 Lecture 11 (Unix Discussion on Files continued) 11 1 / 19

Fall 2015 COMP Operating Systems. Lab #3

CS 355 Operating Systems. Keeping Track of Processes. When are processes created? Process States 1/26/18. Processes, Unix Processes and System Calls

Announcement (1) sys.skku.edu is now available

I/O OPERATIONS. UNIX Programming 2014 Fall by Euiseong Seo

Advanced Unix Concepts. Satyajit Rai

Process Management 1

Exceptional Control Flow Part I

I/O OPERATIONS. UNIX Programming 2014 Fall by Euiseong Seo

CS 201. Processes. Gerson Robboy Portland State University

UNIX Processes. by Armin R. Mikler. 1: Introduction

Processes. q Process concept q Process model and implementation q Multiprocessing once again q Next Time: Scheduling

Reading Assignment 4. n Chapter 4 Threads, due 2/7. 1/31/13 CSE325 - Processes 1

Processes. Dr. Yingwu Zhu

518 Lecture Notes Week 3

#include <sys/types.h> #include <sys/wait.h> pid_t wait(int *stat_loc); pid_t waitpid(pid_t pid, int *stat_loc, int options);

CS631 - Advanced Programming in the UNIX Environment. Process Environment, Process Control

CS Operating Systems Lab 3: UNIX Processes

UNIX. Session 2. UNIX processes and forking fork system call exec system call death of process kill fork and I/O using it UNIX.

Windows architecture. user. mode. Env. subsystems. Executive. Device drivers Kernel. kernel. mode HAL. Hardware. Process B. Process C.

Processes. Today. Next Time. Process concept Process model Implementing processes Multiprocessing once again. Scheduling processes

Preview. Process Control. What is process? Process identifier The fork() System Call File Sharing Race Condition. COSC350 System Software, Fall

Processes in linux. What s s a process? process? A dynamically executing instance of a program. David Morgan. David Morgan

This document gives a general overview of the work done by an operating system and gives specific examples from UNIX.

Chapter 3 Processes we will completely ignore threads today

Interacting with Unix

15-213/ Final Exam Notes Sheet Spring 2013!

Maria Hybinette, UGA. ! One easy way to communicate is to use files. ! File descriptors. 3 Maria Hybinette, UGA. ! Simple example: who sort

Q & A (1) Where were string literals stored? Virtual Address. SSE2033: System Software Experiment 2 Spring 2016 Jin-Soo Kim

Kernel and processes

Contents. IPC (Inter-Process Communication) Representation of open files in kernel I/O redirection Anonymous Pipe Named Pipe (FIFO)

Processes. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Concurrency. Stefan D. Bruda. Winter 2018

CSC 252: Computer Organization Spring 2018: Lecture 19

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Information Sciences and Engineering

Process Management! Goals of this Lecture!

Unix Network Programming Chapter 4. Elementary TCP Sockets 광운대학교컴퓨터과학과 정보통신연구실 석사과정안중현

CS 550 Operating Systems Spring Process III

SE350: Operating Systems

CS631 - Advanced Programming in the UNIX Environment

Pipes. Pipes Implement a FIFO. Pipes (cont d) SWE 545. Pipes. A FIFO (First In, First Out) buffer is like a. Pipes are uni-directional

CSC209F Midterm (L0101) Fall 1999 University of Toronto Department of Computer Science

Introduction to Processes

COMP 2400 UNIX Tools

Processes: Introduction. CS 241 February 13, 2012

OPERATING SYSTEMS: Lesson 2: Operating System Services

using namespace std; //Standard classes are in namspace std //Don't add "using namespace" in header files, only in source files

EXPERIMENT NO : M/C Lenovo Think center M700 Ci3,6100,6th Gen. H81, 4GB RAM,500GB HDD

UNIX System Programming. Overview. 1. A UNIX System. 2. Processes (review) 2.1. Context. Pipes/FIFOs

Operating Systems. Lecture 06. System Calls (Exec, Open, Read, Write) Inter-process Communication in Unix/Linux (PIPE), Use of PIPE on command line

Unix Processes. What is a Process?

Introduction to OS Processes in Unix, Linux, and Windows MOS 2.1 Mahmoud El-Gayyar

CS631 - Advanced Programming in the UNIX Environment. Process Environment, Process Control

Programmation Systèmes Cours 2 Process Management

Getting to know you. Anatomy of a Process. Processes. Of Programs and Processes

Assignment 1. Teaching Assistant: Michalis Pachilakis (

Processes & Threads. Today. Next Time. ! Process concept! Process model! Implementing processes! Multiprocessing once again. ! More of the same J

CSCB09: Software Tools and Systems Programming. Bianca Schroeder IC 460

Preview. Interprocess Communication with Pipe. Pipe from the Parent to the child Pipe from the child to the parent FIFO popen() with r Popen() with w

CSCE Operating Systems Interrupts, Exceptions, and Signals. Qiang Zeng, Ph.D. Fall 2018

System Programming. Process Control II

Operating Systems Lab

The Software Stack: From Assembly Language to Machine Code

Lecture files in /home/hwang/cs375/lecture05 on csserver.

Transcription:

1 Process Identifiers Every process apart from the PID also has a PUID and a PGID. There are two types of PUID and PGID: real and effective. The real PUID is always equal to the user running the process and the same for PGID. The effective ID gives us the rights of that user. Also every process has a parent process which has the relevant process and owner IDs. #include <sys/types.h> #include <unistd.h> pid_t getpid(void); pid_t getppid(void); uid_t getuid(void); uid_t geteuid(void); gid_t getgid(void); gid_t getegid(void);

2 Parents and Children In UNIX, the system initially creates three special processes: init (PID no.1), swapper (scheduler) and pagedaemon. init is the parent of all processes. It spawns off all other processes, including the terminal login process. It is a normal user process with superuser privileges. These superuser privileges are changed upon login. Each process is able to spawn off as many child processes as desired using fork().

3 Process Creation #include <sys/types.h> #include <unistd.h> pid_t fork(void); returns 0 in child child PID in parent -1 on error fork() allocates new space for a new process, copies the parent s memory layout in this space and creates the required descriptors for this process to execute. The child process is assigned a new PID. Execution of the child process starts from inside fork(). The return value of fork() differs for the child and for the parent.

4 Process Creation Parent PID 553 fork() PID 553 PID 645 Parent Child

5 Child Inheritance Child process inherits these from the parents process UID, GID (real and effective) Session ID Pointers to same file table entries current terminal SUID and SGID flag current and root directory file mode creation mask environment FD_CLOEXEC flag resource limits signal mask shared memory segments Parent Table Entry Child status offset v-node

Child Inheritance (cont) 6 It is important to note that all files are shared between child and parent. This also applies to standard output, standard input and standard error. Yet obviously closing a file from one process will not close it for the other process. Race conditions become a serious consideration in spawned off processes. Using fork() main() { int pid ; pid = fork(); if (pid == 0) { child stuff } else { parent stuff }

Executing With just spawning, the new process cannot do much. Thus normally after every spawn, the child process loads a new program and executes it.. The exec() functions load an executable into the current process memory space, overwriting the previous contents and execute it.. After an exec() call the following are preserved from the calling process PID and PPID real UID and GID (not effective) terminal file creation mask resource limits signal mask root and current working directory file locks Open files depending on FD_CLOEXEC 7

8 Executing (cont) #include <unistd.h> int execl(const char *pathname, const char *arg0,, 0); int execv(const char *pathname, char *const argv[]); int execle(const char *pathname, const char *arg0,, 0, char *const envp[]); int execve(const char *pathname, char *const argv[], char *const envp[]); int execlp(const char *filename, const char *arg0,, 0); int execvp(const char *filename, char *const argv[]); return 1 on error, no return on success!! The first four formats take a direct path while the last two search for filename in the PATH environment variable. Note that after an exec() the same PID is maintained but all the user process memory layout is overwritten. example : execl( /bin/ls, -l,(char *) 0);

9 Close On Execute The FD_CLOEXEC is set for an open file using fcntl() and if set, the file will be automatically closed after the exec() call. Default is to leave the file open. #include <sys/types.h> #include <unistd.h> #include <fcntl.h> int fcntl(int fd, F_GETFD); return value of flag int fcntl(int fd, F_SETFD, 0 or 1); return 0 if OK, -1 on error

COW and vfork() 10 Normally when a new child process is spawned using a fork(), it is followed by one of the exec() functions. So what is the use of making a copy of the parent s memory when it is going to be overwritten shortly? Thus most implementations of fork() use Copy On Write, where the same memory space is shared until the child or parent process try to write to this area. vfork() on the other hand creates a new child process but still uses up the same address space until a call to an exec() function occurs. This call is expected to occur after the fork(). Also vfork() guarantees that the parent will be suspended until the child calls exec() or _exit();

11 Process Termination When a child process terminates it sends a signal (SIGCHLD) to the parent. The child remains to exist until the parent reaps this signal. Until this happens, the child is a zombie process. If the parent reaps this termination signal, it receives the termination status of the child and the child stops to exist. If the parent terminates before the child, the child s parent becomes the init() process (init inherits the child). If the child terminates with init() as its parent, init() reaps its termination signal immediately, stopping the system from being hogged with zombie processes.

Waiting for Child 12 Termination #include <sys/types.h> #include <sys/wait.h> pid_t wait(int *statloc); return child ID if ok, 1 on error When parent calls wait() it blocks until a child terminates. If a child zombie process exists, wait() returns immediately. Exit status of child is stored inside *statloc together with special status bits WIFEXITED(stat) WEXITSTATUS(stat) WIFSIGNALED(stat) WTERMSIG(stat) WIFSTOPPED(stat) WSTOPSIG(stat) wait() has to be called for each and every child to clean up the system.

Waiting for Child 13 Termination (cont) #include <sys/types.h> #include <sys/wait.h> pid_t waitpid(pid_t pid, int *statloc, int options); return child ID if ok, 1 on error or 0 waitpid() waits for a specific child to terminate if pid > 0. If pid is 1, then it waits for any child process, like wait(). If pid is 0 then it waits for any child with same PGID. Options can be 0 or WNOHANG. With WNOHANG, if no child zombie process exists, waitpid() returns immediately (does not block) with return value of 0.

14 Set User ID The SUID and SGID is another flag in the file access permission. When a user exec() s a process with the SUID flag set, the process seems to be owned by the file owner rather than by the caller of exec(). The system stores three UIDs and GIDs per process: Effective (what is really used) Real Saved real effective saved exec() SUID OFF SUID ON unchanged unchanged unchanged File UID real File UID

Set User ID (cont) 15 The SUID and SGID file flag are set using the S_ISUID and S_ISGID mode field of chmod(). #include <sys/types.h> #include <sys/stat.h> int chmod(const char *pathname, mode_t mode); int fchmod(int fd, mode_t mode); returns 0 if OK, -1 on error A user can change the effective UID and GID between the real and saved values. When the superuser calls setuid(), all three UIDs and and all three GIDs are set. #include <sys/types.h> #include <unistd.h> int setuid(uid_t uid); int setgid(gid_t gid); return 1 on error, 0 when OK

16 Miscellaneous Functions #include <stdlib.h> int system(const char *cmdstring); return value depends on operation requested #include <unistd.h> char *getlogin(void); return NULL on error system() allows the user to execute a command string from within the program. getlogin() returns the name under which the user logged on the system.

17 Exercises Write a process which spawns off a number of children processes keeping a record of each PID. Terminate the parent process only when all children have terminated. What happens to the value of a variable in the parent if a child changes its value after a vfork() but before an exec() call? Try it. Implement a race condition using fork() ed children. Implement your own system() function call assuming the calls given don t contain command line arguments.