1.思维导图
2.有一个隧道,长1000m,有一辆高铁,每秒100米;有一辆快车,每秒50米;要求模拟这两列火车通过隧道的场景。
1>程序代码:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/un.h>
typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;
#define LENGTH 1000 //隧道的长度
typedef struct
{
const char *name;
int speed;
int position;
}Train;
void *simulate_train(void *arg)
{
Train *train = (Train *)arg;
while (train->position < LENGTH)
{
train->position += train->speed; // 火车前进
if (train->position > LENGTH)
train->position = LENGTH; // 防止超出隧道长度
printf("%s正在 %d 米处\n", train->name, train->position);
sleep(1);
}
printf("%s已出隧道\n", train->name);
return NULL;
}
int main(int argc, const char *argv[])
{
pthread_t high_speed_thread, fast_train_thread;
// 初始化两列火车
Train high_speed_train = {"高铁", 100, 0};
Train fast_train = {"快车", 50, 0};
// 创建线程模拟火车运行
pthread_create(&high_speed_thread, NULL, simulate_train, &high_speed_train);
pthread_create(&fast_train_thread, NULL, simulate_train, &fast_train);
// 等待两列火车完成运行
pthread_join(high_speed_thread, NULL);
pthread_join(fast_train_thread, NULL);
printf("两列火车都已出隧道\n");
return 0;
}
2>运行效果:

3.有一条隧道,长1000m,有一辆高铁,每秒100米;有一辆快车,每秒50米;有一辆慢车,每秒25米;模拟它们通过隧道的场景,要求:高铁最先过隧道,快车其次,慢车最后。
1>程序代码:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/un.h>
typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;
#define LENGTH 1000 // 隧道长度
typedef struct
{
const char *name;
int speed;
int position;
int order; //火车顺序
} Train;
pthread_mutex_t mutex;
pthread_cond_t cond;
int current_order = 1; // 当前允许进入隧道的火车顺序
void *simulate_train(void *arg)
{
Train *train = (Train *)arg;
pthread_mutex_lock(&mutex);
while (train->order != current_order) {
pthread_cond_wait(&cond, &mutex);
}
pthread_mutex_unlock(&mutex);
while (train->position < LENGTH) {
train->position += train->speed; // 火车前进
if (train->position > LENGTH) {
train->position = LENGTH; // 防止超出隧道长度
}
printf("%s正在%d米处\n", train->name, train->position);
sleep(1); // 模拟每秒前进
}
printf("%s已出隧道\n", train->name);
pthread_mutex_lock(&mutex);
current_order++;
pthread_cond_broadcast(&cond);
pthread_mutex_unlock(&mutex);
return NULL;
}
int main(int argc, const char *argv[])
{
pthread_t id1,id2,id3;
// 初始化三列火车
Train high_speed_train = {"高铁", 100, 0, 1};
Train fast_train = {"快车", 50, 0, 2};
Train slow_train = {"慢车", 25, 0, 3};
// 创建线程模拟火车运行
pthread_create(&id1, NULL, simulate_train, &high_speed_train);
pthread_create(&id2, NULL, simulate_train, &fast_train);
pthread_create(&id3, NULL, simulate_train, &slow_train);
// 等待所有线程完成
pthread_join(id1, NULL);
pthread_join(id2, NULL);
pthread_join(id3, NULL);
printf("所有火车都已出隧道\n");
return 0;
}
2>运行效果:

4.使用条件变量实现一个生产消费模型(pv模型)。生产者线程:每秒生成2个苹果;消费者线程:每3秒消费5~9个苹果。
1>程序代码:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/un.h>
typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;
#define MAX_APPLES 100 //定义仓库最大容量
int apple_count = 0; //当前苹果的数量
//使用条件变量
pthread_mutex_t mutex;
pthread_cond_t cond;
//生产者线程
void* producer(void* arg)
{
while(1)
{
pthread_mutex_lock(&mutex);
//生产苹果
if(apple_count+2 <= MAX_APPLES)
{
apple_count += 2;
printf("生产者生产2个苹果,目前苹果总数为:%d\n",apple_count);
}
else
{
printf("仓库已满,生产者等待生产中...\n");
}
//唤醒消费者线程
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
sleep(1);
}
return NULL;
}
//消费者线程
void* consumer(void* arg)
{
while(1)
{
pthread_mutex_lock(&mutex);
int consumption = rand() % 5 + 5; //随机消费5~9个苹果
while(apple_count < consumption)
{
printf("消费者想要消费 %d 个苹果,但是只剩 %d 个苹果,等待...\n",consumption,apple_count);
pthread_cond_wait(&cond,&mutex); //等待生产者生产
}
//消费苹果
apple_count -= consumption;
printf("消费者消费 %d 个苹果,剩余苹果: %d个\n",consumption,apple_count);
pthread_mutex_unlock(&mutex);
sleep(3);
}
return NULL;
}
int main(int argc, const char *argv[])
{
pthread_t id_producer,id_consumer;
pthread_create(&id_producer,0,producer,0);
pthread_create(&id_consumer,0,consumer,0);
pthread_join(id_producer,NULL);
pthread_join(id_consumer,NULL);
return 0;
}
2>运行效果:
