#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#define ItemSize 64
typedef char item[ItemSize];
#define ItemNumber 6
#define ProducingTime 1//s;
#define ConsumingTime 2//s;
#define MaxTimes 16
struct {
item data[ItemNumber];
int in;
int out;
int exist;
}buffer;
sem_t empty, full, mutex;
void initial();
void destroy();
void* producer();
void addItem();
void* consumer();
void getItem();
int main() {
pthread_t producerId, consumerId;
int ret;//return;
initial();
ret = pthread_create(&producerId, NULL, producer, NULL);//step one;
if (ret != 0) {
printf("Error in step one, fail to create producer.\n");
return 1;
}
ret = pthread_create(&consumerId, NULL, consumer, NULL);//step two;
if (ret != 0) {
printf("Error in step two, fail to create consumer.\n");
return 2;
}
ret = pthread_join(producerId, NULL);//step three;
if (ret != 0) {
printf("Error in step three, fail to wait producer.\n");
return 3;
}
ret = pthread_join(consumerId, NULL);//step four;
if (ret != 0) {
printf("Error in step four, fail to wait consumer.\n");
return 4;
}
destroy();
printf("The End.\n");
return 0;
}
void initial() {
sem_init(&empty, 0, ItemNumber);
sem_init(&full, 0, 0);
sem_init(&mutex, 0, 1);
buffer.in = 0;
buffer.out = 0;
buffer.exist = 0;
for (int i = 0; i < ItemNumber; i++) {
int k = 0;
buffer.data[i][k++] = 'N';
buffer.data[i][k++] = 'o';
buffer.data[i][k++] = 't';
buffer.data[i][k++] = 'h';
buffer.data[i][k++] = 'i';
buffer.data[i][k++] = 'n';
buffer.data[i][k++] = 'g';
buffer.data[i][k++] = '\0';
}
return;
}
void destroy() {
sem_destroy(&empty);
sem_destroy(&full);
sem_destroy(&mutex);
return;
}
void* producer() {
for (int i = 1; i <= MaxTimes; i++) {
sem_wait(&empty);
sem_wait(&mutex);
printf("producerId is %lu, ", pthread_self());
printf("add an item to buffer at index %d, ", buffer.in);
addItem();
buffer.in = (buffer.in + 1) % ItemNumber;
buffer.exist++;
printf("now the number of item in buffer is %d.\n", buffer.exist);
sem_post(&mutex);
sem_post(&full);
sleep(ProducingTime);
}
printf("Producing is finished.\n");
return NULL;
}
void addItem() {
int k = 0;
buffer.data[buffer.in][k++] = 'I';
buffer.data[buffer.in][k++] = 't';
buffer.data[buffer.in][k++] = 'e';
buffer.data[buffer.in][k++] = 'm';
switch(buffer.in) {
case 0: {
buffer.data[buffer.in][k++] = 'Z';
buffer.data[buffer.in][k++] = 'e';
buffer.data[buffer.in][k++] = 'r';
buffer.data[buffer.in][k++] = 'o';
break;
}
case 1: {
buffer.data[buffer.in][k++] = 'O';
buffer.data[buffer.in][k++] = 'n';
buffer.data[buffer.in][k++] = 'e';
break;
}
case 2: {
buffer.data[buffer.in][k++] = 'T';
buffer.data[buffer.in][k++] = 'w';
buffer.data[buffer.in][k++] = 'o';
break;
}
case 3: {
buffer.data[buffer.in][k++] = 'T';
buffer.data[buffer.in][k++] = 'h';
buffer.data[buffer.in][k++] = 'r';
buffer.data[buffer.in][k++] = 'e';
buffer.data[buffer.in][k++] = 'e';
break;
}
case 4: {
buffer.data[buffer.in][k++] = 'F';
buffer.data[buffer.in][k++] = 'o';
buffer.data[buffer.in][k++] = 'u';
buffer.data[buffer.in][k++] = 'r';
break;
}
case 5: {
buffer.data[buffer.in][k++] = 'F';
buffer.data[buffer.in][k++] = 'i';
buffer.data[buffer.in][k++] = 'v';
buffer.data[buffer.in][k++] = 'e';
break;
}
default: {
buffer.data[buffer.in][k++] = 'E';
buffer.data[buffer.in][k++] = 'r';
buffer.data[buffer.in][k++] = 'r';
buffer.data[buffer.in][k++] = 'o';
buffer.data[buffer.in][k++] = 'r';
buffer.data[buffer.in][k++] = 'I';
buffer.data[buffer.in][k++] = 'n';
buffer.data[buffer.in][k++] = 'd';
buffer.data[buffer.in][k++] = 'e';
buffer.data[buffer.in][k++] = 'x';
break;
}
}
buffer.data[buffer.in][k++] = '\0';
printf("the item is %s, ", buffer.data[buffer.in]);
return;
}
void* consumer() {
for (int i = 1; i <= MaxTimes; i++) {
sem_wait(&full);
sem_wait(&mutex);
printf("consumerId is %lu, ", pthread_self());
printf("get an item from buffer at index %d, ", buffer.out);
getItem();
buffer.out = (buffer.out + 1) % ItemNumber;
buffer.exist--;
printf("now the number of item in buffer is %d.\n", buffer.exist);
sem_post(&mutex);
sem_post(&empty);
sleep(ConsumingTime);
}
printf("Consuming is finished.\n");
return NULL;
}
void getItem() {
printf("the item is %s, ", buffer.data[buffer.out]);
int k = 0;
buffer.data[buffer.out][k++] = 'N';
buffer.data[buffer.out][k++] = 'o';
buffer.data[buffer.out][k++] = 't';
buffer.data[buffer.out][k++] = 'h';
buffer.data[buffer.out][k++] = 'i';
buffer.data[buffer.out][k++] = 'n';
buffer.data[buffer.out][k++] = 'g';
buffer.data[buffer.out][k++] = '\0';
return;
}
The above is an independent creation completely from the NUC programming god, all rights reserved, and the infringer must be sanctioned by the tiger security.
2022/11/19 16:30 from NUC 11-500, college no.7.