Thread the producer - consumer problem (Semaphore)

Producer - consumer issues (producers and consumers, problem)

/*伪代码*/
#define BUFFER_SIZE k
itemType buffer[BUFFER_SIZE];//全局数组作为线程间共享的数据
semaphore s1=BUFFER_SIZE;//可以使用的空缓冲区数
semaphore s2=0;//缓冲区内可以使用的产品数
int in=0;//in为放入指针
int out=0;//out为取出指针
int count=0;count为缓冲区已放入指针计数量
semaphore mutex=1;//静态创建锁或全局变量动态锁
/*生产者伪代码*/
void producer(){
	while(1){
		produceItem(&item);
		P(s1);
		P(mutex);
		buffer[in]=item;
		in=(in+1)%BUFFER_SIZE;
		V(mutex);
		V(s2);
	}
}
/*消费者伪代码*/
void consumer(){
	itemType x;
	while(1){
		P(s2);
		P(mutex);
		x=buffer[out];
		out=(out+1)%BUFFER_SIZE;
		V(mutex);
		V(s1);
		consumeItem(x);
	}
}
/*使用pthread线程+sem+mutex模拟“生产者-消费者”问题*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <semaphore.h>//信号量sem_t函数头文件
#include "buffer.h"  //缓冲区头文件

buffer_item buffer[BUFFER_SIZE];//全局数组作为线程间共享的数据
int in_pthread=0,out_pthread=0;//in_pthread为放入指针,out_pthread为取出指针
int count_pthread=0;//count_pthread为缓冲区已放入指针计数量
pthread_mutex_t p_lock;//全局变量动态锁
sem_t s1;//可以使用的空缓冲区数
sem_t s2;//缓冲区内可以使用的产品数

int insert_item(buffer_item item);//生产者插入
int remove_item(buffer_item *item);//消费者取出
void *producer(void *param);//生产者线程
void *consumer(void *param);//消费者线程

int main(int argc,char *argv[])
{
    /*判断参数的合法性*/
    if(argc<4)
    {
        printf("lack of parameters\n");
        exit(0);
    }
    int n=atoi(argv[1]);
    if(n<0)
    {
        printf("Wrong input of sleep time!\n");
        exit(0);
    }
    if(atoi(argv[2])<0)
    {
        printf("Wrong input of producer threads numbers!\n");
        exit(0);
    }
    if(atoi(argv[3])<0)
    {
        printf("Wrong input of consumer threads numbers!\n");
        exit(0);
    }
    /*创建锁及设置信号量初值*/
    pthread_mutex_init(&p_lock,NULL);//动态创建锁
    sem_init(&s1,0,BUFFER_SIZE);//为s1设置初值
    sem_init(&s2,0,0);//为s2设置初值
    /*创建生产者线程*/
    int i;
    for(i=1;i<=atoi(argv[2]);i++)
    {
        pthread_t tid_producer;
        pthread_attr_t attr;//const pthread_attr_t *attr的分离属性
        pthread_attr_init(&attr);
        pthread_create(&tid_producer,&attr,(void*)producer,(void *)i);
    }
    /*创建消费者线程*/
    for(i=1;i<=atoi(argv[3]);i++)
    {
        pthread_t tid_consumer;
        pthread_attr_t attr;
        pthread_attr_init(&attr);
        pthread_create(&tid_consumer,&attr,(void*)consumer,(void *)i);
    }
    pthread_mutex_destroy(&p_lock);//删除动态锁
    /*sleep等待n秒*/
    sleep(n);

    return 0;
}
void *producer(void *param)
{
	buffer_item item;
	srand((unsigned)time(NULL));
	while(1)
    {
        sleep(rand()%5+1);//睡1~5范围内的随机秒数
        item=rand()%5+1;//生成1~5范围内的随机整数
        if(insert_item(item))
            printf("Produce error\n");//打印“出错信息”
        else
            printf("Producer thread %d produce %d\n",param,item);//打印“生产者线程param生产了item”
    }
}
void *consumer(void *param)
{
	buffer_item item;
	srand((unsigned)time(NULL));
	while(1)
    {
        sleep(rand()%5+1);//睡1~5范围内的随机秒数
        if(remove_item(&item))
            printf("Remove error\n");//打印“出错信息”
        else
            printf("Consumer thread %d consume %d\n",param,item);//打印“消费者线程param消费了item”
    }
}
int insert_item(buffer_item item)//insert item into buffer
{
    if(count_pthread==BUFFER_SIZE)
        return -1;
    else
    {
        sem_wait(&s1);//信号量减一
        pthread_mutex_lock(&p_lock);//上锁
        buffer[in_pthread]=item;
        in_pthread=(in_pthread+1)%BUFFER_SIZE;
        count_pthread++;
        pthread_mutex_unlock(&p_lock);//解锁
        sem_post(&s2);//信号量加一
    }
	return 0;

}
int remove_item(buffer_item *item)//remove item from buffer
{//使用*item是为了从buffer中取数
    if(count_pthread==0)
        return -1;
    else
    {
        sem_wait(&s2);//信号量减一
        pthread_mutex_lock(&p_lock);//上锁
        *item=buffer[out_pthread];
        buffer[out_pthread]=0;
        out_pthread=(out_pthread+1)%BUFFER_SIZE;
        count_pthread--;
        pthread_mutex_unlock(&p_lock);//解锁
        sem_post(&s1);//信号量加一
    }
	return 0;
}

To understand producer - consumer problem query large amounts of data, data link as follows:
(1) producers and consumers pseudo-code and C language multithreading
https://blog.csdn.net/chen_mama/article/details/ 47,347,261

(2) pthread simulation producers and consumers of
https://blog.csdn.net/sinat_16709955/article/details/75208019

(3) pthread_attr_t control thread attribute structure
https://blog.csdn.net/woxiangzi/article/details/50894588

(4) the amount of the difference signal to the lock
https://blog.csdn.net/everysmile/article/details/50573668

(5) [Linux] Examples of Semaphore Semaphore thread synchronization
https://www.cnblogs.com/cuish/p/4133919.html

(6) linux multithreading 5- semaphore (semaphore)
https://blog.csdn.net/maopig/article/details/8309920

(7) inter-process communication linux - semaphore (semaphore)
https://www.cnblogs.com/fangshenghui/p/4039946.html

Works (8) Semaphore and examples
https://blog.csdn.net/carson0408/article/details/79475723

After completing the program, and some digging on the Internet is also a multi-threaded "producer - consumer issues" program, links are as follows:
(1) the semaphore multithreaded
https://blog.csdn.net/csdn1126274345/article / details / 82315867

(2) Operating System - C language producers and consumers
https://blog.csdn.net/lllllyt/article/details/80506508

(3) Linux-threaded programming of producers and consumers
https://www.cnblogs.com/clover-toeic/p/4029269.html

(4) with multi-thread synchronization solution to producer - consumer issues
https://wenku.baidu.com/view/4df8457f31b765ce050814ea.html

Guess you like

Origin blog.csdn.net/JxufeCarol/article/details/89344789