Linux环境下实现生产者消费者问题

前端之家收集整理的这篇文章主要介绍了Linux环境下实现生产者消费者问题前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

<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;"&gt;while</span>(<span style="color: #800080;"&gt;1</span><span style="color: #000000;"&gt;)
{
    sleep(</span><span style="color: #800080;"&gt;1</span><span style="color: #000000;"&gt;);
    sem_wait(</span>&amp;<span style="color: #000000;"&gt;full_sem);
    pthread_mutex_lock(</span>&amp;<span style="color: #000000;"&gt;mutex);

    </span><span style="color: #0000ff;"&gt;out</span> = <span style="color: #0000ff;"&gt;out</span> %<span style="color: #000000;"&gt; M;
    printf(</span><span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;Consumer %d take product  %d in position %d\n</span><span style="color: #800000;"&gt;"</span>,buff[<span style="color: #0000ff;"&gt;out</span>],<span style="color: #0000ff;"&gt;out</span><span style="color: #000000;"&gt;);
    buff[</span><span style="color: #0000ff;"&gt;out</span>] = <span style="color: #800080;"&gt;0</span><span style="color: #000000;"&gt;;
    </span>++<span style="color: #0000ff;"&gt;out</span><span style="color: #000000;"&gt;;

    pthread_mutex_unlock(</span>&amp;<span style="color: #000000;"&gt;mutex);
    sem_post(</span>&amp;<span style="color: #000000;"&gt;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;"&gt;int</span><span style="color: #000000;"&gt; i;
</span><span style="color: #0000ff;"&gt;int</span><span style="color: #000000;"&gt; ret1[N1];
</span><span style="color: #0000ff;"&gt;int</span><span style="color: #000000;"&gt; ret2[N2];

</span><span style="color: #0000ff;"&gt;int</span> ini1 = sem_init(&amp;empty_sem,<span style="color: #800080;"&gt;0</span>,M);  <span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt;初始化空缓冲区 为 M(10)</span>
<span style="color: #0000ff;"&gt;int</span> ini2 = sem_init(&amp;full_sem,<span style="color: #800080;"&gt;0</span>);<span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt;初始化满缓冲区  0</span>
<span style="color: #0000ff;"&gt;if</span>(ini1 &amp;&amp; ini2 != <span style="color: #800080;"&gt;0</span><span style="color: #000000;"&gt;)
{
    printf(</span><span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;Sem init <a href="https://www.jb51.cc/tag/Failed/" target="_blank" class="keywords">Failed</a>\n</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;);
    exit(</span><span style="color: #800080;"&gt;1</span><span style="color: #000000;"&gt;);
}

</span><span style="color: #0000ff;"&gt;int</span> ini3 = pthread_mutex_init(&amp;mutex,NULL);<span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt;初始化互斥信号量</span>
<span style="color: #0000ff;"&gt;if</span>(ini3 != <span style="color: #800080;"&gt;0</span><span style="color: #000000;"&gt; )
{
    printf(</span><span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;mutex init <a href="https://www.jb51.cc/tag/Failed/" target="_blank" class="keywords">Failed</a>\n</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;);
    exit(</span><span style="color: #800080;"&gt;1</span><span style="color: #000000;"&gt;);
}

fp </span>= fopen(<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;/.data.txt</span><span style="color: #800000;"&gt;"</span>,<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;r</span><span style="color: #800000;"&gt;"</span>);<span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt;打开<a href="https://www.jb51.cc/tag/wenjian/" target="_blank" class="keywords">文件</a>Data.txt</span>
<span style="color: #0000ff;"&gt;if</span>(fp == NULL) exit(<span style="color: #800080;"&gt;1</span><span style="color: #000000;"&gt;);

</span><span style="color: #0000ff;"&gt;for</span>(i = <span style="color: #800080;"&gt;0</span>; i < N1; i++<span style="color: #000000;"&gt;)
{
    ret1[i] </span>= pthread_create(&amp;id1[i],NULL,product,(<span style="color: #0000ff;"&gt;void</span>*)(&amp;i));<span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt;创建生产者线程</span>
    <span style="color: #0000ff;"&gt;if</span>(ret1[i] != <span style="color: #800080;"&gt;0</span><span style="color: #000000;"&gt;)
    {
        printf(</span><span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;product%d creat <a href="https://www.jb51.cc/tag/Failed/" target="_blank" class="keywords">Failed</a>\n</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;,i);
        exit(</span><span style="color: #800080;"&gt;1</span><span style="color: #000000;"&gt;);
    }
}

</span><span style="color: #0000ff;"&gt;for</span>(i = <span style="color: #800080;"&gt;0</span>; i< N2; i++<span style="color: #000000;"&gt;)
{
    ret2[i] </span>= pthread_create(&amp;id2[i],consume,NULL);<span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt;创建消费者线程</span>
    <span style="color: #0000ff;"&gt;if</span>(ret2[i] != <span style="color: #800080;"&gt;0</span><span style="color: #000000;"&gt;)
    {
        printf(</span><span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;consumer%d creat <a href="https://www.jb51.cc/tag/Failed/" target="_blank" class="keywords">Failed</a>\n</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;,i);
        exit(</span><span style="color: #800080;"&gt;1</span><span style="color: #000000;"&gt;);
    }
}


</span><span style="color: #0000ff;"&gt;for</span>(i = <span style="color: #800080;"&gt;0</span>; i < N1; i++<span style="color: #000000;"&gt;) {pthread_join(id1[i],NULL);}
</span><span style="color: #0000ff;"&gt;for</span>(i = <span style="color: #800080;"&gt;0</span>; i < N2; i++<span style="color: #000000;"&gt;) {pthread_join(id2[i],NULL);}

exit(</span><span style="color: #800080;"&gt;0</span><span style="color: #000000;"&gt;);

}

 

原文链接:https://www.f2er.com/linux/238798.html

猜你在找的Linux相关文章