中文字幕 另类精品,亚洲欧美一区二区蜜桃,日本在线精品视频免费,孩交精品乱子片免费

<sup id="3hn2b"></sup>

    1. <sub id="3hn2b"><ol id="3hn2b"></ol></sub><legend id="3hn2b"></legend>

      1. <xmp id="3hn2b"></xmp>

      2. 新聞中心

        EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 文件I/O編程之: 實(shí)驗(yàn)內(nèi)容

        文件I/O編程之: 實(shí)驗(yàn)內(nèi)容

        作者: 時(shí)間:2013-09-13 來(lái)源:網(wǎng)絡(luò) 收藏

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

        (2)編寫代碼。

        本實(shí)驗(yàn)中的生產(chǎn)者程序的源代碼如下所示,其中用到的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'/*頭一個(gè)字符,可以用'A'*/

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

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

        #defineDIGIT_START'0'/*頭一個(gè)字符*/

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

        #defineSIGN_TYPEALPHABET/*本實(shí)例選用英文字符*/

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

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

        /*功能:生產(chǎn)一個(gè)字符并寫入仿真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;/*生產(chǎn)周期*/

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

        if(argc>1)

        {/*第一個(gè)參數(shù)表示生產(chǎn)周期*/

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

        }

        if(argc>2)

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

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

        }

        while(time_life--)

        {

        if(product()0)

        {

        break;

        }

        sleep(time_step);

        }

        exit(EXIT_SUCCESS);

        }

        本實(shí)驗(yàn)中的消費(fèi)者程序的源代碼如下所示。

        /*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í)文件名*/

        /*資源消費(fèi)函數(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);/*消費(fèi)就是在屏幕上簡(jiǎn)單的顯示*/

        counter++;

        }

        fputs(n,stdout);

        close(fd);

        return0;

        }

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

        將count個(gè)字節(jié)數(shù)據(jù)復(fù)制到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;

        }

        /*功能:實(shí)現(xiàn)FIFO消費(fèi)者*/

        intcustom(intneed)

        {

        intfd;

        /*對(duì)資源進(jìn)行消費(fèi),need表示該消費(fèi)的資源數(shù)目*/

        customing(fifo_file,need);

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

        {

        printf(Functionmyfilecopyerrorinsource_file:);

        return-1;

        }

        /*為了模擬FIFO結(jié)構(gòu),對(duì)整個(gè)文件內(nèi)容進(jìn)行平行移動(dòng)*/

        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)/*第一個(gè)參數(shù)指定需要消費(fèi)的資源數(shù)目,默認(rèn)值為10*/

        {

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

        }

        if(customer_capacity>0)

        {

        custom(customer_capacity);

        }

        exit(EXIT_SUCCESS);

        }

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

        $makeclean;make

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

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

        $makeclean;make

        (5)將生成的可執(zhí)行程序下載到目標(biāo)板上運(yùn)行。

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


        關(guān)鍵詞: I/O編程 Linux FIFO通信

        評(píng)論


        相關(guān)推薦

        技術(shù)專區(qū)

        關(guān)閉