• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            The Fourth Dimension Space

            枯葉北風(fēng)寒,忽然年以殘,念往昔,語(yǔ)默心酸。二十光陰無(wú)一物,韶光賤,寐難安; 不畏形影單,道途阻且慢,哪曲折,如渡飛湍。斬浪劈波酬壯志,同把酒,共言歡! -如夢(mèng)令

            再探多線程經(jīng)典生產(chǎn)者與消費(fèi)者問(wèn)題

            所謂進(jìn)步就是與知識(shí)的緣分與不期而遇,本來(lái)選嵌入式課程是為了學(xué)習(xí)嵌入式應(yīng)用方面的知識(shí),沒(méi)想到竟然把生產(chǎn)者消費(fèi)者問(wèn)題學(xué)懂了。其實(shí)程序中最核心的部分是讀者與寫(xiě)著在臨界區(qū)部分的代碼,用三個(gè)信號(hào)量鎖住線程使得同一時(shí)刻只能有一個(gè)線程進(jìn)入臨界區(qū)。
            本程序中寫(xiě)者與寫(xiě)者互斥,讀者與讀者互斥,寫(xiě)者與讀者也互斥。
            其實(shí)這個(gè)程序還可以提高效率,讓讀者與寫(xiě)著不互斥,實(shí)現(xiàn)時(shí)只需在讀者與寫(xiě)者線程中使用獨(dú)立的二值信號(hào)量即可。
            本程序在fedora14(Linux環(huán)境)下實(shí)現(xiàn).
            //coded by abilitytao 
            //2012.6.2
            //department of computer science ,Nanjing university.
            //advisor:professor Jianxin Yu
            //aided by Junjie Ye and XiaoLin Ma 

            #include
            <stdio.h>
            #include
            <stdlib.h>
            #include
            <unistd.h>
            #include
            <fcntl.h>
            #include
            <pthread.h>
            #include
            <errno.h>
            #include
            <semaphore.h>
            #include
            <sys/ipc.h>
            #include
            <sys/types.h>
            #include
            <sys/stat.h>
            #include
            <string.h>



            #define BUFFER_SIZE 8 //attention: it shoud be at least ONE! 
            #define REAL_BUFFER (BUFFER_SIZE+2)

            #define RUN_TIME 2 //run time : programme terminate after <RUN_TIME> seconds 

            #define WRITER_NUM 4  //define the number of writer
            #define READER_NUM 3  //define the number of reader





            //////////////////////////////////////////////QUEUE/////////////////////////////////////////////////////
            struct node
            {
                
            char data[64];
            }
            queue[REAL_BUFFER];
            int front;
            int rear;
            int nQueue;//the number the queue contains

            struct node rand_one_node()
            {
                
            struct node ans;
                
            int i;
                
            for(i= 0 ;i< 9 ;i++)
                
            {
                    
            int tem = rand()%10;
                    ans.data[i]
            ='0'+tem;
                }

                ans.data[
            9]=' ';
                
            for(i = 10; i < 64 ; i++)
                
            {
                    
            int tem = rand()%26;
                    ans.data[i]
            ='a'+tem;
                }

                
            return ans;
            }

            void print_node(struct node x)
            {
                
            int i;
                
            for(i = 0;i< 64 ;i++)
                printf(
            "%c",x.data[i]);
            }


            void queue_init()
            {
                front 
            = 1;
                rear  
            = 0;
                nQueue 
            = 0;
            }

            int is_queue_full(void)
            {
                
            if( (front + 1% REAL_BUFFER == rear )
                    
            return 1;
                
            else
                    
            return 0;
            }

            int is_queue_empty(void)
            {
                
            if( (rear + 1)% REAL_BUFFER == front )
                    
            return 1;
                
            else
                    
            return 0;
            }

            void queue_pushback(struct node x)//
            {
                
            if(is_queue_full())
                
            {
                    printf(
            "the queue is full,please check it befor");
                    
            return ;
                }

                queue[front]
            =x;
                front
            +=1;
                front
            %=BUFFER_SIZE + 2;
                nQueue
            ++;
            }

            struct node queue_popfirst()
            {
                
            if(is_queue_empty())
                
            {
                    printf(
            "the queue is empty,please check it befor");
                }

                
            else
                
            {
                
                    rear
            +=1;
                    rear
            %=REAL_BUFFER;
                    
            return queue[rear];
                    nQueue
            --;
                }

            }

            //////////////////////////////////////END FOR QUEUE///////////////////////////////////////////


            int fd;
            FILE 
            *file;
            time_t end_time;
            sem_t mutex,full,avail;




            void *producer(void *arg)
            {
                
            int real_write;
                
            int delay_time = 0;

                
            while(time(NULL) < end_time)
                
            {
                    
                    
                    sem_wait(
            &avail);
                    sem_wait(
            &mutex);
                    
                    
                    
            struct node tem = rand_one_node();//generate a node

                    
            //judge if the queue if full
                    if(!is_queue_full())
                        queue_pushback(tem);
                    
            //write a node into queue
                    printf("\nWrite:\n");
                    print_node(tem);
                     printf(
            "\nto the FIFO\n");
                    printf(
            "producer delay = %d\n",delay_time);
                    
                    
            //judge if the queue if full
                    if(is_queue_full())
                        printf(
            "\n******************\nthe queue is full\n******************\n\n");
                    
            //sleep for some time
                    delay_time = (int) (rand()%190+ 10;//delay time between 10 ~ 200 ms
                    usleep(delay_time);
                    
                    sem_post(
            &full);
                    sem_post(
            &mutex);
                }

                pthread_exit(NULL);
            }




            void *customer(void *arg)
            {
                
                
            int i;//a counter
                int delay_time;

                
            while(time(NULL) < end_time)
                
            {
                    sem_wait(
            &full);
                    sem_wait(
            &mutex);

                    
            struct node tem;
                    
            //judge if the queue is empty
                    if(!is_queue_empty())
                        tem 
            = queue_popfirst();
                    
            //output the node info in the file
                    for(i = 0;i < 64 ;i++)
                        fprintf(file,
            "%c",tem.data[i]);
                    fprintf(file,
            "\n");

                    
            //read a node
                    printf("\nRead:\n");
                    print_node(tem);
                     printf(
            "\nfrom the FIFO\n");
                    printf(
            "reader delay = %d\n",delay_time);
                    
            //judge if the queue if full
                    if(is_queue_empty())
                        printf(
            "\n******************\nthe queue is empty\n******************\n\n");
                    
            //sleep for some time
                    delay_time = (int) (rand()%80+ 20// delay time between 20 ~ 100 ms
                    usleep(delay_time);
                    
                    sem_post(
            &avail);
                    sem_post(
            &mutex);

                }

                pthread_exit(NULL);
            }


            int main()
            {
                
            //init file point
                freopen("out.txt","w",stdout);    
                file 
            = fopen("G2_ReaderWriter.txt","w");
                
            //init the queue
                queue_init();

                
            //generate run time
                srand(time(NULL));
                end_time 
            = time(NULL) +RUN_TIME;
                
                pthread_t threadWriter[WRITER_NUM];
                pthread_t threadReader[READER_NUM];
                
                
            //pthread_t thrd_prd_id,thrd_cst_id;
                
            //pthread_t mon_th_id;

                sem_init(
            &mutex,0,1);
                sem_init(
            &avail,0, BUFFER_SIZE);
                sem_init(
            &full,0,0);



                
            int i;//counter
                
            //create writer thread
                for(i = 0 ;i < WRITER_NUM ; i++ )
                    pthread_create(
            &threadWriter[i], NULL, producer, NULL);
                
            //create reader thread
                for(i = 0 ;i < READER_NUM ; i++ )
                    pthread_create(
            &threadReader[i], NULL, customer, NULL);
                
            //wait for quit of writer
                for(i =0 ;i<WRITER_NUM ;i++)
                    pthread_join(threadWriter[i],NULL);
                
            //wait for quit of reader    
                for(i = 0;i<READER_NUM ;i++)
                    pthread_join(threadReader[i],NULL);    

                fclose(file);
            //close file point
                fclose(stdout);
                
            return 0;
            }





            PS:此為2012年嵌入式課程大作業(yè),若助教查詢到此網(wǎng)頁(yè),請(qǐng)核對(duì)我的信息以免誤認(rèn)為是作弊.謝謝!

            posted on 2012-06-04 20:26 abilitytao 閱讀(1903) 評(píng)論(3)  編輯 收藏 引用

            評(píng)論

            # re: 再探多線程經(jīng)典生產(chǎn)者與消費(fèi)者問(wèn)題 2012-06-04 22:46 愛(ài)早起

            1個(gè)宇宙,9大行星,204個(gè)國(guó)家,809個(gè)島嶼,7個(gè)大洋, 我竟還有如此榮幸,可以遇見(jiàn)你。


            博主,你博客最上方的這句話該改了!
            現(xiàn)在是8大行星,冥王星已經(jīng)不是行星了!  回復(fù)  更多評(píng)論   

            # re: 再探多線程經(jīng)典生產(chǎn)者與消費(fèi)者問(wèn)題 2012-06-05 09:59 AAA

            樓主,生產(chǎn)者可以是多個(gè),消費(fèi)者最好是一個(gè),這樣可以避免處理的時(shí)候加鎖,另外建議你搜索下雙緩存隊(duì)列  回復(fù)  更多評(píng)論   

            # re: 再探多線程經(jīng)典生產(chǎn)者與消費(fèi)者問(wèn)題[未登錄](méi) 2012-06-05 13:11 abilitytao

            @愛(ài)早起
            網(wǎng)上的名句 呵呵 尊重原創(chuàng)吧  回復(fù)  更多評(píng)論   


            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問(wèn)   Chat2DB   管理


            久久996热精品xxxx| 久久精品综合一区二区三区| 人妻无码精品久久亚瑟影视| 一级a性色生活片久久无| 久久精品午夜一区二区福利| 久久精品国产99久久久香蕉| 日本五月天婷久久网站| 国产V综合V亚洲欧美久久| 久久精品成人免费观看97| 亚洲精品无码久久久影院相关影片| 99久久免费国产特黄| 伊人久久国产免费观看视频| 国产精品久久毛片完整版| 久久久久久久免费视频| 99久久婷婷国产综合精品草原| 一日本道伊人久久综合影| 精品久久久久久国产91| 亚洲精品乱码久久久久久自慰| 精品久久久久久无码免费| 国产亚洲综合久久系列| 国产精品久久婷婷六月丁香| 久久精品一区二区三区中文字幕| 精品国产VA久久久久久久冰| 欧美久久天天综合香蕉伊| 欧美精品一本久久男人的天堂| 亚洲国产精品无码成人片久久| 日产久久强奸免费的看| 久久人妻少妇嫩草AV蜜桃| 99久久精品国产一区二区| 青青草原综合久久| 秋霞久久国产精品电影院| 99久久国语露脸精品国产| 国产91色综合久久免费| 麻豆AV一区二区三区久久| 国内精品久久久久久久久电影网 | 亚洲AV乱码久久精品蜜桃| 91精品国产91久久| 久久久无码精品亚洲日韩软件| 亚洲乱亚洲乱淫久久| 国产激情久久久久影院小草| 国产国产成人久久精品|