#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#define err_sys(x) do { perror(x); exit(1); } while (0)
void invoke_child(char ch)
{
printf("%c\n", ch);
}
int main(int argc, char *argv[])
{
pid_t pid;
int cnt = 3;
char arg[] = "abc";
while(cnt--) {
if ((pid = fork()) < 0) {
err_sys("Fork error");
} else if (pid == 0) {
invoke_child(arg[cnt]);
exit(0);
}
}
return 0;
}
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main(void)
{
int i;
for(i=0; i<2; i++){
fork();
printf("-");
}
return 0;
}
printf("-n");
printf("-");
fflush(stdout);
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main(void)
{
int i;
for(i=0; i<2; i++){
fork();
//注意:下面的printf有“n”
printf("ppid=%d, pid=%d, i=%d n", getppid(), getpid(), i);
}
sleep(10); //让进程停留十秒,这样我们可以用pstree查看一下进程树
return 0;
}
ppid=8858, pid=8518, i=0 ppid=8858, pid=8518, i=1 ppid=8518, pid=8519, i=0 ppid=8518, pid=8519, i=1 ppid=8518, pid=8520, i=1 ppid=8519, pid=8521, i=1
$ pstree -p | grep fork
|-bash(8858)-+-fork(8518)-+-fork(8519)---fork(8521)
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有