linux 下,已知子进程的情况下,打印(显示)父进程

linux 下,已知子进程的情况下,打印(显示)父进程,第1张

在Linux下,可以有两种比较方便的方法:

1. # pstree

通过系统的进程树来查看某个进程的父进程;

2. # ps -ef |grep <进程名>

显示的输出中,第三列就是该进程的父进程PID,然后可以再使用ps命令来查看父进程的名称

# ps -ef |grep <父进程PID>

这应该就是你想要的结果吧?

#include <stdio.h>

#include <string.h>

#include <unistd.h>

#include <stdlib.h>

#include <sys/types.h>

#include <sys/wait.h>

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=1i<=ni++)

{

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

}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/8154476.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-13
下一篇2023-04-13

发表评论

登录后才能评论

评论列表(0条)

    保存