一、进程与信号之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函数出错返回-,成功不返回 execve是系统函数,其他都是库函数
system函数
#include <stdlib.h> int system(const char *command) 返回:成功返回命令状态,出错返回-
功能:简化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 ;
}
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 ();
} if((pid = fork())<){
status = -;
} else if(pid = ){
execl("/bin/sh", "sh", "-c", cmdstring, (char *));
exit();
}
else{
while(waitpid(pid, &status, ) < ){
if(errno != EINTER){
status = -;
break;
}
}
} return status; }
一、进程与信号之exec函数system函数的更多相关文章
- 第6章 进程控制(3)_wait、exec和system函数
5. 等待函数 (1)wait和waitpid 头文件 #include <sys/types.h> #include <sys/wait.h> 函数 pid_t wait(i ...
- UNIX环境编程学习笔记(22)——进程管理之system 函数执行命令行字符串
lienhua342014-10-15 ISO C 定义了 system 函数,用于在程序中执行一个命令字符串.其声明如下, #include <stdlib.h> int system( ...
- system函数遇到的问题
这几天调程序(嵌入式linux),发现程序有时就莫名其妙的死掉,每次都定位在程序中不同的system()函数,直接在shell下输入system()函数中调用的命令也都一切正常.就没理这个bug,以 ...
- APUE学习笔记——10.18 system函数 与waitpid
system函数 system函数用方便在一个进程中执行命令行(一行shell命令). 用法如下: #include <stdio.h> #include <stdlib.h> ...
- system函数的总结
最近在看APUE第10章中关于system函数的POSIX.1的实现.关于POSIX.1要求system函数忽略SIGINT和SIGQUIT,并且阻塞信号SIGCHLD的论述,理解得不是很透彻,本文就 ...
- 二十六、Linux 进程与信号---system 函数 和进程状态切换
26.1 system 函数 26.1.1 函数说明 system(执行shell 命令)相关函数 fork,execve,waitpid,popen #include <stdlib.h> ...
- UNIX高级环境编程(10)进程控制(Process Control)- 竞态条件,exec函数,解释器文件和system函数
本篇主要介绍一下几个内容: 竞态条件(race condition) exec系函数 解释器文件 1 竞态条件(Race Condition) 竞态条件:当多个进程共同操作一个数据,并且结果依赖 ...
- 二十五、Linux 进程与信号---exec函数
25.1 介绍 在用 fork 函数创建子进程后,子进程往往要调用一种 exec 函数以执行另一个程序 当进程调用一种 exec 函数时,该进程完全由新程序代换,替换原有进程的正文,而新程序则从其 m ...
- 信号之system函数
在http://www.cnblogs.com/nufangrensheng/p/3512291.html中已经有了一个system函数的实现,但是该版本并不执行任何信号处理.POSIX.1要求sys ...
随机推荐
- 【转载】ASP.NET线程安全与静态变量的生命周期浅谈
ASP.NET线程安全所涉及的是什么呢?让我们先来看看静态变量的生命周期问题,下面是我理解的静态变量的生命周期: void Application_Start开始 void Application_E ...
- Wpf控件ListBox使用实例2
2.Xaml绑定选择结果 <StackPanel Orientation="Vertical"> <TextBlock Margin="10,10,10 ...
- HTML5 File 对象
实例说明1: <div class="container"> <input type="file" id="file" m ...
- gephi安装好了,为何打不开?
ref: http://www.zhihu.com/question/21268129?sort=created 这个软件我自己也没有弄过,不过我同学要不会装,所以我测试地帮她装,得益于这个哥们的发的 ...
- cocoapods安装失败
ERROR: While executing gem ... (Gem::FilePermissionError) You don't have write permissions for the ...
- C++ 异常处理执行过程
看<clean code>时,又遇到异常处理的例程. 看不明白是因为我一直都将异常处理束之高阁. 今天晚上下决心去找资料看看,看完之后觉得以前是把它想得太难,其实非常简单. 希望以后遇到问 ...
- C#防SQL注入代码的实现方法
对于网站的安全性,是每个网站开发者和运营者最关心的问题.网站一旦出现漏洞,那势必将造成很大的损失.为了提高网站的安全性,首先网站要防注入,最重要的是服务器的安全设施要做到位. 下面说下网站防注入的几点 ...
- JavaScript重复元素处理
判断一个字符串中出现次数最多的字符,统计这个次数 //将字符串的字符保存在一个hash table中,key是字符,value是这个字符出现的次数 var str = "abcdefgadd ...
- Phalcon 的分流bootstrap 设计 主程序入口
<?php use \Phalcon\DI\FactoryDefault as PhDi; error_reporting(E_ALL); date_default_timezone_set(' ...
- Flask学习记录之使用Werkzeug散列密码
数据库中直接存放明文密码是很危险的,Werkzeug库中的security能够方便的实现散列密码的计算 security库中 generate_password_hash(password,metho ...