system、 exec函数族、fork函数用法说明
system(), exec函数族, fork函数用法说明
启动一个新线程的方式:
system()该函数经常用来在C程序中调用shell脚本或者命令行程序.
特点:
效率低下,首先需要创建一个shell, 然后配置shell环境,之后再执行相应的命令。
对shell环境的依赖很大。
exec() 函数族也用来创建新的进程,但会替换原先的进程
int execl(const char *path, const char *arg0, …, (char *)0);
int execp(const char *file, const char *arg0, …, (char *)0);
int execle(const char *path, const char *arg0, …, (char *)0, char *const envp[]);
int execv(const char *path, const char *argv[]);
int execvp(const char *file, const char *argv[]);
int execve(const char *path, const cha *argv[], char *const envp[])
fork()复制原先的进程环境,从而创建一个新的子进程0
示例:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include "../main/basic_utilits.h"
void system_demo()
{
printf("Run a script file in C envirenment\n");
system("../shell/mkPasswd.sh 10");
system("ps aux | awk '{print $11}' | sort | uniq -c | sort -nr | awk '{print $2}' | head");
printf("shell script over\n");
}
void exec_funcs()
{
char *const ps_argv[] =
{"ps", "ax", 0};
char *const ps_envp[] =
{"PATH=/bin:/usr/bin:/usr/local/bin:/sbin", "TERM=console", 0};
execl("/bin/ps", "ps", "-ax", NULL);
execlp("ps", "ps", "ax", NULL);
execle("/bin/ps", "ps", "ax", NULL, ps_envp);
execv("/bin/ps", ps_argv);
execvp("ps", ps_argv);
execve("/bin/ps", ps_argv, ps_envp);
}
void fork_demo()
{
pid_t pid;
int exit_code = 0;
char *message = NULL;
int count = 0;
int i = 0;
pid = fork();
switch(pid){
case -1:
printf("fork_demo: fork error\n");
break;
case 0:
exit_code = 37;
count = 10;
message = "This is child process\n";
break;
default:
exit_code = 0;
count = 5;
message = "This is parent process\n";
break;
}
for(i=0;i<count;i++){
printf("%s", message);
my_msleep(500);
}
if(pid){
int stat_child = 0;
pid_t child_pid;
//child_pid = wait(&stat_child); //获取的退出码需要使用特定的宏函数进行操作
waitpid(pid, &stat_child, 0);
printf("child pid : %d stat_code: %d\n", pid, WEXITSTATUS(stat_child));
}
printf("exit code : %d\n", exit_code);
exit(exit_code);
}
system、 exec函数族、fork函数用法说明的更多相关文章
- Linux 环境下 fork 函数和 exec 函数族的使用
前言 接触 Linux 已经有几个月了,以前在网上看各路大神均表示 Windows 是最烂的开发平台,我总是不以为然,但是经过这段时间琢磨,确实觉得 Linux 开发给我带来不少的便利.下面总结一下学 ...
- system()、exec()、fork()三个与进程有关的函数的比较
启动新进程(system函数) system()函数可以启动一个新的进程. int system (const char *string ) 这个函数的效果就相当于执行sh –c string. 一般 ...
- [转帖]system()、exec()、fork()三个与进程有关的函数的比较
system().exec().fork()三个与进程有关的函数的比较 https://www.cnblogs.com/qingergege/p/6601807.html 启动新进程(system函数 ...
- exec函数族,守护进程,线程同步和互斥
2015.3.2 进程和程序有三点不同:1,存在位置不同,程序:硬盘,磁盘.进程:内存2. 程序是静态的,进程是动态的 执行./a.out -->bash->bash程序调用fork()- ...
- exec函数族的作用与讲解
apue看到第八章,对exec函数族的理解一直都很混乱,总觉得不对劲儿,其实不能理解的先暂时跳过,看到后面,再结合实例也就慢慢的理解了. 以下内容转自:http://www.cppblog.com/p ...
- linux exec函数族
1.简介 在Linux中,并不存在exec()函数,exec指的是一组函数,一共有6个,分别是: #include <unistd.h> extern char **environ; ...
- Linux进程控制——exec函数族
原文:http://www.cnblogs.com/hnrainll/archive/2011/07/23/2114854.html 1.简介 在Linux中,并不存在exec()函数,exec指的是 ...
- fork()和vfork()的区别,signal函数用法,exec()系列函数的用法小结
一:fork()和vfork()的区别: fork()函数可以创建子进程,有两个返回值,即调用一次返回两个值,一个是父进程调用fork()后的返回值,该返回值是刚刚创建的子进程的ID;另一个是子 ...
- Linux进程理解与实践(三)进程终止函数和exec函数族的使用
进程的几种终止方式(Termination) (1)正常退出 从main函数返回[return] 调用exit 调用_exit或者_Exit 最后一个线程从其启动处返回 从最后一个线程调用pthrea ...
随机推荐
- SpringMVC 源码解析笔记
作者笔记仓库:https://github.com/seazean/javanotes 欢迎各位关注我的笔记仓库,clone 仓库到本地后使用 Typora 阅读效果更好. 一.调度函数 请求进入原生 ...
- kali linux重启网卡失败:Job for networking.service failed because the control process exited with error code. See "systemctl status networking.service" and "journalctl -xe" for details. 问题排查
linux菜鸡的时候,总是为了配置网络而烦恼,重启网卡的原因有很多,我这次是因为配置了固定IP[使用第三方工具连接]所以需要重启网卡,出现 Job for networking.service fai ...
- 聊聊 PC 端自动化最佳方案 - WinAppDriver
1. 前言 大家好,我是安果! 一提到自动化,可能大家想到的是 App 端的 Appium.Airtest.AutoJS,亦或是 Selenium.Puppeteer.Cypress 等 Web 端的 ...
- MySQL:获取元数据
元数据就是描述数据的数据,在很多时候我们都需要查询元数据 比如:想知道数据库有多少个表,表里面有哪些字段,数据表是什么时候创建的.在什么时候更新过等等 使用SQL注入的时候也得获取数据库的元数据才能进 ...
- 启动Eclipse时,弹出JVM terminated. Exit code=127..错误的解决方案
在Linux环境下,启动Eclipse,会弹出并报如下的错误,且不能启动该工具 JVM terminated. Exit code=127/eclipse/jdk1.7.0_71/bin/java-D ...
- 《MySQL实战45讲》(8-15)笔记
MySQL实战45讲 目录 MySQL实战45讲 第八节: 事务到底是隔离的还是不隔离的? 在MySQL里,有两个"视图"的概念: "快照"在MVCC里是怎么工 ...
- LiteFlow 2.6.0版本发行注记,项目逻辑解耦的利器
前言 自从LiteFlow 2.5.X版本发布依赖,陆续经历了10个小版本的迭代.社区群也稳固增长,每天都有很多小伙伴在问我问题. 但是我发现最多人问我的还是:什么时候能支持界面编排? 从LiteFL ...
- spring中的组合模式
org.springframework.cache.support.CompositeCacheManager /* * Copyright 2002-2016 the original author ...
- 【原创】深入分析Ubuntu本地提权漏洞CVE-2017-16995
*本文首发阿里云先知安全技术社区,原文链接https://xz.aliyun.com/t/2212 前言: 2018年3月中旬,Twitter 用户 @Vitaly Nikolenko 发布消息,称 ...
- C# prism 框架 MVVM框架 Prism系列之事件聚合器
网址:https://www.cnblogs.com/ryzen/p/12610249.html 本文将介绍如何在.NET Core3环境下使用MVVM框架Prism的使用事件聚合器实现模块间的通信 ...