This assignment will allow you to implement a programming solution using POSIX semaphores and shared memory segments.

You have to simulate the behavior of customers in a fast-food restaurant. Each of your customers will be simulated by a separate process forked by your main program.

Customers arriving at the restaurant will do a semwait operation on the semaphore representing the restaurant and release that semaphore by doing a semsignal operation on that semaphore when they are done.

Your restaurant should have a single FIFO queue that will be represented by a single semaphore.

Your Program:
The number of clerks helping customers in your restaurant should be read from the command line as in:

./a.out 3

It will never be greater than sixteen. All other parameters will be read from the redirected standard input. Each input line will describe one customer arriving at the restaurant and will contain one char array and two integers representing:

1. The name of the customer,
2. The number of seconds elapsed since the arrival of the previous customer (it will be equal to zero for the first customer arriving at); and
3. The number of seconds the customer will take to get processed by the clerk.

One possible set of input could be:
Alice 0 10
Bob 3 5
Carol 4 8
Dean 2 75

You can safely assume that customers' names will never contain spaces, tabs, or any other special characters.

You will notice that there is no direct way for a process to know whether it had to wait before getting help. Your program should thus keep a count of the processes that had to wait. It should store this count in a shared memory segment, so all your processes could access it. You should use a separate mutex semaphore to ensure mutual exclusion for all operations accessing that shared variables. Your program should print a descriptive message every time a customer:

1. Arrives at the restaurant;
2. Starts getting helped; and
3. Leaves the restaurant.

as in:

Carol arrives at the restaurant.
Bob is getting helped.
Alice arrives at the restaurant.

Before terminating, your program must display:
1. The total number of customers that got serviced;
2. The number of customers that did not have to wait; and
3. The number of customers that had to wait.

Solution PreviewSolution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

* File:   main.cpp
* Author:

#include <cstdlib>
#include <cstring>
#include <string>
#include <iostream>
#include <vector>
#include <cstdio>

#include <sys/types.h>   
#include <sys/ipc.h>   
#include <sys/shm.h> /* semaphore functions and structs.    */
#include <sys/sem.h> /* shared memory functions and structs. */
#include <unistd.h>
#include <sys/wait.h>
#include <stdbool.h>

using namespace std;

#define MAX_LEN    64 /* length of string */
#define SEM_ID      250 /* ID for the semaphore.               */

struct client {
    char name[MAX_LEN];
    int arriving; // number of second elapsed since the arrived of the previous client
    int serving; // number of second will be processed by the clerk
    bool waiting;

// golobal variable

char* shm_addr; // address of shared memory segment
int shm_id; // ID of the shared memory segment   
struct shmid_ds shm_desc;

void error(string msg) {
    cout << msg << "\n";

void getInput(vector<client> & clients);
int createSharedMemory(int memory_size);
void deallocateShareMemory();

void putClientToSharedMemory(client * c_arr, vector<client> & clients);

void sem_lock(int sem_set_id);
void sem_unlock(int sem_set_id);
void clientProcess(int sem_set_id, int * clerk_num, client * c_arr, int index);

int main(int argc, char** argv) {

    if (argc < 2) {
       return 0;

    int clerk_num = atoi(argv[1]);

    if (clerk_num <= 0 || clerk_num > 16) {
       return 0;

    vector<client> clients;

    int* clerk; // number of clerk in shared mem
    int * client_num;
    client * c_arr; // pointer to array of client in shared memory

    // allocate a shared memory segment with size of an integer,
    // the number of available clerk + size of another integer which
    // is the number of client
    // and client data
    int sem_set_id = createSharedMemory(sizeof (int) + sizeof (int)
            +clients.size() * sizeof (client) + 64);

    // point to the shared memory segment

    // clerk number
    clerk = (int*) shm_addr;
    *clerk = clerk_num;

    // client number
    client_num = clerk + 1;
    *client_num = clients.size();

    // client array
    c_arr = (client *) (clerk + 2);

    // put data to shared memory space
    putClientToSharedMemory(c_arr, clients);

    /// fork all required processes
    for (int i = 0; i < clients.size(); i++) {
       if (fork() == 0) {
            clientProcess(sem_set_id, clerk, c_arr, i);

By purchasing this solution you'll be able to access the following files:

for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Operating Systems Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Upload a file
Continue without uploading

We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats