Ivanov_G
asked on
Why do I get the following error ?
/mnt/d/__OS_Project/source
/mnt/d/__OS_Project/source
/mnt/d/__OS_Project/source
/mnt/d/__OS_Project/source
The same error occurs in Customer2 functions. Here is the source:
--------------------------
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define CHAIRS 3;
enum sem_mutex_state {LOCKED, UNLOCKED};
typedef struct sem_mutex {
unsigned int count;
pthread_mutex_t mutex;
enum sem_mutex_state state;
} sem_mutex;
// **************************
// Semaphore using Mutex implementation
// **************************
// initialize semaphore using mutex
int sem_mutex_init (sem_mutex *sem, int value)
{
// initialize the mutex and set initial count
pthread_mutex_init(&sem->m
sem->count = value;
sem->state = UNLOCKED;
return 0;
}
// lock a semaphore using mutex (decrement)
int sem_mutex_wait (sem_mutex *sem)
{
// lock -> decrement -> unlock
while (sem->state == LOCKED)
continue;
sem->state = LOCKED;
pthread_mutex_lock(&sem->m
sem->count--;
pthread_mutex_unlock(&sem-
sem->state = UNLOCKED;
return 0;
}
// unlock a semaphore using mutex (increment)
int sem_mutex_post (sem_mutex *sem)
{
// lock -> increment -> unlock
while (sem->state == LOCKED)
continue;
sem->state = LOCKED;
pthread_mutex_lock(&sem->m
sem->count++;
pthread_mutex_unlock(&sem-
sem->state = UNLOCKED;
return 0;
}
// get value from semaphore using mutex
int sem_mutex_getvalue (sem_mutex *sem, int *value)
{
// lock -> get value -> unlock
pthread_mutex_lock(&sem->m
value = &sem->count;
pthread_mutex_unlock(&sem-
return 0;
}
// destroy a semaphore using mutex
int sem_mutex_destroy (sem_mutex *sem)
{
// destroy mutex
pthread_mutex_destroy(&sem
return 0;
}
// **************************
// End of Semaphore using Mutex implementation
// **************************
// globals
sem_mutex mutex;
sem_mutex barber;
sem_mutex customers;
pthread_t id0, id1, id2;
int waiting = 0;
void service(void)
{
printf("The barber is cutting customer's hair\n");
sleep(100);
}
void get_service(int i)
{
sleep(100);
printf("Customer %d got his hair cut\n", i);
}
void* Barber (void* data)
{
while (1)
{
sem_mutex_post(&customers)
sem_mutex_post(&mutex);
waiting--;
sem_mutex_wait(&barber);
sem_mutex_wait(&mutex);
service();
}
}
void* Customer1 (void* data)
{
printf("Customer 1 enters the shop.\n");
sem_mutex_post(&mutex);
if (waiting < CHAIRS) {
waiting++;
printf("Customer 1 sits in the waiting room.\n");
sem_mutex_wait(&customers)
sem_mutex_wait(&mutex);
sem_mutex_post(&barber);
get_service(1);
}
else
{
printf("Customer 1 leaves the shop.\n");
sem_mutex_wait(&mutex);
}
pthread_exit(NULL);
}
void* Customer2 (void* data)
{
printf("Customer 2 enters the shop.\n");
sem_mutex_post(&mutex);
if (waiting < CHAIRS)
{
waiting++;
printf("Customer 2 sits in the waiting room.\n");
sem_mutex_wait(&customers)
sem_mutex_wait(&mutex);
sem_mutex_post(&barber);
get_service(2);
}
else
{
sem_mutex_wait(&mutex);
printf("Customer 2 leaves the shop.\n");
}
pthread_exit(NULL);
}
// main
int main(int argc, char *argv[])
{
sem_mutex_init(&mutex, 1);
sem_mutex_init(&barber, 0);
sem_mutex_init(&customers,
pthread_create(&id0, NULL, Barber, NULL);
pthread_create(&id1, NULL, Customer1, NULL);
pthread_create(&id2, NULL, Customer2, NULL);
pthread_join(id0, NULL);
pthread_join(id1, NULL);
pthread_join(id2, NULL);
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
try to put instead:
mutex = mutex + 1;