3

i've made this in 10 mins, and spent 2 hours trying to figure out why it won't do anything on the terminal and have finally given up and need help. It would be really appreciated if anyone could help. Thanks.

#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;
}
jack sexton
  • 1,227
  • 1
  • 9
  • 28
  • 3
    Time to learn to use a debugger. – simonzack Oct 04 '14 at 04:32
  • There is no output at all? – Martin James Oct 04 '14 at 04:39
  • How do you communicate between the parent process and the child process? Using `fork()` creates process not thread. The variables and memory does not share between processes. – SSC Oct 04 '14 at 04:39
  • from man fork():`Under Linux, fork() is implemented using copy-on-write pages, so the only penalty that it incurs is the time and memory required to dupli‐cate the parent's page tables, and to create a unique task structure for the child.` Get used to spend 3 minutes on a careful reading of the documentation for the calls that use – Ivan Ivanovich Oct 04 '14 at 06:46
  • I'm sorry, I'm a noob at this, thank you for the help everyone and i will learn to use a debugger. I was writing in notepad++, not sure if thats available. – jack sexton Oct 04 '14 at 14:05

2 Answers2

2

The waiting room is filling too fast.

The following process does not pause and quickly fills stdout with endless ""Waiting room is full". Similar for if (barber==0) {

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

Should detect failed fork() the barber < 0/

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

Need to share memory. See https://stackoverflow.com/a/13274800/2410359

"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;
}
Community
  • 1
  • 1
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • it will not work, because each process will have its own variable and will not have any way to communicate – Ivan Ivanovich Oct 04 '14 at 06:40
  • 1
    @IvanIvanovich chux's answer does work as chux uses shared memory(via mmap) to communicate between the two processes. As [mmap man page](http://man7.org/linux/man-pages/man2/mmap.2.html) mentioned, and the link provided by chux in his answer. – SSC Oct 04 '14 at 10:35
0

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

in this block:

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

when the value of the variable waitingroomcust is less than zero, nothing else will never 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 more than 3, nothing else will never happen, cause Nothing can reduce this variable

if you want to use a different process, you need to made a way of communication them, it can be a fifo or signals.

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

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

Ivan Ivanovich
  • 880
  • 1
  • 6
  • 15