zoukankan      html  css  js  c++  java
  • 信号量示例——“生产者——消费者”实验

    互斥模型

     同步模型

     生产者消费者其实是用了两种模型,就是先同步再互斥,就是首先要先由生产者生产产品,后面消费者才能消费产品,同时生产和消费这个过程是要互斥的。

     1 #include<semaphore.h>
     2 #include<pthread.h>
     3 #include<stdio.h>
     4 #include<stdlib.h>
     5 #include<unistd.h>
     6 #include<string.h>
     7 
     8 #define N 10
     9 
    10 typedef struct product
    11 {
    12     char name;
    13 }product;
    14 
    15 product p1[N];
    16 
    17 sem_t mutex;
    18 sem_t synch1;
    19 sem_t synch2;
    20 
    21 int count = 0;
    22 
    23 void *produce_item(void *arg)
    24 {
    25     char ch = 'a';
    26     while (1)
    27     {
    28         printf("生产者生产一个产品
    ");
    29         sem_wait(&synch1);
    30         sem_wait(&mutex);
    31         p1[count].name= ch;
    32         sleep(1); 
    33         printf("------------------
    ");
    34         printf("%c产品已入库
    ",ch);
    35         ch++;
    36         count++;
    37         printf("产品个数%d
    ",count);
    38         printf("------------------
    ");
    39         sem_post(&mutex);
    40         sem_post(&synch2);
    41         sleep(1);
    42     }
    43 }
    44 
    45 void *custom_item(void *arg)
    46 {
    47     sleep(5);
    48     while (1)
    49     {
    50         sem_wait(&synch2);
    51         sem_wait(&mutex);
    52         count--;
    53         printf("消费者消费一个产品
    ");
    54         printf("产品个数%d
    ",count);
    55         sem_post(&mutex);
    56         sem_post(&synch1);
    57         sleep(2);
    58     }
    59     
    60 }
    61 int main(int argc, char const *argv[])
    62 {
    63     pthread_t producer;
    64     pthread_t customer;
    65     if(sem_init(&mutex,0,1) == -1)
    66     {
    67         perror("sem_init mutex");
    68         exit(1);
    69     }
    70     if(sem_init(&synch1,0,N) == -1)
    71     {
    72         perror("sem_init synch1");
    73         exit(1);
    74     }
    75     if(sem_init(&synch2,0,0) == -1)
    76     {
    77         perror("sem_init stnch2");
    78         exit(1);
    79     }
    80     if(pthread_create(&producer,NULL,produce_item,NULL) != 0)
    81     {
    82         perror("pthread_create producer");
    83         exit(1);
    84     }
    85     if(pthread_create(&customer,NULL,custom_item,NULL) == -1)
    86     {
    87         perror("pthread_create custom");
    88         exit(1);
    89     }
    90     pthread_join(producer,NULL);
    91     pthread_join(customer,NULL);
    92     sem_destroy(&mutex);
    93     sem_destroy(&synch1);
    94     sem_destroy(&synch2);
    95     return 0;
    96 }

    运行结果:

  • 相关阅读:
    net.sf.fmj.media.cdp.civil.CaptureDevicePlugger addCaptureDevices解决方法
    SVN快速入门教程
    Struts 2详细工作流程
    未能加载.NET基类问题
    图片上传的例子
    一个.NET发邮件的简单例子
    一种巧妙的删除程序自己的方法
    oracle的问题
    javascript 中对Trim()的实现
    SQL Server 不存在或访问被拒绝的问题
  • 原文地址:https://www.cnblogs.com/953-zjf/p/14706416.html
Copyright © 2011-2022 走看看