Solved

Sleeping Barber

Posted on 1998-05-03
11
3,150 Views
Last Modified: 2009-12-16
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
Comment
Question by:alpine
11 Comments
 
LVL 10

Expert Comment

by:RONSLOW
Comment Utility
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
 

Author Comment

by:alpine
Comment Utility
I tried to delete this message and withdraw my question, but I can't.
0
 
LVL 10

Expert Comment

by:RONSLOW
Comment Utility
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
 
LVL 11

Expert Comment

by:alexo
Comment Utility
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
 

Author Comment

by:alpine
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 10

Expert Comment

by:RONSLOW
Comment Utility
Now I'll be more than happy to help :-)

0
 
LVL 10

Accepted Solution

by:
RONSLOW earned 100 total points
Comment Utility
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
 

Author Comment

by:alpine
Comment Utility
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
 
LVL 10

Expert Comment

by:RONSLOW
Comment Utility
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
 
LVL 10

Expert Comment

by:RONSLOW
Comment Utility
I hope your acceptance means that your problem is solved now.

0
 

Expert Comment

by:psoibel
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

763 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now