Linux创建进程
创建进程:Unix—fork();windows—CreateProcess(…);
终止进程:Unix—exit(EXIT_SUCCESS),kill();windows—ExitProcess(…),TerminateProcess(…);
Linux启动新进程
方法有三个:
1)system函数
#include
int system(const char *string);//运行以字符串形式传递给他的命令。局限性是必须等待system函数启动的Shell返回,
前台进程执执行完时shell返回,后台进程已启动shell返回。
才能继续。system函数在启动进城之前必须启动一个shell,所以system函数效率不高。
eg:
#include <stdlib.h>
#include <stdio.h>
int main()
{
printf("Running ps with system\n");
system("ps ax &");
printf("Done.\n");
exit(0);
}
2)替换进程映像——exec系列函数,将当前进程替换为一个新进程,新进程由path或file参数指定,原程序不再执行。
#include
char **environ;
int execl(const char *path,const char *arg0,…,NULL);
int execlp(const char *file,const char *arg0,…,NULL);
int execle(const char *path,const char *arg0,…,NULL,char *const envp[]);
int execv(const char *path,const char *const argv[],NULL);//argv以NULL结尾
int execvp(const char *file,const char *const argv[],NULL);
int execve(const char *path,const char *const argv[],NULL,char *const envp[]);//基础系统调用,其它函数可由他实现。
l—list,参数列表;v—vector,参数数组;p—path,$PATH变量;e—environment环境变量
eg:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Running ps with execlp\n");
execlp("ps", "ps", "ax", 0);
printf("Done.\n");
exit(0);
}
3)复制进程映像-fork()
#include
#include
pid_t fork();
创建一个新进程,复制当前进程,进程表中创建一个新的表项,新表项的许多属性与当前进程相同,新进程有自己的
数据空间,环境,文件描述符。
还没有评论,来说两句吧...