Dear Members,
The following code creates a child process and sets it process group id to the same process group id of its parent.
Then, every 1 sec, the child and the parent prints their process group id.
For some reason, on the first print, the process group id is as specified but from this point, the process group id changes to 1.
Can you help ?
Thank you in advance,
Z.V
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
main()
{
char str_id[16];
int rc;
if (fork ()==0)
{
//This is the child process
rc=setpgid (getpid(),getppid());
if (rc==-1)
printf ("child: setpgid faild\n");
strcpy (str_id,"child");
}
else
{
//This is the parent process
rc=setpgid (getpid(),getpid());
if (rc==-1)
printf ("parent: setpgid faild\n");
strcpy (str_id,"parent");
}
while (1)
{
printf ("from process:%s %d\n",str_id,getpgid());
sleep (1);
}
}