[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Sleeping Barber

Posted on 1998-05-03
11
Medium Priority
?
3,186 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
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!

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops 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.

656 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