#c #semaphore
#c #семафор
Вопрос:
Я пытаюсь решить проблему с ванной для мужчин, используя pthreads и семафоры. Я установил количество остановок равным 3, как указано в male и female_multiplexes . Моя проблема в том, что когда я добавляю delay() между «Женщина входит и женщина выходит» или «Мужчина входит и мужчина выходит», произойдет голодание. Я хочу, чтобы программа одновременно обрабатывала 3 человека одного пола. После этого ванная комната должна сменить пол и принять противоположный пол. Таким образом, код должен выполняться по схеме 3-3-3-3 с чередованием полов. Пожалуйста, помогите мне в этом. Спасибо!
#define _REENTRANT
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <pthread.h>
#include <semaphore.h>
void *male(void *);
void *female(void *);
sem_t empty; // controls access to the bathroom
sem_t male_mutex; // mutex for male_counter
sem_t male_multiplex; // limits # of men in the bathroom
int male_counter = 0; // # of men in bathroom or waiting
sem_t female_mutex; // mutex for female_counter
sem_t female_multiplex; // limits # of women in the bathroom
int female_counter = 0; // # of women in bathroom or waiting
void delay(void)
{
int i;
int delaytime;
delaytime = random();
for (i = 0; i<delaytime; i );
}
void *male(void *param){
sem_wait(amp;male_mutex);
male_counter ;
if(male_counter == 1){
sem_wait(amp;empty); // make this a male bathroom or wait
}
sem_post(amp;male_mutex);
sem_wait(amp;male_multiplex); // limit # of people in the bathroom
printf("man in!n");
delay();
printf("man out!n");
sem_post(amp;male_multiplex); // let the next one in
sem_wait(amp;male_mutex);
male_counter--;
if (male_counter == 0){
sem_post(amp;empty); // may become female bathroom now
}
sem_post(amp;male_mutex);
}
void *female(void *param){
sem_wait(amp;female_mutex);
female_counter ;
if(female_counter == 1){
sem_wait(amp;empty); // make this a female bathroom or wait
}
sem_post(amp;female_mutex);
sem_wait(amp;female_multiplex); // limit # of people in the bathroom
printf("woman in!n");
delay();
printf("woman out!n");
sem_post(amp;female_multiplex); // let the next one in
sem_wait(amp;female_mutex);
female_counter--;
if (female_counter == 0){
sem_post(amp;empty); // may become male bathroom now
}
sem_post(amp;female_mutex);
}
int main(void){
int i;
int x;
int t;
srand(time(NULL));
int a[100]={};
printf("enter: ");
scanf("%d",amp;t);
for(i=0;i<t;i ){
a[i] = rand()%2;
}
for(i=0;i<t;i ){
printf("%d", a[i]);
}
printf("n");
sem_init(amp;empty, 0, 1);
sem_init(amp;male_mutex, 0, 1);
sem_init(amp;male_multiplex, 0, 3);
sem_init(amp;female_mutex, 0, 1);
sem_init(amp;female_multiplex, 0, 3);
pthread_t *tid;
tid = malloc(80*sizeof(pthread_t));
for(i=0;i<t;i ){
if(a[i] == 0){
pthread_create(amp;tid[i],NULL,female,NULL);
}
else{
pthread_create(amp;tid[i],NULL,male,NULL);
}
}
for(i=0;i<t;i ){
pthread_join(tid[i],NULL);
}
return(0);
}
Комментарии:
1. Разве вы не публиковали идентичный вопрос пару часов назад?
2. Я не вижу заслуживающей доверия попытки заставить ванную комнату переключаться между мужчинами и женщинами после трех человек одного пола в коде.