找回密码
 用户注册

QQ登录

只需一步,快速开始

查看: 6180|回复: 0

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

[复制链接]
发表于 2012-2-24 14:56:51 | 显示全部楼层 |阅读模式
邮箱通讯

声明以下全局变量
char cBuff[256];   //邮箱
int iHead;        //邮箱头指针
int iTail;        //邮箱尾指针

创建两个线程:XXX_Write和XXX_Read。
XXX_Write:读取一个文件(大点的),将文件内容按序写入邮箱,同时修改尾指针。即头尾指针之间的内容是提供给XXX_Read线程读取的。
XXX_Read:从邮箱中读取未读的数据,写入一个新文件,同时修改头指针。
  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. #define MAX 256     /* 邮箱大小*/
  9. #define SIZE 64                /*每次读取大小*/
  10. char cBuff[MAX];
  11. int iHead;
  12. int iTail;
  13. int xxx_write(const char *w_path)
  14. {
  15.         FILE *w_fp;       
  16.         int sizen;          /*实际读取大小*/
  17.         int i;
  18.         if(NULL == (w_fp= fopen(w_path , "r+")))  /*打开一个要读取数据的文件*/
  19.         {
  20.                         printf("error: Can not open %s .\n",w_path);
  21.                        
  22.                         pthread_exit((void *)1);
  23.         }
  24.        
  25.         while(1)
  26.         {
  27.                 if(iTail != (iHead + 1))   /*判断邮箱是否为空*/
  28.                 {
  29.                         continue ;
  30.                 }
  31.                
  32.                 if((MAX - iTail) < SIZE)
  33.                 {
  34.                         iTail = 0;
  35.                         iHead = -1;
  36.                 }
  37.                
  38.                 if((sizen = fread(cBuff+iTail,1,SIZE,w_fp)) == 0)  /*读出数据并写入邮箱?/
  39.                 {
  40.                                 fclose(w_fp);
  41.                        
  42.                                 pthread_exit((void *)2);
  43.                 }
  44.        
  45.                 iTail += sizen; /*移动尾指针*/
  46.                                
  47.         }
  48.        
  49.        
  50. }
  51. int xxx_read(const char *r_path)
  52. {
  53.         FILE *fp;
  54.         int i,j;
  55.        
  56.         if(NULL == (fp = fopen(r_path , "at+")))
  57.         {
  58.                         printf("error: Can not open %s.\n",r_path);
  59.                        
  60.                         pthread_exit((void *)1);
  61.         }
  62.                
  63.         pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL);       
  64.        
  65.         while(1)
  66.         {
  67.                 if((iHead + 1) == iTail) /* 判读邮箱中是否有数据*/
  68.                 {
  69.                         continue ;
  70.                 }
  71.        
  72.        
  73.                 if(fwrite(cBuff+iHead+1,1, (iTail - iHead ),fp) < 0) /*读出数据并写入另一个文件*/
  74.                 {
  75.                         fclose(fp);
  76.                        
  77.                         pthread_exit((void *)2);
  78.                 }
  79.                
  80.                 iHead = iTail - 1  ;  /*移动头指针/
  81.                
  82.         }
  83. }
  84. int main(int argc , char *argv[])
  85. {
  86.         int *value_ptr;
  87.             pthread_t wtid,rtid;
  88.        
  89.         iHead = MAX - 1; /*初始化头尾指针*/
  90.         iTail = 0;
  91.         if(argc != 3)
  92.         {
  93.                         printf("error:please input two files name.\n");
  94.                         return -1;
  95.         }
  96.        
  97.         if(pthread_create(&wtid,NULL,(void *)xxx_write,argv[1]) != 0)
  98.         {
  99.                         printf("error: Can not create xxx_write.\n");
  100.                         return -2;
  101.         }
  102.        
  103.         if(pthread_create(&rtid,NULL,(void *)xxx_read,argv[2]) != 0)
  104.         {
  105.                         printf("error: Can not create xxx_read.\n");
  106.                         return -2;
  107.         }
  108.         pthread_join(wtid,(void **)&value_ptr); /*等待读线程结束*/
  109.        
  110.         pthread_cancel(rtid);
  111.         printf(" over \n");
  112.        
  113.        
  114.         return 0;
  115. }
复制代码

作者:jmq_0000 发表于2012-2-24 12:56:28 原文链接

您需要登录后才可以回帖 登录 | 用户注册

本版积分规则

Archiver|手机版|小黑屋|ACE Developer ( 京ICP备06055248号 )

GMT+8, 2024-5-1 02:12 , Processed in 0.011114 second(s), 6 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表