Reputation: 69
I need to change the PGID of my parent process, so I did something like this:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define KIDS 10
int main()
{
struct timespec a = {10, 0};
int pid;
int* pids = (int*) calloc(KIDS, sizeof(int));
int argument = 0;
int procNumber;
for (procNumber = 0; procNumber < KIDS; procNumber++) {
pid = fork();
argument = procNumber;
if (pid == 0)
break;
pids[procNumber] = pid;
}
if (pid == 0) {
// child stuff
} else {
printf("My group: %d\n", getpgrp());
if (setpgid(0, 6654) == -1)
perror("setpgid() error:");
printf("My new group: %d\n", getpgrp());
}
nanosleep(&a,NULL);
free(pids);
return 0;
}
And I get Operation not permitted
error.
What should I do to avoid this error and change groupid of the process?
Upvotes: 1
Views: 2682
Reputation: 4215
The Operation not permitted
error message is associated with the EPERM
error code, which according to man 2 setpgid
(quoting):
EPERM
An attempt was made to move a process into a process group in a different session, or to change the process group ID of one of the children of the calling process and the child was in a different session, or to change the process group ID of a session leader (setpgid(), setpgrp()).
So, it seems like there are 3 different explanations for the error you receive:
You're trying to move a process into a process group in a different session.
You're trying to change the PGID of a child which is in a different session.
You're trying to change the PGID of a session leader.
Cases 2 and 3 look irrelevant to your problem, so my guess would be that you're in the first case. If 6654
is randomly selected, it could be that process group 6654 is in a different session.
You can verify this running something like $ ps eajx
and checking the SID
field of the output for each of the processes involved, including the process group 6654.
Upvotes: 2
Reputation: 681
if(setpgid(0,6654)==-1)
perror("setpgid() error:");
I think the error means " An attempt was made to move a process into a process group in a different session"
So:
[root@localhost test_c]# sleep 10000 &
[2] 2922
I start a new process which pid is 2922 in my terminal,which means it's process group id also be 2922.
Then i change you source code if(setpgid(0,6654)==-1)
to if(setpgid(0,2922)==-1)
, things works.
Upvotes: 1