[转]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不关心变量的数据类型是一致 ...
随机推荐
- webSocket开源框架:SocketRocket 简单的使用
需要用到webSocket,所以搜集了一下使用方法, git下载地址:square/SocketRocket gitHUB 上没有看懂,就要 cocoaPod 导入了 socketRocket 导入这 ...
- ios菜鸟总结2
这个星期学起oc来可能是最纠结的.话不多说先补充一下上一个星期的快捷键吧.工程导航器:Command+1 显示/隐藏导航器面板:Command+0 .显示/隐藏实用工具面板:Command+Op ...
- KDE声音服务器 arts
KDE声音服务器 arts arts介绍arts是KDE的核心声音系统,支持多音频流.全双工.网络声音请求.ALSA与OSS驱动后端.JACK声音服务器后端等扩展,它既是声音服务器,也 提供一套音频软 ...
- mybatis--MapperScannerConfigurer
一般我们这样配置 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryB ...
- hdu-5698 瞬间移动(数论+快速幂)
题目链接: 瞬间移动 Problem Description 有一个无限大的矩形,初始时你在左上角(即第一行第一列),每次你都可以选择一个右下方格子,并瞬移过去(如从下图中的红色格子能直接瞬移到蓝 ...
- Arduino
========================================= Sites/Blogs http://arduino.cc/ http://www.geek-workshop.co ...
- Table of Contents - Apache Commons
Apache Commons 简述 CLI Usage of CLI Option Properties Codec 常见的编码解码 Compress Configuration2 Quick sta ...
- 前端插件Emmet
Sublime text安装 步骤一:首先你需要为sublime text安装Package Control组件: 按Ctrl+`调出sublime text的console 粘贴以下代码到底部命令行 ...
- DIV+CSS解决IE6,IE7,IE8,FF兼容问题
1.IE8下兼容问题,这个最好处理,转化成IE7兼容就可以.在头部加如下一段代码,然后只要在IE7下兼容了,IE8下面也就兼容了:1. <metahttp-equivmetahttp-equiv ...
- 北大ACM(POJ1010-STAMPS)
Question:http://poj.org/problem?id=1010问题点:DFS.剪枝. Memory: 220K Time: 32MS Language: C++ Result: Acc ...