如果父对象以exit()终止怎么办

时间:2013-10-12 16:41:19

标签: c linux operating-system

方案::

#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()在父进程不与子进程

一起执行

0 个答案:

没有答案