[转]system函数返回值探究
对于system这个函数的功能早就有一定了解,读书期间,就学习了UNIX系统编程这本书,后来买了APUE.我这个人总是有好读书不求甚解的毛病。对于system函数只知其一,不知其二。后来被人问起相关的问题,结果丢了脸。书到用时方恨自己不求甚解。今天仔细探查了下system的一些特性。
- #include <stdlib.h>
- int system(const char *command);
- The value returned is -1 on error (e.g. fork(2) failed), and the return status of the command otherwise. This latter return status is in the format specified in wait(2). 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(127).
- libin@libin:~/program/C/Linux/system$ ./tsys "nosuchcmd"
- sh: nosuchcmd: not found
- status = 32512
- normal termination,exit status = 127
- libin@libin:~/program/C/Linux/system$ ./tsys "ls /noexisted"
- ls: 无法访问/noexisted: 没有那个文件或目录
- status = 512
- normal termination,exit status = 2
- libin@libin:~/program/C/Linux/system$ ls /noexist
- ls: 无法访问/noexist: 没有那个文件或目录
- libin@libin:~/program/C/Linux/system$ echo $?
- 2
- libin@libin:~/program/C/Linux/system$
- Thus, the exit code of the command will be WEXITSTATUS(status)
- WIFEXITED(status) ! =0
- libin@libin:~/program/C/Linux/system$ stty -a
- speed 38400 baud; rows 36; columns 134; line = 0;
- intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = M-^?; eol2 = M-^?; swtch = M-^?; start = ^Q; stop = ^S; susp = ^Z;
- rprnt = ^R; werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 0;
- -parenb -parodd cs8 hupcl -cstopb cread -clocal -crtscts
- -ignbrk brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff -iuclc ixany imaxbel iutf8
- opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
- isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke
- libin@libin:~/program/C/Linux/system$ ./tsys "sleep 7"
- ^Cstatus = 2
- abnormal termination,signal number =2
- libin@libin:~/program/C/Linux/system$ sleep 7
- ^C
- libin@libin:~/program/C/Linux/system$ echo $?
- 130
- libin@libin:~/program/C/Linux/system$ ./tsys "sleep 7"
- ^\status = 3
- abnormal termination,signal number =3
- libin@libin:~/program/C/Linux/system$ sleep 7
- ^\退出
- libin@libin:~/program/C/Linux/system$ echo $?
- 131
- root@libin:~/program/C/Linux/system# ./tsys "sleep 50" &
- [1] 2518
- root@libin:~/program/C/Linux/system# ps -ef
- root 2359 2343 0 12:42 pts/0 00:00:00 /bin/bash
- root 2518 2359 0 12:55 pts/0 00:00:00 ./tsys sleep 50
- root 2519 2518 0 12:55 pts/0 00:00:00 sh -c sleep 50
- root 2520 2519 0 12:55 pts/0 00:00:00 sleep 50
- root 2521 2359 0 12:56 pts/0 00:00:00 ps -ef
- root@libin:~/program/C/Linux/system# kill -3 2520
- Quit
- status = 33536
- normal termination,exit status = 131
- root@libin:~/program/C/Linux/system# ./tsys "sleep 50" &
- [1] 2568
- root@libin:~/program/C/Linux/system# ps -ef
- root 2568 2359 0 13:01 pts/0 00:00:00 ./tsys sleep 50
- root 2569 2568 0 13:01 pts/0 00:00:00 sh -c sleep 50
- root 2570 2569 0 13:01 pts/0 00:00:00 sleep 50
- root 2571 2359 0 13:01 pts/0 00:00:00 ps -ef
- root@libin:~/program/C/Linux/system# kill -3 2569
- status = 3
- abnormal termination,signal number =3

- #define _XOPEN_SOURCE
- #include<stdio.h>
- #include<stdlib.h>
- #include<unistd.h>
- #include<signal.h>
- #include<sys/wait.h>
- void pr_exit(int status)
- {
- printf("status = %d\n",status);
- if(WIFEXITED(status))
- {
- printf("normal termination,exit status = %d\n",WEXITSTATUS(status));
- }
- else if(WIFSIGNALED(status))
- {
- printf("abnormal termination,signal number =%d%s\n",
- WTERMSIG(status),
- #ifdef WCOREDUMP
- WCOREDUMP(status)?"core file generated" : "");
- #else
- "");
- #endif
- }
- }
- int main(int argc,char* argv[])
- {
- int status;
- if(argc<2)
- {
- fprintf(stderr,"usage:tsys cmd\n");
- return -1;
- }
- if((status = system(argv[1]) )<0)
- {
- fprintf(stderr,"system error\n");
- return -2;
- }
- pr_exit(status);
- return 0;
- }
[转]system函数返回值探究的更多相关文章
- Linux system函数返回值
例: status = system("./test.sh"); 1.先统一两个说法: (1)system返回值:指调用system函数后的返回值,比如上例中status为syst ...
- C语言:将3*4矩阵中找出行最大,列最小的那个元素。-将低于平均值的人数作为函数返回值,将低于平均分的分数放入below数组中。
//将3*4矩阵中找出行最大,列最小的那个元素. #include <stdio.h> #define M 3 #define N 4 void fun(int (*a)[N]) { ,j ...
- 【C/C++】引用&的含义/语法/作为函数参数/函数返回值/本质/常量引用
含义 引用不产生副本,只是给原变量起了别名. 对引用变量的操作就是对原变量的操作. 基本语法 数据类型 &别名 = 原名 e.g. int a = 10; int &b = a; // ...
- shell调用函数返回值深入分析
编写shell脚本过程中,我们经常会自定义一些函数,并根据函数的返回值不同来执行相应的流程,那么我们如何来获取函数的返回值呢? 首先shell中调用函数有两种方式: 第一种:value=`functi ...
- Python从线程获取函数返回值
Python中利用强大的threading模块可以很容易的实现多线程开发,提高运行速度.这一般是对某个进行大量计算操作的的函数进行多线程处理,然后合并各线程的结果.获取函数返回值的方法可以如下: 1) ...
- 速战速决 (3) - PHP: 函数基础, 函数参数, 函数返回值, 可变函数, 匿名函数, 闭包函数, 回调函数
[源码下载] 速战速决 (3) - PHP: 函数基础, 函数参数, 函数返回值, 可变函数, 匿名函数, 闭包函数, 回调函数 作者:webabcd 介绍速战速决 之 PHP 函数基础 函数参数 函 ...
- string类find函数返回值判定
string类find函数返回值判定 代码示例 #include<iostream> #include<cstring> using namespace std; int m ...
- c语言main函数返回值、参数详解(返回值是必须的,0表示正常退出)
C语言Main函数返回值 main函数的返回值,用于说明程序的退出状态.如果返回0,则代表程序正常退出:返回其它数字的含义则由系统决定.通常,返回非零代表程序异常退出. 很多人甚至市面上的一些书籍,都 ...
- Python学习教程(learning Python)--2.3.4Python函数返回值
本节讨论Python函数返回值问题. Python和C语言一样,也可以在函数结束时返回一个值.但在定义自己的Python函数时,是不需要指定返回值数据类型的,这和Python不关心变量的数据类型是一致 ...
随机推荐
- Android必会小功能总结
1.获取屏幕尺寸.密度等信息. 1)最常用的方法: WindowManager windowManager = getWindowManager(); Display display = window ...
- 程序编码(机器级代码+汇编代码+C代码+反汇编)
[-1]相关声明 本文总结于csapp: 了解详情,或有兴趣,建议看原版书籍: [0]程序编码 GCC调用了一系列程序,将源代码转化成可执行代码的流程如下: (1)C预处理器扩展源代码,插入所有用#i ...
- Oracle数据库查看执行计划
基于ORACLE的应用系统很多性能问题,是由应用系统SQL性能低劣引起的,所以,SQL的性能优化很重要,分析与优化SQL的性能我们一般通过查看该SQL的执行计划,本文就如何看懂执行计划,以及如何通过分 ...
- 20145102 《Java程序设计》第1周学习总结
20145102 <Java程序设计>第1周学习总结 教材学习内容总结 linux下对于java的安装是非常简便的,只要参照ArchWiki就可以快速安装,没有Windows上那么复杂.I ...
- C语言数据类型
1.概述 C 语言包含的数据类型如下图所示: 2.各种数据类型介绍 2.1整型 整形包括短整型.整形和长整形. 2.1.1短整形 short a=1; 2.1.2整形 一般占4个字节(32位),最高位 ...
- 如何在mac上安装composer(How to install composer on the Mac)
Change into a directory in your path like cd /usr/local/bin Get Composer curl -sS https://getcompose ...
- html使用空格对齐文本( ;&emsp;&ensp;)
字符以及HTML实体 描述以及说明 这是我们使用最多的空格,也就是按下space键产生的空格.在HTML中,如果你用空格键产生此空格,空格是不会累加的(只算1个).要使用html实体表示才可累加. ...
- 自定义input file 属性
<label class="input"><input title="浏览文件" type="file" />浏览… ...
- Echarts 使用遇到的问题
1.在使用ECharts的数据视图时,单击打开数据视图如下,当单击close按钮时,如果当前图像区域包含在一个<from></from>标签中,则会刷整个新页面, 去掉< ...
- js标签放在html的什么位置比较好
推荐的是js的script标签放在body的末尾,</body>标签之前,包含在body内! <body> <!--其它Html标签--> <script&g ...