关于setpgid函数的小知识点

我想在主进程里面调用setpgid()来将第一个子进程的进程组ID设置为第二个进程的进程ID,想知道这样会不会自动创建一个新的进程组,第二个子进程作为进程组组长。
代码如下
 

#include 

#include 
#include 

int main() {
    pid_t pid1,pid2,pid3;
    pid1 = fork();
    if(pid1 == 0){
        printf("I'm the first child process,\n");
        printf("before set pgrpID,\n");
        printf("My processID is %d,my parent processID is %d,my process groupID is %d\n\n",getpid(),getppid(),getpgrp());
        sleep(5);

        printf("[1]after set pgrpID,\n");
        printf("[1]My processID is %d,my parent processID is %d,my process groupID is %d\n\n",getpid(),getppid(),getpgrp());

        //child 1
    }
    else{
        pid2 = fork();
        if(pid2 == 0){
            sleep(1);
            printf("I'm the second child process,\n");
            printf("My processID is %d,my parent processID is %d,my process groupID is %d\n\n",getpid(),getppid(),getpgrp());
            //child2
        }
        else{
            pid3 = fork();
            if(pid3 == 0){
                sleep(2);
                printf("I'm the third child process,\n");
                printf("My processID is %d,my parent processID is %d,my process groupID is %d\n\n",getpid(),getppid(),getpgrp());
                //child3
            }
            else{
                sleep(3);
                printf("I'm the main process.\n");
                printf("My processID is %d\n",getpid());
                printf("pid1: %d,pid2: %d\n",pid1,pid2);
                if(setpgid(pid1,pid2))
                    printf("set failed.\n");
                sleep(8);
                printf("The main process going to shut down.\n\n");
            }

        }
    }
    return 0;
}

输出

/home/gilbert/CLionProjects/C_project/mutex/mutex_lesson1/cmake-build-debug/mutex_lesson1
I'm the first child process,
before set pgrpID,
My processID is 8146,my parent processID is 8143,my process groupID is 8143

I'm the second child process,
My processID is 8147,my parent processID is 8143,my process groupID is 8143

I'm the third child process,
My processID is 8148,my parent processID is 8143,my process groupID is 8143

I'm the main process.
My processID is 8143
pid1: 8146,pid2: 8147
set failed.
[1]after set pgrpID,
[1]My processID is 8146,my parent processID is 8143,my process groupID is 8143

The main process going to shut down.


Process finished with exit code 0

 

该函数的原型为

#include

int setpgid(pid_t pid,pid_t pgid);

返回值,若成功,返回0,若失败,返回-1;

上面的调用失败,原因是进程组组ID等于第二个子进程之进程ID的进程组不存在。

若是修改一下代码

#include 

#include 
#include 

int main() {
    pid_t pid1,pid2,pid3;
    pid1 = fork();
    if(pid1 == 0){
        printf("I'm the first child process,\n");
        printf("before set pgrpID,\n");
        printf("My processID is %d,my parent processID is %d,my process groupID is %d\n\n",getpid(),getppid(),getpgrp());
        sleep(5);

        printf("[1]after set pgrpID,\n");
        printf("[1]My processID is %d,my parent processID is %d,my process groupID is %d\n\n",getpid(),getppid(),getpgrp());

        //child 1
    }
    else{
        pid2 = fork();
        if(pid2 == 0){
            sleep(1);
            printf("I'm the second child process,\n");
            printf("My processID is %d,my parent processID is %d,my process groupID is %d\n\n",getpid(),getppid(),getpgrp());
            setpgid(0,getpid());
            printf("[2]after set pgrpID,\n");
            printf("[2]My processID is %d,my parent processID is %d,my process groupID is %d\n\n",getpid(),getppid(),getpgrp());

            //child2
        }
        else{
            pid3 = fork();
            if(pid3 == 0){
                sleep(2);
                printf("I'm the third child process,\n");
                printf("My processID is %d,my parent processID is %d,my process groupID is %d\n\n",getpid(),getppid(),getpgrp());
                //child3
            }
            else{
                sleep(3);
                printf("I'm the main process.\n");
                printf("My processID is %d\n",getpid());
                printf("pid1: %d,pid2: %d\n",pid1,pid2);
                if(setpgid(pid1,pid2))
                    printf("set failed.\n");
                sleep(8);
                printf("The main process going to shut down.\n\n");
            }

        }
    }

    return 0;
}


就可以设置了。

输出:

/home/gilbert/CLionProjects/C_project/mutex/mutex_lesson1/cmake-build-debug/mutex_lesson1
I'm the first child process,
before set pgrpID,
My processID is 10183,my parent processID is 10182,my process groupID is 10182

I'm the second child process,
My processID is 10184,my parent processID is 10182,my process groupID is 10182

[2]after set pgrpID,
[2]My processID is 10184,my parent processID is 10182,my process groupID is 10184

I'm the third child process,
My processID is 10185,my parent processID is 10182,my process groupID is 10182

I'm the main process.
My processID is 10182
pid1: 10183,pid2: 10184
[1]after set pgrpID,
[1]My processID is 10183,my parent processID is 10182,my process groupID is 10184

The main process going to shut down.


Process finished with exit code 0
 

 

你可能感兴趣的:(linux,C)