天天看点

Linux系统父子进程(wait回收子进程,exec族函数)

1.程序的开始和结束

(1)程序的开始:main函数由编译链接时的引导代码调用。操作系统下的应用程序其实在main执行前也需要先执行一段引导代码才能去执行main,我们写应用程序时不用考虑引导代码的问题。

(2)程序结束:

正常终止:return、exit、_exit。

非正常终止:自己或他人发信号终止进程。

2.进程的引入

(1)操作系统中每个进程在独立地址空间中运行

(2)每个进程的逻辑地址空间均为4GB(32位系统)

(3)0-1G为OS,1-4G为应用

(4)虚拟地址到物理地址空间的映射

(5)进程隔离,提供多进程同时运行

(6)进程就是程序的一次运行过程,一个静态的可执行程序a.out的一次运行过程(./a.out去运行到结束)就是一个进程。

(7)有关进程ID的API,getpid(获取进程的ID),getppid(获取进程的父进程)。

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>


int main(void)
{
	pid_t p1 = -1, p2 = -1;
	
	printf("hello.\n");
	p1 = getpid();                  //获取进程的ID号
	printf("pid = %d.\n", p1);	
	
	p2 = getppid();                 //获取进程的父进程
	printf("parent id = %d.\n", p2);	
	
	
	return 0;
}
           
Linux系统父子进程(wait回收子进程,exec族函数)

3.父子进程

(1)fork的内部原理:进程的分裂生长模式,如果操作系统需要一个新进程来运行一个程序,那么操作系统会用一个现有的进程来复制生成一个新进程。老进程叫父进程,复制生成的新进程叫子进程。子进程有自己独立的PCB子,被内核同等调度。

Linux系统父子进程(wait回收子进程,exec族函数)
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>


int main(void)
{
	pid_t p1 = -1;
	
	p1 = fork();		// 返回2次
	
	if (p1 == 0)
	{
		// 这里是子进程
		printf("子进程, pid = %d.\n", getpid());	 //打印子进程pid	
		printf("hello world.\n");
		printf("子进程, 父进程ID = %d.\n", getppid());  //打印父进程pid
	}
	
	if (p1 > 0)
	{
		// 这里是父进程
		printf("父进程, pid = %d.\n", getpid());  //打印父进程pid
		printf("父进程, p1 = %d.\n", p1);          
	}
	
	if (p1 < 0)
	{
		//出错
	}
	
	return 0;
}
           
Linux系统父子进程(wait回收子进程,exec族函数)

(2)fork函数调用一次会返回2次,返回值等于0的就是子进程,而返回值大于0的就是父进程。

(3)典型的使用fork的方法:使用fork后然后用if判断返回值,并且返回值大于0时就是父进程,等于0时就是子进程。

(4)fork的返回值在子进程中等于0,在父进程中等于本次fork创建的子进程的进程ID。

4.父子进程对文件的操作

(1)子进程继承父进程中打开的文件,父进程先open打开一个文件得到fd,然后在fork创建子进程。之后在父子进程中各自write向fd中写入内容。

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>

int main(void)
{
	// 首先打开一个文件
	int fd = -1;
	pid_t pid = -1;
	
	fd = open("1.txt", O_RDWR | O_TRUNC);
	if (fd < 0)
	{
		perror("open");
		return -1;
	}
	
	// fork创建子进程
	pid = fork();
	if (pid > 0)
	{
		// 父进程中
		printf("parent.\n");
		write(fd, "hello", 5);
		sleep(1);
	}
	else if (pid == 0)
	{
		// 子进程
		printf("child.\n");
		write(fd, "world", 5);
		sleep(1);
	}
	else
	{
		perror("fork");
		exit(-1);
	}
	close(fd);
	return 0;
}
           

测试结论是:接续写,打印出hello world,实际上本质原因是父子进程之间的fd对应的文件指针是彼此关联的。

(2)父子进程各自独立打开同一文件实现共享,父进程open打开1.txt然后写入,子进程打开1.txt然后写入。

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>

int main(void)
{
	// 首先打开一个文件
	int fd = -1;
	pid_t pid = -1;
	
	// fork创建子进程
	pid = fork();
	if (pid > 0)
	{
		// 父进程中
		fd = open("1.txt", O_RDWR );
		if (fd < 0)
		{
			perror("open");
			return -1;
		}
		
		printf("parent.\n");
		write(fd, "hello", 5);
		sleep(1);
	}
	else if (pid == 0)
	{
		// 子进程
		fd = open("1.txt", O_RDWR );
		if (fd < 0)
		{
			perror("open");
			return -1;
		}
		
		printf("child.\n");
		write(fd, "world", 5);
		sleep(1);
	}
	else
	{
		perror("fork");
		exit(-1);
	}
	close(fd);
	return 0;
}
           

结论是:分别写。

原因是父子进程分离后才各自打开的1.txt,这时候这两个进程的PCB已经独立了,文件表也独立了,因此2次读写是完全独立的。

5.进程的诞生和消亡

(1) 进程的诞生:进程0和进程1,fork,vfork。

(2) 进程的消亡:正常终止和异常终止,进程终止时理应完全释放这些资源,操作系统会自动回收这个进程涉及到的所有的资源,每个进程都需要一个帮助它收尸的人,这个人就是这个进程的父进程。

(3) 僵尸进程:子进程先于父进程结束。子进程结束后父进程此时并不一定立即就能帮子进程“收尸”,在这一段(子进程已经结束且父进程尚未帮其收尸)子进程就被成为僵尸进程,

(4) 孤儿进程:父进程先于子进程结束,子进程成为一个孤儿进程,所有的孤儿进程都自动成为一个特殊进程(进程1,也就是init进程)的子进程。

6.父进程wait回收子进程

(1)wait的参数status。status用来返回子进程结束时的状态,父进程通过wait得到status后就可以知道子进程的一些结束状态信息。

(2)wait的返回值pid_t,这个返回值就是本次wait回收的子进程的PID。当前进程有可能有多个子进程,wait函数阻塞直到其中一个子进程结束wait就会返回,wait的返回值就可以用来判断到底是哪一个子进程本次被回收了。

(3)WIFEXITED、WIFSIGNALED、WEXITSTATUS这几个宏用来获取子进程的退出状态。

WIFEXITED宏用来判断子进程是否正常终止(return、exit、_exit退出)

WIFSIGNALED宏用来判断子进程是否非正常终止(被信号所终止)

WEXITSTATUS宏用来得到正常终止情况下的进程返回值的

对wait做个总结:wait主要是用来回收子进程资源,回收同时还可以得知被回收子进程的pid和退出状态。

fork后wait回收:

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>  
#include <sys/wait.h>
#include <stdlib.h>


int main(void)
{
	pid_t pid = -1;
	pid_t ret = -1;
	int status = -1;
	
	pid = fork();
	if (pid > 0)
	{
		// 父进程
		printf("parent.\n");
		ret = wait(&status);
		
		printf("子进程已经被回收,子进程pid = %d.\n", ret);
		printf("子进程是否正常退出:%d\n", WIFEXITED(status));
		printf("子进程是否非正常退出:%d\n", WIFSIGNALED(status));
		printf("正常终止的终止值是:%d.\n", WEXITSTATUS(status));
	}
	else if (pid == 0)
	{
		// 子进程
		printf("child pid = %d.\n", getpid());
		return 51;
		//exit(0);
	}
	else
	{
		perror("fork");
		return -1;
	}
	
	return 0;
}
           
Linux系统父子进程(wait回收子进程,exec族函数)

7.exec族函数

(1)fork子进程是为了执行新程序(fork创建了子进程后,子进程和父进程同时被OS调度执行,因此子进程可以单独的执行一个程序,这个程序宏观上将会和父进程程序同时进行)

(2)可以直接在子进程的if中写入新程序的代码。这样可以,但是不够灵活,因为我们只能把子进程程序的源代码贴过来执行(必须知道源代码,而且源代码太长了也不好控制),譬如说我们希望子进程来执行ls -la 命令就不行了(没有源代码,只有编译好的可执行程序)

(3)使用exec族函数运行新的可执行程序(exec族函数可以直接把一个编译好的可执行程序直接加载运行)

(4)我们有了exec族函数后,我们典型的父子进程程序是这样的:子进程需要运行的程序被单独编写、单独编译连接成一个可执行程序,主程序为父进程,fork创建了子进程后在子进程中exec来执行一个可执行程序,达到父子进程分别做不同程序同时(宏观上)运行的效果。

使用execl和execv运行ls -l -a:

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>  
#include <sys/wait.h>
#include <stdlib.h>

int main(void)
{
	pid_t pid = -1;
	pid_t ret = -1;
	int status = -1;
	
	pid = fork();
	if (pid > 0)
	{
		// 父进程
		printf("parent, 子进程id = %d.\n", pid);
	}
	else if (pid == 0)
	{
		// 子进程
		execl("/bin/ls", "ls", "-l", "-a", NULL);		         // execl函数
		
		//char * const arg[] = {"ls", "-l", "-a", NULL};        //execv函数
		//execv("/bin/ls", arg);
	
		return 0;
	}
	else
	{
		perror("fork");
		return -1;
	}
	
	return 0;
}
           

使用execl运行自己写的程序:

先自己写一个简单的hello world程序,然后gcc hello.c -o hello,把hello.c编译成hello可执行程序。

hello.c:

#include <stdio.h>


int main(int argc, char **argv)
{
	int i = 0;
	
	printf("argc = %d.\n", argc);
	
	while (NULL != argv[i])
	{
		printf("argv[%d] = %s\n", i, argv[i]);
		i++;
	}
	
	return 0;
}

           
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>  
#include <sys/wait.h>
#include <stdlib.h>

int main(void)
{
	pid_t pid = -1;
	pid_t ret = -1;
	int status = -1;
	
	pid = fork();
	if (pid > 0)
	{
		// 父进程
		printf("parent, 子进程id = %d.\n", pid);
	}
	else if (pid == 0)
	{
		// 子进程
		execl("hello", "aaa", "bbb", NULL);      //使用execl函数
		
		//char * const arg[] = {"aaa", "bbb", NULL};    //使用execv函数
		//execv("hello", arg);
		return 0;
	}
	else
	{
		perror("fork");
		return -1;
	}
	
	return 0;
}