Linux 多線程檔案讀寫操作 +執行個體

來源:互聯網
上載者:User


郵箱通訊

 聲明以下全域變數

char cBuff[256];   //郵箱

int iHead;        //郵箱頭指標

int iTail;        //郵箱尾指標

 

建立兩個線程:XXX_Write和XXX_Read。

XXX_Write:讀取一個檔案(大點的),將檔案內容按序寫入郵箱,同時修改尾指標。即頭尾指標之間的內容是提供給XXX_Read線程讀取的。

XXX_Read:從郵箱中讀取未讀的資料,寫入一個新檔案,同時修改頭指標。

#include <stdio.h>#include <pthread.h>#include <stdlib.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <unistd.h>#define MAX 256     /* 郵箱大小*/#define SIZE 99/*每次讀取長度範圍 小於郵箱大小*/char cBuff[MAX];/*郵箱*/int iHead;/*頭指標*/int iTail;/*尾指標*/int jiangmq_read(const char *w_path){FILE *w_fp;int sizen;  /*實際讀入的大小*/if(NULL == (w_fp= fopen(w_path , "r"))){printf("error: Can not open %s .\n",w_path);pthread_exit((void *)1);}while(1){/*判斷郵箱是否已寫滿了*/if((iTail < iHead) && (iTail > (iHead -SIZE)))   {continue ;}/*讀取資料到郵箱中*/if((sizen = fread(cBuff+iTail,1,SIZE,w_fp)) == 0 )  {while(1){if(iHead == iTail){fclose(w_fp);pthread_exit((void *)1);}}}/*寫完一次郵箱 移動尾指標*/iTail += sizen;/*再次判斷郵箱是否 將要 寫滿*/while((iHead == (iTail+sizen))||(((MAX - iTail) < SIZE)&&(iHead <= SIZE))){}/*判斷是否到郵箱尾部*/if((MAX - iTail) < SIZE){iTail = 0;}}}int jiangmq_write(const char *r_path){    FILE *fp;int sizen;/*實際讀取的大小*/int tmp ; /*標示實際要讀取內容的大小*/if(NULL == (fp = fopen(r_path , "w"))){printf("error: Can not open %s.\n",r_path);pthread_exit((void *)1);}pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL);while(1){/*判斷郵箱是否為空白*/if((iHead == iTail) ) {continue ;}tmp=SIZE;/*當郵箱中可讀內容不足標準大小*/if((iTail > iHead)&&((iTail-SIZE) < iHead)){p = iTail-iHead;}/*把郵箱中內容寫入檔案中*/if((sizen = fwrite(cBuff+iHead,1, tmp,fp)) < 0) {fclose(fp);pthread_exit((void *)2);}/*讀完郵箱一次 移動頭指標*/iHead += sizen ; /*判斷是否到郵箱尾部*/if(iHead > (MAX - SIZE)){while(iTail == iHead){}iHead = 0;}}}int main(int argc , char *argv[]){int *value_ptr;    pthread_t wtid,rtid;/*初始化頭尾指標*/iHead = 0; iTail = 0;if(argc != 3){printf("error:please input two files name.\n");return -1;}if(pthread_create(&rtid,NULL,(void *)jiangmq_write,argv[2]) != 0){printf("error: Can not create jiangmq_write.\n");return -2;}if(pthread_create(&wtid,NULL,(void *)jiangmq_read,argv[1]) != 0){printf("error: Can not create jiangmq_read.\n");return -2;}/*等待線程讀入結束*/pthread_join(wtid,(void **)&value_ptr); /*終止寫出線程*/pthread_cancel(rtid);printf(" over \n");return 0;}

改進的全雙工系統通訊代碼

http://download.csdn.net/detail/jmq_0000/4093546

用兩個進程間分別又有兩個線程通過共用記憶體操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.