linux多进程


使用fork创建子进程

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(){

pid_t pid;
pid=fork();

if(pid==0){
	while(1){
		printf("this is the child process\n");
		sleep(1);
	}
}
else{
	while(1){
		printf("this is the father process\n");
		sleep(1);
	}
	
}

}

运行效果如下

this is the father process
this is the child process
this is the father process
this is the child process
this is the father process
this is the child process
this is the father process
this is the child process

vfork

vfork函数用于创建一个新进程,而该新进程的目的是exec一个新进程。
vfork与fork都创建一个子进程,但是它并不会将父进程的地址空间完全复制到子进程中,因为子进程会立即调用exec或exit,于是也就不会引用该地址空间。不过在子进程中调用exec或exit之前,它在父进程的空间中运行。
vfork与fork的另一个区别是:vfork保证子进程先运行,在它调用exec或exit之后父进程才开始运行。

wait和waitpid

  • wait函数等待子进程的结束信号。
    它是阻塞函数,只有任意一个子进程结束,它才能继续往下执行,否则卡住那里等。

  • waitpid等待指定pid对应的进程结束,可以选择阻塞或者不阻塞的方式。

    exec

    exec系列的函数有很多个,主要用来执行一个新的程序,即用一个全新的程序来替换子进程的内容。

参考文章

Linux多进程编程(典藏、含代码)
《UNIX环境高级编程》


文章作者: pcl
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 pcl !
评论
  目录