And child can conjnue forking void fork3 printfl0n

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: d used to synchronize parent/child execuJon and to reap child process Processes 37 University of Washington Summary ¢  Processes §  At any given :me, system has mul:ple ac:ve processes §  Only one can execute at a :me, but each process appears to have total control of the processor §  OS periodically “context switches” between ac:ve processes §  Implemented using excep/onal control flow ¢  Process management §  fork-­‐exec model Processes 38 University of Washington AddiJonal examples Processes 39 University of Washington Fork Example #2 ¢  Both parent and child can conJnue forking void fork2() { printf("L0\n"); fork(); printf("L1\n"); fork(); printf("Bye\n"); } L1 L0 Processes Bye Bye L1 Bye Bye 40 University of Washington Fork Example #3 ¢  Both parent and child can conJnue forking void fork3() { printf("L0\n"); fork(); printf("L1\n"); fork(); printf("L2\n"); fork(); printf("Bye\n"); } L2 Bye Bye L2 L1 L0 Processes L1 Bye Bye Bye Bye L2 Bye Bye L2 41 University of Washington Fork Example #4 ¢  Both parent and child can conJnue forking void fork4() { printf("L0\n"); if (fork() != 0) { printf("L1\n"); if (fork() != 0) { printf("L2\n"); fork(); } } printf("Bye\n"); } Processes Bye Bye L0 L1 L2 Bye Bye 42 University of Washington Fork Example #5 ¢  Both parent and child can conJnue forking void fork5() { printf("L0\n"); if (fork() == 0) { printf("L1\n"); if (fork() == 0) { printf("L2\n"); fork(); } } printf("Bye\n"); } Processes Bye L2 L1 L0 Bye Bye Bye 43 University of Washington Zombie Example void fork7() { if (fork() == 0) { /* Child */ printf("Terminating Child, PID = %d\n", getpid()); exit(0); } else { printf("Running Parent, PID = %d\n", linux> ./forks 7 & getpid()); while (1) [1] 6639 ; /* Infinite loop */ Running Parent, PID = 6639 } Terminating Child, PID = 6640 } linux> ps PID TTY TIME 6585 ttyp9 00:00:00 6639 ttyp9 00:00:03 6640 ttyp9 00:00:00 6641 ttyp9 00:00:00 linux> kill 6639 [1] Terminated linux> ps PID TTY TIME 6585 ttyp9 00:00:00 6642 ttyp9 00:00:00 CMD tcsh forks forks <defunct> ps ¢  ¢  CMD tcsh ps Processes ps shows child process as “defunct” Killing parent allows child to be reaped by init 44 University of Washington Non-­‐terminaJng Child Example void fork8() { if (fork() == 0) { /* Child */ printf("Running Child, PID = %d\n", getpid()); while (1) ; /* Infinite loop */ } else { printf("Terminating Parent, PID = %d\n", getpid()); exit(0); } } linux> ./forks 8 Terminating Parent, PID = 6675 Running Child, PID = 6676 linux> ps PID TTY TIME CMD 6585 ttyp9 00:00:00 tcsh 6676 ttyp9 00:00:06 forks 6677 ttyp9 00:00:00 ps linux> kill 6676 linux> ps PID TTY TIME CMD 6585 ttyp9 00:00:00 tcsh 6678 ttyp9 00:00:00 ps ¢  ¢  Child process s:ll ac:ve even though parent has terminated Must kill explicitly, or else will keep running indefinitely Processes 45 University of Washington wait() Example ¢  ¢  If mul:ple children completed, will take in arbitrary order Can use macros WIFEXITED and WEXITSTATUS to get informa:on about exit status void fork10() { pid_t pid[N]; int i; int child_status; for (i = 0; i < N; i++) if ((pid[i] = fork()) == 0) exit(100+i); /* Child */ for (i = 0; i < N; i++) { pid_t wpid = wait(&child_status); if (WIFEXITED(child_status)) printf("Child %d terminated with exit status %d\n", wpid, WEXITSTATUS(child_status)); else printf("Child %d terminated abnormally\n", wpid); } } Processes 46 University of Washington waitpid(): WaiJng for a Specific Process ¢  waitpid(pid, &status, options) §  suspends current process un:l specific process terminates §  various op:ons (that we won’t talk about) void fork11() { pid_t pid[N]; int i; int child_status; for (i = 0; i < N; i++) if ((pid[i] = fork()) == 0) exit(100+i); /* Child */ for (i = 0; i < N; i++) { pid_t wpid = waitpid(pid[i], &child_status, 0); if (WIFEXITED(child_status)) printf("Child %d terminated with exit status %d\n", wpid, WEXITSTATUS(child_status)); else printf("Child %d terminated abnormally\n", wpid); } Processes 47...
View Full Document

This document was uploaded on 04/04/2014.

Ask a homework question - tutors are online