Linux操作系统实验初学(1)(生产者消费者问题)

数据类型:

sem_t :信号量的数据类型,本质上是一个长整型

   pthread_t:用于声明线程的ID。

pthread_mutex_t:互斥锁

函数:

int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t * attr):用于初始化互斥锁。

int sem_init(sem_t *sem, int pshared, unsigned int value):初始化有sem指向的信号量对象,设置它的共享选项,

并置初始化整数值。参数pshared控制信号量的类型,如果其值为0,就表示该信号量是当前进程的局部信号量,

否则,这个信号量可以在多个进程中共享。

int sem_wait(sem_t *sem);以原子操作的方式将信号量的值减1.

int sem_post(sem_t *sem):以原子操作的方式将信号量的值加1.

int sem_destroy(sem_t *sem):用于清理信号量。

int pthread_mutex_lock(pthread_mutex_t *mutex)

int pthread_mutex_umlock(pthread_mutex_t *mutex)

int pthread_mutex_destroy(pthread_mutex_t *mutex)

int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void*(*start_rtn)(void), void *restrict arg);

函数返回0表示成功,非0表示失败。

int pthread_join(pthread_t thread, void **status);参数thread为指定将要等待的线程,参数status指向另一个指针的指

针,用于指定返回值。

生产者消费者问题


#include
#include
#include
#include
#include
#define n 11 //缓冲池大小
#define PRODUCER_SIZE 5 //生产者数量
#define CONSUMER_SIZE 5 //消费者数量
int pool[n];//缓冲区
int in = 0; // 缓冲池写入指针
int out = 0;// 缓冲池读出指针
pthread_mutex_t mutex; // 互斥信号量
sem_t empty; //空缓冲池数量
sem_t full; //满缓冲池数量
int id1 = 0, id2 = 0; // 生产者id 消费者id
void *producer(void *arg){
    int id = ++id1;
    while(1){
        sleep(1);
        sem_wait(&empty);
        pthread_mutex_lock(&mutex);
        pool[in] = 1;
        in = (in + 1) % n;
        printf("producer %d write to pool\n", id);
        printf("pool size is %d\n", (in - out + n) % n);
        pthread_mutex_unlock(&mutex);
        sem_post(&full);
    }
}
void *consumer(void *arg){
    int id = ++ id2;
    while(1){
        sleep(10);
        sem_wait(&full);
        pthread_mutex_lock(&mutex);
        out = (out + 1) % n;
        printf("consumer %d read from pool\n", id);
        printf("pool size is %d\n", (in - out + n) % n);
        pthread_mutex_unlock(&mutex);
        sem_post(&empty);
    }
}
int main(){
    pthread_t producer_id[PRODUCER_SIZE];
    pthread_t consumer_id[CONSUMER_SIZE];
    pthread_mutex_init(&mutex, NULL); // 初始化互斥量
    int flag = sem_init(&empty, 0, n - 1); // 初始化信号量empty为缓冲池大小
    if(flag != 0){
        printf("sem_init error\n");
        exit(0);
    }
    flag = sem_init(&full, 0, 0); // 初始化信号量full为0
    if(flag != 0){
        printf("sem_init error\n");
        exit(0);
    }
    for(int i = 0; i < PRODUCER_SIZE; ++i){
        //创建生产者线程
        flag = pthread_create(&producer_id[i], NULL, producer, (void*) (&i));
        if(flag != 0){
            printf("producer_id error\n");
            exit(0);
        }
    }
    for(int i = 0; i < CONSUMER_SIZE; ++i){
        //创建消费者线程
        flag = pthread_create(&consumer_id[i], NULL, consumer, (void*) (&i));
        if(flag != 0){
            printf("consumer_id error\n");
            exit(0);
        }
    }
    for(int i = 0; i < PRODUCER_SIZE; ++i){
	pthread_join(producer_id[i], NULL);
	pthread_join(consumer_id[i], NULL);
    }
	return 0;
}


你可能感兴趣的:(Linux操作系统实验初学(1)(生产者消费者问题))