您的位置:首页 > 运维架构 > Linux

【Linux】嵌入式开发,在Linux中使用C语言对Fork函数执行子函数及父函数,命令ps 及 ls 操作

2015-06-23 12:15 786 查看
<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 的信息
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: