【linux高级环境编程学习笔记一】管道通信

一 进程之间为什么要进行通信?

1. 数据传输 一个进程需要将数据发送到另外一个进程;

2. 资源共享 多个进程之间需要贡献资源;

3. 通知事件 一个进程需要向另外一个线程或一组线程发送消息,通知他们发送了某种事件;

4. 进程控制 有些进程希望完全控制另外一个线程的执行,此时控制进程希望能获取被控制线程的所有操作和运行状态。

 

二 进程间常用的通信方式

1. 无名管道和有名管道;

2. 信号;

3. 消息队列;

4. 共享内存;

5. 信号量;

6. 套接字。

 

三 进程间使用无名管道和有名管道进行通信

无名管道(pipe):只能适用于父进程和子进程之间进行通信;

有名管道(fifo): 适用于所有进程之间进行通信。

 

四 pipe通信方式例子

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

int main(int args, char *argv[])
{
        int pipe_fd[2];
        pid_t pid;
        char buf[100];
        int r_num;
        int w_num;

        memset(buf,0,sizeof(buf));

        if(pipe(pipe_fd)<0)
        {
                printf("Pipe creat error!\n");
                return 0;
        }

        if((pid=fork())==0)
        {
                close(pipe_fd[1]);
                printf("I am the child!\n");
                sleep(2);
                if((r_num=read(pipe_fd[0],buf,100))>0)
                {
                        printf("%d bytes read from the pipe is %s\n", r_num, buf);
                        close(pipe_fd[0]);
                        exit(0);
                }
        }

        else if(pid>0)
	{
                printf("I am the paraent!\n");
                close(pipe_fd[0]);
                if(write(pipe_fd[1],"Hello",5)!=-1)
                        printf("paraent writes hello\n");
                if(write(pipe_fd[1]," pipe!",7)!=-1)
                        printf("paraent writes pipe\n");
                close(pipe_fd[1]);
                sleep(3);
                waitpid(-1,NULL,0);
                exit(0);
        }

}


 

输出结果:

I am the paraent!
paraent writes hello
paraent writes pipe
I am the child!
12 bytes read from the pipe is Hello pipe!

 

五 有名管道通信例子(FIFO)

fifo_read.cpp

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

#define FIFO "/home/dxx/myfifo"

int main(int args, char* argv[])
{
	char buf_r[100];
	int fd = 0;
	int num_read = 0;	

	if(mkfifo(FIFO,O_CREAT|O_EXCL)<0 && (errno != EEXIST))
		printf("Can not creat 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 error!");
	}

	while(1)
	{
		memset(buf_r,0,sizeof(buf_r));

		if((num_read = read(fd, buf_r, 100)) == -1)
		{
			if(errno == EAGAIN)
				printf("No data yet!\n");
		}

		printf("Read %d bytes data is %s\n", num_read,buf_r);
		sleep(1);
	}
}


 

fifo_write.cpp

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

#define FIFO_SERVE "/home/dxx/myfifo"

int main(int args, char* argv[])
{
	int fd = 0;
	int num_write = 0;
	char buf_w[100];
	
	if(args == 1)
		printf("Please enter data you want to write!\n");
	fd = open(FIFO_SERVE,O_WRONLY|O_NONBLOCK,0);
	if(fd == -1)
	{
		perror("Open Error!");
	}

	printf("Preparing to write bytes\n");
	
	strcpy(buf_w,argv[1]);
	num_write = write(fd, buf_w, 3);
	if(num_write == -1)
	{
		printf("errno is %d\n",errno);
		if(errno == EAGAIN)
			perror("No data to read");
	}		
	
	printf("%d bytes data has been written, data is %s\n", num_write, buf_w);
}


 

 

 

输出结果:

Read 0 bytes data is
Read 0 bytes data is
Read 0 bytes data is
Read 0 bytes data is
Read 0 bytes data is
Read 3 bytes data is 123
Read 0 bytes data is
Read 0 bytes data is
Read 0 bytes data is
Read 0 bytes data is
Read 0 bytes data is
Read 0 bytes data is

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值