Linux编程——多进程程序设计

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/baidu_28312631/article/details/48158463

    本文学习Linux环境下的多进程编程,在我之前的文章里已经讲过进程与线程。本文,再简单讲一下进程的概念,方便接下来的学习。

    进程定义:进程是一个具有一定独立功能的程序的一次运行活动

    进程状态图:

    

    当一个进程刚创建时一般处于就绪态,当就绪态的进程经过调度程序的调度将会占有CPU,这时候就会处于执行态,进程的运行过程中进行I/O操作,想要从设备读取数据,而此时又无数据可读,这是进程有执行态转换为阻塞态,当有数据来之后,I/O操作完成,进程又由阻塞态转变为就绪态。

进程 ID

    进程 ID(PID):标识进程的唯一数字

    父进程的 ID(PPID)

    启动进程的用户 ID(UID)

进程互斥

    进程互斥是指当有若干进程都要使用某一共享资源时,任何时刻最多允许一个进程使用,其他要使用该资源的进程必须等待,直到占有该资源者释放了该资源为止。

临界资源

    操作系统中将一次只允许一个进程访问的资源称为临界资源。

临界区

    进程中访问临界资源的那段程序代码称为临界区。为实现对临界资源的互斥访问,应保证诸进程互斥地进入进入各自的临界区。

进程同步

    一组并发进程按一定的顺序执行的过程称为进程间的同步。具有同步关系的一组并发进程称为合作进程,合作进程间互相发送的信号称为消息或事件。

进程调度

    概念:按一定算法,从一组待运行的进程中选出一个来占有CPU运行时间。

    调度方式:抢占式和非抢占式。

    调度算法:先来先服务调度算法,短进程优先调度算法,高优先级优先调度算法,时间片轮转算法。

获取进程 ID

    获取本进程 ID:pid_t getpid(void)

    获取父进程 ID:pid_t getppid(void)

    示例代码:  

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>

int main()
{
	printf("PID=%d\n",getpid());
	printf("PPID=%d\n",getppid());
	return 0;
}
进程创建—fork

    需要包含头文件:#include<unistd.h>

    函数调用:pid_t fork(void)

    函数功能:创建子进程。fork 被调用一次,返回两次,它可能有三种不同的返回值:1.在父进程中,fork 返回新创建的子进程的 PID;2.在子进程中,fork 返回0;3.如果出现错误,fork 返回一个负值。

    示例代码: 

int main()
{
	pid_t child;
	/* 创建子进程 */
	if((child=fork())==-1)
	{
		printf("Fork Error : %s\n", strerror(errno));
		exit(1);
	}
	else 
	{
		if(child==0) // 子进程
		{
			printf("I am the child: %d\n", getpid());
			exit(0);
		}
		else          //父进程
		{
			printf("I am the father:%d\n",getpid());
			return 0;
		}
	}
}
进程创建—vfork

    需要包含头文件:#include<sys/types.h>#include<unistd.h>

    函数调用:pid_t vfork(void)

    函数功能:创建子进程

    示例代码: 

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

int main(void)
{
	pid_t child;

	/* 创建子进程 */
	if((child=vfork())==-1)
	{
		printf("Fork Error : %s\n", strerror(errno));
		exit(1);
	}
	else 
	{
		if(child==0) // 子进程
		{
			sleep(1); //子进程睡眠一秒
			printf("I am the child: %d\n", getpid());
			exit(0);
		}
		else        //父进程
		{
			printf("I am the father:%d\n",getpid());
			exit(0);
		}
	}
}
fork 和 vfork 的区别

    fork:子进程拷贝父进程的数据段,且父、子进程的执行次序不确定

    vfork:子进程与父进程共享数据段,子进程先运行,父进程后运行

exec 函数族

    exec 用被执行的程序替换调用它的程序。也就是说 exec 启动一个新程序,替换原有的进程,因此进程的 PID 不会改变。

    需要包含头文件:#include<unistd.h>

    函数调用:int execl(const char *path,const char *arg1,...)

    参数:path:被执行程序名(含完整路径);arg1-argn:被执行程序所需的命令行参数,含程序名。以空指针(NULL)结束

    示例代码:使用 execl 函数在程序内部调用可执行程序创建一个文件。 

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc,char *argv[])
{
    /*判断入参有没有传入文件名*/
	if(argc<2)
	{
	    perror("you haven,t input the filename,please try again!\n");
		exit(EXIT_FAILURE);
	
	}
	/*调用execl函数,用可执行程序file_creat替换本进程*/
	if(execl("./file_creat","file_creat",argv[1],NULL)<0)
		perror("execl error!");
}
    函数调用:int execv(const char *path,const char *argv[ ])

    参数:path:被执行程序名(含完整路径)。argv[ ]:被执行程序所需的命令行参数数组。

    示例代码:

#include<unistd.h>

int main()
{
	char *argv[]={"ls","-al","/etc/passwd",(char *)0};
	execv("/bin/ls",argv);
	return 0;
}
进程等待

    需要包含头文件:#include<sys/types.h>,#include<sys/wait.h>

    函数调用:pid_t wait(int *status)

    功能:阻塞该进程,直到某个子进程退出。

    示例代码:

int main(void)
{
	pid_t child;

	/* 创建子进程 */
	if((child=fork())==-1)
	{
		printf("Fork Error : %s\n", strerror(errno));
		exit(1);
	}
	else 
	{
		if(child==0) // 子进程
		{
			printf("the child process is run\n");
			sleep(1);  //子进程睡眠一秒,但并没有去运行父进程
			printf("I am the child: %d\n", getpid());
			exit(0);
		}
		else        //父进程
		{
			wait(NULL); //等到子进程退出,父进程才会运行
			printf("the father process is run\n");
			printf("I am the father:%d\n",getpid());
			return 0;
		}
	}
}	

猜你喜欢

转载自blog.csdn.net/baidu_28312631/article/details/48158463