叉指令

时间:2014-10-09 17:02:22

标签: c operating-system

我的问题是关于C中的fork()指令。我有以下程序:

void main(){
   int result, status;
   result = fork();
   if(result==0){
      printf("Son:%d\n", getpid());
   }else{
      printf("Pai..:%d\n", getpid());
      wait(&status);
   }
}

为什么我会收到两个printf而不是一个?是因为fork的回归吗? 我的另一个问题是&在状态之前。我为什么需要呢?

2 个答案:

答案 0 :(得分:2)

fork是一个进程创建自身副本的操作。它通常是在内核中实现的系统调用。 Fork是在类Unix操作系统上创建进程的主要方法。在多任务操作系统,进程(运行程序)需要一种方法来创建新进程

当进程调用fork时,它被视为父进程,而新创建的进程则是其子进程。在fork之后,两个进程不仅运行相同的程序,而且还恢复执行,就好像两个进程都调用了系统调用一样。然后,他们可以检查呼叫的返回值,以确定他们的状态,孩子或父母,并采取相应的行动。

    /* Includes */
#include <unistd.h>     /* Symbolic Constants */
#include <sys/types.h>  /* Primitive System Data Types */ 
#include <errno.h>      /* Errors */
#include <stdio.h>      /* Input/Output */
#include <sys/wait.h>   /* Wait for Process Termination */
#include <stdlib.h>     /* General Utilities */

int main()
{
    pid_t childpid; /* variable to store the child's pid */
    int retval;     /* child process: user-provided return code */
    int status;     /* parent process: child's exit status */

    /* only 1 int variable is needed because each process would have its
       own instance of the variable
       here, 2 int variables are used for clarity */

    /* now create new process */
    childpid = fork();

    if (childpid >= 0) /* fork succeeded */
    {
        if (childpid == 0) /* fork() returns 0 to the child process */
        {
            printf("CHILD: I am the child process!\n");
            printf("CHILD: Here's my PID: %d\n", getpid());
            printf("CHILD: My parent's PID is: %d\n", getppid());
            printf("CHILD: The value of my copy of childpid is: %d\n", childpid);
            printf("CHILD: Sleeping for 1 second...\n");
            sleep(1); /* sleep for 1 second */
            printf("CHILD: Enter an exit value (0 to 255): ");
            scanf(" %d", &retval);
            printf("CHILD: Goodbye!\n");    
            exit(retval); /* child exits with user-provided return code */
        }
        else /* fork() returns new pid to the parent process */
        {
            printf("PARENT: I am the parent process!\n");
            printf("PARENT: Here's my PID: %d\n", getpid());
            printf("PARENT: The value of my copy of childpid is %d\n", childpid);
            printf("PARENT: I will now wait for my child to exit.\n");
            wait(&status); /* wait for child to exit, and store its status */
            printf("PARENT: Child's exit code is: %d\n", WEXITSTATUS(status));
            printf("PARENT: Goodbye!\n");             
            exit(0);  /* parent exits */       
        }
    }
    else /* fork returns -1 on failure */
    {
        perror("fork"); /* display error message */
        exit(0); 
    }
}

答案 1 :(得分:1)

fork systemcall将子进程的processid返回给父进程,将0返回给子进程。一次if子句中的代码将被执行(通过子)和一次else子句(通过父级)。

这意味着会调用printf两次。

Wait是另一个系统调用,它阻止执行,直到其中一个子进程返回。 而不是通过返回一些值给出答案,等待通过将信息写入地址来提供信息。

您通过等待变量状态。如果你没有&amp;等待将获得状态的当前值。通过添加&amp;,您可以将状态地址传递给等待。一旦子进程终止,wait将停止并且信息将以状态存储。刚刚终止的过程返回

这里是等待的Linux手册页: http://man7.org/linux/man-pages/man2/wait.2.html