Avatar of Ivanov_G
Ivanov_GFlag for Bulgaria

asked on 

Why do I get the following error ?


/mnt/d/__OS_Project/source/sems/src/sems.c: In function `Customer1':
/mnt/d/__OS_Project/source/sems/src/sems.c:115: error: syntax error before ';' token
/mnt/d/__OS_Project/source/sems/src/sems.c: At top level:
/mnt/d/__OS_Project/source/sems/src/sems.c:123: error: syntax error before "else"

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->mutex, NULL);
      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->mutex);
      sem->count--;
      pthread_mutex_unlock(&sem->mutex);
      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->mutex);
      sem->count++;
      pthread_mutex_unlock(&sem->mutex);
      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->mutex);
      value = &sem->count;
      pthread_mutex_unlock(&sem->mutex);
      return 0;
}

// destroy a semaphore using mutex
int sem_mutex_destroy (sem_mutex *sem)
{
      // destroy mutex
      pthread_mutex_destroy(&sem->mutex);
      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, 0);
      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);
}
System Programming

Avatar of undefined
Last Comment
sunnycoder
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

if this is C and not C++, the line mutex++ is wrong AFAIK.

try to put instead:
mutex = mutex + 1;
ASKER CERTIFIED SOLUTION
Avatar of sunnycoder
sunnycoder
Flag of India image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
System Programming
System Programming

Kernel and system programming is the process of creating the software necessary for a computer or device to function and operate other programs. Some operating systems (such as Microsoft Windows) are proprietary, but others, such as the various Linux distributions, are open source.

41K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo