新聞中心

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

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

        作者: 時間:2013-09-13 來源:網絡 收藏

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

        (2)編寫代碼。

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

        /*producer.c*/

        #includestdio.h>

        #includeunistd.h>

        #includestdlib.h>

        #includestring.h>

        #includefcntl.h>

        #includemylock.h

        #defineMAXLEN10/*緩沖區大小最大值*/

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

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

        #defineCOUNT_OF_ALPHABET26/*字母字符的個數*/

        #defineDIGIT2/*表示使用數字字符*/

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

        #defineCOUNT_OF_DIGIT10/*數字字符的個數*/

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

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

        charbuff[MAXLEN];/*緩沖區*/

        /*功能:生產一個字符并寫入仿真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:/*數字字符*/

        {

        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;/*需要生產的資源數*/

        if(argc>1)

        {/*第一個參數表示生產周期*/

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

        }

        if(argc>2)

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

        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;/*臨時文件名*/

        /*資源消費函數*/

        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個字節數據復制到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;

        }

        /*功能:實現FIFO消費者*/

        intcustom(intneed)

        {

        intfd;

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

        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)/*第一個參數指定需要消費的資源數目,默認值為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)將生成的可執行程序下載到目標板上運行。

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


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

        評論


        相關推薦

        技術專區

        關閉
        主站蜘蛛池模板: 建始县| 鲁山县| 玛多县| 宁武县| 海晏县| 贺兰县| 天台县| 金乡县| 宁阳县| 义乌市| 马公市| 且末县| 惠水县| 建湖县| 论坛| 岐山县| 新干县| 龙胜| 博兴县| 和政县| 鄢陵县| 绵阳市| 包头市| 临清市| 交口县| 项城市| 澄江县| 武邑县| 新昌县| 恩施市| 武宣县| 类乌齐县| 阿尔山市| 通化县| 淮滨县| 大悟县| 安徽省| 罗源县| 静乐县| 九龙城区| 澜沧|