从 C 中的 child 中获取 Parent 进程的 ID
Getting the id of a Parent Process from its child in C
我是一名计算机科学专业的学生,我目前正在上一门要求我们使用 C 的操作系统课程
我试图解决的一个问题如下:
编写一个程序,派生一个 child。
child 应该休眠 5 秒。
child 应该打印“我准备谋杀我的 parent!”
child 应该通过信号杀死它的 parent(使用 kill(parent_id,SIGINT))。
child 应该打印“我现在是孤儿”。
parent 应该等待 child 并打印“我是 parent”。
我尝试使用以下代码解决它,但我无法找到获取 parent 的 ID 的方法。感谢任何帮助,在此先感谢 :)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <signal.h>
int main ()
{
pid_t pid =fork();
if (pid<0)
{
printf("%s\n","Error in forking");
}
else if (pid==0)//child
{
sleep(5);
printf("%s","I am ready to murder my parent!");
kill(//parent id here,SIGINT);
printf("%s","I am an orphan now");
}
else{ // parent
printf("%s\n","I am the parent");
}
return 0;
}
您可以使用函数getppid()
getppid() returns the process ID of the parent of the calling process.
正如哈里斯在他的 , You should use getppid() 中提到的那样,要获得父级的 pid
。
回应您的评论,
well i tried it , and lol that might seems funny , but my program killed my os , my linux machine shuted down
让我们看看你的代码,
else if (pid==0){ //child
sleep(5);
printf("%s","I am ready to murder my parent!");
kill(//parent id here,SIGINT);
printf("%s","I am an orphan now");
}
else{ // parent
printf("%s\n","I am the parent");
}
父进程执行完printf("%s\n","I am the parent");
后会做什么?它终止了。
那么,谁是原始父进程已终止的进程的父进程?子进程成为孤儿。引用自 Wikipedia
An orphan process is a computer process whose parent process has finished or terminated, though it remains running itself. In a Unix-like operating system any orphaned process will be immediately adopted by the special init system process.
因此,当您调用 kill()
时,您是在 init
进程中这样做。这就是 lol that might seems funny , but my program killed my os , my linux machine shuted down
的原因
一定要看看 this answer。
我是一名计算机科学专业的学生,我目前正在上一门要求我们使用 C 的操作系统课程 我试图解决的一个问题如下:
编写一个程序,派生一个 child。
child 应该休眠 5 秒。
child 应该打印“我准备谋杀我的 parent!”
child 应该通过信号杀死它的 parent(使用 kill(parent_id,SIGINT))。
child 应该打印“我现在是孤儿”。
parent 应该等待 child 并打印“我是 parent”。
我尝试使用以下代码解决它,但我无法找到获取 parent 的 ID 的方法。感谢任何帮助,在此先感谢 :)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <signal.h>
int main ()
{
pid_t pid =fork();
if (pid<0)
{
printf("%s\n","Error in forking");
}
else if (pid==0)//child
{
sleep(5);
printf("%s","I am ready to murder my parent!");
kill(//parent id here,SIGINT);
printf("%s","I am an orphan now");
}
else{ // parent
printf("%s\n","I am the parent");
}
return 0;
}
您可以使用函数getppid()
getppid() returns the process ID of the parent of the calling process.
正如哈里斯在他的 pid
。
回应您的评论
well i tried it , and lol that might seems funny , but my program killed my os , my linux machine shuted down
让我们看看你的代码,
else if (pid==0){ //child
sleep(5);
printf("%s","I am ready to murder my parent!");
kill(//parent id here,SIGINT);
printf("%s","I am an orphan now");
}
else{ // parent
printf("%s\n","I am the parent");
}
父进程执行完printf("%s\n","I am the parent");
后会做什么?它终止了。
那么,谁是原始父进程已终止的进程的父进程?子进程成为孤儿。引用自 Wikipedia
An orphan process is a computer process whose parent process has finished or terminated, though it remains running itself. In a Unix-like operating system any orphaned process will be immediately adopted by the special init system process.
因此,当您调用 kill()
时,您是在 init
进程中这样做。这就是 lol that might seems funny , but my program killed my os , my linux machine shuted down
一定要看看 this answer。