linux启动进程 c,如何在C,C++的Linux操作系统上启动进程

标准UNIX方式是通过电话exec(3)使用fork(2)其次(还有的全家他们,选择任何一个适合您需要的最好的)。

例子来说明使用:

#include

#include

#include

int main(int argc, char **argv)

{

pid_t pid;

printf("before fork\n");

if ((pid = fork()) < 0) {

//It may fail -- super rare

perror("Fork failed");

} else if (pid > 0) {

//If it returns a positive number, you're in the parent process and pid holds the pid of the child

printf("Mah kid's pid is %d\n", pid);

printf("Mine's %d\n", getpid());

} else {

//If it returns zero, you're in the child process

//you can do some preparatory work here (e.g., close filedescriptors)

printf("I'm the child and my pid is %d\n", getpid());

//exec will replace the process image with that of echo (wherever in the PATH environment variable it is be found (using the execlP version here)

execlp("echo", "echo", "hello world from echo; I'm now the child because I've replaced the original child because it called an exec function", (char*)NULL);

printf("This won't run because now we're running the process image of the echo binary. Not this.");

}

return EXIT_SUCCESS;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值