在线看毛片网站电影-亚洲国产欧美日韩精品一区二区三区,国产欧美乱夫不卡无乱码,国产精品欧美久久久天天影视,精品一区二区三区视频在线观看,亚洲国产精品人成乱码天天看,日韩久久久一区,91精品国产91免费

<menu id="6qfwx"><li id="6qfwx"></li></menu>
    1. <menu id="6qfwx"><dl id="6qfwx"></dl></menu>

      <label id="6qfwx"><ol id="6qfwx"></ol></label><menu id="6qfwx"></menu><object id="6qfwx"><strike id="6qfwx"><noscript id="6qfwx"></noscript></strike></object>
        1. <center id="6qfwx"><dl id="6qfwx"></dl></center>

            新聞中心

            EEPW首頁 > 嵌入式系統(tǒng) > 設計應用 > 文件I/O編程之: 實驗內容

            文件I/O編程之: 實驗內容

            作者: 時間:2013-09-13 來源:網(wǎng)絡 收藏

            本文引用地址:http://www.biyoush.com/article/257134.htm

            (2)編寫代碼。

            本實驗中的生產者程序的源代碼如下所示,其中用到的lock_set()函數(shù)可參見第6.3.2節(jié)。

            /*producer.c*/

            #includestdio.h>

            #includeunistd.h>

            #includestdlib.h>

            #includestring.h>

            #includefcntl.h>

            #includemylock.h

            #defineMAXLEN10/*緩沖區(qū)大小最大值*/

            #defineALPHABET1/*表示使用英文字符*/

            #defineALPHABET_START'a'/*頭一個字符,可以用'A'*/

            #defineCOUNT_OF_ALPHABET26/*字母字符的個數(shù)*/

            #defineDIGIT2/*表示使用數(shù)字字符*/

            #defineDIGIT_START'0'/*頭一個字符*/

            #defineCOUNT_OF_DIGIT10/*數(shù)字字符的個數(shù)*/

            #defineSIGN_TYPEALPHABET/*本實例選用英文字符*/

            constchar*fifo_file=./myfifo;/*仿真FIFO文件名*/

            charbuff[MAXLEN];/*緩沖區(qū)*/

            /*功能:生產一個字符并寫入仿真FIFO文件中*/

            intproduct(void)

            {

            intfd;

            unsignedintsign_type,sign_start,sign_count,size;

            staticunsignedintcounter=0;

            /*打開仿真FIFO文件*/

            if((fd=open(fifo_file,O_CREAT|O_RDWR|O_APPEND,0644))0)

            {

            printf(Openfifofileerrorn);

            exit(1);

            }

            sign_type=SIGN_TYPE;

            switch(sign_type)

            {

            caseALPHABET:/*英文字符*/

            {

            sign_start=ALPHABET_START;

            sign_count=COUNT_OF_ALPHABET;

            }

            break;

            caseDIGIT:/*數(shù)字字符*/

            {

            sign_start=DIGIT_START;

            sign_count=COUNT_OF_DIGIT;

            }

            break;

            default:

            {

            return-1;

            }

            }/*endofswitch*/

            sprintf(buff,%c,(sign_start+counter));

            counter=(counter+1)%sign_count;

            lock_set(fd,F_WRLCK);/*上寫鎖*/

            if((size=write(fd,buff,strlen(buff)))0)

            {

            printf(Producer:writeerrorn);

            return-1;

            }

            lock_set(fd,F_UNLCK);/*解鎖*/

            close(fd);

            return0;

            }

            intmain(intargc,char*argv[])

            {

            inttime_step=1;/*生產周期*/

            inttime_life=10;/*需要生產的資源數(shù)*/

            if(argc>1)

            {/*第一個參數(shù)表示生產周期*/

            sscanf(argv[1],%d,time_step);

            }

            if(argc>2)

            {/*第二個參數(shù)表示需要生產的資源數(shù)*/

            sscanf(argv[2],%d,time_life);

            }

            while(time_life--)

            {

            if(product()0)

            {

            break;

            }

            sleep(time_step);

            }

            exit(EXIT_SUCCESS);

            }

            本實驗中的消費者程序的源代碼如下所示。

            /*customer.c*/

            #includestdio.h>

            #includeunistd.h>

            #includestdlib.h>

            #includefcntl.h>

            #defineMAX_FILE_SIZE100*1024*1024/*100M*/

            constchar*fifo_file=./myfifo;/*仿真FIFO文件名*/

            constchar*tmp_file=./tmp;/*臨時文件名*/

            /*資源消費函數(shù)*/

            intcustoming(constchar*myfifo,intneed)

            {

            intfd;

            charbuff;

            intcounter=0;

            if((fd=open(myfifo,O_RDONLY))0)

            {

            printf(Functioncustomingerrorn);

            return-1;

            }

            printf(Enjoy:);

            lseek(fd,SEEK_SET,0);

            while(counterneed)

            {

            while((read(fd,buff,1)==1)(counterneed))

            {

            fputc(buff,stdout);/*消費就是在屏幕上簡單的顯示*/

            counter++;

            }

            fputs(n,stdout);

            close(fd);

            return0;

            }

            /*功能:從sour_file文件的offset偏移處開始

            將count個字節(jié)數(shù)據(jù)復制到dest_file文件*/

            intmyfilecopy(constchar*sour_file,

            constchar*dest_file,intoffset,intcount,intcopy_mode)

            {

            intin_file,out_file;

            intcounter=0;

            charbuff_unit;

            if((in_file=open(sour_file,O_RDONLY|O_NONBLOCK))0)

            {

            printf(Functionmyfilecopyerrorinsourcefilen);

            return-1;

            }

            if((out_file=open(dest_file,

            O_CREAT|O_RDWR|O_TRUNC|O_NONBLOCK,0644))0)

            {

            printf(Functionmyfilecopyerrorindestinationfile:);

            return-1;

            }

            lseek(in_file,offset,SEEK_SET);

            while((read(in_file,buff_unit,1)==1)(countercount))

            {

            write(out_file,buff_unit,1);

            counter++;

            }

            close(in_file);

            close(out_file);

            return0;

            }

            /*功能:實現(xiàn)FIFO消費者*/

            intcustom(intneed)

            {

            intfd;

            /*對資源進行消費,need表示該消費的資源數(shù)目*/

            customing(fifo_file,need);

            if((fd=open(fifo_file,O_RDWR))0)

            {

            printf(Functionmyfilecopyerrorinsource_file:);

            return-1;

            }

            /*為了模擬FIFO結構,對整個文件內容進行平行移動*/

            lock_set(fd,F_WRLCK);

            myfilecopy(fifo_file,tmp_file,need,MAX_FILE_SIZE,0);

            myfilecopy(tmp_file,fifo_file,0,MAX_FILE_SIZE,0);

            lock_set(fd,F_UNLCK);

            unlink(tmp_file);

            close(fd);

            return0;

            }

            intmain(intargc,char*argv[])

            {

            intcustomer_capacity=10;

            if(argc>1)/*第一個參數(shù)指定需要消費的資源數(shù)目,默認值為10*/

            {

            sscanf(argv[1],%d,customer_capacity);

            }

            if(customer_capacity>0)

            {

            custom(customer_capacity);

            }

            exit(EXIT_SUCCESS);

            }

            (3)先在宿主機上編譯該程序,如下所示:

            $makeclean;make

            (4)在確保沒有編譯錯誤后,交叉編譯該程序,此時需要修改Makefile中的變量

            CC=arm-linux-gcc/*修改Makefile中的編譯器*/

            $makeclean;make

            (5)將生成的可執(zhí)行程序下載到目標板上運行。

            linux操作系統(tǒng)文章專題:linux操作系統(tǒng)詳解(linux不再難懂)


            關鍵詞: I/O編程 Linux FIFO通信

            評論


            相關推薦

            技術專區(qū)

            關閉