[转]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不关心变量的数据类型是一致 ...
随机推荐
- Linux_内存查询
查询磁盘使用情况: [root@css-management ~]# df -hl 文件系统 容量 已用 可用 已用% 挂载点 Filesystem Size Used Avail Use% Moun ...
- Android Bitmap详细介绍(转)
转自: Bitmap详细介绍 package com.testbitmapscale; import java.io.File; import java.io.FileInputStream; imp ...
- 深入浅出ExtJS 第六章 布局
6.1 布局的用途 6.1 布局的用途 //决定把什么东西放到什么位置; var vieport = new Ext.Viewport({ layout:'border', //使用Border ...
- Android点击其他任意位置收起软键盘
在Android应用开发中,经常出现这样的需求,用户在输入文字的过程中,可能不想继续输入了,通过滑动或者点击其他位置(除软键盘和EditText以外的任何位置),希望能够自动收回键盘,这个功能可能有些 ...
- 从数据库里面取值绑定到Ztree
1.效果图(思路:将数据库表按照一定的格式排序,然后序列化成json字符串,绑定到Ztree上并显示出来) zTree v3.5.16 API 文档 :http://www.ztree.me/v3/a ...
- 英特尔® 实感™ SDK 架构
英特尔® 实感™ SDK在架构上完全不同于其前代系统 — 英特尔® 感知计算 SDK. 如果您使用英特尔感知计算 SDK 进行应用开发,会很快发现,全新的 SDK 可提供增强型编程模式,从而可通过几种 ...
- DNS服务器搭建
注意正式运行的dns服务器主dns设置 中没有启用转发器,所以部分网页如taobao解析时可能很慢.开启转发器即可转发器地址指向电信dns. [root@master ~]# lsb_release ...
- iOS 之单例,代理,通知,KVO,Block全能解析
//单例 //.h + (Instannce *)shareInstance; //.m static Instannce *instance = nil; @implementation Insta ...
- BoneCP主要配置参数
二.BoneCP主要配置参数 1.jdbcUrl 设置数据库URL 2.username 设置数据库用户名 3.password 设置数据库密码 4.partitionCount 设置分区个数.这个参 ...
- (转)linux查看CPU性能及工作状态的指令mpstat,vmstat,iostat,sar,top
衡量CPU性能的指标: 1,用户使用CPU的情况:CPU运行常规用户进程CPU运行niced processCPU运行实时进程 2,系统使用CPU情况:用于I/O管理:中断和驱动用于内存管理:页面交换 ...