实验4 经典进程同步问题的实现

一、实验目的

1. 掌握信号通信机制

2. 使用信号量机制实现经典进程同步的生产者和消费者问题

二、实验工具与设备

装有Linux系统的计算机。

三、实验预备知识

1.创建多个线程,模拟生产者和消费者问题的求解。

参考代码:

#include<stdio.h>  

#include<stdlib.h>  

#include<unistd.h>  

#include<pthread.h>  

#include<semaphore.h>  

  

#define PRODUCER_NUM 5 //生产者数目  

#define CONSUMER_NUM 5 //消费者数目  

#define POOL_SIZE    11  //缓冲池大小  

int pool[POOL_SIZE];    //缓冲区  

int head=0; //缓冲池读取指针  

int rear=0; //缓冲池写入指针  

sem_t   room_sem;       //同步信号信号量,表示缓冲区有可用空间  

sem_t   product_sem;        //同步信号量,表示缓冲区有可用产品  

pthread_mutex_t mutex;  

void *producer_fun(void *arg)  

{  

    while (1)  

    {  

        sleep(1);  

        sem_wait(&room_sem);  

        pthread_mutex_lock(&mutex);  

        //生产者往缓冲池中写入数据  

        pool[rear] = 1;  

        rear = (rear + 1) % POOL_SIZE;  

        printf("producer %d write to pool\n", (int)arg);  

        printf("pool size is %d\n",(rear-head+POOL_SIZE)%POOL_SIZE);  

        pthread_mutex_unlock(&mutex);  

        sem_post(&product_sem);  

    }  

}  

  

  

void *consumer_fun(void *arg)  

{  

    while (1)  

    {  

        int data;  

        sleep(10);  

        sem_wait(&product_sem);  

        pthread_mutex_lock(&mutex);  

        //消费者从缓冲池读取数据  

        data = pool[head];  

        head = (head + 1) % POOL_SIZE;  

        printf("consumer %d read from pool\n", (int)arg);  

        printf("pool size is %d\n",(rear-head+POOL_SIZE)%POOL_SIZE);  

        pthread_mutex_unlock(&mutex);  

        sem_post(&room_sem);  

    }  

}  

  

int main()  

{  

    pthread_t producer_id[PRODUCER_NUM];  

    pthread_t consumer_id[CONSUMER_NUM];  

    pthread_mutex_init(&mutex, NULL);   //初始化互斥量  

    int ret = sem_init(&room_sem, 0, POOL_SIZE-1);  //初始化信号量room_sem为缓冲池大小  

    if (ret != 0)  

    {  

        printf("sem_init error");  

        exit(0);  

    }  

    ret = sem_init(&product_sem, 0, 0); //初始化信号量product_sem为0,开始时缓冲池中没有数据  

    if (ret != 0)  

    {  

        printf("sem_init error");  

        exit(0);  

    }  

    for (int i = 0; i < PRODUCER_NUM; i++)  

    {  

        //创建生产者线程  

        ret =pthread_create(&producer_id[i], NULL, producer_fun, (void*)i);  

        if (ret != 0)  

        {  

            printf("producer_id error");  

            exit(0);  

        }  

        //创建消费者线程  

        ret = pthread_create(&consumer_id[i], NULL, consumer_fun, (void*)i);  

        if (ret != 0)  

        {  

            printf("consumer_id error");  

            exit(0);  

        }  

    }  

    for(int i=0;i<PRODUCER_NUM;i++)  

    {  

        pthread_join(producer_id[i],NULL);  

        pthread_join(consumer_id[i],NULL);  

    }  

  

    exit(0);  

}  

 

2.创建多个进程,模拟生产者和消费者问题的求解。(选作)

五、实验总结

1.写出实验报告。

2.总结解决生产者和消费者问题,关键的问题在哪儿,你是怎么解决的。

 

 1.报错:warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type /usr/include/pthread.h:225: note: expected ‘void * (*)(void *)’ but argument is of type ‘void (*)(void *)’

经查函数原型为:void *sayhello(void *arg)”改成“void sayhello(void *arg)”会报错

所以将void producer_fun(void *arg)  改为void *producer_fun(void *arg)

void consumer_fun(void *arg)  改为void *onsumer_fun(void *arg)即可

  

error: stray ‘\302’ in program error: stray ‘\240’ in program 处理

在编译器外面写完想复制回虚拟机的编译器里,结果报错,怀疑是某些字符的问题,于是在word中“只粘贴文本”进行粘贴再复制到编译器同样报错,粘贴到记事本中再复制到编译器也报错;于是命令行输入:od -c 文件名

会显示出如图,回到源程序在上图对应的302和240处相应做出删除即可

 

 

 

 

 

 

 

 

 

GitHub 加速计划 / li / linux-dash
10.39 K
1.2 K
下载
A beautiful web dashboard for Linux
最近提交(Master分支:2 个月前 )
186a802e added ecosystem file for PM2 4 年前
5def40a3 Add host customization support for the NodeJS version 4 年前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐