linux 中进程为0,linux中进程0是怎么被创建的?

你说的问题我也不太懂,不过我这有些代码可以给你参考下:

建立一个新的进程forkdemo1.c:

/* forkdemo1.c

* shows how fork creates two processes, distinguishable

* by the different return values from fork()

*/

#include

main()

{

int ret_from_fork, mypid;

mypid = getpid(); /* who am i? */

printf("Before: my pid is %d\n", mypid); /* tell the world */

ret_from_fork = fork();

sleep(1);

printf("After: my pid is %d, fork() said %d\n",

getpid(), ret_from_fork);

}

子进程创建进程frokdemo2.c:

/* forkdemo2.c - shows how child processes pick up at the return

* from fork() and can execute any code they like,

* even fork(). Predict number of lines of output.

*/

main()

{

printf("my pid is %d\n", getpid() );

fork();

fork();

fork();

printf("After, my pid is %d\n", getpid() );

}

分辨父进程和子进程forkdemo3.c:

/* forkdemo3.c - shows how the return value from fork()

* allows a process to determine whether

* it is a child or process

*/

#include

main()

{

int fork_rv;

printf("Before: my pid is %d\n", getpid());

fork_rv = fork(); /* create new process */

if ( fork_rv == -1 ) /* check for error */

perror("fork");

else if ( fork_rv == 0 )

printf("I am the child. my pid=%d\n", getpid());

else

printf("I am the parent. my child is %d\n", fork_rv);

}

父进程等待子进程的退出waitdemo.c:

/* waitdemo.c - shows how parent pauses until child finishes

*/

#include

#define DELAY 2

main()

{

int newpid;

void child_code(), parent_code();

printf("before: mypid is %d\n", getpid());

if ( (newpid = fork()) == -1 )

perror("fork");

else if ( newpid == 0 )

child_code(DELAY);

else

parent_code(newpid);

}

/*

* new process takes a nap and then exits

*/

void child_code(int delay)

{

printf("child %d here. will sleep for %d seconds\n", getpid(), delay);

sleep(delay);

printf("child done. about to exit\n");

exit(17);

}

/*

* parent waits for child then prints a message

*/

void parent_code(int childpid)

{

int wait_rv; /* return value from wait() */

wait_rv = wait(NULL);

printf("done waiting for %d. Wait returned: %d\n", childpid, wait_rv);

}

取消

评论

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值