linux进程间通信(一)

         进程是一个具有一定独立功能的程序的一次活动。进程所具有的特点:1、动态性 2、并发性 3、独立性 4、异步性

         1、为什么进程间需要通信:

          一个进程所使用的内存区域是独立的需要将它的数据发给另一个进程需要进行通讯;多个进程之间共享同样的资源;一个进程需要向另一个进程或一组进程发送消息,通知它们发生了某种事件;某些进程希望完全由另一个进程控制,此时控制进程希望能够拦截另一个进程的所有操作,并能够及时直道它的状态。

          linux进程间通信(IPC)发展:

          早期的UNIX进程间的通讯方式包括:管道、FIFO、信号。

          基于System V进程间通讯方式包括:system V消息队列、system V信号灯、system V共享内存。

          POSIX进程间通信包括:posix消息队列、posix信号灯、posix共享内存。


          POSIX(Portable Operating System Interface)表示可移植操作系统接口。电气和电子工程师协会(Institute of Electrical and ElectronicsEngineers,IEEE)最初开发POSIX 标准,是为了提高UNIX 环境下应用程序的可移植性。然而,POSIX 并不局限于UNIX,许多其它的操作系统,例如DEC OpenVMS 和MicrosoftWindows,都支持POSIX 标准。

          System V,也被称为AT&T System V,是Unix操作系统众多版本中的一支。


          如今的linux结合以上这些进程间通信方式产生了如下的进程间通信方式:

         1、有名管道(主要用于进程)和无名管道(主要用于父子进程之间等)

         2、信号(signal)

         3、消息队列

         4、共享内存

         5、信号量

         6、socket套接字

          管道通信:管道通信的特点是单向的、先进先出(FIFO),它把一个进程的输出和另一个进程的输入连接在一起。一个进程(写进程)在管道的尾部写入数据,另一个进程从管道的头部读出数据。数据被一个进程读出后,将被从管道中删除,其它的进程将不能再读到这些数据。管道提供了简单的流控制机制,进程试图度空管道时,进程将阻塞。同样,管道已经满时,进程再试图向管道写入数据,进程将阻塞。

          管道创建:管道包括有名和无名管道,前者用于进程间的通讯,后者用于父子进程间的通讯。


      无名管道

        无名管道有pipe()函数创建:

    无名管道创建:int pipe(int file_pipe[2]);
         该函数创建成功返回一个大于0的数,失败返回小于0的数。

         file_pipe[0]用于读管道,file_pipe[1]用于写管道。

         管道关闭调用普通的close关闭file_pipe[0]和file_pipe[1]这两个文件描述符即可。

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
     int pipe_fd[2];
     if(pipe(pipe_fd)<0)
     {
          printf("pipe create error\n");
          return -1;
     }
     else
          printf("pipe create success\n");
     close(pipe_fd[0]);
     close(pipe_fd[1]);
}

         管道的读写用于不同进程间通信。通常先创建一个管道,在通过fork函数创建一个子进程,该子进程会继承父进程所创建的管道。需要注意的是在调用fork函数之前,必须先调用pipe函数,否则子进程将不会继承文件描述符。

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

int main()
{
	int pipe_fd[2];
	pid_t pid;
	char buf_r[100];
	char* p_wbuf;
	int r_num;
	
	memset(buf_r,0,sizeof(buf_r));
	
	/*创建管道*/
	if(pipe(pipe_fd)<0)
	{
		printf("pipe create error\n");
		return -1;
	}
	/*创建子进程*/
	if((pid=fork())==0)  
	{
		printf("\n");
		close(pipe_fd[1]);
		sleep(2); 
		if((r_num=read(pipe_fd[0],buf_r,100))>0)
		{
			printf(   "%d numbers read from the pipe is %s\n",r_num,buf_r);
		}	
		close(pipe_fd[0]);
		exit(0);
  	}
	else if(pid>0)
	{
		close(pipe_fd[0]);
		if(write(pipe_fd[1],"Hello",5)!=-1)
			printf("parent write1 Hello!\n");
		if(write(pipe_fd[1]," Pipe",5)!=-1)
			printf("parent write2 Pipe!\n");
		close(pipe_fd[1]);
		sleep(3);
		waitpid(pid,NULL,0); /*等待子进程结束*/
		exit(0);
	}
	return 0;
}

         命名管道

           命名管道和无名管道基本相同,但也有不同点:无名管道只能由父子进程使用;但是通过命名管道,不相关的进程也能交换数据。

           创建命名管道:

#include <sys/types.h>
#include <sys/stat.h>
int mkfifo(const char * pathname, mode_t mode)
pathname:FIFO文件名
vmode:属性
一旦创建了一个FIFO,就可用open打开它,一般的文件访问函数(close、read、write等)都可用于FIFO。

当打开FIFO时,非阻塞标志(O_NONBLOCK)将对以后的读写产生如下影响:
1、没有使用O_NONBLOCK:访问要求无法满足时进程将阻塞。如试图读取空的FIFO,将导致进程阻塞。
2、使用O_NONBLOCK:访问要求无法满足时不阻塞,立刻出错返回,errno是ENXIO。

write_fifo

#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FIFO_SERVER "/tmp/myfifo"

main(int argc,char** argv)
{
	int fd;
	char w_buf[100];
	int nwrite;
		
	/*打开管道*/
	fd=open(FIFO_SERVER,O_WRONLY|O_NONBLOCK,0);
	
	if(argc==1)
	{
		printf("Please send something\n");
		exit(-1);
	}
	
	strcpy(w_buf,argv[1]);
	
	/* 向管道写入数据 */
	if((nwrite=write(fd,w_buf,100))==-1)
	{
		if(errno==EAGAIN)
			printf("The FIFO has not been read yet.Please try later\n");
	}
	else 
		printf("write %s to the FIFO\n",w_buf);
}
read_fifo

#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FIFO "/tmp/myfifo"

main(int argc,char** argv)
{
	char buf_r[100];
	int  fd;
	int  nread;
	
	/* 创建管道 */
	if((mkfifo(FIFO,O_CREAT|O_EXCL)<0)&&(errno!=EEXIST))
		printf("cannot create fifoserver\n");
	
	printf("Preparing for reading bytes...\n");
	
	memset(buf_r,0,sizeof(buf_r));
	
	/* 打开管道 */
	fd=open(FIFO,O_RDONLY|O_NONBLOCK,0);
	if(fd==-1)
	{
		perror("open");
		exit(1);	
	}
	while(1)
	{
		memset(buf_r,0,sizeof(buf_r));
		
		if((nread=read(fd,buf_r,100))==-1)
		{
			if(errno==EAGAIN)
				printf("no data yet\n");
		}
		printf("read %s from FIFO\n",buf_r);
		sleep(1);
	}	
	pause(); /*暂停,等待信号*/
	unlink(FIFO); //删除文件
}





       

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值