Linux C 获取进程退出值的实现代码
本篇文章是对在Linux下使用c语言获取进程退出值的方法进行了详细的分析介绍,需要的朋友参考下
如以下代码所示:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <errno.h>
int main(int argc, char *argv[])
{
pid_t pid;
int stat;
int exit_code;
pid = fork();
if(pid == 0)
{
sleep(3);
exit(5);
}
else if( pid < 0 )
{
fprintf(stderr, "fork failed: %s", strerror(errno));
return -1;
}
wait(&stat); // 等待一个子进程结束
if(WIFEXITED(stat)) // 如果子进程通过 return, exit, _exit 正常结束, WIFEXITED() 返回 true
{
exit_code = WEXITSTATUS(stat);
printf("child's exit_code: %d\n", exit_code);
}
return 0;
}
参考: "man 2 wait"
代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <errno.h>
int main(int argc, char *argv[])
{
pid_t pid;
int stat;
int exit_code;
pid = fork();
if(pid == 0)
{
sleep(3);
exit(5);
}
else if( pid < 0 )
{
fprintf(stderr, "fork failed: %s", strerror(errno));
return -1;
}
wait(&stat); // 等待一个子进程结束
if(WIFEXITED(stat)) // 如果子进程通过 return, exit, _exit 正常结束, WIFEXITED() 返回 true
{
exit_code = WEXITSTATUS(stat);
printf("child's exit_code: %d\n", exit_code);
}
return 0;
}
参考: "man 2 wait"
收藏文章
热评话题
- Android定时器实现的几种方式整理及removeCallbacks失效问题解决_Android开发_龙盟编程网
- 在linux中导入sql文件的方法分享(使用命令行转移mysql数据库)_MySQL 技术_龙盟编程网
- 如何应用PHP函数imagettftext处理图片_php编程_个人技术分享
- 如何解决C语言,函数名与宏冲突_C/C++开发_龙盟编程网
- 在ASP.Net中应用Javascript_Javascript编程_龙盟编程网
- 查询反向链接中link和domain的区别_SEO优化_龙盟编程网
- sql 查询本年、本月、本日记录的语句,附SQL日期函数_Sql Server开发_龙盟编程网
精彩图集
精彩文章