【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

 

你可能感兴趣的:(linux,进程通信,管道)