执行的if / else的if / else用叉子() [英] execution of if/else if/else with a fork()

查看:137
本文介绍了执行的if / else的if / else用叉子()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾尝试推行OS程序。这里是code:

I have tried implementing an os program. Here is the code:

#include<sys/types.h>
#include<stdio.h>
#include<unistd.h>

int main()
{
    pid_t pid, pid1;

    pid = fork();

    if(pid<0)
    {
            fprintf(stderr,"Fork Failed");
            return 1;
    }

    else if(pid == 0) /* child process */
    {
            pid1 = getpid();
            printf("child: pid = %d\n",pid);
            printf("child: pid1 = %d\n",pid1);

    }

    else   /* parent process */
    {
            pid1 = getpid();
            printf("parent: pid = %d\n",pid);
            printf("parent: pid1 = %d\n",pid1);

    }

    return 0;
}

和它的O / P:

parent: pid = 1836
parent: pid1 = 1835
child: pid = 0
child: pid1 = 1836

有人可以给我解释一下它是如何工作的,即执行了如果 / 否则,如果 / 别人写在code 语句。我认为,一旦如果 其他不执行的一部分,但在这​​里它已经执行父条件为真,那么其他过程的一部分,即其他部分,那么孩子的部分.....怎么来的?

can somebody explain me how is it working , i.e. the sequence of the execution for the if/else-if/else statements written in the code. I would think once the else if condition becomes true then else part is not executed, however here it has executed the parent process part i.e. else part and then the child part ..... how come?

推荐答案

您应该在叉读了()。一旦你打一个叉()语句第二个进程开始,它的一切副本的父进程,但它可以运行一个独立的执行和返回它看到从是比父所看到的不同。

You should read up on fork(). Once you hit a fork() statement a second process is started, it has a copy of everything the parent process has but it can run a separate execution, and the return it sees from the fork is different than what the parent sees.

 int main()
 {
   pid_t pid, pid1;
                   <--- up to here you have one process running
   pid = fork();   <--- when this returns you have two processes:
                          parent has pid = child's pid               child has pid = 0


   if(pid<0)       <--- child and parent both check this, it's not true so they move on
   {
     ....
   }
   else if(pid == 0)<--- this is true for the child, not the parent
   {
     ....           <--- child will now execute this code
   }
   else             <-- nothing else was true for the parent so it sees this
     ....           <-- and executes this code

所以,是的,你是正确的,一旦你打了如果否则,如果其他你不打算进入code的另一分支,的在一个进程的执行的。你看到的否则,如果其他,因为你必须运行两个过程。

So yes, you are correct, once you hit the if, or the else if or the else you're not going to get into another branch of the code, in a single process’ execution. You’re seeing the else if and the else because you have two processes running.

请注意 PID1 的是不同的,因为 GETPID()将返回哪些进程正在运行code,你可以看到你有两个不同的过程,一是采否则,如果其他选秀权其他

note how the pid1's are different, because getpid() is returning which process is running that code, and you can see you have two different processes, one picks the else if the other picks the else.

这篇关于执行的if / else的if / else用叉子()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆