1

程序::

#include <sys/types.h>
#include <stdio.h>  //standard input output header file
#include <unistd.h>  // POSIX operating system API
#include <stdlib.h>
int main()
{
    pid_t pid;

    pid = fork();
    system("clear");
    if(pid < 0)
    {
      fprintf(stderr, "Fork failed to create process\n");
      return 1;
    }
    else if(pid == 0)
    {
      printf("This is the child process \'%d\' of \'%d\'\n\n",getpid(),getppid());
      execlp("/bin/ls","ls",NULL);
    }
   else
   {
      printf("This is parent process \'%d\' my parent is %d\n\n",getpid(),getppid());
      wait(NULL);
      printf("DONE\n\n");
      exit(0);
   }
   return 0;
}

输出:

This is the child process '3979' of '3978'
This is parent process '3978' my parent is '3681'

DONE

vijay@workspace:~/Documents/os$ a.out  os_fork.c

我无法理解为什么 execlp() 在父进程之后执行而不是与子进程一起执行

4

0 回答 0