溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

unix 無關(guān)系進程間通信-有名管道

發(fā)布時間:2020-07-03 00:54:12 來源:網(wǎng)絡 閱讀:502 作者:xieyihua 欄目:系統(tǒng)運維
  1. 有名管道是專用于無關(guān)系進程間的通信
  2. open("../share_fifo", O_RDONLY, 777); 這個以只讀打開有名管道,會產(chǎn)生阻塞,直到有其它進程以寫打開才會繼續(xù)執(zhí)行下去
  3. open("../share_fifo", O_RDONLY|O_NONBLOCK, 777); 這個以只讀且注明以非阻塞打開,open不會產(chǎn)生阻塞,導致read()函數(shù)對有無數(shù)據(jù)讀完即離開
  4. open(fifo,O_WRONLY, 777) 阻塞到其它進行以讀打開這個管道
  5. open(fifo,O_WRONLY|O_NONBLOCK, 777) 沒有阻塞,如果沒有其它進行以讀打開管道會立即返回報錯-1 ENXIO
  6. 寫一個無讀打開的管道會產(chǎn)生一個信號 SIGPIPE,可以捕捉以信號來進行后續(xù)處理
  7. 此例程,功能為一個服務進程 與 多個客戶進程之間的通信
  8. 程序還需要進行一步完美


server.c

  1. #include <stdlib.h> 
  2. #include <sys/types.h> 
  3. #include <sys/stat.h> 
  4. #include <fcntl.h> 
  5. #include <unistd.h> 
  6. #include <limits.h> 
  7. #include <string.h> 
  8. #include <stdbool.h> 
  9. #include <stdio.h> 
  10. #include <stdlib.h> 
  11.  
  12. #define MAXLINE 100 
  13.  
  14. pid_t str_handler(char*); 
  15. void handler_client_corrupt(int); 
  16. static char* itoa(int);//因為itoa是一個非標準C函數(shù),因此需要自己定義 
  17. void del_fifo(void);//退出時將管道刪除 
  18.  
  19. int main() 
  20.         int fd; 
  21.         int count = 10
  22.         int BUF_SIZE; 
  23.         pid_t client_id; 
  24. //注冊 程序退出時執(zhí)行刪除管道 
  25.       if(atexit(del_fifo)) 
  26.       { 
  27.             perror("atexit\n"); 
  28.             exit(1);          
  29.       } 
  30. #if 1 
  31. // 創(chuàng)建一個客戶端到服務器共享有命管道,此管道必需不存在  
  32.         if(0 != mkfifo("../share_fifo", 0777 )) 
  33.         { 
  34.                 perror("mkfifo\n"); 
  35.                 exit(1); 
  36.         } 
  37.         fputs("share_fifo has been created\n",stdout); 
  38. #endif 
  39. //打開管道并設置只讀 
  40.         fd = open("../share_fifo", O_RDONLY, S_IRUSR|S_IRGRP|S_IROTH); 
  41.          if(fd<0
  42.         { 
  43.                 perror("open\n"); 
  44.                 exit(1);                
  45.         }  
  46. //初始化緩存數(shù)組 
  47.         (MAXLINE > PIPE_BUF) ?  (BUF_SIZE = PIPE_BUF) :  (BUF_SIZE = MAXLINE); 
  48.         //原子操作寫進fifo最大數(shù)據(jù)量,若超過會導致進行間的竟爭 MAXLINE < PIPE_BUFPIPE_BUF = 4096 
  49.         //#define PIPE_BUF     4096 
  50.         //usr/lib/limits.h 
  51.         char buf[BUF_SIZE]; 
  52.         memset(buf, sizeof(buf), 0); 
  53. //讀取數(shù)據(jù)并進行處理 
  54.         while(20) 
  55.         {      
  56. //讀客戶端發(fā)來的信息內(nèi)容,并沖洗標準輸入出進行顯示 
  57.                 int num;   
  58.                 num = read(fd, buf, BUF_SIZE); 
  59.                 if(-1 == num) 
  60.                 { 
  61.                         perror("read\n"); 
  62.                         exit(1);                       
  63.                 } 
  64.                 fputs(buf,stdout); 
  65.                 //由于標準輸出需要遇到換行符“\n”才會進行顯示,而收到內(nèi)容中沒有換行符,使此函數(shù)進行沖洗顯示 
  66.                 putchar('\n'); 
  67.                  
  68. //回復客戶端,并對回復的內(nèi)容進行處理                
  69.                 client_id = str_handler(buf); 
  70.                  
  71.                 //處理回復內(nèi)容字符串 
  72.                 char str[100] = "receided successfully:"; 
  73.                 strcat(str,itoa(client_id)); 
  74.                 int len = strlen(str); 
  75.                 char str_reply[len+1]; 
  76.                 strcpy(str_reply, str); 
  77.                  
  78.                 //處理客戶端路徑 
  79.                 char tmp[100]= "../"; 
  80.                 strcat(tmp,itoa(client_id)); 
  81.                 len = strlen(tmp); 
  82.                 char path[len+1]; 
  83.                 strcpy(path,tmp); 
  84.             
  85.                 //打開對應客戶端的管道進行寫操作 
  86.                 int cfd = open(path,O_WRONLY, S_IWUSR|S_IWGRP); 
  87.                 if(cfd<0
  88.                 { 
  89.                         perror("open_1\n"); 
  90.                         exit(1);                
  91.                 } 
  92.                 //回復對應的客戶端             
  93.                 if(write(cfd, str_reply, (strlen(str_reply)+1) ) == -1) 
  94.                 { 
  95.                         perror("write\n"); 
  96.                         exit(1);                
  97.                 } 
  98.                 //寫完后關(guān)閉對應的管道 
  99.                 if(close(cfd)) 
  100.                 { 
  101.                         perror("close\n"); 
  102.                         exit(1);                   
  103.                 } 
  104.                 sleep(1); 
  105.         } 
  106.       if(close(fd)) 
  107.       { 
  108.             perror("close\n"); 
  109.             exit(1);                   
  110.       }        
  111.         
  112.         return 0; 
  113.  
  114.  
  115. pid_t str_handler(char*buf) 
  116.         pid_t tmp;  
  117.         int len = strlen(buf); 
  118.          
  119.         tmp = atoi(buf); 
  120.         printf("len :%d tmp:%d,buf:%s\n",len,tmp,buf); 
  121.         if( (tmp == 0) || (tmp < 0) ) 
  122.         { 
  123.                 return -1; 
  124.         } 
  125.         return tmp; 
  126.  
  127. static char* itoa(int i) 
  128.       char * local =  (char*) malloc(10); 
  129.       memset(local,10,0); 
  130.       sprintf(local,"%d",i); 
  131.       return local; 
  132.  
  133.  
  134.  
  135. void del_fifo(void) 
  136.       if(remove("../share_fifo")) 
  137.       { 
  138.             perror("remove\n"); 
  139.             exit(1);     
  140.       } 



client.c

  1. #include <unistd.h> 
  2. #include <stdio.h> 
  3. #include <string.h> 
  4. #include <stdlib.h> 
  5. #include <sys/types.h> 
  6. #include <sys/stat.h> 
  7. #include <fcntl.h> 
  8.  
  9. #define MAXLINE 100 
  10. char buf[MAXLINE]; 
  11.  
  12. char tmp[100]= "../"; 
  13. char* path; 
  14.  
  15. static char* itoa(int i) 
  16.       char * local =  (char*) malloc(10); 
  17.       memset(local,10,0); 
  18.       sprintf(local,"%d",i); 
  19.       return local; 
  20.  
  21. void del_fifo(void) 
  22.       if(remove(path)) 
  23.       { 
  24.             perror("remove\n"); 
  25.             exit(1);     
  26.       } 
  27.  
  28.  
  29. int main(int argc, char** argv) 
  30.       int fd0 , fd1; 
  31.        
  32.       int len; 
  33.             
  34.       strcat(tmp,itoa(getpid())); 
  35.       len = strlen(tmp); 
  36.       char tmp_path[len+1]; 
  37.       strcpy(tmp_path,tmp); 
  38.       path = tmp_path
  39.             
  40.       if(0 != mkfifo(path, 0777 )) 
  41.       { 
  42.             perror("mkfifo\n"); 
  43.             exit(1); 
  44.       }   
  45.        
  46.  #if 0         
  47.       fd0 = open("../client0", O_RDONLY, S_IRUSR|S_IRGRP); 
  48.       if(fd0<0
  49.       { 
  50.             perror("open_0\n"); 
  51.             exit(1);                
  52.       }       
  53. #endif        
  54.     //處理回復內(nèi)容字符串 
  55.     
  56.     char str[10]; 
  57.     memset(str,10,0); 
  58.     strcpy(str,itoa(getpid())); 
  59.     len = strlen(str); 
  60.     char str_send[len+1]; 
  61.     strcpy(str_send, str); 
  62.     printf("%s\n",str_send); 
  63.      
  64.        fd1 = open("../share_fifo", O_WRONLY, S_IWUSR|S_IWGRP); 
  65.       if(fd1<0
  66.       { 
  67.             perror("open_1\n"); 
  68.             exit(1);                
  69.       } 
  70.  
  71.       fd0 = open(path, O_RDONLY|O_NONBLOCK, S_IRUSR|S_IRGRP); 
  72.       if(fd0<0
  73.       { 
  74.             perror("open_0\n"); 
  75.             exit(1);                
  76.       } 
  77.              
  78.     int count = 10
  79.       while(count--) 
  80.       { 
  81.  
  82.             if(write(fd1, str_send, (strlen(str_send)+1) ) == -1) 
  83.             //if(write(fd1, "test\n", 5 ) == -1) 
  84.             { 
  85.                   perror("write\n"); 
  86.                   exit(1);                
  87.             } 
  88.             fputs("write complete\n",stdout);   
  89.           #if 0   
  90.             if(close(fd1)) 
  91.             { 
  92.                   perror("close\n"); 
  93.                   exit(1);                   
  94.             }   
  95.           #endif   
  96.             while(read(fd0, buf, MAXLINE)<=0); 
  97.             printf("%s\n",buf);  
  98.             sleep(1);   
  99.              
  100.       } 
  101.       return 0; 

 

附件:http://down.51cto.com/data/2362203
向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI