Sleeping hairdresser algorithm - code not executed

I did it 10 minutes later and spent 2 hours trying to figure out why it wouldn't do anything on the terminal and finally gave up and needed help. It would be really nice if someone could help. Thank.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>

int main (int argc, char const *argv[])
{
    int waitingRoomCust = 0;
    srand(time(NULL));
    int barber = fork();
    printf("%d\n",barber);
    if (barber==0) {
        while(1) {
            if(waitingRoomCust > 0) {
                waitingRoomCust--;
                sleep((rand() % 12));
                printf("Customer has been given a haircut.");
            }
        }
    }
    if(barber!=0) {
        while(1) {
            if(waitingRoomCust <= 3) {
                waitingRoomCust++;
                printf("The waiting room has now %i customers.", waitingRoomCust);
            }
            else {
                printf("Waiting room is full, customer has left.");
            }
        }
    }
    return 0;
}

      

+3


source to share


2 answers


The waiting room is filling up too quickly.

The next process does not pause and quickly fills with an stdout

endless "Waiting room full". Similarly forif (barber==0) {

if(barber!=0) {
  while(1) {
     ...
  }
}

      

If a failure is detected fork()

barber < 0

/



// if(barber!=0) {
if (barber>0) {
  ...
}
if(barber<0) {
  printf("failed %i ", barber);
}

      

It is necessary to exchange memory. See fooobar.com/questions/151296 / ...

The "working" code follows with various debug prints.

#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

static int *waitingRoomCust;

int main(int argc, char const *argv[]) {
  waitingRoomCust = mmap(NULL, sizeof *waitingRoomCust, PROT_READ | PROT_WRITE,
  MAP_SHARED | MAP_ANONYMOUS, -1, 0);

  *waitingRoomCust = 01;

  //srand(time(NULL));
  int barber = fork();
  printf("%d\n", barber);
  fflush(stdout);
  if (barber == 0) {
    while (1) {
      if (*waitingRoomCust > 0) {
        (*waitingRoomCust)--;
        printf("Customer has been given a haircut.\n");
        fflush(stdout);
        sleep((rand() % 12));
      } else {
        printf("sleep %d\n", *waitingRoomCust);
        fflush(stdout);
        sleep(1);
      }

    }
  }

  if (barber > 0) {
    while (1) {
      sleep(7);
      if (*waitingRoomCust <= 3) {
        (*waitingRoomCust)++;
        printf("The waiting room has now %i customers.\n", *waitingRoomCust);
        fflush(stdout);
      } else {
        printf("Waiting room is full, customer has left.\n");
        fflush(stdout);
      }
    }
  }
  if (barber < 0) {
    printf("failed %i ", barber);
    fflush(stdout);
  }
  return 0;
}

      

+2


source


after the fork, each process has its own copy of the waitingRoomCust variable.

in this block:



        if(waitingRoomCust > 0) {
            waitingRoomCust--;
            sleep((rand() % 12));
            printf("Customer has been given a haircut.");
        }

      

when waitingroomcust is less than zero, nothing else will ever happen, cause Nothing can increase this variable

in this block:



        if(waitingRoomCust <= 3) {
            waitingRoomCust++;
            printf("The waiting room has now %i customers.", waitingRoomCust);
        }

      

when the value of the variable waitingroomcust is greater than 3, nothing else will ever happen, the reason nothing can decrease this variable

if you want to use a different process, you need to make a way to pass them, it can be fifo or signals.

if you want to use shared memory then you need threads, they are like processes but use the same memory.

if you need, I can describe each of the three methods in more detail

0


source







All Articles