一、进程与信号之exec函数system函数

exec函数:

子进程调用exec函数执行另一个程序,exec函数进程完全由新程序代替,替换原有程序正文,数据,堆,栈段

#include <unistd.h>
extern char **environ;
int execl(const char *path,const char *arg, ...);
int execlp(const char *file,  const char *arg, ...);
int execle(const char *path,const char *arg, ..., char * const envp[]);
int execv(const char *path,char *const argv[]);
int execvp(const char *file, char *const argv[]);
int execve(const char *file, char *const argv[], char *const envp[]);

exec函数出错返回-1,成功不返回

execve是系统函数,其他都是库函数

system函数

#include <stdlib.h>

int system(const char *command)

返回:成功返回命令状态,出错返回-1
功能:简化exec函数使用

system函数内部构建一个子进程,由子进程调用exec函数
等同于/bin/bash -c "cmd" 或者 exec("bash","-c","cmd")
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

char *cmd1="/bin/date";

int main(void)
{
    system("clear");
    system(cmd1);

    return 0;
}

system函数源码

#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <unistd.h>
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("/bin/sh", "sh", "-c", cmdstring, (char *)0);
        exit(127);
        }
    else{
            while(waitpid(pid, &status, 0) < 0){
                if(errno != EINTER){
                    status = -1;
                    break;
                }
            }
        }

        return status;

}
时间: 2024-07-30 13:52:28

一、进程与信号之exec函数system函数的相关文章

第6章 进程控制(3)_wait、exec和system函数

5. 等待函数 (1)wait和waitpid 头文件 #include <sys/types.h> #include <sys/wait.h> 函数 pid_t wait(int* status); pid_t waitpid(pid_t pid, int* status, int options); 返回值 成功返回子进程ID,出错返回-1 功能 等待子进程退出并回收,防止僵尸进程的产生 参数 (1)status参数: ①为空时,代表任意状态结束的子进程: ②不为空时,则等待指

UNIX环境编程学习笔记(22)——进程管理之system 函数执行命令行字符串

lienhua342014-10-15 ISO C 定义了 system 函数,用于在程序中执行一个命令字符串.其声明如下, #include <stdlib.h> int system(const char *cmdstring); system 函数在其实现中调用了 fork.exec 和 waitpid 函数.system 函数调用 fork 函数创建子进程,然后由子进程调用’/bin/sh -c cmdstring’ 来执行命令行参数 cmdstring,此命令执行完后便返回调用的进程

2信号处理之:信号产生原因,进程处理信号行为,信号集处理函数,PCB的信号集,sigprocmask()和sigpending(),信号捕捉设定,sigaction,C标准库信号处理函数,可重入函数,

 1信号产生原因 2.进程处理信号行为 manpage里信号3中处理方式: SIG_IGN SIG_DFL                                            默认Term动作 a signal handling function 进程处理信号 A默认处理动作 term   中断 core    core(调试的时候产生) gcc –g file.c ulimit –c 1024 gdb a.out core ign      忽略 stop     停止

APUE学习笔记——10.18 system函数 与waitpid

system函数 system函数用方便在一个进程中执行命令行(一行shell命令). 用法如下: #include <stdio.h> #include <stdlib.h> int main() { printf("Hello\n"); system("sleep 5"); return 0; } 在程序中通过system调用了命令行 sleep 5.(这里知识举一个例子,当然可以执行一个类似" bash test.sh&quo

system函数的总结

最近在看APUE第10章中关于system函数的POSIX.1的实现.关于POSIX.1要求system函数忽略SIGINT和SIGQUIT,并且阻塞信号SIGCHLD的论述,理解得不是很透彻,本文就通过实际的实例来一探究竟吧. 一.为什么要阻塞SIGCHLD信号 #include <stdlib.h> int system(const char *command); 函数工作大致流程:system()函数先fork一个子进程,在这个子进程中调用/bin/sh -c来执行command指定的命

二十六、Linux 进程与信号---system 函数 和进程状态切换

26.1 system 函数 26.1.1 函数说明 system(执行shell 命令)相关函数 fork,execve,waitpid,popen 1 #include <stdlib.h> 2 int system(const char * string); 函数功能:简化 exec 函数 函数说明 system()会调用 fork() 产生子进程,由子进程来调用 /bin/sh -c string 来执行参数 string 字符串所代表的命令,此命令执行完后随即返回原调用的进程. 在调

进程控制(十二)---system函数

system函数是用来在进程中执行 shell 命令的.注意其实 exec 族函数其实也可以用来在进程中执行 shell 命令,但是这两个函数实现执行 shell 命令的原理是完全不同的. system函数相当于是 fork->exec->wait 这样的一个过程,也就是说system函数会调用fork函数来创建一个子进程,然后在子进程中去执行 shell命令,不影响父进程的执行. 而直接利用 exec 族函数来执行 shell命令是将 shell命令的代码段.数据段直接覆盖掉本进程原来的代码

PHP在Linux下Apache环境中执行exec,system,passthru等服务器命令函数

若在服务器中使用php test.php运行exec,system,passthru等命令相关的脚本能成功运行,在web页面却没反应,则 查看下apache的error_log日志,一般是这几个函数 system() has been disabled for security reasons in /var/www/html/test.php on line 34 报错了. 看下php.ini中的disable_functions 有没有禁用了这些函数,删除禁用后保存. 重启下php和Apac

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

本文转载自:http://www.cnblogs.com/mickole/p/3187974.html 一,system()理解 功能:system()函数调用“/bin/sh -c command”执行特定的命令,阻塞当前进程直到command命令执行完毕 原型: int system(const char *command); 返回值: 如果无法启动shell运行命令,system将返回127:出现不能执行system调用的其他错误时返回-1.如果system能够顺利执行,返回那个命令的退出