日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > linux >内容正文

linux

linux父进程中显示子进程pid,请教linux下c语言函数fork父进程打印子进程的PID

發(fā)布時(shí)間:2025/3/15 linux 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux父进程中显示子进程pid,请教linux下c语言函数fork父进程打印子进程的PID 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

請教linux下c語言函數(shù)fork父進(jìn)程打印子進(jìn)程的PID

關(guān)注:296??答案:2??信息版本:手機(jī)版

解決時(shí)間 2019-01-14 04:55

雨不眠的下

2019-01-13 12:23

用于輸入:n (在父進(jìn)程中輸入)

輸出:從1到n的整數(shù)(子進(jìn)程負(fù)責(zé)打印)

父進(jìn)程打印子進(jìn)程的PID,然后等待子進(jìn)程結(jié)束,最后輸出child complete,退出系統(tǒng)

最佳答案

逐風(fēng)

2019-01-13 12:37

#include

#include

#include

#include

#include

#include

int main()

{

int pipe_fds[2];

int pid;

if(pipe(pipe_fds))

{

fprintf(stderr,"pipe error!\n");

return -1;

}

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

{

fprintf(stderr, "fork error!\n");

return -1;

}

if(pid == 0)

{

char buf[20] = {0};

int n,i;

close(pipe_fds[1]);

read(pipe_fds[0],buf,sizeof(buf));

n=atoi(buf);

for(i=1;i<=n;i++)

{

if(i%10 == 0)

printf("\n");

printf("%d\t",i);

}

close(pipe_fds[0]);

}

else

{

int m;

char bf[20] = {0};

close(pipe_fds[0]);

printf("the child pid:%d\n",pid);

printf("please input number:");

scanf("%d",&m);

printf("\n");

sprintf(bf,"%d",m);

write(pipe_fds[1],bf,sizeof(bf));

wait(NULL);

printf("child complete!\n");

}

return 0;

}

全部回答

1樓時(shí)間的塵埃

2019-01-13 14:07

#include

#include

#include

int main()

{

pid_t pid;

char *message;

int n;

pid_t chldpid;

pid_t ppid;

printf("fork program starting\n");

pid = fork();

switch(pid)

{

case -1:

perror("fork failed");

_exit(1);

case 0:

chldpid = getpid();

printf("chldpid = %ld ppid=%ld\n",(unsigned int)chldpid,(unsigned int)getppid());

message = "this is the child";

n = 5;

break;

default:

ppid = getpid();

printf("ppid = %ld ppid=%ld\n",(unsigned int)ppid,(unsigned int)getppid());

message = "this is the parent";

n = 3;

break;

}

for(; n > 0; n--) {

puts(message);

sleep(1);

}

_exit(0);

}

我要舉報(bào)

如以上問答內(nèi)容為色情/暴力/低俗/不良/侵權(quán)等信息,可以點(diǎn)下面鏈接進(jìn)行舉報(bào),我們會(huì)做出相應(yīng)處理,感謝你的支持!

大家都在看

推薦資訊

總結(jié)

以上是生活随笔為你收集整理的linux父进程中显示子进程pid,请教linux下c语言函数fork父进程打印子进程的PID的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。