• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3192
  • Last Modified:

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.  
0
alpine
Asked:
alpine
1 Solution
 
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now