Linux C 獲取進程退出值的實現(xiàn)代碼
更新時間:2013年05月27日 15:18:39 作者:
本篇文章是對在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"
相關文章
error LNK2019: 無法解析的外部符號 問題的解決辦法
error LNK2019: 無法解析的外部符號 問題的解決辦法,需要的朋友可以參考一下2013-05-05C語言使用DP動態(tài)規(guī)劃思想解最大K乘積與乘積最大問題
Dynamic Programming動態(tài)規(guī)劃方法采用最優(yōu)原則來建立用于計算最優(yōu)解的遞歸式,并且考察每個最優(yōu)決策序列中是否包含一個最優(yōu)子序列,這里我們就來展示C語言使用DP動態(tài)規(guī)劃思想解最大K乘積與乘積最大問題2016-06-06C語言編程gcc如何生成靜態(tài)庫.a和動態(tài)庫.so示例詳解
本文主要敘述了gcc如何生成靜態(tài)庫(.a)和動態(tài)庫(.so),幫助我們更好的進行嵌入式編程。因為有些時候,涉及安全,所以可能會提供靜態(tài)庫或動態(tài)庫供我們使用2021-10-10