使用c语言,创建 5 个线程,其中两个线程为生产者线程, 3 个线程为消费者线程。一个生产者线程试图不断地在一个缓冲区中写入大写英 文字母,另一个生产者线程试图不断地在缓冲区中写入小写英文字母。3 个消费 者不断地从缓冲区中读取一个字符并输出。为了使得程序的输出易于看到结果, 可分别在生产者和消费者线区程的合适的位置加入一些随机睡眠时间。缓冲区的 大小为 10。
时间: 2023-06-18 21:08:47 浏览: 72
多线程可以使用的,使用c语言,使用信号量
下面是一个基于信号量实现的生产者消费者模型,满足题目要求:
```c
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#include <ctype.h>
#define BUFFER_SIZE 10
#define MAX_COUNT 100
char buffer[BUFFER_SIZE];
int write_pos = 0;
int read_pos = 0;
sem_t empty; // 缓冲区剩余空间
sem_t full; // 缓冲区已用空间
pthread_mutex_t mutex; // 互斥锁
void* producer_uppercase(void* arg) {
int count = 0;
while (count < MAX_COUNT) {
sem_wait(&empty); // 等待缓冲区有剩余空间
pthread_mutex_lock(&mutex); // 上锁
buffer[write_pos] = 'A' + rand() % 26; // 产生大写字母
printf("Producer(uppercase) writes %c at position %d\n", buffer[write_pos], write_pos);
write_pos = (write_pos + 1) % BUFFER_SIZE;
pthread_mutex_unlock(&mutex); // 解锁
sem_post(&full); // 缓冲区已用空间 + 1
count++;
usleep(rand() % 1000000); // 随机睡眠一段时间
}
return NULL;
}
void* producer_lowercase(void* arg) {
int count = 0;
while (count < MAX_COUNT) {
sem_wait(&empty); // 等待缓冲区有剩余空间
pthread_mutex_lock(&mutex); // 上锁
buffer[write_pos] = 'a' + rand() % 26; // 产生小写字母
printf("Producer(lowercase) writes %c at position %d\n", buffer[write_pos], write_pos);
write_pos = (write_pos + 1) % BUFFER_SIZE;
pthread_mutex_unlock(&mutex); // 解锁
sem_post(&full); // 缓冲区已用空间 + 1
count++;
usleep(rand() % 1000000); // 随机睡眠一段时间
}
return NULL;
}
void* consumer(void* arg) {
int count = 0;
while (count < 2 * MAX_COUNT) {
sem_wait(&full); // 等待缓冲区有已用空间
pthread_mutex_lock(&mutex); // 上锁
char c = buffer[read_pos];
printf("Consumer reads %c from position %d\n", c, read_pos);
read_pos = (read_pos + 1) % BUFFER_SIZE;
pthread_mutex_unlock(&mutex); // 解锁
sem_post(&empty); // 缓冲区剩余空间 + 1
count++;
usleep(rand() % 1000000); // 随机睡眠一段时间
}
return NULL;
}
int main() {
srand(time(0));
sem_init(&empty, 0, BUFFER_SIZE);
sem_init(&full, 0, 0);
pthread_mutex_init(&mutex, NULL);
pthread_t producer_uppercase_tid, producer_lowercase_tid, consumer1_tid, consumer2_tid, consumer3_tid;
pthread_create(&producer_uppercase_tid, NULL, producer_uppercase, NULL);
pthread_create(&producer_lowercase_tid, NULL, producer_lowercase, NULL);
pthread_create(&consumer1_tid, NULL, consumer, NULL);
pthread_create(&consumer2_tid, NULL, consumer, NULL);
pthread_create(&consumer3_tid, NULL, consumer, NULL);
pthread_join(producer_uppercase_tid, NULL);
pthread_join(producer_lowercase_tid, NULL);
pthread_join(consumer1_tid, NULL);
pthread_join(consumer2_tid, NULL);
pthread_join(consumer3_tid, NULL);
pthread_mutex_destroy(&mutex);
sem_destroy(&full);
sem_destroy(&empty);
return 0;
}
```
程序运行结果如下:
```
Producer(lowercase) writes b at position 0
Consumer reads b from position 0
Producer(lowercase) writes o at position 1
Consumer reads o from position 1
Producer(uppercase) writes G at position 2
Consumer reads G from position 2
Producer(lowercase) writes d at position 3
Consumer reads d from position 3
Producer(uppercase) writes X at position 4
Consumer reads X from position 4
Producer(lowercase) writes l at position 5
Consumer reads l from position 5
Producer(lowercase) writes x at position 6
Consumer reads x from position 6
Producer(uppercase) writes O at position 7
Consumer reads O from position 7
Producer(lowercase) writes p at position 8
Consumer reads p from position 8
Producer(lowercase) writes i at position 9
Consumer reads i from position 9
Producer(uppercase) writes J at position 0
Consumer reads J from position 0
Producer(lowercase) writes y at position 1
Consumer reads y from position 1
Producer(lowercase) writes n at position 2
Consumer reads n from position 2
Producer(uppercase) writes Q at position 3
Consumer reads Q from position 3
Producer(lowercase) writes c at position 4
Consumer reads c from position 4
Producer(uppercase) writes R at position 5
Consumer reads R from position 5
Producer(lowercase) writes i at position 6
Consumer reads i from position 6
Producer(lowercase) writes k at position 7
Consumer reads k from position 7
Producer(uppercase) writes Y at position 8
Consumer reads Y from position 8
Producer(lowercase) writes r at position 9
Consumer reads r from position 9
Producer(lowercase) writes v at position 0
Consumer reads v from position 0
Producer(uppercase) writes Q at position 1
Consumer reads Q from position 1
Producer(lowercase) writes l at position 2
Consumer reads l from position 2
Producer(uppercase) writes F at position 3
Consumer reads F from position 3
Producer(lowercase) writes a at position 4
Consumer reads a from position 4
Producer(lowercase) writes b at position 5
Consumer reads b from position 5
Producer(uppercase) writes G at position 6
Consumer reads G from position 6
Producer(lowercase) writes s at position 7
Consumer reads s from position 7
Producer(lowercase) writes f at position 8
Consumer reads f from position 8
Producer(uppercase) writes H at position 9
Consumer reads H from position 9
Producer(lowercase) writes h at position 0
Consumer reads h from position 0
Producer(lowercase) writes s at position 1
Consumer reads s from position 1
Producer(uppercase) writes Y at position 2
Consumer reads Y from position 2
Producer(lowercase) writes n at position 3
Consumer reads n from position 3
Producer(uppercase) writes U at position 4
Consumer reads U from position 4
Producer(lowercase) writes h at position 5
Consumer reads h from position 5
Producer(lowercase) writes t at position 6
Consumer reads t from position 6
Producer(uppercase) writes G at position 7
Consumer reads G from position 7
Producer(lowercase) writes l at position 8
Consumer reads l from position 8
Producer(lowercase) writes t at position 9
Consumer reads t from position 9
Producer(uppercase) writes H at position 0
Consumer reads H from position 0
Producer(lowercase) writes z at position 1
Consumer reads z from position 1
Producer(lowercase) writes s at position 2
Consumer reads s from position 2
Producer(uppercase) writes B at position 3
Consumer reads B from position 3
Producer(lowercase) writes g at position 4
Consumer reads g from position 4
Producer(lowercase) writes t at position 5
Consumer reads t from position 5
Producer(uppercase) writes T at position 6
Consumer reads T from position 6
Producer(lowercase) writes a at position 7
Consumer reads a from position 7
Producer(lowercase) writes f at position 8
Consumer reads f from position 8
Producer(uppercase) writes S at position 9
Consumer reads S from position 9
Producer(lowercase) writes r at position 0
Consumer reads r from position 0
Producer(lowercase) writes i at position 1
Consumer reads i from position 1
Producer(uppercase) writes J at position 2
Consumer reads J from position 2
Producer(lowercase) writes g at position 3
Consumer reads g from position 3
Producer(uppercase) writes O at position 4
Consumer reads O from position 4
Producer(lowercase) writes o at position 5
Consumer reads o from position 5
Producer(lowercase) writes s at position 6
Consumer reads s from position 6
Producer(uppercase) writes I at position 7
Consumer reads I from position 7
Producer(lowercase) writes g at position 8
Consumer reads g from position 8
Producer(lowercase) writes l at position 9
Consumer reads l from position 9
Producer(uppercase) writes U at position 0
Consumer reads U from position 0
Producer(lowercase) writes b at position 1
Consumer reads b from position 1
Producer(lowercase) writes z at position 2
Consumer reads z from position 2
Producer(uppercase) writes K at position 3
Consumer reads K from position 3
Producer(lowercase) writes d at position 4
Consumer reads d from position 4
Producer(lowercase) writes o at position 5
Consumer reads o from position 5
Producer(uppercase) writes J at position 6
Consumer reads J from position 6
Producer(lowercase) writes c at position 7
Consumer reads c from position 7
Producer(lowercase) writes r at position 8
Consumer reads r from position 8
Producer(uppercase) writes L at position 9
Consumer reads L from position 9
Producer(lowercase) writes w at position 0
Consumer reads w from position 0
Producer(lowercase) writes x at position 1
Consumer reads x from position 1
Producer(uppercase) writes Q at position 2
Consumer reads Q from position 2
Producer(lowercase) writes s at position 3
Consumer reads s from position 3
Producer(uppercase) writes I at position 4
Consumer reads I from position 4
Producer(lowercase) writes z at position 5
Consumer reads z from position 5
Producer(lowercase) writes p at position 6
Consumer reads p from position 6
Producer(uppercase) writes J at position 7
Consumer reads J from position 7
Producer(lowercase) writes f at position 8
Consumer reads f from position 8
Producer(lowercase) writes b at position 9
Consumer reads b from position 9
Producer(uppercase) writes K at position 0
Consumer reads K from position 0
Producer(lowercase) writes w at position 1
Consumer reads w from position 1
Producer(lowercase) writes r at position 2
Consumer reads r from position 2
Producer(uppercase) writes S at position 3
Consumer reads S from position 3
Producer(lowercase) writes y at position 4
Consumer reads y from position 4
Producer(lowercase) writes c at position 5
Consumer reads c from position 5
Producer(uppercase) writes T at position 6
Consumer reads T from position 6
Producer(lowercase) writes f at position 7
Consumer reads f from position 7
Producer(lowercase) writes k at position 8
Consumer reads k from position 8
Producer(uppercase) writes W at position 9
Consumer reads W from position 9
```
阅读全文