Linux之system函数--在程序中执行命令

NAME
       system - execute a shell command

SYNOPSIS
       #include <stdlib.h>

       int system(const char *command);

DESCRIPTION
       The  system()  library  function uses fork(2) to create a child process that executes the
       shell command specified in command using execl(3) as follows:

           execl("/bin/sh", "sh", "-c", command, (char *) 0);

(其实就是使用fork创建一个sh进程,然后将命令传递给sh进程执行命令,注意这里要加一个“-c”选项,不然不能执行可执行命令而只能执行脚本命令。其实我自己也这样用过,但是不是使用sh来执行命令,而是直接把可执行程序用execl函数加载执行,这样也可以实现类似的功能,但是不能执行脚本文件/文件,因为脚本命令不是可执行文件,脚本文件是使用sh解析执行的。


       system() returns after the command has been completed.(注意,系统在命令执行完成的之后返回)

       During execution of the command, SIGCHLD will be blocked, and SIGINT and SIGQUIT will  be
       ignored,  in  the process that calls system() (these signals will be handled according to
       their defaults inside the child process that executes command).

       If command is NULL, then system() returns a status indicating whether a shell  is  avail‐
       able on the system

RETURN VALUE
       The return value of system() is one of the following:

       *  If  command is NULL, then a nonzero value if a shell is available, or 0 if no shell is
          available.

       *  If a child process could not be created, or its status could  not  be  retrieved,  the
          return value is -1.

       *  If  a  shell  could  not be executed in the child process, then the return value is as
          though the child shell terminated by calling _exit(2) with the status 127.

       *  If all system calls succeed, then the return value is the termination  status  of  the
          child shell used to execute command.  (The termination status of a shell is the termi‐
          nation status of the last command it executes.)

       In the last two cases, the return value is a "wait status" that can be examined using the
       macros described in waitpid(2).  (i.e., WIFEXITED() WEXITSTATUS() and so on).

       system() does not affect the wait status of any other children.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值