0% found this document useful (0 votes)
7 views2 pages

Barman

The document is a C program that simulates a bartender and a client interaction using process forking and signals. The client requests two glasses, while the bartender serves them after a delay, demonstrating inter-process communication. The program handles errors during forking and uses signals to synchronize the actions of the client and bartender.

Uploaded by

Antonio Zreiby
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

Barman

The document is a C program that simulates a bartender and a client interaction using process forking and signals. The client requests two glasses, while the bartender serves them after a delay, demonstrating inter-process communication. The program handles errors during forking and uses signals to synchronize the actions of the client and bartender.

Uploaded by

Antonio Zreiby
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

barman

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

void demander_verre(int n)
{
printf("Client:I want glass %d ! \n", n);
sleep(4);
kill(getppid(), SIGCONT);
}
void servir_verre(int n)
{
printf("Bartender:Here is glass %d ! \n", n);
}

void error(char *msg)


{
perror(msg);
exit(EXIT_FAILURE);
}
int main()
{
int pid, status;

pid = fork();
switch (pid)
{
case -1:
{
error("error while forking");
}
case 0:
{
demander_verre(1);
kill(getpid(), SIGSTOP);

demander_verre(2);
exit(0);
}
default:
{
kill(getpid(), SIGSTOP);

servir_verre(1);
sleep(4);
kill(pid, SIGCONT);
kill(getpid(), SIGSTOP);

servir_verre(2);
wait(&status);

exit(0);
}
}
return 0;
}

You might also like