【问题标题】:execution of if/else if/else with a fork()使用 fork() 执行 if/else if/else
【发布时间】:2012-11-02 22:35:32
【问题描述】:

我尝试过实现一个操作系统程序。这是代码:

#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

有人可以解释一下它是如何工作的,即代码中编写的if/else-if/else 语句的执行顺序。我认为一旦else if 条件变为真,那么else 部分就不会被执行,但是这里它已经执行了父进程部分,即else 部分,然后是子部分.....怎么会?

【问题讨论】:

  • If 和 else 将在父子上下文中执行
  • 如果你不懂 fork(),难道只有我一个人在思考“实现一个 os 程序”的智慧吗?

标签: c operating-system fork


【解决方案1】:

有两个不同的进程正在运行您的程序。在fork的调用之后,下面的指令有两次执行,它们的值是不同的pid。所以else if 和else 部分都由两个进程之一执行。

【讨论】:

    【解决方案2】:

    做man fork

    fork 实际上返回zero 为子进程id,non-zero 为父进程意味着子进程的实际进程id 返回给父进程

    else if(pid ==0 ) {...} 之后是子进程

    而else {...} 是父进程。

    fork 的基本含义是创建新进程。

    如果 main 调用了一个 fork(),那么只会创建一个具有自己地址空间的子级。就在 fork 调用下方,父进程和子进程的所有语句都是相同的。

    使用fork创建进程意味着parent和child这两个进程是独立的进程,默认不共享任何数据

    但是在你的情况下,在 fork 调用之后这两个是独立的,所以这两个进程的执行顺序是未指定的。 你可能会得到这些东西:

    1. parent executed and program terminates
    parent: pid = 1235
    parent: pid1 = 1234
    
    
    2. child executed then parent and then program terminates :
    child : pid = 0
    child :pid1 = 1235
    parent : pid = 1235
    parent :pid1 = 1234
    

    如果你想确保孩子必须先执行,然后父母应该退出。 在父代码wait(NULL); 中添加一行作为else{..} 中的第一条语句 这意味着父母将等到孩子终止。

    【讨论】:

      【解决方案3】:

      fork() 创建一个新进程,该进程独立于原进程执行。

      您的新子进程运行 else if 部分。

      您现有的父进程运行else 部分。

      您的子进程和父进程之间的执行顺序不是确定性的,它们现在是不同的进程,操作系统会安排它们运行,但内核感觉如何。父进程或子进程是否先运行其代码或多或少是随机的。

      你甚至可以让孩子和父母的 printfs 交错。

      【讨论】:

        【解决方案4】:

        您应该阅读fork()。一旦你点击了fork() 语句,第二个进程就会启动,它拥有父进程所有内容的副本,但它可以运行单独的执行,并且它从fork 看到的返回与父进程看到的不同。

         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
        

        所以是的,你是对的,一旦你点击if、else if 或else,你就不会进入代码的另一个分支,在一个进程中'执行。您会看到 else if 和 else,因为您有两个进程正在运行。

        注意pid1 的不同之处,因为getpid() 正在返回哪个进程正在运行该代码,并且您可以看到您有两个不同的进程,一个选择else if,另一个选择else .

        【讨论】:

        • @POOJAGUPTA - 乐于助人!
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-16
        • 2017-11-30
        • 1970-01-01
        • 2018-03-14
        相关资源
        最近更新 更多