doujie7886 2014-04-02 08:02
浏览 146
已采纳

c程序fork并等待golang进程状态

C program:

pid = fork();

if (pid == 0) {
    execv("Golang Process");
} else (pid > 0) {
    wait(&status);
    printf("process %d status: %d
", pid);
}

Golang Program:

func main() {
    ......
    os.Exit(1)
}

But, output is: process XXX status: 256

if set os.Exit(2), output is: process XXX status: 512

if set os.Exit(3), output is: process XXX status: 768

Why?

  • 写回答

1条回答 默认 最新

  • duansha3771 2014-04-02 08:57
    关注

    See wait manual:

    If status is not NULL, wait() and waitpid() store status information in the int to which it points. This integer can be inspected with the following macros (which take the integer itself as an argument, not a pointer to it, as is done in wait() and waitpid()!):

    WIFEXITED(status) returns true if the child terminated normally, that is, by calling exit(3) or _exit(2), or by returning from main().

    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.

    Your issue is unrelated to golang, you just have to use these macros to extract the status code:

    if (WIFEXITED(status)) {
      printf("process %d status: %d
    ", pid, WEXITSTATUS(status));
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘