- 论坛徽章:
- 0
|
本帖最后由 pppadr 于 2013-07-24 09:03 编辑
如题,可以很方便的用posix_spawn相关的函数来创建新的进程:
- #include <spawn.h>
- #include <stdio.h>
- #include <errno.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- extern char **environ;
- int main(int argc, char * argv[])
- {
- posix_spawnattr_t attr;
- posix_spawn_file_actions_t fact;
- pid_t pid;
- char cmd[]="ls";
- char opt[]="-l";
- char *args[3];
- args[0]=cmd;
- args[1]=opt;
- args[2]=NULL;
- posix_spawnattr_init(&attr);
- posix_spawn_file_actions_init(&fact);
- posix_spawn(&pid,"/bin/ls",&fact,&attr,args,environ);
- perror("posix_spawn");
- printf("pid=%d,child pid = %d\n",getpid(),pid);
- int stat=0;
- waitpid(pid,&stat,0);
- printf("stat is %d\n",stat);
- return 0;
- }
复制代码 可是这和fork/execl或者system()调用,在效果上有什么区别呢? 有什么事情是posix_spawn能做的而fork/execl和system不能做的?
如果功能一样,干嘛弄两套东西出来?
|
|