system函数返回值,Linux

article/2025/8/22 17:21:04

理论

我们先看下man手册是怎么说的man system

RETURN VALUEThe 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 speci‐fied  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 acommand that does exit(127).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.

机翻一下
返回值
错误时返回的值为-1(例如,fork(2)failed),否则返回命令的状态。后一个返回状态采用wait(2)中指定的格式。因此,命令的退出代码将是WEXITSTATUS(status)。在无法执行/bin/sh的情况下,退出状态将是退出(127)的命令的状态。
如果command的值为空,如果shell可用,system()将返回非零,如果不可用,则返回零。
system()不影响任何其他子项的等待状态。


参考:System函数返回值 和popen
system是个综合的操作,分解开来看就是相当于执行了
1 fork 生成一个子进程。
2 在子进程执行 execl("/bin/sh",“sh”,"-c" command,(char*)0);
3 waitpid

返回值:
1 如果fork失败了,或者waitpid返回除了EINTR之外的错误,system返回 -1;
2 execl执行失败,其返回值如同shell执行了"exit(127)" 一样。
3 如果上述三步都执行成功,那么,system返回值是shell的终止状态。

如果/bin/sh拉起shell命令失败,或者是shell命令没有正常执行 (比如命令根本就是非法的命令),那么,将原因填入status的8~15位。
一般错误来说
status = 256 * 信号编码
WEXITSTATUS(status) = 128 + 信号编号。

实测

下面给出一个程序。system函数传入一串乱七八糟的字符,我们看下结果

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <sys/types.h>int main(void) 
{pid_t status = system("wrqwqwd");if (-1 == status){printf("返回-1,fork失败了,或者waitpid返回除了EINTR之外的错误\n");return 0;}else{printf("exit status value = [0x%x]\n", status);if (WIFEXITED(status)){if (0 == WEXITSTATUS(status)){printf("0 == WEXITSTATUS(status),运行完毕\n");}else{printf("0 != WEXITSTATUS(status),script exit code: %d\n", WEXITSTATUS(status));return 0;}}else{printf("WIFEXITED(status)为假,exit status = [%d]\n", WEXITSTATUS(status));return 0;}}return 0;
} 

在这里插入图片描述
16进制 7f00 转 10进制 为 32515。WEXITSTATUS(status)为127
如果execl执行失败,也即command没有顺利执行,比如被信号中断,或者command命令根本不存在,system()函数返回127.

我们传入 ls 试试

pid_t status = system("ls");

在这里插入图片描述
status 为 0,WEXITSTATUS(status)为0
执行成功,exit 0

我们执行下没有执行权限的shell脚本

pid_t status = system("./start.sh");

在这里插入图片描述
WEXITSTATUS(status)为126
shell脚本没有执行权限。

我们执行下有执行权限但是错误的shell脚本

start.sh

whiledo#echo 1
done

在这里插入图片描述
status为 ox200 即 512,WEXITSTATUS(status)为 2.
具体原因参考:System函数返回值 和popen

我们可以看到,直接在终端执行sleep 7,然后用Ctrl+C发送SIGINT信号,异常退出,shell返回值为130,130的原因,
APUE上解释了,因为SIGINT 等于2,终止状态是128+信号编号,所以为130.
按照APUE上,我们的system调用应该将SIGINT忽略掉,然后正常返回,同时返回值为130,但是实际上LINUX下并不是这样 
的。实际上system的返回值为2,并且异常退出了。

我们执行下有执行权限但是死循环的shell脚本

start.sh

while true
doecho 1;
done

在这里插入图片描述
同样是死循环,然后我按了ctrl+C,发送 SIGINT信号。status为2

当我们运行死循环的shell脚本,然后kill 它

在这里插入图片描述
kill -9,status为9
同理,测试
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

命令kill -l

在这里插入图片描述
常见的信号
在这里插入图片描述
在这里插入图片描述

简单来说

system 返回值,调用函数后的返回值
shell 返回值,指脚本执行后的返回值

阶段1:创建子进程等准备工作。如果失败,返回-1
阶段2:调用/bin/sh拉起脚本,如果拉起失败或者shell未正常执行,原因值被写入ret中
阶段3:如果shell脚本执行成功,shell脚本的返回值写入ret中
WIFEXITED 用来判断阶段二的返回值
WEXITSTATUS 用来判断阶段三的返回值
判断一个脚本是否执行成功,应该满足三个条件:-1 != retWIFEXITED(ret)为真0 == WEXITSTATUS(ret)

注意:当shell脚本不存在时、没有执行条件等,前两个条件也会成立,此时WEXITSTATUS(ret)为127,所以shell脚本中不能将127作为返回值,shell脚本中的异常返回值最好从1开始递增,成功返回0。

一般错误来说
status = 256 * 信号编码
WEXITSTATUS(status) = 128 + 信号编号。

参考文章

Linux信号列表
System函数返回值 和popen
Linux system 函数

补充说明

man system

SYSTEM(3)                  Linux Programmer's Manual                 SYSTEM(3)NAMEsystem - execute a shell commandSYNOPSIS#include <stdlib.h>int system(const char *command);DESCRIPTIONsystem()  executes a command specified in command by calling /bin/sh -ccommand, and returns after the command has been completed.  During exe‐cution  of the command, SIGCHLD will be blocked, and SIGINT and SIGQUITwill be ignored.RETURN VALUEThe value returned is -1 on  error  (e.g.,  fork(2)  failed),  and  thereturn  status  of the command otherwise.  This latter return status isin the format specified in wait(2).  Thus, the exit code of the commandwill  be  WEXITSTATUS(status).   In case /bin/sh could not be executed,the exit status will be that of a command that does exit(127).If the value of command is NULL, system() returns nonzero if the  shellis available, and zero if not.system() does not affect the wait status of any other children.
ATTRIBUTESFor   an   explanation   of   the  terms  used  in  this  section,  seeattributes(7).┌──────────┬───────────────┬─────────┐│Interface │ Attribute     │ Value   │├──────────┼───────────────┼─────────┤│system()  │ Thread safety │ MT-Safe │└──────────┴───────────────┴─────────┘
CONFORMING TOC89, C99, POSIX.1-2001.NOTESIf the _XOPEN_SOURCE feature test macro is  defined  (before  includingany header files), then the macros described in wait(2) (WEXITSTATUS(),etc.) are made available when including <stdlib.h>.As mentioned, system() ignores SIGINT and SIGQUIT.  This may make  pro‐grams  that  call it from a loop uninterruptible, unless they take carethemselves to check the exit status of the child.  E.g.while (something) {int ret = system("foo");if (WIFSIGNALED(ret) &&(WTERMSIG(ret) == SIGINT || WTERMSIG(ret) == SIGQUIT))break;}Do not use system() from a program  with  set-user-ID  or  set-group-IDprivileges, because strange values for some environment variables mightbe used to subvert system integrity.  Use the exec(3) family  of  func‐tions  instead,  but not execlp(3) or execvp(3).  system() will not, infact, work properly from  programs  with  set-user-ID  or  set-group-IDprivileges  on systems on which /bin/sh is bash version 2, since bash 2drops privileges on startup.  (Debian uses a modified bash  which  doesnot do this when invoked as sh.)In  versions  of  glibc before 2.1.3, the check for the availability of/bin/sh was not actually performed if command was NULL; instead it  wasalways  assumed to be available, and system() always returned 1 in thiscase.  Since glibc 2.1.3, this check is performed because, even  thoughPOSIX.1-2001  requires  a conforming implementation to provide a shell,that shell may not be available or executable if  the  calling  programhas   previously   called   chroot(2)   (which   is  not  specified  byPOSIX.1-2001).It is possible for the shell command to return 127, so that code is nota sure indication that the execve(2) call failed.
SEE ALSOsh(1), signal(2), wait(2), exec(3)COLOPHONThis  page  is  part of release 3.53 of the Linux man-pages project.  Adescription of the project, and information about reporting  bugs,  canbe found at http://www.kernel.org/doc/man-pages/.2010-09-10                         SYSTEM(3)

http://chatgpt.dhexx.cn/article/sexTtEbW.shtml

相关文章

Linux---system函数

一、关于system函数 #include <stdlib.h>int system(const char *command);返回值&#xff1a;成功&#xff0c;返回进程的状态值&#xff1b;当sh不能运行时&#xff0c;返回127&#xff1b;失败&#xff0c;返回-1。 源代码如下&#xff1a; int system(const char …

system函数的详细使用

目录 函数接口 作用 返回值 测试代码 参数 MODE命令 color命令 函数接口 _DCRTIMP int __cdecl system( _In_opt_z_ char const* _Command ); system函数已经被收录在标准c库中&#xff0c;头文件为<stdlib.h> 作用 执行系统命令调用命令处理器来执行命令。 如果…

基础知识篇——system函数

system是一个C语言和C下的函数。windows操作系统下system () 函数详解主要是在C语言中的应用&#xff0c;system函数需加头文件<stdlib.h>后方可调用。 Windows函数 函数名&#xff1a; system 功 能&#xff1a; 发出一个DOS命令 用 法&#xff1a; int system(char *…

php system函数用法,system函数如何使用?总结system函数实例用法

这篇文章主要简单分析了linux下system函数,具有一定的参考价值,感兴趣的小伙伴们可以参考一下简单分析了linux下system函数的相关内容,具体内容如下int libc_system (const char *line) {if (line == NULL) /* Check that we have a command processor available. It might …

system函数返回值

system&#xff08;执行shell 命令&#xff09; 相关函数 fork&#xff0c;execve&#xff0c;waitpid&#xff0c;popen 表头文件 #include<stdlib.h> 定义函数 int system(const char * string); 函数说明 system()会调用fork()产生子进程&#xff0c;由子进程来调…

c++system函数

希望各位给个赞&#xff0c;来个关注&#xff0c;100%回关 前言 system是一个比较常用的函数&#xff0c;说白了就是dos指令&#xff0c;下面说几个常用的 所需头文件&#xff1a;windows.h 1.system("cls") cls的作用是清屏,会把控制台都清空 #include<iostream&…

system函数

system函数的头文件: #include<stdlib.h>(我喜欢用万能头) 这是一个很好的用来 坑人 的函数 system(“cls”)可以实现清屏操作 #include<bits/stdc.h> #include<windows.h>//Sleep函数 using namespace std; int main() { cout<<“abcdefg”; Sleep(50…

system 函数常用方法

Hello&#xff0c;又见到你了&#xff0c;今天继续来介绍system函数的用法&#xff1b; 如果你的扫毒软件拦截了这个&#xff0c;说是病毒&#xff0c;不用怕&#xff0c;把它关了&#xff0c;我保证它不是病毒&#xff0c;关了之后就可以用了 1&#xff0c;首先登场的就是 sys…

对于强化学习的梳理

强化学习&#xff08;增强学习&#xff09; 概述 知识联系 强化学习是属于机器学习的一种&#xff0c;机器学习主要分监督学习、非监督学习、半监督学习、增强学习。 强化学习的核心逻辑&#xff0c;那就是智能体&#xff08;Agent&#xff09;可以在环境&#xff08;Envir…

多智能体强化学习:合作关系设定下的多智能体强化学习

0 前言 在多智能体系统中&#xff0c;一个智能体未必能观测到全局状态 S。设第 i 号智能体有一个局部观测&#xff0c;它是S的一部分。 我们假设所有的局部观测的总和构成了全局状态 1 合作关系设定下的策略学习 MARL 中的 完全合作关系 (Fully-Cooperative) 意思是所有智能…

强化学习基础

https://www.toutiao.com/a6641864763305361927/ 2019-01-02 19:47:27 内容目录&#xff1a; 一、强化学习的成功 二、概念和基础 2.1设计强化学习系统 2.2人工智能环境类型 三、问题公式化 3.1数学公式 3.2马尔可夫决策过程 3.3价值函数 四、RL训练术语 4.1基于模型…

强化学习基础05——gym

OpenAI gym OpenAI gym是强化学习最常用的标准库&#xff0c;如果研究强化学习&#xff0c;肯定会用到gym。 gym有几大类控制问题&#xff0c;第一种是经典控制问题,比如cart pole和pendulum。 Cart pole要求给小车一个左右的力&#xff0c;移动小车&#xff0c;让他们的杆子…

联邦强化学习

本博客地址&#xff1a;https://security.blog.csdn.net/article/details/123710121 一、联邦强化学习介绍 强化学习&#xff08;RL&#xff09;是机器学习的一个分支&#xff0c;主要研究序列决策问题&#xff0c;强化学习系统通常由一个动态环境和与环境进行交互的一个或多…

初探强化学习(7)基于模型的强化学习的应用综述

本文是直接翻译一篇文章的&#xff0c;我先把原文地址放在这里。 原文名字为&#xff1a;Survey of Model-Based Reinforcement Learning: Applications on Robotics 这是一个2017年的论文 1. Introduction 强化学习&#xff08;Regulation Learning&#xff0c;RL&#xff0…

强化学习常见案例

文章目录 1. 有趣的强化学习视频1.1 小红小蓝捉迷藏1.2 红球绿球1.3 OpenAI机器人跑步1.4 OpenAI赛艇游戏&#xff08;CoastRunners&#xff09; 2.可以交互操作的游戏2.1 GridWorld2.2 Puck world 入门强化学习的时候&#xff0c;看到许多教程都给了很多强化学习的例子&#x…

多任务深度强化学习入门

理论概述 多任务深度强化学习&#xff0c;英文Multi-Task Deep Reinforcement Learning &#xff0c;简称MTDRL或MTRL。于MARL不同&#xff0c;它可以是单智能体多任务的情况&#xff0c;也可以是多智能体多任务的情况。 现在的深度强化学习领域虽然在很多特定任务上有着超越…

初探强化学习(11)Dyna类型的强化学习

为什么研究Dyna类型的强化学习呢&#xff1f; 主要是因为这个类型的强化学习是融合了model-based和model free两个类型的强化学习。 主要参考的博客是这个。说实话&#xff0c;我也是时隔三天后&#xff0c;第三次看了这个博客才彻底明白的。至于为什么名师&#xff0c;是因为我…

强化学习笔记:强化学习的约束

1 所需的样本数量过大 深度强化学习一个严重的问题在于需要巨大的样本量。举个例子&#xff0c;玩Atari游戏 图 19.17 中纵轴是算法的分数与 人类分数的比值&#xff0c; 100% 表示达到人类玩家的水准。 图中横轴是收集到的游戏帧数&#xff0c;即样本数量。 Rainbow DQN 需…

强化学习的模型

文章目录 前言一、强化学习是什么&#xff1f;二、基本模型1.基本框架2.学习过程 三.马尔科夫决策过程&#xff08;Markov Decision Process. MDP&#xff09;1.马尔科夫性质2.MDP的基本组成部分3.MDP的基本流程 四、基于模型和免模型的强化学习1.模型2.基于模型的强化学习&…

深度强化学习调研

深度强化学习&#xff08;DRL&#xff09; &#xff08;一&#xff09;强化学习 强化学习&#xff08;Reinforcement Learning&#xff0c;简称RL&#xff09;是机器学习领域的一个研究热点&#xff0c;当前已经广泛应用于工业制造、仿真模拟、机器人控制、优化与调度、游戏博…