Unix System Calls

Unix System Calls

Computer Systems II Creang and Execung Processes 1 Unix System Calls fork( ) wait( ) exit( ) 2 1 How To Create New Processes? 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 fork() child 3 Process Creaon AHer a fork, both parent and child keep running, and each can fork off other processes. A process tree results. The root of the tree is a special process created by the OS during startup. A process can choose to wait for children to terminate. For example, if C issued a wait() system call, it would block unNl G finished. 4 2 Bootstrapping When a computer is switched on or reset, there must be an iniNal program that gets the system running This is the bootstrap program - IniNalize CPU registers, deVice controllers, memory - Load the OS into memory - Start the OS running OS starts the first process (such as “init”) OS waits for some eVent to occur - Hardware interrupts or soHware interrupts (traps) 5 Fork System Call Current process split into two: parent, child fork() Returns -1 if unsuccessful Returns 0 in the child Stack Stack Returns the child’s idenNfier in the parent ret = 0 Data Data Text ret = xxx Text 6 3 Fork System Call The child process inherits from parent - idenNcal copy of memory - CPU registers - all files that have been opened by the parent Execuon proceeds concurrently with the instrucNon following the fork system call The execuNon context (PCB) for the child process is a copy of the parent’s context at the Nme of the call 7 How fork Works (1) pid = 25 Data Resources Text Stack PCB File ret = fork(); switch(ret) { case -1: perror(“fork”); exit(1); case 0: // I am the child <code for child > exit(0); default: // I am parent ... UNIX <code for parent > wait(0); } 8 4 How fork Works (2) pid = 25 pid = 26 Data Resources Data Text Text Stack Stack PCB File PCB ret = 0 ret = fork(); ret = 26 ret = fork(); switch(ret) switch(ret) { { case -1: case -1: perror(“fork”); perror(“fork”); exit(1); exit(1); case 0: // I am the child case 0: // I am the child <code for child > <code for child > exit(0); exit(0); default: // I am parent ... default: // I am parent ... <code for parent > UNIX <code for parent > wait(0); wait(0); } } 9 How fork Works (3) pid = 25 pid = 26 Data Resources Data Text Text Stack Stack PCB File PCB ret = 0 ret = fork(); ret = 26 ret = fork(); switch(ret) switch(ret) { { case -1: case -1: perror(“fork”); perror(“fork”); exit(1); exit(1); case 0: // I am the child case 0: // I am the child <code for child > <code for child > exit(0); exit(0); default: // I am parent ... default: // I am parent ... <code for parent > UNIX <code for parent > wait(0); wait(0); } } 10 5 How fork Works (4) pid = 25 pid = 26 Data Resources Data Text Text Stack Stack PCB File PCB ret = 0 ret = fork(); ret = 26 ret = fork(); switch(ret) switch(ret) { { case -1: case -1: perror(“fork”); perror(“fork”); exit(1); exit(1); case 0: // I am the child case 0: // I am the child <code for child > <code for child > exit(0); exit(0); default: // I am parent ... default: // I am parent ... <code for parent > UNIX <code for parent > wait(0); wait(0); } } 11 How fork Works (5) pid = 25 pid = 26 Data Resources Data Text Text Stack Stack PCB File PCB ret = 0 ret = fork(); ret = 26 ret = fork(); switch(ret) switch(ret) { { case -1: case -1: perror(“fork”); perror(“fork”); exit(1); exit(1); case 0: // I am the child case 0: // I am the child <code for child > <code for child > exit(0); exit(0); default: // I am parent ... default: // I am parent ... <code for parent > UNIX <code for parent > wait(0); wait(0); } } 12 6 How fork Works (6) pid = 25 Data Resources Text Stack Process Status File ret = fork(); ret = 26 switch(ret) { case -1: perror(“fork”); exit(1); case 0: // I am the child <code for child > exit(0); default: // I am parent ... <code for parent > UNIX wait(0); < … > 13 Orderly Terminaon: exit() To finish execuNon, a child may call exit(number) This system call: - Saves result = argument of exit - Closes all open files, connecNons - Deallocates memory - Checks if parent is aliVe - If parent is aliVe, holds the result Value unNl 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) 14 7 WaiNng for the Child to Finish Parent may want to wait for children to finish - Example: a shell waiNng for operaons to complete WaiNng for any some child to terminate: wait() - Blocks unNl some child terminates - Returns the process ID of the child process - Or returns -1 if no children exist (i.e., already exited) WaiNng for a specific child to terminate: waitpid() - Blocks Nll a child with parNcular process ID terminates #include <sys/types.h> #include <sys/wait.h> pid_t wait(int *status); pid_t waitpid(pid_t pid, int *status, int options); 15 State TransiNon on wait and exit Calls 16 8 Other useful system calls: getpid, getppid getpid returns the idenNfier of the calling process. Example call (pid is an integer): pid = getpid(); getppid returns the idenNfier of the parent. Note: - Zombies can be noNced by running the 'ps' command (shows the process list); you will see the string "<defunct>" as their command name: ps -ef ps –ef | grep mdamian 17 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); } } 18 9 Fork Example 2 key Points - Both parent and child can conNnue forking void fork2() { printf("L0\n"); Bye fork(); L1 Bye printf("L1\n"); fork(); Bye printf("Bye\n"); L0 L1 Bye } 19 Fork Example 3 void fork3() Bye { L2 Bye printf("L0\n"); fork(); Bye printf("L1\n"); L1 L2 Bye fork(); Bye printf("L2\n"); L2 Bye fork(); printf("Bye\n"); Bye } L0 L1 L2 Bye 20 10 Fork Example 4 void fork4() { printf("L0\n"); if (fork() != 0) { printf("L1\n"); if (fork() != 0) { printf("L2\n"); fork(); } } printf("Bye\n"); } 21 Fork Example 5 void fork5() { printf("L0\n"); if (fork() == 0) { printf("L1\n"); if (fork() == 0) { printf("L2\n"); fork(); } } printf("Bye\n"); } 22 11 Summary Fork - Creates a duplicate of the calling process - The result is two processes: parent and child - Both conNnue execuNng from the same point on Exit - Orderly program terminaon - Unblocks waiNng parent Wait - Used by parent - Waits for child to finish execuNon 23 12 .

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    12 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us