• C

Sleeping Barber

We have to write a Sleeping Barber program that implements two functions, Barber() and Customer().  The main program will have to create several children using the fork system call.  Five of the children should be customers and two should be barbers.  Barbers should be numbered one and two, and customers one through five.  We should use the POSIX V system calls semget, semctl, and semop.

Each time a new barber is created, it calls the Barber() function and stays there until the program exits.  Similarly, each time a new customer is created, it calls the Customer() function and stays there until the program exits.  In order to show how the customer and barber processes communicate, make them print their interaction in the following form

    Barber 1 is servicing:
    Customer 1

    Barber 2 is servicing:
    Customer 2

    Instead of calling cut_hair() and get_haircut(), make the processes sleep for 2 seconds (use the sleep system call).  The parent process in your program should sleep for 15 seconds allowing for the barber customer interaction should happen.  Then it should send all children termination signal and exit.  
alpineAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RONSLOWCommented:
Thank you for telling us about your assignment problem .. congratulations .. I hope you enjoy writing it.

As you said "We have to write" .. not the experts at EE, but YOU.

Perhaps when you have made an attempt to do your homework, we could help you with whatever particular problems you may have?

Until then you are not likely to get any of the experts here to do your homework for you.

0
alpineAuthor Commented:
I tried to delete this message and withdraw my question, but I can't.
0
RONSLOWCommented:
You could leave it here so that experts here can help you with any problems you have with the code you write.  Just post any problems you are having with your assignment as a comment.

Or you can send a message to linda@experts-exchange.com and see if she can remove the question for you.  Or just put up a message asking to remove this question in the "Customer Service" Experts Exchage topic area.

0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

alexoCommented:
1) Go to http://www.experts-exchange.com/topics/bin/NewQForm?ta=4 and ask Linda in a REALLY NICE tone to remove question #10050904

2) RONSLOW was a bit harsh but I agree to the spirit of his message.  We'll be glad to HELP you with SPECIFIC questions, not to do your homework FOR YOU.
0
alpineAuthor Commented:
OK, I have attached the following code that I have now.  The problem is that I can't seem to get the downs and ups working correctly.  Any help on this point would be greatly appreciated.  Thanks.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <sys/sem.h>
#include <sys/ipc.h>
#include <signal.h>


#define BARBERS 2
#define CUSTOMERS 5
#define MUTEX 1
#define TRUE 1

void up(int s);
void down(int s);
void Barber(int id);
void Customer(int id);

int cus;
int bar;
int mut;


void main (void)
{
pid_t pid,pids[7];
int id,i;
key_t key;
union semun param;

cus=semget(IPC_PRIVATE,1,0777|IPC_CREAT);
bar=semget(IPC_PRIVATE,1,0777|IPC_CREAT);
mut=semget(IPC_PRIVATE,1,0777|IPC_CREAT);


param.val=0;
semctl(bar,BARBERS,SETVAL,param);
semctl(cus,CUSTOMERS,SETVAL,param);
param.val=1;
semctl(mut,MUTEX,SETVAL,param);

 for(i=0; i<2; i++){
   pid=fork();
   pids[i] = pid;
   Barber(i);
 }/* for */

 for(i=0; i<5; i++){
   pid=fork();
   pids[i+2]=pid;
   Customer(i);
 } /* for */


sleep(15);

 for(i=0; i<7; i++){
   kill(pids[i], SIGTERM);
 } /* for */


}/*main*/

void Barber(int id)
{

 while (TRUE){
   down(cus); /*go to sleep if # of customers is 0 */
   down(mut); /* acquire access to 'waiting' */
   printf("Barber %d is servicing:\n", id);
   up(bar); /* one barber is now ready to cut hair*/
   up(mut); /* release waiting */
   sleep(2); /* outside critical region */

 } /* while TRUE */

} /* void Barber */

void Customer (int id)
{

   down(mut); /* Enter critical region */
  while(TRUE){
   printf("Customer %d\n\n", id);
   up(cus); /* wake up barber if necessary */
   up(mut); /* release access to waiting */
   down(bar); /* go to sleep if # of free barbers is 0 */
   sleep(2); /* be seated and be serviced */

  }/* While true */
}/* void Customer */

void down(int s){
 struct sembuf sb;
 
 sb.sem_num = s;
 sb.sem_op = -1;
 sb.sem_flg = SEM_UNDO;
 if(semop(s, &sb, 1) == -1){
   printf("Down Error\n\n");
 }
 else{
   printf("Down has happened\n");
 }/* else */

}/* void down */

void up(int s){
 struct sembuf sb;
 
 sb.sem_num = s;
 sb.sem_op = 1;
 sb.sem_flg = SEM_UNDO;
 if(semop(s, &sb, 1) == -1){
   printf("Up Error\n");
 }/* if */
 else{
   printf("Up has happened\n");
 }/* else */

}/* void up */







0
RONSLOWCommented:
Now I'll be more than happy to help :-)

0
RONSLOWCommented:
In your Customer function you write

   down(mut); /* Enter critical region */

BEFORE the loop
and then call

   up(mut); /* release access to waiting */

repeatedly inside the loop

This doesn't seem write (cf Barber function)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
alpineAuthor Commented:
Thanks for the info.  I know that I am a real rookie at this, so please bear with me. I tried putting the

down(mut); /* Enter critical region */

in the loop but now the program just sits there and prints nothing.  Was there something else that I should have done that you inferred?

0
RONSLOWCommented:
Put a bit more printing code in the main and in the barber and customer routines (eg. print just before each line) and look at what is actually happening .. it may then be obvious from the output.
0
RONSLOWCommented:
I hope your acceptance means that your problem is solved now.

0
psoibelCommented:
how do i use
#include <unistd.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <sys/sem.h>
#include <sys/ipc.h>
#include <signal.h>
in visual c++ 6.0, or can I?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.