1 Unix system calls fork( ) wait( ) exit( ). 2 How To Create New Processes? n Underlying mechanism -A process runs fork to create a child process -Parent.

Slides:



Advertisements
Similar presentations
1 CS345 Operating Systems Φροντιστήριο Άσκησης 1.
Advertisements

Processes Topics Process context switches Creating and destroying processes CS 105 “Tour of the Black Holes of Computing!”
Process Control Hua LiSystems ProgrammingCS2690Process Control Page 1 of 41.
UNIX Process Control Bach 7 Operating Systems Course Hebrew University Spring 2007.
1 Processes Professor Jennifer Rexford
1 Processes and Pipes COS 217 Professor Jennifer Rexford.
CS 311 – Lecture 14 Outline Process management system calls Introduction System calls  fork()  getpid()  getppid()  wait()  exit() Orphan process.
1 Processes and Pipes. 2 "He was below me. I saw his markings, manoeuvred myself behind him and shot him down. If I had known it was Saint-Exupery, I.
Process in Unix, Linux and Windows CS-3013 C-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
CS-502 Fall 2006Processes in Unix, Linux, & Windows 1 Processes in Unix, Linux, and Windows CS502 Operating Systems.
CSSE Operating Systems
Unix & Windows Processes 1 CS502 Spring 2006 Unix/Windows Processes.
Fork and Exec Unix Model Tutorial 3. Process Management Model The Unix process management model is split into two distinct operations : 1. The creation.
University of Pennsylvania 9/12/00CSE 3801 Multiprogramming CSE 380 Lecture Note 3.
Processes in Unix, Linux, and Windows CS-502 Fall Processes in Unix, Linux, and Windows CS502 Operating Systems (Slides include materials from Operating.
Process. Process Concept Process – a program in execution Textbook uses the terms job and process almost interchangeably A process includes: – program.
Process Control. Major Requirements of an Operating System Interleave the execution of several processes to maximize processor utilization while providing.
Unix Processes Slides are based upon IBM technical library, Speaking Unix, Part 8: Unix processes Extended System Programming Laboratory (ESPL) CS Department.
Shell (Part 1). Process r A process is an instance of an application running r If there are two instances of an application running then there are two.
Process in Unix, Linux, and Windows CS-3013 A-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
Today’s Topics Introducing process: the basic mechanism for concurrent programming –Process management related system calls Process creation Process termination.
Process. Processes A process is an abstraction for sequence of operations that implement a computation/program. A process may be manipulated, suspended,
Operating Systems Chapter 2
1 Week 2 The Crunchy Shell to the Soft and Chewy Kernel… Sarah Diesburg 8/3/2010 COP4610 / CGS5765.
Creating and Executing Processes
CS 241 Section Week #2 9/9/10. 2 Topics This Section MP1 issues MP2 overview Process creation using fork()‏ Debugging tools: valgrind, gdb.
Process Control Process identifiers Process creation fork and vfork wait and waitpid Race conditions exec functions system function.
System calls for Process management
Operating Systems Processes 1.
Concurrent Processes Processes can concurrently run same program. Processes can concurrently run same program. Processes can start other processes. Processes.
Process Management CS3320 Spring Process A process is an instance of a running program. –Not the same as “program” or “processor” Process provides.
Operating Systems Process Creation
CS4315A. Berrached:CMS:UHD1 Process Management Chapter 6.
What is a Process? u A process is an executable “cradle” in which a program may run u This “cradle” provides an environment in which the program can run,
CSCI 330 UNIX and Network Programming Unit XII: Process & Pipe Part 1.
Process Management Azzam Mourad COEN 346.
1 A Seven-State Process Model. 2 CPU Switch From Process to Process Silberschatz, Galvin, and Gagne  1999.
Tutorial 3. In this tutorial we’ll see Fork() and Exec() system calls.
System calls for Process management Process creation, termination, waiting.
1 Exceptional Control Flow Ⅱ. 2 Outline Kernel Mode and User Mode Process context switches Three States of Processes Context Switch System Calls and Error.
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
Slide 1 COMP 3438 System Programming UNIX Processes UNIX Processes (Chapters 2 & 3)
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
4.1 Operating Systems Lecture 9 Fork and Exec Read Ch
Process Related System Calls By Neha Hulkoti & Kavya Bhat.
1 Intro to the Shell with Fork, Exec, Wait Sarah Diesburg Operating Systems CS 3430.
A process is a program in execution A running system consists of multiple processes – OS processes Processes started by the OS to do “system things” –
CSCI 4061 Recitation 2 1.
Section 8: Processes What is a process Creating processes Fork-Exec
Unix Process Management
Chapter 3: Processes.
Processes in Unix, Linux, and Windows
Processes in Unix, Linux, and Windows
Lecture 5: Process Creation
More on UART Interrupts; System Calls
Fork and Exec Unix Model
Processes in Unix, Linux, and Windows
Process Creation Process Termination
Tutorial 3 Tutorial 3.
Process Programming Interface
Processes Prof. Ikjun Yeom TA – Mugyo
CS 105 “Tour of the Black Holes of Computing!”
CS 105 “Tour of the Black Holes of Computing!”
Lecture 6: Multiprogramming and Context Switching
Processes CSE 351 Autumn 2018 Guest Instructor: Teaching Assistants:
Processes in Unix, Linux, and Windows
Processes in Unix and Windows
EECE.4810/EECE.5730 Operating Systems
EECE.4810/EECE.5730 Operating Systems
Presentation transcript:

1 Unix system calls fork( ) wait( ) exit( )

2 How To Create New Processes? n Underlying mechanism -A process runs fork to create a child process -Parent and children execute concurrently -Child process is a duplicate of the parent process parent child fork()

3 n After a fork, both parent and child keep running, and each can fork off other processes. n A process tree results. The root of the tree is a special process created by the OS during startup. Process Creation n A process can choose to wait for children to terminate. For example, if C issued a wait() system call, it would block until G finished.

4 Fork System Call n Current process split into 2 processes: parent, child Text Data Stack Text Data Stack ret = xxx ret = 0 fork() n Returns -1 if unsuccessful n Returns 0 in the child n Returns the child’s identifier in the parent

5 Fork System Call n The child process inherits from parent -identical copy of memory -CPU registers -all files that have been opened by the parent n Execution proceeds concurrently with the instruction following the fork system call n The execution context (PCB) for the child process is a copy of the parent’s context at the time of the call

6 How fork Works (1) Text Stack Data File Resources pid = 25 ret = fork(); switch(ret) { case -1: perror(“fork”); exit(1); case 0: // I am the child exit(0); default: // I am parent... wait(0); } PCB UNIX

7 How fork Works (2) Text PCB Stack Data File Resources pid = 25 ret = fork(); switch(ret) { case -1: perror(“fork”); exit(1); case 0: // I am the child exit(0); default: // I am parent... wait(0); } UNIX ret = 26 Text PCB Stack Data pid = 26 ret = fork(); switch(ret) { case -1: perror(“fork”); exit(1); case 0: // I am the child exit(0); default: // I am parent... wait(0); } ret = 0

8 How fork Works (3) Text PCB Stack Data File Resources pid = 25 Text PCB Stack Data pid = 26 ret = fork(); switch(ret) { case -1: perror(“fork”); exit(1); case 0: // I am the child exit(0); default: // I am parent... wait(0); } ret = fork(); switch(ret) { case -1: perror(“fork”); exit(1); case 0: // I am the child exit(0); default: // I am parent... wait(0); } UNIX ret = 26 ret = 0

9 How fork Works (4) Text PCB Stack Data File Resources pid = 25 Text PCB Stack Data pid = 26 ret = fork(); switch(ret) { case -1: perror(“fork”); exit(1); case 0: // I am the child exit(0); default: // I am parent... wait(0); } ret = fork(); switch(ret) { case -1: perror(“fork”); exit(1); case 0: // I am the child exit(0); default: // I am parent... wait(0); } UNIX ret = 26 ret = 0

10 ret = fork(); switch(ret) { case -1: perror(“fork”); exit(1); case 0: // I am the child exit(0); default: // I am parent... wait(0); } How fork Works (5) Text PCB Stack Data File Resources pid = 25 Text PCB Stack Data pid = 26 ret = fork(); switch(ret) { case -1: perror(“fork”); exit(1); case 0: // I am the child exit(0); default: // I am parent... wait(0); } UNIX ret = 26 ret = 0

11 How fork Works (6) Text Process Status Stack Data File Resources pid = 25 ret = fork(); switch(ret) { case -1: perror(“fork”); exit(1); case 0: // I am the child exit(0); default: // I am parent... wait(0); ret = 26 UNIX

12 Orderly Termination: exit() n To finish execution, a child may call exit(number) n This system call: -Saves result = argument of exit -Closes all open files, connections -Deallocates memory -Checks if parent is alive -If parent is alive, holds the result value until the parent requests it (with wait); in this case, the child process does not really die, but it enters a zombie/defunct state -If parent is not alive, the child terminates (dies)

13 Waiting for the Child to Finish n Parent may want to wait for children to finish -Example: a shell waiting for operations to complete n Waiting for any some child to terminate: wait() -Blocks until some child terminates -Returns the process ID of the child process -Or returns -1 if no children exist (i.e., already exited) n Waiting for a specific child to terminate: waitpid() -Blocks till a child with particular process ID terminates #include pid_t wait(int *status); pid_t waitpid(pid_t pid, int *status, int options);

14 State Transition on wait and exit Calls

15 Other useful system calls: getpid, getppid n getpid returns the identifier of the calling process. Example call (pid is an integer): pid = getpid(); n getppid returns the identifier of the parent. n Note: ps -ef

16 Fork Example 1: What Output? int main() { pid_t pid; int x = 1; pid = fork(); if (pid != 0) { printf(“parent: x = %d\n”, --x); exit(0); } else { printf(“child: x = %d\n”, ++x); exit(0); }

17 Fork Example 2 n Key Points -Both parent and child can continue forking -No of process = 2 n (n – no of forks) void fork2() { printf("L0\n"); fork(); printf("L1\n"); fork(); printf("Bye\n"); } L0 L1 Bye

18 Fork Example 3 void fork3() { printf("L0\n"); fork(); printf("L1\n"); fork(); printf("L2\n"); fork(); printf("Bye\n"); } L1L2 Bye L1L2 Bye L0

19 Fork Example 4 void fork4() { printf("L0\n"); pid1=fork(); if (pid1!=0) { printf("L1\n"); pid2=fork(); if (pid2!= 0) { printf("L2\n"); fork(); } printf("Bye\n"); }

20 Fork Example 5 void fork5() { printf("L0\n"); if (fork() == 0) { printf("L1\n"); if (fork() == 0) { printf("L2\n"); fork(); } printf("Bye\n"); }

21 Summary n Fork -Creates a duplicate of the calling process -The result is two processes: parent and child -Both continue executing from the same point on n Exit -Orderly program termination -Unblocks waiting parent n Wait -Used by parent -Waits for child to finish execution

22 Unix system calls execv execl

23 Unix’s execv The system call execv executes a file, transforming the calling process into a new process. After a successful execv, there is no return to the calling process. execv(const char * path, char * const argv[])  path is the full path for the file to be executed  argv is the array of arguments for the program to execute  each argument is a null-terminated string  the first argument is the name of the program  the last entry in argv is NULL

24 How execv Works (1) UNIX kernel Text PCB Stack Data File Resources char * argv[ ] = {“/bin/ls”, 0}; int cpid = fork( ); if (cpid = = 0) { execv(argv[0], argv); exit(0); } wait(&cpid); pid = 25 Text PCB Stack Data pid = 26 cpid = 0cpid = 26 char * argv[ ] = {“/bin/ls”, 0}; int cpid = fork( ); if (cpid = = 0) { execv(argv[0], argv); exit(0); } wait(&cpid); /bin/ls

25 How execv Works (2) UNIX kernel Text PCB Stack Data File Resources char * argv[ ] = {“/bin/ls”, 0}; int cpid = fork( ); if (cpid = = 0) { execv(argv[0], argv); exit(0); } wait(&cpid); pid = 25pid = 26 cpid = 26 /bin/ls Exec destroys the process image of the calling process. A new process image is constructed from the executable file (ls).

26 How execv Works (3) UNIX kernel Text PCB Stack Data File Resources pid = 25pid = 26 /bin/ls exit(0); Text PCB Stack Data char * argv[ ] = {“/bin/ls”, 0}; int cpid = fork( ); if (cpid = = 0) { execv(argv[0], argv); exit(0); } wait(&cpid); cpid = 26

27 Example: A Simple Shell n Shell is the parent process -E.g., bash n Parses command line -E.g., “ls –l” n Invokes child process -Fork, execvp n Waits for child -Wait fork ls waitexecvp bash

28 execv Example #include char * argv[] = {“/bin/ls”, “-l”, 0}; int main() { int pid, status; if ( (pid = fork() ) < 0 ) { printf(“Fork error \n”); exit(1); } if(pid == 0) { /* Child executes here */ execv(argv[0], argv); printf(“Exec error \n”); exit(1); } else /* Parent executes here */ wait(&status); printf("Hello there! \n”); return 0; } Note the NULL string at the end

29 execv Example – Sample Output n Sample output: total 282 drwxr-xr-x 2 mdamian faculty 512 Jan 29 14:02 assignments -rw-r--r-- 1 mdamian faculty 3404 Jan 29 14:05 index.html drwxr-xr-x 2 mdamian faculty 512 Jan 28 15:02 notes Hello there!

30 execl n Same as execv, but takes the arguments of the new program as a list, not a vector: n Example: execl(“/bin/ls”, “/bin/ls”, “-l”, 0); n Is equivalent to char * argv[] = {“/bin/ls”, “-l”, 0}; execv(argv[0], argv); n execl is mainly used when the number of arguments is known in advance Note the NULL string at the end

31 General purpose process creation n In the parent process: int childPid; char * const argv[ ] = {…}; main { childPid = fork(); if(childPid == 0) { // I am child... // Do some cleaning, close files execv(argv[0], argv); } else { // I am parent... wait(0); }

32 Combined fork/exec/wait n Common combination of operations -Fork to create a new child process -Exec to invoke new program in child process -Wait in the parent process for the child to complete n Single call that combines all three -int system(const char *cmd); n Example int main() { system(“echo Hello world”); }