<dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><small id="yhprb"></small><dfn id="yhprb"></dfn><small id="yhprb"><delect id="yhprb"></delect></small><small id="yhprb"></small><small id="yhprb"></small> <delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"></dfn><dfn id="yhprb"></dfn><s id="yhprb"><noframes id="yhprb"><small id="yhprb"><dfn id="yhprb"></dfn></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><small id="yhprb"></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn> <small id="yhprb"></small><delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn>

新聞中心

EEPW首頁(yè) > 嵌入式系統 > 設計應用 > 多線(xiàn)程編程之:實(shí)驗內容——“生產(chǎn)者消費者”實(shí)驗

多線(xiàn)程編程之:實(shí)驗內容——“生產(chǎn)者消費者”實(shí)驗

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

本文引用地址:http://dyxdggzs.com/article/257119.htm

(3)編寫(xiě)代碼

的代碼中采用的有界緩沖區擁有3個(gè)單元,每個(gè)單元為5個(gè)字節。為了盡量體現每個(gè)信號量的意義,在程序中生產(chǎn)過(guò)程和消費過(guò)程是隨機(采取0~5s的隨機時(shí)間間隔)進(jìn)行的,而且生產(chǎn)者的速度比消費者的速度平均快兩倍左右(這種關(guān)系可以相反)。生產(chǎn)者一次生產(chǎn)一個(gè)單元的產(chǎn)品(放入“hello”字符串),消費者一次消費一個(gè)單元的產(chǎn)品。

/*producer-customer.c*/

#includestdio.h>

#includestdlib.h>

#includeunistd.h>

#includefcntl.h>

#includepthread.h>

#includeerrno.h>

#includesemaphore.h>

#includesys/ipc.h>

#defineMYFIFOmyfifo/*緩沖區有名管道的名字*/

#defineBUFFER_SIZE3/*緩沖區的單元數*/

#defineUNIT_SIZE5/*每個(gè)單元的大小*/

#defineRUN_TIME30/*運行時(shí)間*/

#defineDELAY_TIME_LEVELS5.0/*周期的最大值*/

intfd;

time_tend_time;

sem_tmutex,full,avail;/*3個(gè)信號量*/

/*生產(chǎn)者線(xiàn)程*/

void*producer(void*arg)

{

intreal_write;

intdelay_time=0;

while(time(NULL)end_time)

{

delay_time=(int)(rand()*DELAY_TIME_LEVELS/(RAND_MAX)/2.0)+1;

sleep(delay_time);

/*P操作信號量avail和mutex*/

sem_wait(avail);

sem_wait(mutex);

printf(nProducer:delay=%dn,delay_time);

/*生產(chǎn)者寫(xiě)入數據*/

if((real_write=write(fd,hello,UNIT_SIZE))==-1)

{

if(errno==EAGAIN)

{

printf(TheFIFOhasnotbeenreadyet.Pleasetrylatern);

}

}

else

{

printf(Write%dtotheFIFOn,real_write);

}

/*V操作信號量full和mutex*/

sem_post(full);

sem_post(mutex);

}

pthread_exit(NULL);

}

/*消費者線(xiàn)程*/

void*customer(void*arg)

{

unsignedcharread_buffer[UNIT_SIZE];

intreal_read;

intdelay_time;

while(time(NULL)end_time)

{

delay_time=(int)(rand()*DELAY_TIME_LEVELS/(RAND_MAX))+1;

sleep(delay_time);

/*P操作信號量full和mutex*/

sem_wait(full);

sem_wait(mutex);

memset(read_buffer,0,UNIT_SIZE);

printf(nCustomer:delay=%dn,delay_time);

if((real_read=read(fd,read_buffer,UNIT_SIZE))==-1)

{

if(errno==EAGAIN)

{

printf(Nodatayetn);

}

}

printf(Read%sfromFIFOn,read_buffer);

/*V操作信號量avail和mutex*/

sem_post(avail);

sem_post(mutex);

}

pthread_exit(NULL);

}

intmain()

{

pthread_tthrd_prd_id,thrd_cst_id;

pthread_tmon_th_id;

intret;

srand(time(NULL));

end_time=time(NULL)+RUN_TIME;

/*創(chuàng )建有名管道*/

if((mkfifo(MYFIFO,O_CREAT|O_EXCL)0)(errno!=EEXIST))

{

printf(Cannotcreatefifon);

returnerrno;

}

/*打開(kāi)管道*/

fd=open(MYFIFO,O_RDWR);

if(fd==-1)

{

printf(Openfifoerrorn);

returnfd;

}

/*初始化互斥信號量為1*/

ret=sem_init(mutex,0,1);

/*初始化avail信號量為N*/

ret+=sem_init(avail,0,BUFFER_SIZE);

/*初始化full信號量為0*/

ret+=sem_init(full,0,0);

if(ret!=0)

{

printf(Anysemaphoreinitializationfailedn);

returnret;

}

/*創(chuàng )建兩個(gè)線(xiàn)程*/

ret=pthread_create(thrd_prd_id,NULL,producer,NULL);

if(ret!=0)

{

printf(Createproducerthreaderrorn);

returnret;

}

ret=pthread_create(thrd_cst_id,NULL,customer,NULL);

if(ret!=0)

{

printf(Createcustomerthreaderrorn);

returnret;

}

pthread_join(thrd_prd_id,NULL);

pthread_join(thrd_cst_id,NULL);

close(fd);

unlink(MYFIFO);

return0;

}

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

tcp/ip相關(guān)文章:tcp/ip是什么




評論


相關(guān)推薦

技術(shù)專(zhuān)區

關(guān)閉
国产精品自在自线亚洲|国产精品无圣光一区二区|国产日产欧洲无码视频|久久久一本精品99久久K精品66|欧美人与动牲交片免费播放
<dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><small id="yhprb"></small><dfn id="yhprb"></dfn><small id="yhprb"><delect id="yhprb"></delect></small><small id="yhprb"></small><small id="yhprb"></small> <delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"></dfn><dfn id="yhprb"></dfn><s id="yhprb"><noframes id="yhprb"><small id="yhprb"><dfn id="yhprb"></dfn></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><small id="yhprb"></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn> <small id="yhprb"></small><delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn>