现在的位置: 首页 > 综合 > 正文

socketpair理解

2018年03月18日 ⁄ 综合 ⁄ 共 3769字 ⁄ 字号 评论关闭

原文:http://liulixiaoyao.blog.51cto.com/1361095/533469/

今天跟人谈到socketpair的问题,晚上回来写了个程序验证下自己的猜测!

     先说说我的理解:socketpair创建了一对无名的套接字描述符(只能在AF_UNIX域中使用),描述符存储于一个二元数组,eg. s[2] .这对套接字可以进行双工通信,每一个描述符既可以读也可以写。这个在同一个进程中也可以进行通信,向s[0]中写入,就可以从s[1]中读取(只能从s[1]中读取),也可以在s[1]中写入,然后从s[0]中读取;但是,若没有在0端写入,而从1端读取,则1端的读取操作会阻塞,即使在1端写入,也不能从1读取,仍然阻塞;反之亦然......

验证所用代码:

  1. #include <stdio.h> 
  2. #include <string.h> 
  3. #include <unistd.h> 
  4. #include <sys/types.h> 
  5. #include <error.h> 
  6. #include <errno.h> 
  7. #include <sys/socket.h> 
  8. #include <stdlib.h> 
  9.  
  10. #define BUF_SIZE 30 
  11.  
  12. int main(){ 
  13.         int s[2]; 
  14.         int w,r; 
  15.         char * string = "This is a test string"
  16.         char * buf = (char*)calloc(1 , BUF_SIZE); 
  17.  
  18.         if( socketpair(AF_UNIX,SOCK_STREAM,0,s) == -1 ){ 
  19.                 printf("create unnamed socket pair failed:%s\n",strerror(errno) ); 
  20.                 exit(-1); 
  21.         } 
  22.  
  23.         /*******test in a single process ********/ 
  24.         if( ( w = write(s[0] , string , strlen(string) ) ) == -1 ){ 
  25.                 printf("Write socket error:%s\n",strerror(errno)); 
  26.                 exit(-1); 
  27.         } 
  28.         /*****read*******/ 
  29.         if( (r = read(s[1], buf , BUF_SIZE )) == -1){ 
  30.                 printf("Read from socket error:%s\n",strerror(errno) ); 
  31.                 exit(-1); 
  32.         } 
  33.         printf("Read string in same process : %s \n",buf); 
  34.         if( (r = read(s[0], buf , BUF_SIZE )) == -1){ 
  35.                 printf("Read from socket s0 error:%s\n",strerror(errno) ); 
  36.                 exit(-1); 
  37.         } 
  38.         printf("Read from s0 :%s\n",buf); 
  39.  
  40.         printf("Test successed\n"); 
  41.         exit(0); 

 

     若fork子进程,然后在服进程关闭一个描述符eg. s[1] ,在子进程中再关闭另一个 eg. s[0]    ,则可以实现父子进程之间的双工通信,两端都可读可写;当然,仍然遵守和在同一个进程之间工作的原则,一端写,在另一端读取;

     这和pipe有一定的区别,pipe是单工通信,一端要么是读端要么是写端,而socketpair实现了双工套接字,也就没有所谓的读端和写端的区分

验证代码:

  1. #include <stdio.h> 
  2. #include <string.h> 
  3. #include <unistd.h> 
  4. #include <sys/types.h> 
  5. #include <error.h> 
  6. #include <errno.h> 
  7. #include <sys/socket.h> 
  8. #include <stdlib.h> 
  9.  
  10. #define BUF_SIZE 30 
  11.  
  12. int main(){ 
  13.         int s[2]; 
  14.         int w,r; 
  15.         char * string = "This is a test string"
  16.         char * buf = (char*)calloc(1 , BUF_SIZE); 
  17.         pid_t pid; 
  18.  
  19.         if( socketpair(AF_UNIX,SOCK_STREAM,0,s) == -1 ){ 
  20.                 printf("create unnamed socket pair failed:%s\n",strerror(errno) ); 
  21.                 exit(-1); 
  22.         } 
  23.  
  24.         /***********Test : fork but don't close any fd in neither parent nor child process***********/ 
  25.         if( ( pid = fork() ) > 0 ){ 
  26.                 printf("Parent process's pid is %d\n",getpid()); 
  27.              close(s[1]); 
  28.                 if( ( w = write(s[0] , string , strlen(string) ) ) == -1 ){ 
  29.                         printf("Write socket error:%s\n",strerror(errno)); 
  30.                         exit(-1); 
  31.                 } 
  32.         }else if(pid == 0){ 
  33.                 printf("Fork child process successed\n"); 
  34.                 printf("Child process's pid is :%d\n",getpid()); 
  35.                 close(s[0]); 
  36.         }else
  37.                 printf("Fork failed:%s\n",strerror(errno)); 
  38.                 exit(-1); 
  39.         } 
  40.  
  41.         /*****read***In parent and child****/ 
  42.         if( (r = read(s[1], buf , BUF_SIZE )) == -1){ 
  43.                 printf("Pid %d read from socket error:%s\n",getpid() , strerror(errno) ); 
  44.                 exit(-1); 
  45.         } 
  46.         printf("Pid %d read string in same process : %s \n",getpid(),buf); 
  47.         printf("Test successed , %d\n",getpid()); 
  48.         exit(0); 

以上代码中在父子进程之间各关闭了一个描述符,则在父进程写可从子进程读取,反之若子进程写,父进程同样可以读取;大家可以验证下

另外,我也测试了在父子进程中都不close(s[1]),也就是保持两个读端,则父进程能够读到string串,但子进程读取空串,或者子进程先读了数据,父进程阻塞于read操作!

 

之所以子进程能读取父进程的string,是因为fork时,子进程继承了父进程的文件描述符的,同时也就得到了一个和父进程指向相同文件表项的指针;若父子进程均不关闭读端,因为指向相同的文件表项,这两个进程就有了竞争关系,争相读取这个字符串.父进程read后将数据转到其应用缓冲区,而子进程就得不到了,只有一份数据拷贝(若将父进程阻塞一段时间,则收到数据的就是子进程了,已经得到验证,让父进程sleep(3),子进程获得string,而父进程获取不到而是阻塞)

抱歉!评论已关闭.