Return value about System("command")
2011-05-11 14:32
369 查看
System函数:
是由fork、execve和waitpid三个系统调用实现的。
例如执行:system("ls /tm") --返回值为256
所以如果execve出错,则直接调用_exit(256),所以变量ret的值等于256.
在system函数执行时,会启动一个子进程运行shell,然后通过将ls /tm作为参数传给shell,如果shell命令运行有错,就调用exit XXX作为system的返回值返回,而XXX是system函数的返回值,而不是shell运行ls /tm后的返回值。
Exit函数:
exit时,退出状态码只能是一个字节,超过部分被截取.
在父进程获取其状态时,把这个字节的数据放在了低地址第二个字节位置了.这些都是对补码操作的.
因此,exit (-1) 和exit (255)时,父进程取的状态码是一样的,你无法区分
因此,我们一般只根据退出状态为0或则非0来判断成功或失败,而不做进一步的区分.
注意:
如果把system的返回值除以256就可以得到真正的返回值了,因为system()返回的结果分两个部分,前面是命令的返回值,后面是运行因为信号终止时的信号值。所以要/256就是这个原因。
是由fork、execve和waitpid三个系统调用实现的。
例如执行:system("ls /tm") --返回值为256
所以如果execve出错,则直接调用_exit(256),所以变量ret的值等于256.
在system函数执行时,会启动一个子进程运行shell,然后通过将ls /tm作为参数传给shell,如果shell命令运行有错,就调用exit XXX作为system的返回值返回,而XXX是system函数的返回值,而不是shell运行ls /tm后的返回值。
Exit函数:
exit时,退出状态码只能是一个字节,超过部分被截取.
在父进程获取其状态时,把这个字节的数据放在了低地址第二个字节位置了.这些都是对补码操作的.
因此,exit (-1) 和exit (255)时,父进程取的状态码是一样的,你无法区分
因此,我们一般只根据退出状态为0或则非0来判断成功或失败,而不做进一步的区分.
注意:
如果把system的返回值除以256就可以得到真正的返回值了,因为system()返回的结果分两个部分,前面是命令的返回值,后面是运行因为信号终止时的信号值。所以要/256就是这个原因。
相关文章推荐
- About the return value of the main function
- "Can't use method return value in write context"错误提示
- 属性的字符串表示形式“<%=Value %>"创建“System.Int32”类型的对象。
- About the return value of SetTimer function
- set command will return the value of the argument( variable, actually )
- LinkButton->OnCommand、OnClientClick="javascript:return confirm('确定删除吗?')"、href="javascript:void(0)"
- PowerShell: 如何解决File **.ps1 cannot be loaded because the execution of scripts is disabled on this system. Please see "get- help about_sig" for more de
- [VS Tip]About "of" command in mini comand window in VS Find Combox
- One point about the return value in Javascript
- cmd.Parameters["@Input"].Value = System.DBNull.Value
- wchar_t*,wchar_t,wchat_t数组,char,char*,char数组,std::string,std::wstring,CString 以及system("command")
- System call return value
- Change the value of "Maximum number of concurrent connections" about SQL Server 2012
- not authorized on test to execute command { $eval: \"return 1111\"
- Cannot modify the return value of 'System...ClientRectangle' because it is not a variable
- 调用js报错:try { document.getElementById("").SetReturnValue(__flash__toXML(MY_FUNCTION(
- SystemConfig.getPropertyValue("test");配置文件已经加了test=abc,但是取得时候空字符串
- The return value of "mktime" in different time zone
- 003 about zip command in window system
- Windows 10+Ubuntu 16.04在MBR分区上安装双系统之后启动菜单的System Setup选项提示:can't find command "fwsetup"