Linux启动新进程的三种方法启动新进程的三种方法
本篇文章主要介绍了Linux启动新进程的三种方法以及这三种方法之间的区别。具有很好的参考价值。下面跟着
小编一起来看下吧
程序中,我们有时需要启动一个新的进程,来完成其他的工作。
下面介绍了三种实现方法,以及这三种方法之间的区别。
1.system函数函数-调用调用shell进程,开启新进程进程,开启新进程
system函数,是通过启动shell进程,然后执行shell命令进程。
原型:
int system(const char *string);
string:shell命令字符串
返回值:成功返回命令退出码,无法启动shell,返回127错误码,其他错误,返回-1。
代码示例如下:
process_system.c
#include<stdlib.h>
#include<stdio.h>
int main()
{
printf("Running ps with system");
int code = system("ps au");//新进程结束后,system函数才返回
//int code = system("ps au");//system函数立即返回
printf("%d",code);
printf("ps Done");
exit(0);
}
输出结果:
system函数,在启动新进程时,必须先启动shell进程,因此使用system函数的效率不高。
2.exec系列函数系列函数-替换进程映像替换进程映像
exec系列函数调用时,启动新进程,替换掉当前进程。即程序不会再返回到原进程,
除非exec调用失败。
exec启动的新进程继承了原进程的许多特性,如在原进程中打开的文件描述符在新进程中仍保持打开。
需要注意的是,在原进程中打开的文件流在新进程中将关闭。原因在于,我们在前面讲过进程间通信的方式,进程之间需要管
道才能通信。
原型:
int execl(const char *path,const char *arg0,...,(char*)0);
int execlp(const char *file,const char *arg0,...,(char*)0);