fork() 子进程和父进程

发布于 2024-12-02 09:54:17 字数 869 浏览 4 评论 0原文

我正在尝试创建一个使用 fork() 来创建新进程的程序。示例输出应如下所示:

这是子进程。我的 pid 是 733,我父母的 id 是 772。
这是父进程。我的 pid 是 772,我孩子的 id 是 773。

这就是我编写程序的方式:

#include <stdio.h>
#include <stdlib.h>

int main() {
    printf("This is the child process. My pid is %d and my parent's id is %d.\n", getpid(), fork());

    return 0;
}

这导致输出:

这是子进程。我的 pid 是 22163,我父母的 id 是 0。
这是子进程。我的 pid 是 22162,我父母的 id 是 22163。

为什么它打印了两次语句,如何让它在第一句中显示子 id 后正确显示父母的 id?

编辑:

#include <stdio.h>
#include <stdlib.h>

int main() {
int pid = fork();

if (pid == 0) {
    printf("This is the child process. My pid is %d and my parent's id is %d.\n", getpid(), getppid());
}
else {
    printf("This is the parent process. My pid is %d and my parent's id is %d.\n", getpid(), pid);
}

return 0;
}

I am trying to create a program that uses fork() to create a new process. The sample output should look like so:

This is the child process. My pid is 733 and my parent's id is 772.
This is the parent process. My pid is 772 and my child's id is 773.

This is how I coded my program:

#include <stdio.h>
#include <stdlib.h>

int main() {
    printf("This is the child process. My pid is %d and my parent's id is %d.\n", getpid(), fork());

    return 0;
}

This results in the output:

This is the child process. My pid is 22163 and my parent's id is 0.
This is the child process. My pid is 22162 and my parent's id is 22163.

Why is it printing the statement twice and how can I get it to properly show the parent's id after the child id displays in the first sentence?

EDIT:

#include <stdio.h>
#include <stdlib.h>

int main() {
int pid = fork();

if (pid == 0) {
    printf("This is the child process. My pid is %d and my parent's id is %d.\n", getpid(), getppid());
}
else {
    printf("This is the parent process. My pid is %d and my parent's id is %d.\n", getpid(), pid);
}

return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

我的影子我的梦 2024-12-09 09:54:17

首先阅读fork 手册页以及getppid / getpid 手册页。

来自叉子的

成功时,子进程的 PID 将在父进程中返回
执行线程,并且在子线程中返回 0
执行。失败时,将在父级上下文中返回 -1,
不会创建任何子进程,并且 errno 将被适当设置。

所以这应该是关于你的问题的内容

if ((pid=fork())==0){
    printf("yada yada %u and yada yada %u",getpid(),getppid());
}
else{ /* avoids error checking*/
    printf("Dont yada yada me, im your parent with pid %u ", getpid());
}

这是子进程。我的 pid 是 22163,我父母的 id 是 0。

这是子进程。我的 pid 是 22162,我父母的 id 是
22163。

fork()printf 之前执行。因此,完成后,您将有两个进程执行相同的指令。因此,printf会执行两次。调用fork()将向子进程返回0,并向父进程返回子进程的pid

您将获得两个正在运行的进程,每个进程都会执行此 instruction 语句:

printf ("... My pid is %d and my parent's id is %d",getpid(),0); 

printf ("... My pid is %d and my parent's id is %d",getpid(),22163);  

~

总结一下,上面的行是子进程,指定其 pid。第二行是父进程,指定其 id (22162) 及其子进程 (22163)。

Start by reading the fork man page as well as the getppid / getpid man pages.

From fork's

On success, the PID of the child process is returned in the parent's
thread of execution, and a 0 is returned in the child's thread of
execution. On failure, a -1 will be returned in the parent's context,
no child process will be created, and errno will be set appropriately.

So this should be something down the lines of

if ((pid=fork())==0){
    printf("yada yada %u and yada yada %u",getpid(),getppid());
}
else{ /* avoids error checking*/
    printf("Dont yada yada me, im your parent with pid %u ", getpid());
}

As to your question:

This is the child process. My pid is 22163 and my parent's id is 0.

This is the child process. My pid is 22162 and my parent's id is
22163.

fork() executes before the printf. So when its done, you have two processes with the same instructions to execute. Therefore, printf will execute twice. The call to fork() will return 0 to the child process, and the pid of the child process to the parent process.

You get two running processes, each one will execute this instruction statement:

printf ("... My pid is %d and my parent's id is %d",getpid(),0); 

and

printf ("... My pid is %d and my parent's id is %d",getpid(),22163);  

~

To wrap it up, the above line is the child, specifying its pid. The second line is the parent process, specifying its id (22162) and its child's (22163).

可爱暴击 2024-12-09 09:54:17

它打印该语句两次,因为它同时为父级和子级打印该语句。父级的父级 id 为 0

尝试如下操作:

 pid_t  pid;
 pid = fork();
 if (pid == 0) 
    printf("This is the child process. My pid is %d and my parent's id is %d.\n", getpid(),getppid());
 else 
    printf("This is the parent process. My pid is %d and my parent's id is %d.\n", getpid(), getppid() );

It is printing the statement twice because it is printing it for both the parent and the child. The parent has a parent id of 0

Try something like this:

 pid_t  pid;
 pid = fork();
 if (pid == 0) 
    printf("This is the child process. My pid is %d and my parent's id is %d.\n", getpid(),getppid());
 else 
    printf("This is the parent process. My pid is %d and my parent's id is %d.\n", getpid(), getppid() );
韵柒 2024-12-09 09:54:17

我们通过if、else语句控制fork()进程的调用。请参阅下面我的代码:

int main()
{
   int forkresult, parent_ID;

   forkresult=fork();
   if(forkresult !=0 )
   {
        printf(" I am the parent my ID is = %d" , getpid());
        printf(" and my child ID is = %d\n" , forkresult);
   }
   parent_ID = getpid();

   if(forkresult ==0)
      printf(" I am the  child ID is = %d",getpid());
   else
      printf(" and my parent ID is = %d", parent_ID);
}

We control fork() process call by if, else statement. See my code below:

int main()
{
   int forkresult, parent_ID;

   forkresult=fork();
   if(forkresult !=0 )
   {
        printf(" I am the parent my ID is = %d" , getpid());
        printf(" and my child ID is = %d\n" , forkresult);
   }
   parent_ID = getpid();

   if(forkresult ==0)
      printf(" I am the  child ID is = %d",getpid());
   else
      printf(" and my parent ID is = %d", parent_ID);
}
风吹雨成花 2024-12-09 09:54:17

它打印两次,因为您调用 printf 两次,一次在程序执行中,一次在 fork 中。尝试将 fork() 从 printf 调用中取出。

It is printing twice because you are calling printf twice, once in the execution of your program and once in the fork. Try taking your fork() out of the printf call.

娇妻 2024-12-09 09:54:17

这是获得正确输出的正确方法...但是,子进程的父进程 id 有时可能会打印为 1,因为父进程被终止,并且 pid = 1 的根进程控制着这个孤儿进程。

 pid_t  pid;
 pid = fork();
 if (pid == 0) 
    printf("This is the child process. My pid is %d and my parent's id 
      is %d.\n", getpid(), getppid());
 else 
     printf("This is the parent process. My pid is %d and my parent's 
         id is %d.\n", getpid(), pid);

This is the correct way for getting the correct output.... However, childs parent id maybe sometimes printed as 1 because parent process gets terminated and the root process with pid = 1 controls this orphan process.

 pid_t  pid;
 pid = fork();
 if (pid == 0) 
    printf("This is the child process. My pid is %d and my parent's id 
      is %d.\n", getpid(), getppid());
 else 
     printf("This is the parent process. My pid is %d and my parent's 
         id is %d.\n", getpid(), pid);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文