在的popen和pclose函数的不同行为

编程入门 行业动态 更新时间:2024-10-18 22:33:39
本文介绍了在的popen和pclose函数的不同行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

近日,笔者得到一个奇怪的问题,当我使用的popen(3)和 pclose函数(3)。就像这样:

Recently, I get a strange problem when i used popen(3) and pclose(3). Just like this:

//a.sh #!/bin/bash cat /etc/issue sleep 3 //b.sh #!/bin/bash cat /etc/issue echo "before sleep" sleep 3

我的C程序:

#include <stdio.h> int main(void) { 1: FILE *fl = popen("sh a.sh", "r"); 2: FILE *fl = popen("sh b.sh", "r"); int t = pclose(fl); printf("%d\n", t); return 0; }

接下来,我将编译并运行此程序为4个步骤:

next, i will compile and run this program as 4 steps:

案例1:注释行2,然后编译并运行,程序不打印0​​,直到shell CMD终止。

case 1: comment line 2, then compile and run, the program do not print 0 until the shell cmd terminate.

案例2:注释行1,然后编译并运行,程序会马上终止,其打印13,也就是说函数,pclose 的退出状态为13 ,我抬头linux的错误号:

case 2: comment line 1, then compile and run, the program will terminate right now and it print 13, that is to say the exit status of pclose is 13, i look up the linux errno:

13 EACCES +Permission denied

samebody将帮助我,告诉我原因吗?谢谢。

samebody would help me and tell me the reason? thanks.

推荐答案

您应该使用的 WEXITSTATUS 宏来获得实际收益code。

You should use the WEXITSTATUS macro to get the real return code.

更多推荐

在的popen和pclose函数的不同行为

本文发布于:2023-11-15 23:29:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1598922.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   popen   pclose

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!