11 pid_t parentPID
= getpid();
12 printf("parent pid: %d\n", parentPID
);
13 printf("parent pgid: %d\n", getpgrp());
15 pid_t childPID
= fork();
17 fprintf(stderr
, "fork() failed: %s\n", strerror(errno
));
19 } else if (childPID
== 0) {
22 printf("child pid: %d, pgid: %d\n", childPID
, getpgrp());
24 printf("child setpgid(0, 0)\n");
25 if (setpgid(0, 0) < 0) {
26 fprintf(stderr
, "child: first setpgid() failed: %s\n", strerror(errno
));
29 // printf("child setsid()\n");
30 // if (setsid() < 0) {
31 // fprintf(stderr, "child: setsid() failed: %s\n", strerror(errno));
35 printf("child pgid: %d\n", getpgrp());
37 pid_t grandChildPID
= fork();
38 if (grandChildPID
< 0) {
39 fprintf(stderr
, "fork() 2 failed: %s\n", strerror(errno
));
41 } else if (grandChildPID
== 0) {
43 grandChildPID
= getpid();
44 printf("gchild pid: %d, pgid: %d\n", grandChildPID
, getpgrp());
46 printf("gchild pid: %d, pgid: %d\n", grandChildPID
, getpgrp());
51 printf("child setpgid(0, %d)\n", parentPID
);
52 if (setpgid(0, parentPID
) < 0) {
53 // printf("child setpgid(0, 0)\n");
54 // if (setpgid(0, 0) < 0) {
55 fprintf(stderr
, "child: second setpgid() failed: %s\n", strerror(errno
));
59 printf("child pgid: %d\n", getpgrp());