Linux内核分叉子状态返回

发布于 2024-12-11 02:40:55 字数 1121 浏览 0 评论 0原文

所以这是我的困境:为什么当我 fork(); 一个进程的子进程并且它结束时,返回的子进程的 &status 向左移动 8少量?

例如,假设我在分叉子进程的末尾有 exit(4); ,我的状态在 wait(&status); 父进程的值获取到 0x400

所以这是一些代码来说明我的意思

#include <stdio.h>

main() {
  int n, status, cpid;

  printf("Parent pid = %d\n",  getpid());

  n = fork();

  if (n != 0) {
     //  parent code
     printf ("I'm the parent with PID %d \t fork returned %d \t my parent is %d\n", getpid(), n, getppid()); 
     status = 0;
     sleep(5);      // verify status of child
     cpid = wait(&status);

     // so when i printf the hex value of status it gets shifted 
     printf("I received from my child %d this information %x\n", cpid, status);

   } else {
        // child code
        printf ("I'm the child with PID %d \t fork returned %d \t my parent is %d\n", getpid(), n, getppid());
        sleep(20);
        printf("Child complete\n");
        status=12345;

        // the line that returns the shifted value
        exit(4);
     }

     printf("Parent complete\n");
     exit(15);
}

So here's my dilemma: Why when I fork(); a child of a process and it ends, the returned &status of the child is shifted to the left by 8 bit?

For example let's say i have exit(4); at the end of the forked child , my status in the wait(&status);
of the father process gets me 0x400.

so here is some code that illustrates what I mean

#include <stdio.h>

main() {
  int n, status, cpid;

  printf("Parent pid = %d\n",  getpid());

  n = fork();

  if (n != 0) {
     //  parent code
     printf ("I'm the parent with PID %d \t fork returned %d \t my parent is %d\n", getpid(), n, getppid()); 
     status = 0;
     sleep(5);      // verify status of child
     cpid = wait(&status);

     // so when i printf the hex value of status it gets shifted 
     printf("I received from my child %d this information %x\n", cpid, status);

   } else {
        // child code
        printf ("I'm the child with PID %d \t fork returned %d \t my parent is %d\n", getpid(), n, getppid());
        sleep(20);
        printf("Child complete\n");
        status=12345;

        // the line that returns the shifted value
        exit(4);
     }

     printf("Parent complete\n");
     exit(15);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

金橙橙 2024-12-18 02:40:55

阅读有关 wait(3) 的文档。返回的值是一个 32 位整数,包含退出状态(如果进程正常退出)以及一些标志位。要确定进程是否正常退出,请使用 WIFEXITED() 宏。如果返回 true,则使用 WEXITSTATUS() 宏获取实际退出状态:

int status;
if(wait(&status) > 0)
{
    // Did the process exit normally?
    if(WIFEXITED(status))
    {
        // This is the value you really want
        int actual_status = WEXITSTATUS(status);
        ...
    }
}

Read the documentation on wait(3). The value returned is a 32-bit integer containing the exit status (if the process exited normally), as well as a number of flag bits. To determine if the process exited normally, use the WIFEXITED() macro. If that returns true, then use the WEXITSTATUS() macro to get the actual exit status:

int status;
if(wait(&status) > 0)
{
    // Did the process exit normally?
    if(WIFEXITED(status))
    {
        // This is the value you really want
        int actual_status = WEXITSTATUS(status);
        ...
    }
}
丑丑阿 2024-12-18 02:40:55

因为 status 不仅保存子进程的返回代码,还保存其他信息。
如果你想知道退出值,你必须使用

wait 手册中的 WEXITSTATUS 宏:

WEXITSTATUS(状态)
返回子进程的退出状态。这包括
状态参数的最低有效 8 位
在对 exit(3) 或 _exit(2) 的调用中指定或作为参数指定
main() 中的 return 语句。这个宏应该只
如果 WIFEXITED 返回 true,则雇用。

这意味着 status 整数保存着另一个信息,要提取此信息,您应该使用 sys/wait.h 中定义的宏

Because status hold not only the return code of the child bu it holds another information.
if you want to know the exit value you have to use the WEXITSTATUS macro

from wait manual:

WEXITSTATUS(status)
returns the exit status of the child. This consists of the
least significant 8 bits of the status argument that the child
specified in a call to exit(3) or _exit(2) or as the argument
for a return statement in main(). This macro should only be
employed if WIFEXITED returned true.

that's mean that the status integer holds another information and to extract this information you should use the macros which defined in sys/wait.h

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文