Process Group Notes

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.

Controlling Terminal

Process Groups

Example program

#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();
}