<span style="font-family: Arial, Helvetica, sans-serif;">//fork.c</span>
#include "sys/types.h"
#include "unistd.h"
#include "stdio.h"
#include "stdlib.h"
int main()
{
pid_t result;
result=fork();
//报错处理
if(result==-1)
{
printf("Fork Error\n");
}
//son
else if(result==0)
{//调用execlp()函数,相当于"ps -ef"
if((result=execlp("ps","ps",NULL))<0);
printf("son\n");
}
//father
else
{
if((result=execlp("ls","ls",NULL))<0);
printf("father\n");
}
}
代码如上,执行后,显示ps及ls 的信息