Question

In: Computer Science

using 2 semaphores and 1 mutex: (solve in simple c++ language for linux terminal and add...

using 2 semaphores and 1 mutex: (solve in simple c++ language for linux terminal and add comments please)

The barber shop has one barber (a thread), one barber chair, and n chairs for waiting customers (semaphore), if any, to sit on. If there are no customers (each customer is a thread) present, the barber sits down in the barber chair and falls asleep. When a customer arrives, he has to wake up the sleeping barber. If additional customers arrive while the barber is cutting a customer's hair, they either sit down (if there are empty chairs) or leave the shop (if all chairs are full). The problem is to program the barber and the customers without getting into race conditions.

Solutions

Expert Solution

Solution:

Sleeping Barber Problem :

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<pthread.h>
#include<errno.h>
#include<sys/ipc.h>
#include<semaphore.h>

#define N 5

time_t end_time;
sem_t mutex,customers,barbers;    //Three semaphors
int count=0;       // number of customers waiting for haircuts

void barber(void *arg);
void customer(void *arg);

int main(int argc,char *argv[])
{
        pthread_t id1,id2;
        int status=0;
        end_time=time(NULL)+20;       //Barber Shop Hours is 20 seconds

        //Semaphore initialization
        sem_init(&mutex,0,1);
        sem_init(&customers,0,0);
        sem_init(&barbers,0,1);

        //Barber thread initialization
        status=pthread_create(&id1,NULL,(void *)barber,NULL);
        if(status!=0)
                perror("create barbers is failure!\n");
        //Customer thread initialization
        status=pthread_create(&id2,NULL,(void *)customer,NULL);
        if(status!=0)
                perror("create customers is failure!\n");

        //Customer_thread first blocked
        pthread_join(id2,NULL);
        pthread_join(id1,NULL);

        exit(0);
}

void barber(void *arg)    //Barber Process
{
        while(time(NULL)<end_time || count>0)
        {
                sem_wait(&customers);           
                sem_wait(&mutex);
                count--;
                cout<<"Barber:cut hair,count is:\n"<<count;
                sem_post(&mutex);
                sem_post(&barbers);
                sleep(3);       
        }
}

void customer(void *arg)     //Customers Process
{
        while(time(NULL)<end_time)
        {
                sem_wait(&mutex);
                if(count<N)
                {
                        count++;
                        cout<<"Customer:add count,count is:\n"<<count;
                        sem_post(&mutex);
                        sem_post(&customer);
                        sem_wait(&barbers);
                }
                else
                        
                        //If the number is full of customers,just put the mutex lock let go
                        sem_post(&mutex);
                sleep(1);
        }
}

Related Solutions

Task 2 [10 pts] Implementing Synchronization of Chat Client Threads Using Semaphores In c language In...
Task 2 [10 pts] Implementing Synchronization of Chat Client Threads Using Semaphores In c language In this task, a character buffer should be created (e.g., char buf[500]) in the server which is shared among threads that are created to handle communication with clients. Specifically, when the server receives a message (a string) from a client, the server should (1) store the message in the buffer, (2) broadcast the message to all connected clients, and (3) clear the buffer. Make sure...
This program is a simple version of the linux shell using execvp by forking using C...
This program is a simple version of the linux shell using execvp by forking using C Currently this program of the shell assumes user enters no spaces before and no extra spaces between strings. Using exec, an executable binary file (eg: a.out) can be converted into a process. An example of using exec is implementing a shell program or a command interpreter. A shell program takes user commands and executes them. int execvp(const char *file, char *const argv[]); Same as...
3. Add mutex locks to tprog2.c to achieve synchronization, and screenshot the output tprog2.c #include <stdio.h>...
3. Add mutex locks to tprog2.c to achieve synchronization, and screenshot the output tprog2.c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <pthread.h> void* print_i(void *ptr) { printf("1: I am \n"); sleep(1); printf("in i\n"); } void* print_j(void *ptr) { printf("2: I am \n"); sleep(1); printf("in j\n"); } int main() { pthread_t t1,t2; int rc1 = pthread_create(&t1, NULL, print_i, NULL); int rc2 = pthread_create(&t2, NULL, print_j, NULL); exit(0); }
Please solve this question in C++ language using recursion. Q (1) Write functions for each of...
Please solve this question in C++ language using recursion. Q (1) Write functions for each of the following problems. Each problem should be solved by writing a recursive function. Your final program should not have any loops in it. Write a function that uses recursion to raise a number to a power. The function should take two arguments, the number to be raised to the power (floating point) and the power (a non-negative int).                                                                                       (10 Points) Write a boolean...
Using Virtualbox in Debian, write a simple program (a single .cpp file) in Linux shell C++...
Using Virtualbox in Debian, write a simple program (a single .cpp file) in Linux shell C++ Rules: -Use fork(), exec(), wait(), and exit() _______________________________________________________________________________________________________________________________________________ -A line of input represents a token group. -Each token group will result in the shell forking a new process and then executing the process. e.g. cat –n myfile.txt // a token group -Every token group must begin with a word that is called the command(see example above). The words immediately following a command are calledarguments(e.g....
Using Virtualbox in Debian, write a simple program (a single .cpp file) in Linux shell C++...
Using Virtualbox in Debian, write a simple program (a single .cpp file) in Linux shell C++ Rules: -Use fork(), exec(), wait(), and exit() _______________________________________________________________________________________________________________________________________________ -A line of input represents a token group. -Each token group will result in the shell forking a new process and then executing the process. e.g. cat –n myfile.txt // a token group -Every token group must begin with a word that is called the command(see example above). The words immediately following a command are calledarguments(e.g....
Design a simple calculator program using C++ which is able to: 1. ADD two decimal numbers...
Design a simple calculator program using C++ which is able to: 1. ADD two decimal numbers 2. MULTIPLY two decimal numbers. The following features must be incorporated in your program. 1. Must have an interface for the user to be able to either select the ADD option or MULTIPLY option or to EXIT the program. NOTE: If the user makes a wrong selection, a display must be shown to inform the user and the user must be given a choice...
Using C Language Write a program segment that computes 1 + 2 + 3 + ......
Using C Language Write a program segment that computes 1 + 2 + 3 + ... + ( n - 1) + n , where n is a data value. Follow the loop body with an if statement that compares this value to (n * (n + 1)) / 2 and displays a message that indicates whether the values are the same or different. Please give me code to just copy and paste
Lab 1 Write a program in the C/C++ programming language to input and add two fractions...
Lab 1 Write a program in the C/C++ programming language to input and add two fractions each represented as a numerator and denominator. Do not use classes or structures. Print your result (which is also represented as a numerator/denominator) to standard out. If you get done early, try to simplify your result with the least common denominator. The following equation can be used to add fractions: a/b + c/d = (a*d + b*c)/(b*d) Example: 1/2 + 1/4 = ( 1(4)...
PROGRAM LANGUAGE IN C NOT C# or C++ KEEP IT SIMPLE EVEN IF IT IS REDUNDANT...
PROGRAM LANGUAGE IN C NOT C# or C++ KEEP IT SIMPLE EVEN IF IT IS REDUNDANT PLEASE. Problem: Driving Function driving(): Write a function driving(), that updates the odometer and fuel gauge of a car. The function will take in a reference to the variables storing the odometer and fuel gauge readings, along with a double representing the miles per gallon (mpg) the car gets and the number of miles the driver intends to go. The function should update the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT