Linux进程通信

来源:互联网 发布:jquery储存数据 编辑:程序博客网 时间:2024/05/17 23:25

                                                                                                              Lniux进程间通信

目的

1.数据传输:一个进程需要将他的数据发送给另一个进程

2.资源共享:多个进程之间共享同样的资源

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

4.进程控制:有些进程希望完全控制另一个进程的执行(如Debug进程),此时控制进程希望拦截另一个进程的所有操作,并能够及时知道它的状态变化。

 

分类

1.无名管道(pipe)和有名管道(FIFO

2.信号(signal

3.消息队列

4.共享内存

5.信号量

6.套接字(socket

 

 

管道通信

   管道是单项的,先进先出的,它的一个进程的输出和另一个进程的输入连接在一起。

   一个进程(写进程)在管道的尾部写入数据,另一个进程(读进程)从管道的头部读出数据.

   数据被一个进程读出后,将被从管道中删除,其他读进程将不能再读到这些数据。

   管道提供了简单的流控制机制,进程试图读空管道时,进程将阻塞。同样,管道已经满时,进程再试图向管道写入数据,进程将阻塞。

   管道包括无名管道和有名管道两种,前者用于父进程和子进程间的通信,后者可用于运行于同一系统中的任意两个进程间的通信。

 

 

无名管道创建

创建管道pipe

读管道read

写管道write

关闭管道close

 

Pipe

函数的作用:创建无名管道

函数的原型:int pipe(int fds[2])

函数的头文件:#include <unistd.h>

函数的参数:新建的两个描述符有fds数组返回,fds[0]:用于读管道;fds[1]:用于写管道

返回值:成功0;出错-1

 Eg_1:pipe.c

 ##include <unistd.h>

#include <errno.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]);

}

 

Eg_2:pipe_rw.c

#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)  //子进程 OR父进程?

{

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;

}

 

 

 

 

管道读写

 

管道用于不同进程间通信,通常先创建一个管道,再通过fork函数创建一个子进程,该子进程会继承父进程所创建的管道

 

 

1.管道通讯是单向的,有固定的读端和写端

2.数据被进程从管道读出后,在管道中该数据不存在了

3.当进程去读取空管道的时候,进程会阻塞

4.当进程往满管道写入数据时,进程会阻塞

5.管道容量为64K

注意事项:必须在系统调用fork前调用pipe函数,否则子进程将不会继承文件描述符

 

 

有名管道:

   有名管道和无名管道基本相同,但也有不同点:

   无名管道只能有父子进程使用;

   但是通过命名管道,不相关的进程也能交换数据。

 

有名管道操作:

           创建管道mkfifo

           打开管道open

           读管道read

   写管道write

   关闭管道close

   删除管道unlink

 

 

Mkfifo

函数的作用:创建有名管道

函数的原型:int mkfifo(const char * filename,mode_t mode)

参数的作用:filename:有名管道的路径,名称

            Mode:打开管道的方式:1.O_NONBLOCK:FIFO打开的时候,非阻塞

                                 2.O_RDONLY:只读

                                 3.O_WRONLY:只写

 4.O_RDWR:可读写

 

Eg_1:fifo_write.c

#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)

{

printf("The FIFO has not been read yet.Please try later\n");

}

else

printf("write %s to the FIFO\n",w_buf);

}

 

 

Eg_2:fifo_read.c

#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)

{

printf("The FIFO has not been read yet.Please try later\n");

}

else

printf("write %s to the FIFO\n",w_buf);

}



0 0
原创粉丝点击