Linux system 函数的一些注意事项
在日常的代码编程中 , 我们可以利用system 函数去调用一些我们自己想调用的命令 , 并获取他的返回值。
函数的原型如下:
int system(const char *command);
上一段我自己写的代码:
int main(void)
{
unsigned int val ; val = system("./a.out") ; val = WEXITSTATUS(val);
printf("vl = %d\n" , val) ; return ;
}
这段代码是直接调用了当前文件的a.out文件 , 并获取了他的返回值.
今天着重记一下这个返回值得注意事项,先看下man system 里面关于返回值的说明:
RETURN VALUE
The value returned is - on error (e.g. fork() failed), and the return
status of the command otherwise. This latter return status is in the
format specified in wait(). Thus, the exit code of the command will be
WEXITSTATUS(status). In case /bin/sh could not be executed, the exit
status will be that of a command that does exit(). If the value of command is NULL, system() returns nonzero if the shell is
available, and zero if not. system() does not affect the wait status of any other children.
返回值分为3类,
第一类 : 如果 system 分配子进程失败, 他会返回 -1;
第二类 : 如果 system 执行命令失败 , 返回127;
第三类 : 执行命令子进程命令成功 , 返回子进程所返回的值。
这个值是有问题的。这个值是一个16位的值。
低8位 , 存放的是如果你执行命令失败,所存放的值(127 == 7个1),
高8位, 存放的是你执行的那条命令所返回值。
就是这个8位的问题,让我着重讲一下:
通过上面的程序 , 我用了一个宏定义:
val = WEXITSTATUS(val);
这个直接左移动8位。
注意这个值只有八位 , 如果你的命令返回一个高于八位的值, 他就会出现我们无法避免的一个状况, 返回值有点不符我们的理想:
看一下我的 a.out 源代码:
int main(void)
{
printf("hello kitty \n") ;
return ;
}
这里我的返回值是255, 看我执行一下system 那个程序 , 得出结果:
aplex@aplex:~/test/chen_test$ ./test
hello kitty
vl =
返回值正常 , 但是, 我再把返回值加1:
int main(void)
{
printf("hello kitty \n") ;
return ;
}
再执行system 函数代码:
aplex@aplex:~/test/chen_test$ ./test
hello kitty
vl =
没错, 他只能返回0~255 的值, 超出8位就不能正常返回。
Linux system 函数的一些注意事项的更多相关文章
- linux system函数引发的错误
转: https://my.oschina.net/renhc/blog/54582 先看一下问题 简单封装了一下system()函数: int pox_system(const char *cm ...
- Linux system函数详解
system 功能:system()函数调用"/bin/sh -c command"执行特定的命令,阻塞当前进程直到command命令执行完毕 原型 int system(cons ...
- linux system函数分析
system函数是在应用编程里面想调用外部命令时最方便的方式了,除非想要获取命令行执行的输出信息, 那system就不行了,需要用popen.但是system内部具体怎么实现及怎么处理它的返回值经常被 ...
- Linux system函数返回值
例: status = system("./test.sh"); 1.先统一两个说法: (1)system返回值:指调用system函数后的返回值,比如上例中status为syst ...
- linux system()函数详解
system(3) - Linux man page Name system - execute a shell command Synopsis #include <stdlib.h> ...
- 对于linux下system()函数的深度理解(整理)
原谅: http://blog.sina.com.cn/s/blog_8043547601017qk0.html 这几天调程序(嵌入式linux),发现程序有时就莫名其妙的死掉,每次都定位在程序中不同 ...
- 转:对于linux下system()函数的深度理解(整理)
这几天调程序(嵌入式linux),发现程序有时就莫名其妙的死掉,每次都定位在程序中不同的system()函数,直接在shell下输入system()函数中调用的命令也都一切正常.就没理这个bug,以为 ...
- 【C/C++】Linux下使用system()函数一定要谨慎
[C/C++]Linux下使用system()函数一定要谨慎 http://my.oschina.net/renhc/blog/53580 曾经的曾经,被system()函数折磨过,之所以这样,是因为 ...
- 【C/C++】Linux下system()函数引发的错误
http://my.oschina.net/renhc/blog/54582 [C/C++]Linux下system()函数引发的错误 恋恋美食 恋恋美食 发布时间: 2012/04/21 11:3 ...
随机推荐
- PyQ1_介绍
PyQt是一个创建GUI应用程序的工具包.它是Python编程语言和Qt库的成功融合.Qt库是目前最强大的库之一.PyQt是由Phil Thompson 开发. PyQt实现了一个Python模块 ...
- JavaScript学习笔记——变量和数据类型
一.javascript命名规范 1. 严格区分大小写 2. 变量的命名必须以字母或 _或 $开头,余下的部分可以是任意的字母,数字,或者是 _或者是$ 3.不能用关键字或者是保留字命名. 4.jav ...
- C++ typedef用法小结 (※不能不看※)
C++ typedef用法小结 (※不能不看※) 第一.四个用途 用途一: 定义一种类型的别名,而不只是简单的宏替换.可以用作同时声明指针型的多个对象.比如:char* pa, pb; // 这多数不 ...
- document.body.scrollTop用法
网页可见区域宽: document.body.clientWidth;网页可见区域高: document.body.clientHeight;网页可见区域宽: document.body.offset ...
- JAVA基础知识(转)
本文就java基础部分容易混淆的一些知识点进行了一下总结.因为Java本身知识点非常多,不可能在很短的篇幅就能叙述完,而且就某一个点来讲,如欲仔细去探究,也能阐述的非常多.这里不做全面仔细的论述,仅做 ...
- Robot Framework--07 变量的声明、赋值及其使用
转自:http://blog.csdn.net/tulituqi/article/details/7984642 一.变量的声明 1.变量标识符 每个变量都可以用 变量标识符{变量名} 来进行 ...
- Struts2的模板和主题theme及自定义theme的使用
Struts2的模板和主题theme及自定义theme 标签: struts2 2016-03-29 11:22 190人阅读 评论(0) 收藏 举报 分类: javaweb(8) Struts2 ...
- log4j2 使用
转载自 Blog of 天外的星星: http://www.cnblogs.com/leo-lsw/p/log4j2tutorial.html Log4j 2的好处就不和大家说了,如果你搜了2,说明你 ...
- python抓取网站URL小工具
1.安装Python requests模块(通过pip): 环境搭建好了! 2.测试一下抓取URL的过程: 抓取出来的URL有JavaScript代码,正则上还有待更加完善,有兴趣的可以研究下~! 工 ...
- ElasticSearch已经配置好ik分词和mmseg分词(转)
ElasticSearch是一个基于Lucene构建的开源,分布式,RESTful搜索引擎.设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便.支持通过HTTP使用JSON进行数据索引 ...