子进程c的返回值

子进程c的返回值

本文介绍了子进程c的返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助,将子程序中的状态码"返回给父程序,在父程序中它将检查状态码,打印代码并退出父程序.这是针对一个类项目的,因此我将在此处放置一些相关的代码,但是出于明显的原因,我不会发布整个项目.

I need help returning a "status code" from my child program back to the parent, where it will check the status code, print code, and exit the parent. This is for a class project, so I will put some relevant code here, but I don't to post the entire project for obvious reasons.

我通过exec分叉并创建了子进程.父级执行一些复杂的数学运算,并使用命名管道将数据推送到子级进程.孩子多做一些花哨的数学.当我使用关键字时,孩子需要将其在末端执行奇特数学的次数返回给父母,在父母那里,父母会看到这个次数,打印出返回的数字,然后退出父母.

I have forked and created the child process through exec. The parent does some fancy math and uses a named pipe to push data to the child process. The child does some more fancy math. When I uses a keyword, the child needs to return the number of times it did the fancy math on its end back to the parent, where the parent will see this, print out the returned number, and exit the parent.

    int status;
    pid_t child_id;
    child_id = fork();
    if (child_id == 0)
    {
            // put child code here
            exec(opens second process);
    }
     if (child_id < 0)
    {
            perror("fork failed\n");
            exit(EXIT_FAILURE);
    }

     while(child_id != 0)
     {
       //parent process
       //do fancy math stuff here
       // pipe
      //open pipe
      //converted math to "string" to pass to other program
      // use fprintf to send data to other program
      fprintf(fp,"%s",str);
      //close pipe
//**************************************************
//this block doesn't work, always returns 0
      if (WIFEXITED(status)){
            int returned = WEXITSTATUS(status);
            printf("exited normally with status %d\n",returned);
      }
//***************************************************
   return 0;

第二个c程序只是对管道的简单读取,做一些更有趣的数学运算,并且在要返回数字的位置放置了return语句.

second c program is just a simple reading of the pipe, does some more fancy math, and has return statements placed where I want to return a number.

据我所知,有一种方法可以传递子程序的返回值,但是我似乎不明白该怎么做.我已经添加了一段发现的代码,但是似乎无法使它正常工作.我已经读过,但是也许我遗漏了一些东西?

As far as I understand it, there is a way to pass the return value from the child program, but I can't seem to understand how. I haved added a piece of code that I found, but I can't seem to get it to work. I have read about it, but maybe I am missing something?

请忽略任何语法或结构问题.

please disregard any syntax or structure issues.

推荐答案

您正试图通过WIFEXITED函数读取status,但从未给它赋值.尝试读取未初始化的值会调用未定义的行为.

You're attempting to read status via the WIFEXITED function, but you never give it a value. Attempting to read an uninitialized value invokes undefined behavior.

您需要调用wait函数,该函数告诉父母等待孩子完成并接收其返回代码:

You need to call the wait function, which tells the parent to wait for a child to finish and receive its return code:

wait(&status);
if (WIFEXITED(status)){
      int returned = WEXITSTATUS(status);
      printf("exited normally with status %d\n",returned);
}

这篇关于子进程c的返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 16:50