使用<信号量>实现<线程互斥>——(解析:用信号量实现两个线程互斥输出1-10数字)
巧妙运用< 信号量 >控制 <两个线程>之间<谁先执行>
巧妙运用<信号量>实现<控制n线程>之间<按什么顺序轮转>
要求:
解析:
fruitOnPlate,fruitType,#define APPLE 1 #define ORANGE 2
,表示父母端,放入苹果/橘子流程图示意:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#define APPLE 1
#define ORANGE 2
//表示放入水果
int fruitOnPlate=0;
int fruitType=0;
//设置信号量
sem_t plate, appleReady, orangeReady;
void *father(void *arg) {
while (1) {
sem_wait(&plate);
fruitOnPlate = APPLE; //放入苹果
fruitType=APPLE;
printf("Father put an apple.\n");
sem_post(&appleReady);
sleep(rand() % 10); //睡眠随机时间
}
}
void *mother(void *arg) {
while (1) {
sem_wait(&plate);
fruitOnPlate = ORANGE; //放入橘子
fruitType=ORANGE;
printf("Mother put an orange.\n");
sem_post(&orangeReady);
sleep(rand() % 10); //睡眠随机时间
}
}
void *son(void *arg) {
while (1) {
sem_wait(&orangeReady); // 等待橘子
if (fruitType == ORANGE) {
printf("Son ate an orange.\n");
sem_post(&plate);
}
}
}
void *daugther(void *arg) {
while (1) {
sem_wait(&appleReady); // 等待苹果
if (fruitType == APPLE) {
printf("Daugther ate an orange.\n");
sem_post(&plate);
}
}
}
int main() {
pthread_t f_thread,m_thread,son_thread,dau_thread;
sem_init(&plate, 0, 1);
sem_init(&appleReady, 0, 0);
sem_init(&orangeReady, 0, 0);
pthread_create(&f_thread, NULL, father, NULL);
pthread_create(&m_thread, NULL, mother, NULL);
pthread_create(&son_thread, NULL, son, NULL);
pthread_create(&dau_thread, NULL, daughter, NULL);
pthread_join(f_thread, NULL);
pthread_join(m_thread, NULL);
pthread_join(son_thread, NULL);
pthread_join(dau_thread, NULL);
sem_destroy(&plate);
sem_destroy(&appleReady);
sem_destroy(&orangeReady);
return 0;
}
要求:
拓展要求:
解析:
流程示意图:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#define APPLE 1
#define ORANGE 2
//表示放入水果
int fruitOnPlate=0;
int fruitType=0;
//设置turn:0表示父亲优先放,1表示母亲优先放
turn=0;
//设置信号量
sem_t plate, appleReady, orangeReady;
void *father(void *arg) {
while (1) {
if(turn ==0){
sem_wait(&plate);
fruitOnPlate = APPLE; //放入苹果
fruitType=APPLE;
printf("Father put an apple.\n");
sem_post(&appleReady);
sleep(rand() % 10); //睡眠随机时间
}
else{
sleep(1);
continue;
}
turn=1;//切换到母亲
}
}
void *mother(void *arg) {
while (1) {
if(turn ==0){
sem_wait(&plate);
fruitOnPlate = ORANGE; //放入橘子
fruitType=ORANGE;
printf("Mother put an orange.\n");
sem_post(&orangeReady);
sleep(rand() % 10); //睡眠随机时间
}
else{
sleep(1);
continue;
}
turn=0;//切换到父亲
}
}
void *son(void *arg) {
while (1) {
sem_wait(&orangeReady); // 等待橘子
if (fruitType == ORANGE) {
printf("Son ate an orange.\n");
sem_post(&plate);
}
}
}
void *daugther(void *arg) {
while (1) {
sem_wait(&appleReady); // 等待苹果
if (fruitType == APPLE) {
printf("Daugther ate an orange.\n");
sem_post(&plate);
}
}
}
int main() {
pthread_t f_thread,m_thread,son_thread,dau_thread;
sem_init(&plate, 0, 1);
sem_init(&appleReady, 0, 0);
sem_init(&orangeReady, 0, 0);
pthread_create(&f_thread, NULL, father, NULL);
pthread_create(&m_thread, NULL, mother, NULL);
pthread_create(&son_thread, NULL, son, NULL);
pthread_create(&dau_thread, NULL, daughter, NULL);
pthread_join(f_thread, NULL);
pthread_join(m_thread, NULL);
pthread_join(son_thread, NULL);
pthread_join(dau_thread, NULL);
sem_destroy(&plate);
sem_destroy(&appleReady);
sem_destroy(&orangeReady);
return 0;
}