linux 创建子进程

来源:互联网 发布:suse linux和opensuse 编辑:程序博客网 时间:2024/05/16 09:07

函数:fork

原型:

#include <sys/types.h>

#include <unistd.h>


pid_t  fork(void);



返回值:

-1:失败,如果超出了父进程所规定的子进程的数量,errno 设为EAGAIN ,如果内存不足  设为ENOMEM

0 :子进程

等待一个进程:

#include <sys/types.h>

#include <sys/wait.h>


pid_t wait(int *stat_loc);



wait系统条用将暂停父进程直到他的子进程结束为止。返回子进程的PID,


pid=fork();


if(pid != 0){

int stat_val;

pid_t child_pid;


child_pid = wait(&stat_val);

printf("child has finished : pid = %d \n",child_pid);

if(WINFWXITED(stat_val))

printf("child exited with code %d \n",WEXITSTATUS(stat_val));

else

printf("child terminated abnormally \n");


}


*僵尸进程

fork 子进程之后 ,运行,直到子进程终止,但进程表中的子进程表项不会立刻释放,虽然子进程已经把不在运行,但它还处于系统中,这样的子进程成为僵尸进程,直到父进程执行完毕退出。






0 0
原创粉丝点击