封装fork/wait等操作, 编写函数 process_create(pid_t* pid, void* func, void* arg),func回调函数就是子进程执行的入口函数, arg是传递给func回调函数的参数.
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
int process_create(int (*func)(),const char* file,char *argv[])
{
pid_t pid = fork();
if(pid == -1){
perror("fork error!");
exit(0);
}
if(pid == 0){
int ret = func(file,argv);
if(ret == -1){
perror("func error!");
exit(1);
}
}else{
int st ;
pid_t ret = wait(&st);
if(ret == -1){
perror("wait error!");
exit(2);
}
}
return 0;
}
int main()
{
char *argv1[] = {"ls"};
char *argv2[] = {"ls","-al","/etc/passwd",0};
process_create(execvp,*argv1,argv2);
return 0;
}
![这里写图片描述](https://img-blog.csdn.net/20180417132529534?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvU2hhd2VpXw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)