进程间通信

进程间通信方式:

1)普通文件

                这种方式基本上所有的教科书上都没有提到过,但是我个人认为这种我们最早接触到的进程间通信的方式;进程可以通过读写同一个文件,并解析文件内的内容来进行通信。

2)管道(pipe)

               管道是以半双工机制来通信的,分为匿名管道和有名管道;匿名管道只能用于有亲缘关系的进程,有名管道能用于有亲缘关系和无亲缘关系的进程。 

        From manpages
        #include <unistd.h>
        int pipe(int pipefd[2]);
        Description: 
                pipe() creates a pipe, a unidirectional data channel that can be used for interpeocess communication. The array pipefd is used to return 
            file descriptors referring to the ends of pipe.  pipefd[0] refers to the read end of pipe. pipefd[1] refers to the read end of the pipe.
                pipe()创建一个单向的可用于进程间通信的管道, 数组pipefd用来返回指向管道两端的文件描述符. pipefd[0]代表管道读端, pipefd[1]代表管道写端. 

        #include <sys/types.h>
        #include <sys/stat.h>
        int mkfifo(const char* pathname, mode_t mode)
        Description:
             mkfifo() makes a FIFO special file with name pathname. mode specifies the FIFO's permissions. It is modified by the process's umack in the 
        usual way : the permissions of the created file are (mode &~umask).
             A FILO special file is similar to a pipe, except that it is created in a different way. Instead of being an anonymous communications channel ,
        a FIFO special file is entered into the filesystem calling mkfife().
             mkfifo()创建一个名为 pathname 的特殊文件. mode 用来说明文件的权限. 这个权限可实际值为 mode&~umask.
             除了创建的方式不一样,FIFO特殊文件和管道很类。 FIFE文件通过调用mkfifo()进入文件系统, 而不是通过匿名通道.  
 
                至于文件描述符的读写,就不在此赘述了!

3)信号(signal)

4)共享内存(shared memory)

        From manpages
        #include <sys/mman.h>
        #include <sys/stat.h>
        #include <fcntl.h>

        int shm_open(const char* name,  // 文件名
                    int oflag,         // O_RDONLY, O_RDWR等, 和open()标志一样
                    mode_t mode);      // 文件访问权限, 如:0755
            Return: 成功,则返回文件描述符; 失败, 返回-1.

        void *mmap(void* addr,     // 共享内存起始地址 
                   size_t length,  // 共享内存长度,字节为单位
                   int prot,       // PROT_EXEC 可执行, PROT_READ 可读, PROT_WRITE 可写, PROT_NONE not be accessed  
                   int flags,      // MAP_SHARED | MAP_PRIVATE 一般设为MAP_SHARED 
                   int fd,         // shm_open返回的文件描述符
                   off_t offset);  // 偏移
        
        int munmap(void* addr, size_t length);

       int shm_unlink(const char* name);

        Link with -lrt

5)socketpair

        From manpages
        #include <sys/types.h>
        #include <sys/socket.h>

        int socketpair(int domain, int type, int protocol, int sv[2])
        Description:
             The socketpair() call creates an unnamed pair of connected sockets in the specified domain, of the specified type, and using the optionally 
        specified protocol.
             socketpair()调用创建一个匿名的,面向连接的socket,通过 domain, type, protocol等参数来说明.
 

6)socket

通信方式的比较:

1)数据量较大,可以用普通文本文件( 效率低 )和共享内存( 效率高 )实现进程间通信;

2)所有的方式都能在无亲缘关系的进程间通信,socket可以实现不同主机间的进程通信;

3)管道是以半双工的方式工作(可以通过两个管道模拟全双工),socketpair是以全双工的方式工作;

举例:

1)匿名管道(模拟全双工)

ipc_unnamed_pipe.c------------2016-11-22 20:00---------------------------------
                 ---Modified--2016-11-23 09:00---------------------------------
        #include <stdio.h>
        #include <stdlib.h>
        #include <unistd.h>
        #include <string.h>

       int main(int argc, char* argv[])
       {      
            int  fd_first[2], fd_second[2];
            if( !(pipe(fd_first) && pipe(fd_second)) ){
                 perror("pipe");
                 return -1;
           }

            pid_t pid = fork();
            if(pid == 0){          // Child process
                 close(fd_first[0]);    close(fd_second[1]);

                 char msg[128] = "Hi, parent peocess ...";
                 write(fd_first[1], msg, strlen(msg) + 1);
                 memset(msg, 0, sizeof(msg));
                 read(fd_second[0], msg, sizeof(msg));

                 printf("Message from Parent : %s\n", msg);
            }else{               // Parent process
                 close(fd_first[1]);    close(fd_second[0]);

                 char msg[128] = {0};
                 read(fd_first[0], msg, sizeof(msg));
                 printf("Message from Child : %s\n", msg);
         
                 memset(msg, 0, sizeof(msg));
                 strcpy(msg, "Hi, Child ...");
                 write(fd_second[1], msg, strlen(msg));
                 wait(NULL);
            }
            return 0;
       }

2)有名管道

         

  ipc_fifo_write.c ---------2016-11-23 09:00-----------------------------------------------------
       #include <stdio.h>
       #include <stdlib.h>
       #include <sys/types.h>
       #include <sys/stat.h>
       
       int main()
       {
             int ret = mkfifo("filename.ff", 0755);
             if(ret != 0){
                 perror("mkfifo");
                 exit(-1);
             }

             int fd = open("filename.ff", O_WRONLY);
             if(fd == -1){
                 perror("open");
                 exit(-1);
             }

             char msg[128] = "Hello world!";
             ret = write(fd, msg, strlen(msg) + 1);  // Write to fifo.
             if(ret < 0){
                 perror("write");
             } 
             
             close(fd);
             return 0;
       }
 ipc_fifo_read.c -------2016-11-23 09:00------------------------------------------------------------
       #include <stdio.h>
       #include <stdlib.h>
       
       int main()
       {
            int fd = open("filename.ff", O_RDONLY);
            if(fd == -1){
                 perror("open");
                 exit(-1);
            }
            
            char msg[128] = {0};
            int ret = read(fd, msg, sizeof(msg));
            if(ret > 0){
                  printf("Message from FIFO : %s\n", msg);
            }
            perror("read");
            close(fd);
            return 0;
       }


3)共享内存

           匿名共享内存:

 ipc_unnamed_shm.c------2016-11-22 20:00------------------------------------------
       #include <sys/mman.h>
       #include <sys/stat.h>
       #include <fcntl.h>
       #include <stdio.h>
       #include <stdlib.h>
       #include <fcntl.h>
       
       int main()
       {
           void* shm = mmap(NULL, 1024, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANOYMOUS, -1,0);
           if(shm == MAP_FAILED){
                perror("mmap");
                return 0;
           } 
           
           pid_t pid = fork();
           if(pid == 0){
                *(int*)shm = 100;
           }else{
                sleep(1);
                printf("From parent : %d\n", *(int*)shm);  
                wait(NULL);
           }
           munmap(shm, 1024);
           return 0;
       }
 
              有名共享内存

 ipc_shm.c------2016-11-22 20:00-----------------------------------------     
       #include <sys/mman.h>
       #include <sys/stat.h>
       #include <fcntl.h>
       #include <stdio.h>
       #include <stdlib.h>
       #include <fcntl.h>

       int main()
       {
           int fd = shm_open("/test.shm", O_RDWR, 0755);
           if(fd < 0){
               fd = shm_open("/test.shm", O_RDWR | O_CREAT, 0755);
               if(fd < 0){
                   perror("shm_open");
                   return -1;
               } 
               ftruncate(fd, 4096);
           }
           
           char* shm = (char*)mmap(NULL, 1024, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
           pid_t pid = fork();
           if(pid == 0){
               strcmp(shm, "Hello world\n");
           }else{
               sleep(1);
               printf("From shared memory : %s\n", shm);
               wait(NULL);
           }
           munmap(shm);
           return 0;
       } 
              把子进程的代码写在另一个.c文件里,就可以实现无亲缘关系进程间的通信。

 4)socketpair

                 

 ipc_socketpair.c-----2016-11-23 09:00---------------------------------------------------------
       #include <stdio.h>
       #include <stdlib.h>
       #include <sys/socket.h>
       #include <sys/types.h>
       #include <unistd.h>

       int main()
      {
            int fd[2];
            int ret = socketpair(AF_UNIX, SOCK_STREAM, 0,fd);
            if(ret != 0){
                  perror("socketpair");
                  return -1;
            }
            
            pid_t pid = fork();
            if(pid == 0){             // Child process
                  close(fd[1]);
           
                  int val = 0;
                  while(1){
                        sleep(1);
                        ++val;
                         
                        write(fd[0], &val, sizeof(val));
                        read(fd[0], &val, sizeof(val));
                        printf("Child : %d\n", val);
                  }
            }else{                  // Parent process
                   close(fd[0]);

                   int val;
                   while(1){
                         read(fd[1], &val, sizeof(val));
                         printf("Parent : %d\n", val);
                         ++val;
                         write(fd[1], &val, sizeof(val));
                   }
            }
            return 0;
     }

       

            第一次写 ... .... 好累。如果有什么地方不正确,或者有歧义,还望各位批评指正,以便"作者"尽快更正, 避免误导更多人。 谢谢!

            以后再跟吧!

-------------- Created at   : 2016-11-22 20:00

-------------- Author          : Monk

-------------- Description :

-------------- Modified      : 2016-11-23 09:00    Implemetations of  ipc_socketpair.c

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值