IOday6互斥和同步
1:思维导图
2:有一个隧道,长1000m,有一辆高铁,每秒100米,有一辆快车,每秒50m 要求模拟这两列火车通过隧道的场景
#include <stdio.h>
#include <string.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>
#include <unistd.h>
pthread_mutex_t mutex;
void* train(void* arg)
{
pthread_mutex_lock(&mutex);
printf("高铁进入隧道\n");
sleep(1);
printf("高铁通过隧道\n");
pthread_mutex_unlock(&mutex);
}
void* car(void* arg)
{
pthread_mutex_lock(&mutex);
printf("快车进入隧道\n");
sleep(2);
printf("快车通过隧道\n");
pthread_mutex_unlock(&mutex);
}
int main(int argc, const char *argv[])
{
pthread_mutex_init(&mutex,NULL);
pthread_t c,t;
pthread_create(&t, 0, train, 0);
pthread_create(&c, 0, car, 0);
pthread_detach(c);
pthread_detach(t);
while(1);
return 0;
}
输出结果:
3:有一个隧道,长1000m,有一辆高铁,每秒100米,有一辆快车,每秒50m,有一辆慢车每秒25m 要求模拟这两列火车通过隧道的场景,但是要求高铁最先过隧道,快车其次,慢车最后
#include <stdio.h>
#include <string.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>
#include <unistd.h>
pthread_mutex_t tmutex;
pthread_mutex_t cmutex;
pthread_mutex_t lcmutex;
void* train(void* arg)
{
while(1)
{
pthread_mutex_lock(&tmutex);
printf("高铁进入隧道\n");
sleep(1);
printf("高铁通过隧道\n");
pthread_mutex_unlock(&cmutex);
}
}
void* car(void* arg)
{
while(1)
{
pthread_mutex_lock(&cmutex);
printf("快车进入隧道\n");
sleep(1);
printf("快车通过隧道\n");
pthread_mutex_unlock(&lcmutex);
}
}
void* lcar(void* arg)
{
while(1)
{
pthread_mutex_lock(&lcmutex);
printf("慢车进入隧道\n");
sleep(1);
printf("慢车通过隧道\n");
pthread_mutex_unlock(&tmutex);
}
}
int main(int argc, const char *argv[])
{
pthread_mutex_init(&tmutex,NULL);
pthread_mutex_init(&cmutex,NULL);
pthread_mutex_init(&lcmutex,NULL);
pthread_mutex_lock(&cmutex);
pthread_mutex_lock(&lcmutex);
pthread_t c,t,lc;
pthread_create(&t, 0, train, 0);
pthread_create(&c, 0, car, 0);
pthread_create(&lc, 0, lcar, 0);
pthread_detach(c);
pthread_detach(t);
pthread_detach(lc);
while(1);
return 0;
}
输出结果:
4:使用条件变量实现一个生产者消费者模型(pv)模型 生产者线程:每秒生成2个苹果 消费者线程:没3秒消费 5~9个苹果 要求消费者在消费之前一定要有足够的苹果给消费
#include <stdio.h>
#include <string.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>
#include <unistd.h>
#define MAX_APPLES 10
pthread_mutex_t mutex;
pthread_cond_t cond;
int apples = 0;
int consum=0;
void* producer(void* arg)
{
while (1)
{
pthread_mutex_lock(&mutex);
while (apples >= consum)
{
pthread_cond_wait(&cond, &mutex);
}
apples += 2;
printf("生产者生产了2个苹果,当前苹果数量:%d\n", apples);
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
sleep(1);
}
return NULL;
}
void* consumer(void* arg)
{
while (1)
{
pthread_mutex_lock(&mutex);
while (apples < 5)
{
pthread_cond_wait(&cond, &mutex);
}
consume = 5 + rand() % 5;
apples -= consume;
printf("消费者消费了%d个苹果,当前苹果数量:%d\n", consume, apples);
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
sleep(3);
}
return NULL;
}
int main(int argc, const char *argv[])
{
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&cond, NULL);
pthread_t prod_thread, cons_thread;
pthread_create(&prod_thread, NULL, producer, NULL);
pthread_create(&cons_thread, NULL, consumer, NULL);
pthread_join(prod_thread, NULL);
pthread_join(cons_thread, NULL);
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
return 0;
}