c - 使用嵌套开关 fork 两个 child

标签 c switch-statement exec fork

<分区>

我正在尝试使用嵌套开关 fork 两个 child 并 exec() 他们。但是,我的代码似乎没有进入我第二个 child 的案例。

// fork the first child 
switch (player0PID = fork()){
    case -1: // error
        return -1;
    case 0: // in child
        // exec ("turn into") a new program
        // first child successfully execs
        execl("./client", "0", "&", (char *) NULL);
        _exit(127); // failed exec
    default: // inside parent 
        // fork second child
        switch (player1PID = fork()){
            case -1: // error
                printf("err spawning of client 1");
                return -1;
            case 0: // in child
                // exec ("turn into") a new program
                // below exec does not execute
                // second child not exec'd
                execl("./client", "1", "&", (char *) NULL);
                _exit(127); // failed exec
            default: // inside parent 
                ;
                // a lot of code the parent executes
                break;
        break; 

我的 switch 语句有问题吗?第一个 child 执行得很好,父代码将执行。但是,由于某种原因,我的第二个 child 没有被 fork 或执行。对于第二个开关,代码不会进入“case 0”。


更新:

解决方案 是更正我对 execl 的使用。

我的第二个 exec 失败了,因为两个 exec 语句将 & 作为程序的参数。为了正确处理 &,我需要使用 sh 程序运行这些程序。第二个 exec 可能没有工作,因为不允许在前台同时运行两个进程?

Example execl 用法:

execl("/bin/sh", "sh", "-c", "./client 1 &", (char *) NULL);

最佳答案

fork()系统函数会返回pid给父进程,0给子进程。

在您的情况下,与第一个 switch 不同,第二个 switch 不会同时执行父项和子项。

switch (player1PID = fork())

此条件仅执行父进程,这是您的第二个 switch 中的默认情况。而不是这个条件尝试以下条件 -

switch (fork() == 0)

在这种情况下,它将在第二个开关中执行 case 0:(child)。但是这次它不会执行default case(parent)。

为避免此类错误,同时执行子进程和父进程的更好方法是使用嵌套 if-else。试试下面的代码,它会为你工作-

代码,来自一个父进程-> 2 个子进程-

int main()
{
    pid_t player0PID, player1PID;
    if(player0PID = fork()){
            // child 1
            printf("In child1 process\n");
            // do your stuff here
    }
    else{
            printf("In parent process\n");
            if(player1PID = fork()){
                    //child 2
                    printf("In child2 process\n");
                    // do your stuff
            }
            else{
                    printf("In parent process\n");                  
                    // do your stuff
            }
    }
    return 0;
}

代码为,来自父进程->child1进程->child2进程-

int main()
{
    pid_t player0PID, player1PID;
    if(player0PID = fork()){
            // child 1
            printf("In child1 process\n");
            // do your stuff here
            if(player1PID = fork()){
                    //child 2
                    printf("In child2 process\n");
                    // do your stuff
            }
            else{
                    printf("In child1 is parent process here\n");
                    // do your stuff
            }
    }
    else{
            printf("In parent process\n");
            // do your stuff here
    }
    return 0;
}

关于c - 使用嵌套开关 fork 两个 child ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25256708/

相关文章:

c - 理解c中的链表结构

c - 前N个素数优化

c - 段错误和指向不存在结构的指针不为空

java - 如何在java中替换/转换/扩展字符串

c++ - 在运行时查询模板特化/避免大切换的方法

C# 7 模式匹配与元组

python - Python 2.7.6 中的执行

php - 使用 php 使用 sudo 执行命令

c - 如何将结构用作指针和函数中的结构

linux - 如何使用 find -exec 删除部分文件扩展名