?
Solved

Why do I get the following error ?

Posted on 2006-05-07
2
Medium Priority
?
300 Views
Last Modified: 2013-12-26

/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);
}
0
Comment
Question by:Ivanov_G
2 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 16625730
if this is C and not C++, the line mutex++ is wrong AFAIK.

try to put instead:
mutex = mutex + 1;
0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 500 total points
ID: 16625746
#define CHAIRS 3;

should read

#define CHAIRS 3

remove the extra ;
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question