Test based on cyclic buffering in this blog version (Linux environment)

Source: Internet
Author: User

#include <stdlib.h>#include<stdio.h>#include<pthread.h>#include"Ringbuffer.h"Static intB_flag =0;p thread_mutex_t Mutex=Pthread_mutex_initializer;#defineTx_lock (LOCK) pthread_mutex_lock (&lock)#defineTx_unlock (Lock) Pthread_mutex_unlock (&lock)void* Readbufferthread (void*param) {Ringbuffer* pstring = (Ringbuffer *) param; FILE* Fpout =NULL; size_t ret=0; Uint8 szbuf[ -*1024x768] = {0}; intNlen =0; Fpout= fopen ("File.bak","W"); if(Fpout = =NULL) {        returnNULL; }     while(B_flag) {tx_lock (mutex); Nlen= Ringbuffer_read (pstring, Szbuf,sizeof(SZBUF));        Tx_unlock (mutex); if(nlen) {ret= Fwrite (Szbuf,sizeof(Char), Nlen, fpout); if(Ret! =Nlen) {printf ("fwrite failure!, ret =%d, Nlen =%d\n", ret, nlen); }} usleep ( -);    } fclose (Fpout); returnNULL;}intMainintargcChar**argv) {FILE* Fpin =NULL; size_t ret=0; Uint8 szbuf[ -*1024x768] = {0}; intNwritelen =0; intNoffset =0; intNrereadflag =0; pthread_t ThreadID=0; Ringbuffer* Pstring =NULL; if(ARGC <2) {printf ("Usage:%s file_to_read\n", argv[0]); return-1; } pstring= Ringbuffer_create (2*1024x768*1024x768); if(Pstring = =NULL) {printf ("Create ring buffer failure!\n"); return-1; } B_flag=1; Pthread_create (&ThreadID, NULL, Readbufferthread, pstring); Fpin= fopen (argv[1],"R"); if(Fpin = =NULL) {B_flag=0;        Pthread_cancel (ThreadID);        Pthread_join (Threadid,null); return-1; }     while(1)    {        if(Nrereadflag = =0) {ret= Fread (Szbuf,sizeof(Char),sizeof(SZBUF), fpin); if(Ret! =sizeof(SZBUF)) {printf ("reach to the end of the file RET =%d, EOF =%d!\n", Ret,feof (Fpin));                Tx_lock (mutex); Nwritelen=Ringbuffer_write (pstring, szbuf, ret);                Tx_unlock (mutex); if(Nwritelen! =ret) {ret= RET-Nwritelen; Noffset=Nwritelen; Nrereadflag=1; printf ("Here still need to write%d bytes to buffer!\n", ret); }                Else{ret=0; Nwritelen=0; Noffset=0; Nrereadflag=0; }                 Break;        }} tx_lock (mutex); Nwritelen= Ringbuffer_write (pstring, &Szbuf[noffset], ret);        Tx_unlock (mutex); if(Nwritelen! =ret) {printf ("%d, still need to write%d, write len =%d, offset =%d\n", __line__, ret, Nwritelen, noffset); RET-=Nwritelen; Noffset+=Nwritelen; Nrereadflag=1; }        Else{printf ("ret =%d, Nwritelen =%d\n", ret, nwritelen); Nwritelen=0; Noffset=0; RET=0; Nrereadflag=0; } usleep ( $);        } fclose (Fpin); printf ("already finished read the file!\n");  while(ret) {printf ("Here again write%d byte...\n", ret);        Tx_lock (mutex); Nwritelen= Ringbuffer_write (pstring, &Szbuf[noffset], ret);        Tx_unlock (mutex); if(Nwritelen! =ret) {ret-=Nwritelen; Noffset+=Nwritelen; }        Else{ret=0; Nwritelen=0; Noffset=0; } usleep ( -); }     while(1) {tx_lock (mutex); RET=Ringbuffer_empty (pstring);        Tx_unlock (mutex); if(ret) {printf ("The buffer is empty...\n");  Break; } usleep ( -);    } Ringbuffer_destroy (pstring); B_flag=0;      Pthread_cancel (ThreadID);    Pthread_join (Threadid,null); return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.