个
#include <pthread.h>
#include <semaphore.h>
#define BUFFER_SIZE 5
int buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
sem_t empty;
sem_t full;
pthread_mutex_t mutex;
// 生产者线程函数
void *producer(void *arg) {
int item;
for (int i = 0; i < 10; i++) {
item = i;
sem_wait(&empty); // 等待缓冲区有空位
pthread_mutex_lock(&mutex); // 进入临界区
buffer[in] = item;
printf("Produced %d at position %d\n", item, in);
in = (in + 1) % BUFFER_SIZE;
pthread_mutex_unlock(&mutex); // 离开临界区
sem_post(&full); // 通知缓冲区有新数据
}
pthread_exit(NULL);
}
// 消费者线程函数
void *consumer(void *arg) {
int item;
for (int i = 0; i < 10; i++) {
sem_wait(&full); // 等待缓冲区有数据
pthread_mutex_lock(&mutex); // 进入临界区
item = buffer[out];
printf("Consumed %d from position %d\n", item, out);
out = (out + 1) % BUFFER_SIZE;
pthread_mutex_unlock(&mutex); // 离开临界区
sem_post(&empty); // 通知缓冲区有空位
}
pthread_exit(NULL);
}
int main() {
pthread_t producer_thread, consumer_thread;
// 初始化信号量和互斥锁
sem_init(&empty, 0, BUFFER_SIZE);
sem_init(&full, 0, 0);
pthread_mutex_init(&mutex, NULL);
// 创建生产者和消费者线程
pthread_create(&producer_thread, NULL, producer, NULL);
pthread_create(&consumer_thread, NULL, consumer, NULL);
// 等待线程结束
pthread_join(producer_thread, NULL);
pthread_join(consumer_thread, NULL);
// 销毁信号量和互斥锁
sem_destroy(&empty);
sem_destroy(&full);
pthread_mutex_destroy(&mutex);
return 0;
}
第二个
#include <stdio.h>
#include <pthread.h>
#define BUFFER_SIZE 5
int buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
int count = 0;
pthread_mutex_t mutex;
pthread_cond_t not_full;
pthread_cond_t not_empty;
// 生产者线程函数
void *producer(void *arg) {
int item;
for (int i = 0; i < 10; i++) {
item = i;
pthread_mutex_lock(&mutex);
while (count == BUFFER_SIZE) {
pthread_cond_wait(&not_full, &mutex); // 等待缓冲区有空位
}
buffer[in] = item;
printf("Produced %d at position %d\n", item, in);
in = (in + 1) % BUFFER_SIZE;
count++;
pthread_cond_signal(&not_empty); // 通知缓冲区有新数据
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
// 消费者线程函数
void *consumer(void *arg) {
int item;
for (int i = 0; i < 10; i++) {
pthread_mutex_lock(&mutex);
while (count == 0) {
pthread_cond_wait(&not_empty, &mutex); // 等待缓冲区有数据
}
item = buffer[out];
printf("Consumed %d from position %d\n", item, out);
out = (out + 1) % BUFFER_SIZE;
count--;
pthread_cond_signal(&not_full); // 通知缓冲区有空位
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
int main() {
pthread_t producer_thread, consumer_thread;
// 初始化互斥锁和条件变量
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&not_full, NULL);
pthread_cond_init(&not_empty, NULL);
// 创建生产者和消费者线程
pthread_create(&producer_thread, NULL, producer, NULL);
pthread_create(&consumer_thread, NULL, consumer, NULL);
// 等待线程结束
pthread_join(producer_thread, NULL);
pthread_join(consumer_thread, NULL);
// 销毁互斥锁和条件变量
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&not_full);
pthread_cond_destroy(&not_empty);
return 0;
}
第三个
#include <stdio.h>
#include <pthread.h>
#define BUFFER_SIZE 5
int buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
pthread_mutex_t mutex;
// 生产者线程函数
void *producer(void *arg) {
int item;
for (int i = 0; i < 10; i++) {
item = i;
pthread_mutex_lock(&mutex);
while ((in + 1) % BUFFER_SIZE == out) {
// 缓冲区满,等待
}
buffer[in] = item;
printf("Produced %d at position %d\n", item, in);
in = (in + 1) % BUFFER_SIZE;
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
// 消费者线程函数
void *consumer(void *arg) {
int item;
for (int i = 0; i < 10; i++) {
pthread_mutex_lock(&mutex);
while (in == out) {
// 缓冲区空,等待
}
item = buffer[out];
printf("Consumed %d from position %d\n", item, out);
out = (out + 1) % BUFFER_SIZE;
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
int main() {
pthread_t producer_thread, consumer_thread;
// 初始化互斥锁
pthread_mutex_init(&mutex, NULL);
// 创建生产者和消费者线程
pthread_create(&producer_thread, NULL, producer, NULL);
pthread_create(&consumer_thread, NULL, consumer, NULL);
// 等待线程结束
pthread_join(producer_thread, NULL);
pthread_join(consumer_thread, NULL);
// 销毁互斥锁
pthread_mutex_destroy(&mutex);
return 0;
}
第四个
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#define N 5
sem_t chopsticks[N];
// 哲学家线程函数
void *philosopher(void *num) {
int id = *(int *)num;
int left = id;
int right = (id + 1) % N;
while (1) {
// 思考
printf("Philosopher %d is thinking.\n", id);
// 尝试拿起筷子
sem_wait(&chopsticks[left]);
printf("Philosopher %d picked up left chopstick.\n", id);
sem_wait(&chopsticks[right]);
printf("Philosopher %d picked up right chopstick.\n", id); // 吃饭
printf("Philosopher %d is eating.\n", id);
// 放下筷子
sem_post(&chopsticks[right]);
printf("Philosopher %d put down right chopstick.\n", id);
sem_post(&chopsticks[left]);
printf("Philosopher %d put down left chopstick.\n", id);
}
pthread_exit(NULL);
}
int main() {
pthread_t threads[N];
int ids[N];
// 初始化信号量
for (int i = 0; i < N; i++) {
sem_init(&chopsticks[i], 0, 1);
}
// 创建哲学家线程
for (int i = 0; i < N; i++) {
ids[i] = i;
pthread_create(&threads[i], NULL, philosopher, &ids[i]);
}
// 等待线程结束
for (int i = 0; i < N; i++) {
pthread_join(threads[i], NULL);
}
// 销毁信号量
for (int i = 0; i < N; i++) {
sem_destroy(&chopsticks[i]);
}
return 0;
}
第五个
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h> // 用于usleep减少CPU占用
#define N 5 // 哲学家数量
#define MAX_MEALS 3 // 每个哲学家最大进食次数
#define THINK_TIME 1 // 思考时间(秒)
#define EAT_TIME 1 // 进食时间(秒)
sem_t chopsticks[N];
int eat_count[N] = {0}; // 记录各哲学家进食次数
// 哲学家线程函数
void *philosopher(void *num) {
int id = *(int *)num;
int left = id;
int right = (id + 1) % N;
while (eat_count[id] < MAX_MEALS) {
// 思考阶段
printf("Philosopher %d is thinking.\n", id);
usleep(THINK_TIME * 1000000); // 转换为微秒
// 交替获取筷子策略
if (id % 2 == 0) {
sem_wait(&chopsticks[left]);
sem_wait(&chopsticks[right]);
} else {
sem_wait(&chopsticks[right]);
sem_wait(&chopsticks[left]);
}
// 进食阶段
printf("Philosopher %d is eating (meal %d).\n", id, eat_count[id] + 1);
usleep(EAT_TIME * 1000000);
eat_count[id]++;
// 释放筷子
sem_post(&chopsticks[left]);
sem_post(&chopsticks[right]);
printf("Philosopher %d released chopsticks.\n", id);
}
printf("Philosopher %d has finished eating.\n", id); pthread_exit(NULL);
}
int main() {
pthread_t threads[N];
int ids[N];
// 初始化信号量和进食计数器
for (int i = 0; i < N; i++) {
sem_init(&chopsticks[i], 0, 1);
eat_count[i] = 0;
}
// 创建哲学家线程
for (int i = 0; i < N; i++) {
ids[i] = i;
if (pthread_create(&threads[i], NULL, philosopher, &ids[i]) != 0) {
perror("Failed to create thread");
return 1;
}
}
// 等待所有线程结束
for (int i = 0; i < N; i++) {
pthread_join(threads[i], NULL);
}
// 销毁信号量
for (int i = 0; i < N; i++) {
sem_destroy(&chopsticks[i]);
}
printf("All philosophers have finished dining.\n");
return 0;
}
第六个
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#define N 5 // 哲学家数量
#define MAX_MEALS 3 // 每个哲学家最大进食次数
#define THINK_TIME 1 // 思考时间(秒)
#define EAT_TIME 1 // 进食时间(秒)
sem_t chopsticks[N];
sem_t room;
int meals_eaten[N] = {0}; // 记录各哲学家进食次数
// 哲学家线程函数
void *philosopher(void *num) {
int id = *(int *)num;
int left = id;
int right = (id + 1) % N;
while (meals_eaten[id] < MAX_MEALS) {
// 思考阶段
printf("Philosopher %d is thinking (meal %d/%d).\n", id, meals_eaten[id], MAX_MEALS);
usleep(THINK_TIME * 1000000);
// 进入房间(限制最多N-1人同时进餐)
sem_wait(&room);
printf("Philosopher %d entered the room.\n", id);
// 拿起筷子
sem_wait(&chopsticks[left]);
sem_wait(&chopsticks[right]);
printf("Philosopher %d picked up chopsticks %d and %d.\n",
id, left, right);
// 进食阶段
printf("Philosopher %d is eating (meal %d/%d).\n",
id, meals_eaten[id] + 1, MAX_MEALS);
usleep(EAT_TIME * 1000000);
meals_eaten[id]++;
// 放下筷子
sem_post(&chopsticks[left]);
sem_post(&chopsticks[right]);
printf("Philosopher %d released chopsticks %d and %d.\n",
id, left, right);
// 离开房间
printf("Philosopher left the room.\n");
printf("\n"); // 分开两个换行
sem_post(&room);
}
printf("Philosopher %d has finished dining (ate %d meals).\n", id, MAX_MEALS);
pthread_exit(NULL);
}
int main() {
pthread_t threads[N];
int ids[N];
// 初始化信号量
for (int i = 0; i < N; i++) {
sem_init(&chopsticks[i], 0, 1);
}
sem_init(&room, 0, N-1); // 允许最多N-1个哲学家同时进餐
// 创建哲学家线程
for (int i = 0; i < N; i++) {
ids[i] = i;
if (pthread_create(&threads[i], NULL, philosopher, &ids[i]) != 0) {
perror("Failed to create thread");
return 1;
}
}
// 等待所有线程结束
for (int i = 0; i < N; i++) {
pthread_join(threads[i], NULL);
}
// 销毁信号量
for (int i = 0; i < N; i++) {
sem_destroy(&chopsticks[i]);
}
sem_destroy(&room);
printf("All philosophers have completed dining.\n");
return 0;
}以上代码的思路和详细代码分析让初学者能看明白了解给我
最新发布