Solved

Sleeping Barber

Posted on 1998-05-03
11
3,166 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
ID: 1256903
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
ID: 1256904
I tried to delete this message and withdraw my question, but I can't.
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1256905
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
Portable, direct connect server access

The ATEN CV211 connects a laptop directly to any server allowing you instant access to perform data maintenance and local operations, for quick troubleshooting, updating, service and repair.

 
LVL 11

Expert Comment

by:alexo
ID: 1256906
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
ID: 1256907
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
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1256908
Now I'll be more than happy to help :-)

0
 
LVL 10

Accepted Solution

by:
RONSLOW earned 100 total points
ID: 1256909
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
ID: 1256910
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
ID: 1256911
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
ID: 1256912
I hope your acceptance means that your problem is solved now.

0
 

Expert Comment

by:psoibel
ID: 12480842
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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
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 conditional statements in the C programming language.

829 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