This contains links to information used in the lectures. Note that the code segments have less than and greater than replaced by < and > so that they display correctly.
#include <stdio.h> #include <unistd.h> #include <errno.h> void brag(void) { pid_t mypid,myppid,mypgid,myppgid; mypid = getpid(); mypgid = getpgrp(); myppid = getppid(); myppgid = getpgrp2(myppid); printf ("My process id is %d\n",mypid); printf ("My process group is %d\n",mypgid); printf ("my parent is %d\n",myppid); printf ("and the process group is %d \n",myppgid); if (myppid == mypgid) { printf(" My parent is my process leader, now\n"); } else { printf(" My parent is not my process leader, bummer!\n"); } } main (int argc, char * argv[]) { pid_t pid; pid_t mypid,mypgid; brag(); if (!setsid()) { perror(""); } mypid = getpid(); mypgid = getpgrp(); if (mypid == mypgid) { printf ("I set my group id equal to my process id %d\n",mypid); printf ("WOW that means I'm a process leader now \n"); } if (!(pid = fork())) { int i=0; brag(); /* This is an infinite loop so the program hangs about long enough for to do a ps -ef and see the process chain */ while (1) { i++; } } printf (" A child called %d was spawned \n",pid); wait(); }
farrell 2157 1 0 03:43:02 ? 0:02 xterm -ls -T Condor -n Condor -T Condor farrell 2158 2157 0 03:43:02 ttyp1 0:00 -cshNote that the xterm (pid 2157) is the parent of the csh (pid 2158) and that init (pid 1) is the parent of the xterm.
#at this stage brag is run as a function from play #The parent is the csh (pid 2158) #The csh sets the process group of play to play's process id (2570) #since it is a separate "shell job" #Hence it is different from that of the parent (2158) My process id is 2570 My process group is 2570 my parent is 2158 and the process group is 2158 My parent is not my process leader, bummer! # I set my group id equal to my process id 2570 #Actually this has no effect since it already is WOW that means I'm a process leader now #Now fork a child (pid 2571) and have the child run brag as a function My process id is 2571 My process group is 2570 my parent is 2570 and the process group is 2570 #Note that the csh has set the process group of the spawned process #to that of the parent My parent is my process leader, now #The parent prints the pid of the spawned process #Note this was returned by fork() A child called 2571 was spawned
farrell 2157 1 0 03:43:02 ? 0:02 xterm -ls -T Condor -n Condor -T Condor farrell 2158 2157 1 03:43:02 ttyp1 0:00 -csh farrell 2570 2158 0 05:30:00 ttyp1 0:00 play farrell 2571 2570 10 05:30:00 ttyp1 11:06 play
farrell 2157 1 1 03:43:02 ? 0:04 xterm -ls -T Condor -n Condor -T Condor farrell 2158 2157 0 03:43:02 ttyp1 0:00 -csh farrell 2634 2158 0 05:52:39 ttyp1 0:00 sh
My process id is 2650 My process group is 2634 my parent is 2634 and the process group is 2634 My parent is my process leader, now #Notice that the sh which is the parent is the process leader #since the Bourne shell does not change the process group I set my group id equal to my process id 2650 #This changed the process leader WOW that means I'm a process leader now My process id is 2651 My process group is 2650 my parent is 2650 and the process group is 2650 My parent is my process leader, now #The parent prints the child's pid A child called 2651 was spawned
farrell 2157 1 0 03:43:02 ? 0:04 xterm -ls -T Condor -n Condor -T Condor farrell 2158 2157 0 03:43:02 ttyp1 0:00 -csh farrell 2634 2158 0 05:52:39 ttyp1 0:00 sh farrell 2650 2634 0 05:56:06 ? 0:00 play farrell 2651 2650 -1 05:56:06 ? 4:57 play