Linux启动新进程的几种方法及比较[转]
- #include <stdlib.h>
- int system (const char *string);
- #include <stdlib.h>
- #include <stdio.h>
- int main()
- {
- printf("Running ps with system\n");
- //ps进程结束后才返回,才能继续执行下面的代码
- system("ps au");// 1
- printf("ps Done\n");
- exit(0);
- }

- #include <unistd.h>
- char **environ;
- int execl (const char *path, const char *arg0, ..., (char*)0);
- int execlp(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, char *const argv[]);
- int execvp(cosnt char *file, char *const argv[]);
- int execve(const char *path, char *const argv[], char *const envp[]);
- char *const ps_envp[] = {"PATH=/bin:usr/bin", "TERM=console", 0};
- char *const ps_argv[] = {"ps", "au", 0};
- execl("/bin/ps", "ps", "au", 0);
- execlp("ps", "ps", "au", 0);
- execle("/bin/ps", "ps", "au", 0, ps_envp);
- execv("/bin/ps", ps_argv);
- execvp("ps", ps_argv);
- execve("/bin/ps", ps_argv, ps_envp);
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
- int main()
- {
- printf("Running ps with execlp\n");
- execlp("ps", "ps", "au", (char*)0);
- printf("ps Done");
- exit(0);
- }

- #include <sys/type.h>
- #include <unistd.h>
- pid_t fork();
- #include <unistd.h>
- #include <sys/types.h>
- #include <stdio.h>
- #include <stdlib.h>
- int main()
- {
- pid_t pid = fork();
- switch(pid)
- {
- case -1:
- perror("fork failed");
- exit(1);
- break;
- case 0:
- //这是在子进程中,调用execlp切换为ps进程
- printf("\n");
- execlp("ps", "ps", "au", 0);
- break;
- default:
- //这是在父进程中,输出相关提示信息
- printf("Parent, ps Done\n");
- break;
- }
- exit(0);
- }

- #include <sys/types.h>
- #include <sys/wait.h>
- pid_t wait(int *stat_loc);
- pid_t waitpid(pid_t pid, int *stat_loc, int options);
- #include <unistd.h>
- #include <sys/types.h>
- #include <stdio.h>
- #include <stdlib.h>
- int main()
- {
- pid_t pid = fork();
- int stat = 0;
- switch(pid)
- {
- case -1:
- perror("fork failed");
- exit(1);
- break;
- case 0:
- //这是在子进程中,调用execlp切换为ps进程
- printf("\n");
- execlp("ps", "ps", "au", 0);
- break;
- default:
- //这是在父进程中,等待子进程结束并输出相关提示信息
- pid = wait(&stat);
- printf("Child has finished: PID = %d\n", pid);
- //检查子进程的退出状态
- if(WIFEXITED(stat))
- printf("Child exited with code %d\n", WEXITSTATUS(stat));
- else
- printf("Child terminated abnormally\n");
- printf("Parent, ps Done\n");
- break;
- }
- exit(0);
- }

Linux启动新进程的几种方法及比较[转]的更多相关文章
- Linux启动新进程的几种方法汇总
有时候,我们需要在自己的程序(进程)中启动另一个程序(进程)来帮助我们完成一些工作,那么我们需要怎么才能在自己的进程中启动其他的进程呢?在Linux中提供了不少的方法来实现这一点,下面就来介绍一个这些 ...
- Linux启动新进程的几种方法及比较
有时候,我们需要在自己的程序(进程)中启动另一个程序(进程)来帮助我们完成一些工作,那么我们需要怎么才能在自己的进程中启动其他的进程呢?在Linux中提供了不少的方法来实现这一点,下面就来介绍一个这些 ...
- Linux启动新进程的三种方法
程序中,我们有时需要启动一个新的进程,来完成其他的工作.下面介绍了三种实现方法,以及这三种方法之间的区别. 1.system函数-调用shell进程,开启新进程system函数,是通过启动shell进 ...
- Linux中Kill进程的N种方法
常规篇: 首先,用ps查看进程,方法如下: $ ps -ef …… smx 1822 1 0 11:38 ? 00:00:49 gnome-terminal smx ...
- linux 下隐藏进程的一种方法
前言 本文所用到的工具在 https://github.com/gianlucaborello/libprocesshider 可以下载 思路就是利用 LD_PRELOAD 来实现系统函数的劫持 LD ...
- Linux之Kill进程的N种方法
常规篇: 首先,用ps查看进程,方法如下: $ ps -ef …… smx 1822 1 0 11:38 ? 00:00:49 gnome-terminal smx ...
- linux下查询进程占用的内存方法总结
linux下查询进程占用的内存方法总结,假设现在有一个「php-cgi」的进程 ,进程id为「25282」.现在想要查询该进程占用的内存大小.linux命令行下有很多的工具进行查看,现总结常见的几种方 ...
- python实现Linux启动守护进程
python实现Linux启动守护进程 DaemonClass.py代码: #/usr/bin/env python # -*- coding: utf-8 -*- import sys import ...
- Linux 下操作GPIO(两种方法,驱动和mmap)(转载)
目前我所知道的在Linux下操作GPIO有两种方法: 1.编写驱动,这当然要熟悉Linux下驱动的编写方法和技巧,在驱动里可以使用ioremap函数获得GPIO物理基地址指针,然后使用这个指针根据io ...
随机推荐
- ES6就是ES2015 的主要内容
转自 https://segmentfault.com/a/1190000004365693 ECMAScript 6(以下简称ES6)是JavaScript语言的下一代标准.因为当前版本的ES6是在 ...
- iOS 加载Image的两种方式
Apple官方文档对于加载image提供了两个方法 + (nullable UIImage *)imageNamed:(NSString *)name; + (nullable UIImage *)i ...
- C#:继承多态的方法实现数的简单加减乘除运算
// 定义一个抽象的父类 abstract class Figure { //声明抽象方法: //父类中的所有家里人可以用的方法必须都应用到子类中 ...
- oracle_index的建立、修改、删除
索引索引是关系数据库中用于存放每一条记录的一种对象,主要目的是加快数据的读取速度和完整性检查.建立索引是一项技术性要求高的工作.一般在数据库设计阶段的与数据库结构一道考虑.应用系统的性能直接与索引的合 ...
- iOS ARC与MRC混编的一些解决方法
1. ARC & MRC 混合开发 在项目开发中,遇到使用MRC开发的第三方库怎么办? 例如:ASI 1> 尝试使用Xcode的转换工具(失败率比较高) 2> 在编译选项中,为MR ...
- unionpay技术服务开放平台
URL: https://open.unionpay.com/ajweb/index USER: jimingsong PWD: qweasd
- pyhon的数据类型
1.数字 整型和浮点型 在32位机器上,整数的位数为32位,取值范围为-2**31-2**31-1,即-2147483648-2147483647在64位系统上,整数的位数为64位,取值范围为-2** ...
- 跑github上的Symfony项目遇到的问题2
最近学习symfony框架,下载了大量的github上的别人的项目,要想跑起别人的项目,总结了以下几个步骤: 第一, 克隆一份代码; 第二,安装依赖,前提是安装了composer, 1:在你下载的项目 ...
- Git之”make sure you have the correct access…”
git 命令在windows下无法使用pull.fetch.push等命令,提示 “please make sure you have the correct access and the repos ...
- JavaScript忍者秘籍——驯服线程和定时器
1.定时器和线程 - 设置和清除定时器 JavaScript提供了两种方式,用于创建定时器以及两个相应的清除方法.这些方法都是window对象上的方法. 方法 格式 描述 setTimeout i ...