#include <semaphore.h> /* 信号量有关的函数 */
#include <pthread.h> /* 线程有关的函数 */
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#define NBUFF 10
int nitems;
int buff[NBUFF]; /* 缓冲区 */
sem_t mutex,nempty,nstored; /* 信号量 */
void *produce(void *), *consume(void *);
12
int main(int argc,char **argv)
{
pthread_t tid_produce,tid_consume;
if(argc != 2) perror("usage: prodcons2<#items>");
nitems = atoi(argv[1]); /* 字符串转化为整型*/
sem_init(&mutex, 0, 1); /* 信号量的建立即初始化 */
sem_init(&nempty, 0, NBUFF);
sem_init(&nstored, 0, 0);
if(pthread_create(&tid_produce,NULL,produce,NULL) ){ /* 创建一个新线程*/
perror("pthread_create: tid_produce");
exit(EXIT_FAILURE);
}
if(pthread_create(&tid_consume,NULL,consume,NULL)){
perror("pthread_create: tid_consume");
exit(EXIT_FAILURE);
}
pthread_join(tid_produce,NULL); /* 挂起当前线程 */
pthread_join(tid_consume,NULL);
sem_destroy(&mutex); /* 信号量的撤消 */
sem_destroy(&nempty);
sem_destroy(&nstored);
exit(0);
}
void *produce(void *arg) /* 生产者线程 */
{
int i,j = 0;
for(i = 1; i <= nitems; i++)
{
sem_wait(&nempty); /* 信号量原语 */
sem_wait(&mutex);
/* 可以写任何的事件 */
while(buff[j % NBUFF] != 0) j++; /* 从当前位置寻找空的存储单元 */
buff[j % NBUFF] = i;
13
printf("produce buff[%d] = %d\n",j % NBUFF,buff[j % NBUFF]);
sem_post(&mutex);
sem_post(&nstored);
sleep(1);
}
return (NULL);
}
void *consume(void *arg) /* 消费者线程 */
{
int i,j = 0;
for(i = 0; i < nitems; i++)
{
sem_wait(&nstored);
sem_wait(&mutex);
/* 可以写任何的事件 */
while(buff[j % NBUFF] == 0) j++; /* 从当前位置寻找存储着数据的单元 */
printf("consume buff[%d] = %d\n",j % NBUFF,buff[j % NBUFF]);
buff[j % NBUFF] = 0;
sem_post(&mutex);
sem_post(&nempty);
sleep(4);
}
return (NULL);
}
14
/*
* 实验四2进程同步与互斥
* 功能生产者与消费者问题锁机制(System V)
*/
#include <sys/types.h>
#include <pthread.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#define NBUFF 10
int nitems; /* 生产消费的数目 */
int buff[NBUFF]; /* 生产消费的缓冲区 */
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; /* 创建一个快速互斥 */
void *produce(void *), *consume(void *);
int main(int argc,char **argv)
{
pthread_t tid_produce,tid_consume; /* 线程标识符 */
if(argc != 2) perror("usage: prodcons2<#items>");
nitems = atoi(argv[1]);
if(pthread_create(&tid_produce,NULL,produce,NULL)){/* 创建一个新线程*/
perror("pthread_create: tid_produce");
exit(EXIT_FAILURE);
}
if(pthread_create(&tid_consume,NULL,consume,NULL)){
perror("pthread_create: tid_consume");
exit(EXIT_FAILURE);
}
pthread_join(tid_produce,NULL); /* 挂起当前线程 */
pthread_join(tid_consume,NULL);
exit(EXIT_SUCCESS);
}
void *produce(void *arg)
15
{
int i,j = 0;
for(i = 1;i <= nitems; i++)
{
if(pthread_mutex_lock(&mutex) != 0){ /* 加锁 */
perror("pthread_mutex_lock");
exit(EXIT_FAILURE);
}
while(j < NBUFF && buff[j % NBUFF] != 0) j++;
if(j < NBUFF){
buff[j] = i;
printf("produce buff[%d] = %d\n", j, buff[j]);
if(j == NBUFF-1) j = 0;
}
if(pthread_mutex_unlock(&mutex) != 0){ /* 开锁 */
perror("pthread_mutex_unlock");
exit(EXIT_FAILURE);
}
sleep(1);
}
return (NULL);
}
void *consume(void *arg)
{
int i,j = 0;
for(i = 0;i < nitems; i++)
{
if(pthread_mutex_trylock(&mutex) != 0){
perror("pthread_mutex_lock");
exit(EXIT_FAILURE);
}
while(j < NBUFF && buff[j % NBUFF] == 0) j++;
if(buff[j % NBUFF] != 0 && j < NBUFF){
printf("consume buff[%d] = %d\n", j, buff[j]);
buff[j % NBUFF] = 0;
if(j == NBUFF-1)j = 0;
}
if(pthread_mutex_unlock(&mutex) != 0){
perror("pthread_mutex_unlock");
exit(EXIT_FAILURE);
}
sleep(3);
}
return (NULL);
}