You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
3.8 KiB

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/types.h>
#include <semaphore.h>
int n=10, in=0,out=0,buffer[10];
sem_t empty,full, mutex;
void* producer(void *arg){
int tag= pthread_self()%100;
int nextPro;
srand(time(NULL)+tag);
for(int i = 0; i < 10; i++){
nextPro = rand()%97;
sem_wait(&empty);
buffer[in] = nextPro;
printf("production:%d %d\n",nextPro,buffer[in]);
usleep(1000*1000/20);
in = (in+1)%n;
sem_post(&full);
usleep(1000*1000/2);
}
}
void* consumer(void *arg) {
int item;
for(int i = 1; i < 10; i++){
sem_wait(&full);
item = buffer[out];
sem_post(&empty);
printf("consume:%d\n",buffer[out]);
out = (out+1)%n;
usleep(1000*1000/10);
}
}
int main() {
pthread_t tid[6];
//初始有10个缓冲区
sem_init( &empty, 0,10);
sem_init( &full, 0,0);
//****************一个生产者和一个消费者***********
//pthread_create(&tid[0], NULL, producer, NULL);
//pthread_create(&tid[1], NULL, consumer, NULL);
//**********************************************
//***************一个生产者和多个消费者*************
/*pthread_create(&tid[0], NULL, producer, NULL);
for(int i = 1; i < 6; i++){//需要互斥变量
pthread_create(&tid[i], NULL, consumer, NULL);
}*/
//*****************************************
//***************多个生产者和一个消费者*************
/*pthread_create(&tid[0], NULL, consumer, NULL);
for(int i = 1; i < 6; i++){//需要互斥变量
pthread_create(&tid[i], NULL, producer, NULL);
}*/
//********************************************
//************多个生产者和多个消费者******************
/*for(int i = 0; i < 3; i++){
pthread_create(&tid[i], NULL, producer, NULL);
}
for(int i = 3; i < 6; i++){
pthread_create(&tid[i], NULL, consumer, NULL);
}*/
//***********************************************
for(int i = 0; i < 6; i++)
pthread_join(tid[i], NULL);
printf("main is over\n");
return 0;
}
//第一题
sem_t A, B, C;
void* printA(void* arg){
for(int i = 0; i < 10; i++){
sem_wait(&A);
printf("A\n");
sem_post(&B);
}
}
void* printB(void* arg){
for(int i = 0; i < 10; i++){
sem_wait(&B);
printf("B\n");
sem_post(&C);
}
}
void* printC(void* arg){
for(int i = 0; i < 10; i++){
sem_wait(&C);
printf("C\n");
sem_post(&A);
}
}
int main(){
sem_init(&A, 0, 1);
sem_init(&B, 0, 0);
sem_init(&C, 0, 0);
pthread_t tid[3];
pthread_create(&tid[0], NULL, printA, NULL);
pthread_create(&tid[1], NULL, printB, NULL);
pthread_create(&tid[2], NULL, printC, NULL);
for(int i = 0; i < 3; i++)
pthread_join(tid[i], NULL);
}
//第二题
sem_t A, B, C;
void* printA(void* arg){
for(int i = 0; i < 30; i++){
sem_wait(&A);
printf("A\n");
sem_post(&B);
}
}
void* printB(void* arg){
for(int i = 0; i < 30; i++){
sem_wait(&B);
printf("B\n");
sem_post(&C);
}
}
void* printC(void* arg){
for(int i = 0; i < 30; i++){
sem_wait(&C);
printf("C\n");
sem_post(&A);
}
}
int main(){
sem_init(&A, 0, 3);//信号量改成3即可输出三次
sem_init(&B, 0, 0);
sem_init(&C, 0, 0);
pthread_t tid[3];
pthread_create(&tid[0], NULL, printA, NULL);
pthread_create(&tid[1], NULL, printB, NULL);
pthread_create(&tid[2], NULL, printC, NULL);
for(int i = 0; i < 3; i++)
pthread_join(tid[i], NULL);
return 0;
}