随笔 - 298  文章 - 377  trackbacks - 0
<2013年3月>
242526272812
3456789
10111213141516
17181920212223
24252627282930
31123456

常用链接

留言簿(34)

随笔分类

随笔档案

文章档案

相册

收藏夹

搜索

  •  

最新评论

阅读排行榜

评论排行榜

邮箱通讯

 

 声明以下全局变量

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

用两个进程间分别又有两个线程通过共享内存操作

posted on 2015-05-03 23:44 聂文龙 阅读(1349) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理