简体   繁体   English

无法更改进程的gid

[英]Unable to change gid of a process

I need to change the PGID of my parent process, so I did something like this: 我需要更改父进程的PGID,所以我做了类似这样的事情:

#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. 我得到Operation not permitted错误。

What should I do to avoid this error and change groupid of the process? 我该怎么做才能避免此错误并更改进程的groupid?

The Operation not permitted error message is associated with the EPERM error code, which according to man 2 setpgid (quoting): Operation not permitted错误消息与EPERM错误代码相关联,根据man 2 setpgid (引用):

EPERM 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()). 尝试将进程移动到不同会话中的进程组,或者更改调用进程的其中一个子进程的进程组ID,并且子进程在不同的会话中,或者更改进程组ID会话负责人(setpgid(),setpgrp())。

So, it seems like there are 3 different explanations for the error you receive: 因此,对于您收到的错误,似乎有3种不同的解释:

  1. You're trying to move a process into a process group in a different session. 您正尝试将流程移动到不同会话中的流程组。

  2. You're trying to change the PGID of a child which is in a different session. 您正在尝试更改处于不同会话中的子项的PGID。

  3. You're trying to change the PGID of a session leader. 您正在尝试更改会话负责人的PGID。

Cases 2 and 3 look irrelevant to your problem, so my guess would be that you're in the first case. 案例2和3看起来与你的问题无关,所以我的猜测是你在第一种情况。 If 6654 is randomly selected, it could be that process group 6654 is in a different session. 如果随机选择6654 ,则可能是进程组6654处于不同的会话中。

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. 您可以验证此运行类似$ ps eajx并检查所涉及的每个进程(包括进程组6654)的输出的SID字段。

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. 我启动了一个新进程,在我的终端中pid是2922,这意味着它的进程组ID也是2922。

Then i change you source code if(setpgid(0,6654)==-1) to if(setpgid(0,2922)==-1) , things works. 然后我改变你的源代码if(setpgid(0,6654)==-1) if(setpgid(0,2922)==-1) ,事情有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM