(命名)管道,信号量,消息队列,共享内存,信号,socket

(命名)管道,信号量,消息队列,共享内存,信号,socket。

# 管道( pipe ):管道是一种半双工的通信方式,数据只能单向流动,而且只能在具有亲缘关系的进程间使用。进程的亲缘关系通常是指父子进程关系。

所需头文件 #include<unistd.h>

函数原型 int pipe(int fd[2])

  1. #include <unistd.h>  
  2. #include <stdio.h>  
  3.   
  4. int main( void )  
  5. {  
  6.     int filedes[2];  
  7.     char buf[80];  
  8.     pid_t pid;  
  9.       
  10.     pipe( filedes );  
  11.     pid=fork();          
  12.     if (pid > 0)  
  13.     {  
  14.         printf( "This is in the father process,here write a string to the pipe.\n" );  
  15.         char s[] = "Hello world , this is write by pipe.\n";  
  16.         write( filedes[1], s, sizeof(s) );  
  17.         close( filedes[0] );  
  18.         close( filedes[1] );  
  19.     }  
  20.     else if(pid == 0)  
  21.     {  
  22.         printf( "This is in the child process,here read a string from the pipe.\n" );  
  23.         read( filedes[0], buf, sizeof(buf) );  
  24.         printf( "%s\n", buf );  
  25.         close( filedes[0] );  
  26.         close( filedes[1] );  
  27.     }  
  28.       
  29.     waitpid( pid, NULL, 0 );  
  30.       
  31.     return 0;  
  32. }  


# 有名管道 (named pipe) : 有名管道也是半双工的通信方式,但是它允许无亲缘关系进程间的通信。

mkfifo(PATH,0666)创建命名管道,各个进程使用open打开它,直接一个进程收一个进程发即可:

使用了mkfifo后,操作PATH就会阻塞,除非用open用O_NONBLOCK。不然open,access都会阻塞(我在嵌入式平台是这样,pc没试过)。

common.h

[cpp]  view plain copy
  1. #include <stdio.h>  
  2. #include <sys/types.h>  
  3. #include <sys/stat.h>  
  4. #include <fcntl.h>  
  5. #define FIFO_PATH "/tmp/myfifo"  

server.c

[cpp]  view plain copy
  1. /* 
  2.  *将从FIFO收到到数据(字符)转换为大写,并输出到屏幕 
  3.  */  
  4. #include "common.h"  
  5. int main()  
  6. {  
  7.     int ret;  
  8.     int fd;  
  9.     char buffer;  
  10.     int nread;  
  11.     int i;  
  12.     /*建立FIFO*/  
  13.     ret = mkfifo(FIFO_PATH, 0777);  
  14.     /*打开FIFO*/  
  15.     fd = open(FIFO_PATH, O_RDONLY);  
  16.     if(-1 == fd)  
  17.     {  
  18.         printf("error/n");  
  19.         return -1;  
  20.     }  
  21.     while(1)  
  22.     {  
  23.         nread = read(fd, &buffer, 1);  
  24.         if(nread > 0)   
  25.         {  
  26.             buffer = toupper(buffer);  
  27.             printf("%c", buffer);  
  28.         }  
  29.     }  
  30. }  

运行server后,可看到创建了文件/tmp/myfifo,这是mkfifo函数指定的命名管道的路径(名字)。

当然,系统不会真的在磁盘上创建这个文件。

client.c

[cpp]  view plain copy
  1. /* 
  2.  *读取输入,并写入FIFO 
  3.  */  
  4. #include "common.h"  
  5. int main()  
  6. {  
  7.     int fd;  
  8.     int ret;  
  9.     char c;  
  10.     fd = open(FIFO_PATH, O_WRONLY);  
  11.     if(-1 == fd)  
  12.     {  
  13.         printf("error/n");  
  14.         return -1;  
  15.     }  
  16.     while(c = getchar())  
  17.     {  
  18.         write(fd, &c, 1);  
  19.     }  


# 信号量( semophore ) : 信号量是一个计数器,可以用来控制多个进程对共享资源的访问。它常作为一种锁机制,防止某进程正在访问共享资源时,其他进程也访问该资源。因此,主要作为进程间以及同一进程内不同线程之间的同步手段。

ftok,semget,semctl,semop,semtimedop.经常与ftok,shmget,shmmat,shmctl,shmdt用。

# 消息队列( message queue ) : 消息队列是由消息的链表,存放在内核中并由消息队列标识符标识。消息队列克服了信号传递信息少、管道只能承载无格式字节流以及缓冲区大小受限等缺点。

ftok,msgget,msgctl,msgsend,msgrcv

# 信号 ( sinal ) : 信号是一种比较复杂的通信方式,用于通知接收进程某个事件已经发生。

信号发送函数sigqueue和信号安装函数sigaction

# 共享内存( shared memory ) :共享内存就是映射一段能被其他进程所访问的内存,这段共享内存由一个进程创建,但多个进程都可以访问。共享内存是最快的 IPC 方式,它是针对其他进程间通信方式运行效率低而专门设计的。它往往与其他通信机制,如信号两,配合使用,来实现进程间的同步和通信。

ftok,semget,semctl,semop,semtimedop.经常与ftok,shmget,shmmat,shmctl,shmdt用。

# 套接字( socket ) : 套解口也是一种进程间通信机制,与其他通信机制不同的是,它可用于不同及其间的进程通信。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值