Linux 多线程文件读写操作 +实例

来源:互联网 发布:逆向建模软件 编辑:程序博客网 时间:2024/05/20 02:53

邮箱通讯

声明以下全局变量

char cBuff[256];   //邮箱

int iHead;        //邮箱头指针

int iTail;        //邮箱尾指针

创建两个线程:XXX_Write和XXX_Read。

XXX_Write:读取一个文件(大点的),将文件内容按序写入邮箱,同时修改尾指针。即头尾指针之间的内容是提供给XXX_Read线程读取的。

XXX_Read:从邮箱中读取未读的数据,写入一个新文件,同时修改头指针。


[html] view plaincopyprint?
  1. #include <stdio.h> 
  2. #include <pthread.h> 
  3. #include <stdlib.h> 
  4. #include <sys/types.h> 
  5. #include <sys/stat.h> 
  6. #include <fcntl.h> 
  7. #include <unistd.h> 
  8.  
  9.  
  10.  
  11.  
  12. #define MAX 256     /* 邮箱大小*/ 
  13. #define SIZE 99     /*每次读取长度范围 小于邮箱大小*/ 
  14.  
  15.  
  16. char cBuff[MAX];    /*邮箱*/ 
  17. int iHead;          /*头指针*/ 
  18. int iTail;          /*尾指针*/ 
  19.  
  20.  
  21. int jiangmq_read(const char *w_path) 
  22.     FILE *w_fp;  
  23.     int sizen;      /*实际读入的大小*/  
  24.  
  25.  
  26.     if(NULL == (w_fp=fopen(w_path , "r"))) 
  27.     { 
  28.             printf("error: Can not open %s .\n",w_path); 
  29.              
  30.             pthread_exit((void *)1); 
  31.     } 
  32.      
  33.      
  34.     while(1) 
  35.     { 
  36.  
  37.  
  38.         /*判断邮箱是否已写满了*/ 
  39.         if((iTail < iHead) && (iTail> (iHead -SIZE)))    
  40.         { 
  41.             continue ; 
  42.         } 
  43.  
  44.  
  45.         /*读取数据到邮箱中*/ 
  46.         if((sizen = fread(cBuff+iTail,1,SIZE,w_fp)) == 0 )   
  47.         { 
  48.                 while(1) 
  49.                 { 
  50.                     if(iHead == iTail) 
  51.                     {    
  52.                         fclose(w_fp); 
  53.                         pthread_exit((void *)1); 
  54.                     } 
  55.                 } 
  56.         } 
  57.  
  58.  
  59.         /*写完一次邮箱 移动尾指针*/ 
  60.         iTail += sizen;              
  61.          
  62.          
  63.         /*再次判断邮箱是否 将要 写满*/ 
  64.         while((iHead == (iTail+sizen))||(((MAX - iTail)< SIZE)&&(iHead<= SIZE))){} 
  65.  
  66.  
  67.  
  68.  
  69.         /*判断是否到邮箱尾部*/ 
  70.         if((MAX - iTail) < SIZE
  71.                 {                    
  72.                     iTail =0;           
  73.                 } 
  74.  
  75.  
  76.  
  77.  
  78.     } 
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85. int jiangmq_write(const char *r_path) 
  86.      
  87.     FILE *fp; 
  88.     int sizen;  /*实际读取的大小*/ 
  89.     int tmp ;   /*标示实际要读取内容的大小*/ 
  90.      
  91.     if(NULL == (fp =fopen(r_path , "w"))) 
  92.     { 
  93.             printf("error: Can not open %s.\n",r_path); 
  94.              
  95.             pthread_exit((void *)1); 
  96.     } 
  97.  
  98.  
  99.     pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL);  
  100.      
  101.      
  102.     while(1) 
  103.     { 
  104.  
  105.  
  106.         /*判断邮箱是否为空*/ 
  107.         if((iHead == iTail) )  
  108.         { 
  109.             continue ; 
  110.         } 
  111.          
  112.  
  113.  
  114.         tmp=SIZE
  115.  
  116.  
  117.         /*当邮箱中可读内容不足标准大小*/ 
  118.         if((iTail > iHead)&&((iTail-SIZE)< iHead)) 
  119.         { 
  120.             p = iTail-iHead; 
  121.              
  122.         } 
  123.  
  124.  
  125.  
  126.  
  127.         /*把邮箱中内容写入文件中*/ 
  128.         if((sizen = fwrite(cBuff+iHead,1, tmp,fp)) < 0)  
  129.         { 
  130.             fclose(fp); 
  131.              
  132.             pthread_exit((void *)2); 
  133.         } 
  134.          
  135.  
  136.  
  137.         /*读完邮箱一次 移动头指针*/ 
  138.         iHead += sizen ;  
  139.  
  140.  
  141.  
  142.  
  143.         /*判断是否到邮箱尾部*/ 
  144.         if(iHead > (MAX - SIZE)) 
  145.         { 
  146.                 while(iTail == iHead){} 
  147.                  
  148.                 iHead =0
  149.         } 
  150.          
  151.      
  152.          
  153.          
  154.     } 
  155.  
  156.  
  157.  
  158.  
  159. int main(int argc , char *argv[]) 
  160.     int *value_ptr; 
  161.     pthread_t wtid,rtid; 
  162.      
  163.  
  164.  
  165.     /*初始化头尾指针*/ 
  166.     iHead = 0;  
  167.     iTail = 0
  168.  
  169.  
  170.  
  171.  
  172.     if(argc != 3) 
  173.     { 
  174.             printf("error:please input two files name.\n"); 
  175.             return -1; 
  176.     } 
  177.      
  178. if(pthread_create(&rtid,NULL,(void *)jiangmq_write,argv[2]) != 0)
  179.     { 
  180.             printf("error: Can not create jiangmq_write.\n"); 
  181.             return -2; 
  182.     } 
  183.      
  184.  
  185.  
  186.     if(pthread_create(&wtid,NULL,(void *)jiangmq_read,argv[1]) != 0) 
  187.     { 
  188.             printf("error: Can not create jiangmq_read.\n"); 
  189.             return -2; 
  190.     } 
  191.  
  192.  
  193.     /*等待线程读入结束*/     
  194.     pthread_join(wtid,(void **)&value_ptr);  
  195.      
  196.     /*终止写出线程*/ 
  197.     pthread_cancel(rtid); 
  198.  
  199.  
  200.     printf(" over \n"); 
  201.          
  202.     return 0; 
0 0
原创粉丝点击