<div class="cnblogs_code">
include <stdio.h><span style="color: #000000;">include <semaphore.h><span style="color: #000000;">
include <stdlib.h><span style="color: #000000;">
include <pthread.h><span style="color: #000000;">
include <unistd.h>
<span style="color: #0000ff;">#define N1 3<span style="color: #008000;">//<span style="color: #008000;">定义3个生产者
<span style="color: #0000ff;">#define N2 4<span style="color: #008000;">//<span style="color: #008000;">定义4 个消费者
<span style="color: #0000ff;">#define M 10<span style="color: #008000;">//<span style="color: #008000;">定义10个大小缓冲区<span style="color: #0000ff;">int <span style="color: #0000ff;">in = <span style="color: #800080;">0<span style="color: #000000;">;
<span style="color: #0000ff;">int <span style="color: #0000ff;">out = <span style="color: #800080;">0<span style="color: #000000;">;<span style="color: #0000ff;">int buff[M] = {<span style="color: #800080;">0};<span style="color: #008000;">//<span style="color: #008000;">缓冲区大小为10
<span style="color: #000000;">
sem_t empty_sem;<span style="color: #008000;">//<span style="color: #008000;">空缓冲区数量
sem_t full_sem;<span style="color: #008000;">//<span style="color: #008000;">满缓冲区数量
pthread_mutex_t mutex;<span style="color: #008000;">//<span style="color: #008000;">互斥访问缓冲区<span style="color: #0000ff;">int product_id = <span style="color: #800080;">0<span style="color: #000000;">;
<span style="color: #0000ff;">int consumer_id = <span style="color: #800080;">0<span style="color: #000000;">;<span style="color: #0000ff;">int<span style="color: #000000;"> data;
FILE *<span style="color: #000000;">fp;<span style="color: #0000ff;">void *<span style="color: #000000;"> product()
{
<span style="color: #0000ff;">int id = ++<span style="color: #000000;">product_id;
<span style="color: #0000ff;">while(<span style="color: #800080;">1<span style="color: #000000;">)
{
sleep(<span style="color: #800080;">1<span style="color: #000000;">);
sem_wait(&<span style="color: #000000;">empty_sem);
pthread_mutex_lock(&<span style="color: #000000;">mutex);
<span style="color: #008000;">//<span style="color: #008000;">if(feof(fp) != 0) fseek(fp,SEEK_SET);
<span style="color: #0000ff;">if(fscanf(fp,<span style="color: #800000;">"<span style="color: #800000;">%d<span style="color: #800000;">",&data)==<span style="color: #000000;">EOF)
{
fseek(fp,<span style="color: #800080;">0<span style="color: #000000;">,SEEK_SET);
fscanf(fp,<span style="color: #800000;">"<span style="color: #800000;">%d<span style="color: #800000;">",&<span style="color: #000000;">data);
}
<span style="color: #0000ff;">in = <span style="color: #0000ff;">in %<span style="color: #000000;"> M;
buff[<span style="color: #0000ff;">in] =<span style="color: #000000;"> data;
printf(<span style="color: #800000;">"<span style="color: #800000;">Producter %d produce %d in position %d\n<span style="color: #800000;">",id,buff[<span style="color: #0000ff;">in],<span style="color: #0000ff;">in<span style="color: #000000;">);
++<span style="color: #0000ff;">in<span style="color: #000000;">;
pthread_mutex_unlock(&<span style="color: #000000;">mutex);
sem_post(&<span style="color: #000000;">full_sem);
}
}<span style="color: #0000ff;">void *<span style="color: #000000;">consume()
{
<span style="color: #0000ff;">int id = ++<span style="color: #000000;">consumer_id;</span><span style="color: #0000ff;">while</span>(<span style="color: #800080;">1</span><span style="color: #000000;">) { sleep(</span><span style="color: #800080;">1</span><span style="color: #000000;">); sem_wait(</span>&<span style="color: #000000;">full_sem); pthread_mutex_lock(</span>&<span style="color: #000000;">mutex); </span><span style="color: #0000ff;">out</span> = <span style="color: #0000ff;">out</span> %<span style="color: #000000;"> M; printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">Consumer %d take product %d in position %d\n</span><span style="color: #800000;">"</span>,buff[<span style="color: #0000ff;">out</span>],<span style="color: #0000ff;">out</span><span style="color: #000000;">); buff[</span><span style="color: #0000ff;">out</span>] = <span style="color: #800080;">0</span><span style="color: #000000;">; </span>++<span style="color: #0000ff;">out</span><span style="color: #000000;">; pthread_mutex_unlock(</span>&<span style="color: #000000;">mutex); sem_post(</span>&<span style="color: #000000;">empty_sem); }
}
<span style="color: #0000ff;">int<span style="color: #000000;"> main()
{
pthread_t id1[N1];<span style="color: #008000;">//<span style="color: #008000;">定义生产者线程
pthread_t id2[N2];<span style="color: #008000;">//<span style="color: #008000;">定义消费者线程<span style="color: #0000ff;">int</span><span style="color: #000000;"> i; </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> ret1[N1]; </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> ret2[N2]; </span><span style="color: #0000ff;">int</span> ini1 = sem_init(&empty_sem,<span style="color: #800080;">0</span>,M); <span style="color: #008000;">//</span><span style="color: #008000;">初始化空缓冲区 为 M(10)</span> <span style="color: #0000ff;">int</span> ini2 = sem_init(&full_sem,<span style="color: #800080;">0</span>);<span style="color: #008000;">//</span><span style="color: #008000;">初始化满缓冲区 0</span> <span style="color: #0000ff;">if</span>(ini1 && ini2 != <span style="color: #800080;">0</span><span style="color: #000000;">) { printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">Sem init <a href="https://www.jb51.cc/tag/Failed/" target="_blank" class="keywords">Failed</a>\n</span><span style="color: #800000;">"</span><span style="color: #000000;">); exit(</span><span style="color: #800080;">1</span><span style="color: #000000;">); } </span><span style="color: #0000ff;">int</span> ini3 = pthread_mutex_init(&mutex,NULL);<span style="color: #008000;">//</span><span style="color: #008000;">初始化互斥信号量</span> <span style="color: #0000ff;">if</span>(ini3 != <span style="color: #800080;">0</span><span style="color: #000000;"> ) { printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">mutex init <a href="https://www.jb51.cc/tag/Failed/" target="_blank" class="keywords">Failed</a>\n</span><span style="color: #800000;">"</span><span style="color: #000000;">); exit(</span><span style="color: #800080;">1</span><span style="color: #000000;">); } fp </span>= fopen(<span style="color: #800000;">"</span><span style="color: #800000;">/.data.txt</span><span style="color: #800000;">"</span>,<span style="color: #800000;">"</span><span style="color: #800000;">r</span><span style="color: #800000;">"</span>);<span style="color: #008000;">//</span><span style="color: #008000;">打开<a href="https://www.jb51.cc/tag/wenjian/" target="_blank" class="keywords">文件</a>Data.txt</span> <span style="color: #0000ff;">if</span>(fp == NULL) exit(<span style="color: #800080;">1</span><span style="color: #000000;">); </span><span style="color: #0000ff;">for</span>(i = <span style="color: #800080;">0</span>; i < N1; i++<span style="color: #000000;">) { ret1[i] </span>= pthread_create(&id1[i],NULL,product,(<span style="color: #0000ff;">void</span>*)(&i));<span style="color: #008000;">//</span><span style="color: #008000;">创建生产者线程</span> <span style="color: #0000ff;">if</span>(ret1[i] != <span style="color: #800080;">0</span><span style="color: #000000;">) { printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">product%d creat <a href="https://www.jb51.cc/tag/Failed/" target="_blank" class="keywords">Failed</a>\n</span><span style="color: #800000;">"</span><span style="color: #000000;">,i); exit(</span><span style="color: #800080;">1</span><span style="color: #000000;">); } } </span><span style="color: #0000ff;">for</span>(i = <span style="color: #800080;">0</span>; i< N2; i++<span style="color: #000000;">) { ret2[i] </span>= pthread_create(&id2[i],consume,NULL);<span style="color: #008000;">//</span><span style="color: #008000;">创建消费者线程</span> <span style="color: #0000ff;">if</span>(ret2[i] != <span style="color: #800080;">0</span><span style="color: #000000;">) { printf(</span><span style="color: #800000;">"</span><span style="color: #800000;">consumer%d creat <a href="https://www.jb51.cc/tag/Failed/" target="_blank" class="keywords">Failed</a>\n</span><span style="color: #800000;">"</span><span style="color: #000000;">,i); exit(</span><span style="color: #800080;">1</span><span style="color: #000000;">); } } </span><span style="color: #0000ff;">for</span>(i = <span style="color: #800080;">0</span>; i < N1; i++<span style="color: #000000;">) {pthread_join(id1[i],NULL);} </span><span style="color: #0000ff;">for</span>(i = <span style="color: #800080;">0</span>; i < N2; i++<span style="color: #000000;">) {pthread_join(id2[i],NULL);} exit(</span><span style="color: #800080;">0</span><span style="color: #000000;">);
}