I developed a function for setpgid (), when I execute this function, the result will be rejected. then I logged in as the root user, and this time will also display an error message with permission. which user can use this function. Can someone explain to me?
#include<stdio.h> #include<unistd.h> #include<stdlib.h> main() { printf("parent pid=%d\tpgid=%d\n",getpid(),getpgid(getpid())); pid_t pid,pgid; pgid=getpgid(getpid()); if((pid=fork())==0) { printf("befor sessionchild pid=%d\tpgid=%d\n",getpid(),getpgid(getpid())); sleep(5); pid_t p; printf("child pid=%d\tpgid=%d\n",getpid(),getpgid(getpid())); if((p=fork())==0){ sleep(2); setsid(); printf("child2 pid=%d\tpgid=%d\n",getpid(),getpgid(getpid())); setpgid(getpid(),pgid); perror("Error"); printf("after setting group id child2 pid=%d\tpgid=%d\n",getpid(),getpgid(getpid())); } wait(0); exit(0); } exit(0); }
source share