c - Which user can use setpgid() function? -
i worked out function setpgid() when executing function result permission denied. logged in root user time print error message permission denied. user can use function. can explain 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); }
first of all, need check return value of function calls before call perror()
, otherwise don't know of calls failed - might not been recent call before perror()
statement failed. code should like:
if (setpgid(getpid(),pgid) != 0) { perror("setpgid"); }
if setpgid() fails, here's docs says:
eperm attempt made move process process group in different session, or change process group id of 1 of children of calling process , child in different session, or change process group id of session leader (setpgid(), setpgrp()).
so sounds hitting first case described since child process calls setsid()
.
the glibc docs job control has bit of reading material on subject.
Comments
Post a Comment