linux系统编程之进程(七):system()函数使用

一,香港服务器租用,system()理解

功能:system()函数调用“/bin/sh -c command”执行特定的命令,香港服务器租用,阻塞当前进程直到command命令执行完毕

原型:

int system(const char *command);

返回值:

如果无法启动shell运行命令,system将返回127;出现不能执行system调用的其他错误时返回-1。如果system能够顺利执行,返回那个命令的退出码。

说明:

man帮助:

       #include <stdlib.h>

       int system(const char *command);

DESCRIPTION        system()  executes a command specified in command by calling /bin/sh -c       command, and returns after the command has been completed.  During exe-        cution  of the command, SIGCHLD will be blocked, and SIGINT and SIGQUIT        will be ignored.

RETURN VALUE        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).

       If the value of command is NULL, system() returns non-zero if the shell        is available, and zero if not.

       system() does not affect the wait status of any other children.

二,system()函数原理

system函数执行时,会调用fork、execve、waitpid等函数。

linux版system函数的源码:

int system(const char * cmdstring) {pid_t pid;int status;if(cmdstring == NULL){return (1);}if((pid = fork())<0){status = -1;}else if(pid == 0){execl(, , , cmdstring, (char *)0);_exit(127); //子进程正常执行则不会执行此语句}else{while(waitpid(pid, &status, 0) < 0){if(errno != EINTER){status = -1;break;}}}return status; },虚拟主机人生没有彩排,只有现场直播,所以每一件事都要努力做得最好

linux系统编程之进程(七):system()函数使用

相关文章:

你感兴趣的文章:

标签云: