Linux创建进程

创建进程:Unix--fork();windows--CreateProcess(...);

终止进程:Unix--exit(EXIT_SUCCESS),kill();windows--ExitProcess(...),TerminateProcess(...);

Linux启动新进程
方法有三个:
1)system函数
#include <stdlib.h>
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 <unistd.h>
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 <sys/types.h>
#include <unistd.h>
pid_t fork();
    创建一个新进程,复制当前进程,进程表中创建一个新的表项,新表项的许多属性与当前进程相同,新进程有自己的
数据空间,环境,文件描述符。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值