首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用嵌套交换机分叉两个子节点

问使用嵌套交换机分叉两个子节点
EN

Stack Overflow用户
提问于 2014-08-12 05:24:08
回答 2查看 909关注 0票数 0

我正在尝试使用一个嵌套开关将两个子级和exec()两个子程序分叉。然而,我的代码似乎没有进入我的第二个孩子的情况。

代码语言:javascript
复制
// 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; 

我的开关声明做错了吗?第一个子程序可以执行,并且父代码将执行。然而,我的第二个孩子并没有因为某种原因而被分叉或执行。第二个开关的代码不会进入"case 0“。

更新:

解决方案是为了纠正我对execl的使用。

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

示例execl用法:

代码语言:javascript
复制
execl("/bin/sh", "sh", "-c", "./client 1 &", (char *) NULL);
EN

回答 2

Stack Overflow用户

发布于 2014-08-12 06:09:24

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

在您的示例中,与第一个switch不同的是,第二个switch并不同时执行父级和子级。

switch (player1PID = fork())

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

代码语言:javascript
复制
switch (fork() == 0)

在这种情况下,它将在第二个交换机中执行case 0:(子)。但这一次,它将不会执行default案例(父)。

为了避免这种类型的错误,执行子进程和父进程的一个更好的方法是使用nested if-else。试试下面的代码,它会对你有用-

来自父进程-> 2子进程的代码-

代码语言:javascript
复制
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进程-

代码语言:javascript
复制
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;
}
票数 1
EN

Stack Overflow用户

发布于 2014-08-12 07:30:29

printf()输出没有出现的关键原因是没有用换行符终止它。这意味着它在子节点中保持缓冲,但这意味着当exec()发生时,缓冲输出将丢失。您需要在格式字符串的末尾添加fflush(stdout);或fflush(0);或换行符。这是一个通用的指导方针--使用换行符的printf()格式字符串,除非您希望在相同的输出行上遵循其他内容。

就我个人而言,我不喜欢用开关来处理叉子;我使用if语句。然而,开关确实能工作。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25256708

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档