1:使用互斥锁或者信号量,实现一个简单的生产者消费者模型 一个线程每秒生产3个苹果,另一个线程每秒消费8个苹果
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#define BUFFER_SIZE 10
#define PRODUCE_RATE 3
#define CONSUME_RATE 8
int buffer[BUFFER_SIZE] = {0};
int head = 0;
int tail = 0;
int count = 0;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t full = PTHREAD_COND_INITIALIZER;
pthread_cond_t empty = PTHREAD_COND_INITIALIZER;
void *producer(void *arg) {
int i = 0;
while (1) {
pthread_mutex_lock(&mutex);
while (count == BUFFER_SIZE) {
pthread_cond_wait(&full, &mutex);
}
buffer[tail] = i++;
tail = (tail + 1) % BUFFER_SIZE;
count++;
printf("Produced apple %d. Current count: %d\n", buffer[tail - 1], count);
pthread_cond_signal(&empty);
pthread_mutex_unlock(&mutex);
usleep(1000000); // Sleep for 1 second
}
return NULL;
}
void *consumer(void *arg) {
int i = 0;
while (1) {
pthread_mutex_lock(&mutex);
while (count == 0) {
pthread_cond_wait(&empty, &mutex);
}
int apple = buffer[head];
head = (head + 1) % BUFFER_SIZE;
count--;
printf("Consumed apple %d. Current count: %d\n", apple, count);
pthread_cond_signal(&full);
pthread_mutex_unlock(&mutex);
usleep(100000 / CONSUME_RATE); // Adjusted for faster consumption rate
}
return NULL;
}
int main() {
pthread_t producer_tid, consumer_tid;
pthread_create(&producer_tid, NULL, producer, NULL);
pthread_create(&consumer_tid, NULL, consumer, NULL);
pthread_join(producer_tid, NULL);
pthread_join(consumer_tid, NULL);
return 0;
}
2:第二题: 有一个盘子,盘子里面最多放3个苹果,5个橘子 2个生产者线程,一个每秒放1个苹果,另一个每秒2个橘子 放了苹果就不能放橘子,放了橘子就不能放苹果 2个消费者线程,1号消费者线程每秒消费2个苹果,2号消费者线程,每秒消费3个橘子 解题思路: 苹果生产者和消费者之间,需要2组条件变量 橘子生产者和消费者之间,需要2组条件变量 苹果生产者和橘子生产者之间,需要一组互斥锁或者信号量
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#define BUFFER_SIZE_APPLE 3
#define BUFFER_SIZE_ORANGE 5
#define PRODUCE_RATE_APPLE 1
#define PRODUCE_RATE_ORANGE 2
#define CONSUME_RATE_APPLE 2
#define CONSUME_RATE_ORANGE 3
int apple_buffer[BUFFER_SIZE_APPLE] = {0};
int orange_buffer[BUFFER_SIZE_ORANGE] = {0};
int apple_head = 0;
int orange_head = 0;
int apple_tail = 0;
int orange_tail = 0;
int apple_count = 0;
int orange_count = 0;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t apple_full = PTHREAD_COND_INITIALIZER;
pthread_cond_t apple_empty = PTHREAD_COND_INITIALIZER;
pthread_cond_t orange_full = PTHREAD_COND_INITIALIZER;
pthread_cond_t orange_empty = PTHREAD_COND_INITIALIZER;
void *apple_producer(void *arg) {
int i = 0;
while (1) {
pthread_mutex_lock(&mutex);
while (apple_count == BUFFER_SIZE_APPLE) {
pthread_cond_wait(&apple_full, &mutex);
}
apple_buffer[apple_tail] = i++;
apple_tail = (apple_tail + 1) % BUFFER_SIZE_APPLE;
apple_count++;
printf("Produced apple %d. Current count: %d\n", apple_buffer[apple_tail - 1], apple_count);
pthread_cond_signal(&apple_empty);
pthread_mutex_unlock(&mutex);
usleep(1000000 / PRODUCE_RATE_APPLE); // Sleep for 0.5 seconds
}
return NULL;
}
void *orange_producer(void *arg) {
int i = 0;
while (1) {
pthread_mutex_lock(&mutex);
while (orange_count == BUFFER_SIZE_ORANGE) {
pthread_cond_wait(&orange_full, &mutex);
}
orange_buffer[orange_tail] = i++;
orange_tail = (orange_tail + 1) % BUFFER_SIZE_ORANGE;
orange_count++;
printf("Produced orange %d. Current count: %d\n", orange_buffer[orange_tail - 1], orange_count);
pthread_cond_signal(&orange_empty);
pthread_mutex_unlock(&mutex);
usleep(1000000 / PRODUCE_RATE_ORANGE); // Sleep for 0.5 seconds
}
return NULL;
}
void *apple_consumer(void *arg) {
while (1) {
pthread_mutex_lock(&mutex);
while (apple_count == 0) {
pthread_cond_wait(&apple_empty, &mutex);
}
int apple = apple_buffer[apple_head];
apple_head = (apple_head + 1) % BUFFER_SIZE_APPLE;
apple_count -= CONSUME_RATE_APPLE;
printf("Consumed apple %d. Current count: %d\n", apple, apple_count);
pthread_cond_signal(&apple_full);
pthread_mutex_unlock(&mutex);
usleep(1000000 / CONSUME_RATE_APPLE); // Sleep for 0.5 seconds
}
return NULL;
}
void *orange_consumer(void *arg) {
while (1) {
pthread_mutex_lock(&mutex);
while (orange_count == 0) {
pthread_cond_wait(&orange_empty, &mutex);
}
int orange = orange_buffer[orange_head];
orange_head = (orange_head + 1) % BUFFER_SIZE_ORANGE;
orange_count -= CONSUME_RATE_ORANGE;
printf("Consumed orange %d. Current count: %d\n", orange, orange_count);
pthread_cond_signal(&orange_full);
pthread_mutex_unlock(&mutex);
usleep(1000000 / CONSUME_RATE_ORANGE); // Sleep for 0.5 seconds
}
return NULL;
}
int main() {
pthread_t apple_producer_tid, orange_producer_tid, apple_consumer_tid, orange_consumer_tid;
pthread_create(&apple_producer_tid, NULL, apple_producer, NULL);
pthread_create(&orange_producer_tid, NULL, orange_producer, NULL);
pthread_create(&apple_consumer_tid, NULL, apple_consumer, NULL);
pthread_create(&orange_consumer_tid, NULL, orange_consumer, NULL);
pthread_join(apple_producer_tid, NULL);
pthread_join(orange_producer_tid, NULL);
pthread_join(apple_consumer_tid, NULL);
pthread_join(orange_consumer_tid, NULL);
return 0;
}
3:
#include <termios.h> #include <unistd.h> #include <sys/types.h> #include <stdio.h> #include <assert.h> int getch(){ int c=0; struct termios org_opts, new_opts; int res=0; res=tcgetattr(STDIN_FILENO, &org_opts); assert(res==0); new_opts = org_opts; new_opts.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL | ECHOPRT | ECHOKE | ICRNL); tcsetattr(STDIN_FILENO, TCSANOW, &new_opts); c=getchar(); res=tcsetattr(STDIN_FILENO, TCSANOW, &org_opts); assert(res==0); return c; 将上述函数封装成静态库,并测试,描述该函数的功能 }
#ifndef GETCH_H #define GETCH_H int getch(); #endif /* GETCH_H */