如何获取exec执行的程序的返回值? [英] how can I get the return value of a program executed by exec?

查看:84
本文介绍了如何获取exec执行的程序的返回值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下C代码:

if(fork()==0){
  execl("/usr/bin/fsck", "fsck", "/dev/c0d0p1s0", NULL);
}

它调用execl来运行fsck来检查文件系统/dev/c0d0p1s0.

it calls execl to run fsck for checking the filesystem /dev/c0d0p1s0.

我的问题是:如何获得fsck的返回值?

My question is: how can I get the return value of fsck?

我需要返回值fsck来检查文件系统是否一致.

I need the return value of fsck to check whether the file system is consistence or not.

谢谢.

推荐答案

让父进程等待子进程退出:

Have the parent process wait for the child to exit:

pid_t pid = fork();
if (pid == -1) {
  // error, no child created
}
else if (pid == 0) {
  // child
}
else {
  // parent
  int status;
  if (waitpid(pid, &status, 0) == -1) {
    // handle error
  }
  else {
    // child exit code in status
    // use WIFEXITED, WEXITSTATUS, etc. on status
  }
}

这篇关于如何获取exec执行的程序的返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆