Write in ANSWER BOOKLET
You are to create an interprocess communication mechanism.
A program’s initial process, P0, creates and maintains a stack data structure. Many child processes are
created thereafter and they rely on the same stack data structure of the parent process P0.
Only the parent process P0 can modify the stack contents. Any child process can operate on the stack
by communicating with P0.
There are two types of child processes:
• Worker - performs operations with the stack if is is not full or not empty.
• Cleaner - performs operations with the stack when it is full or when it is empty. The cleaner aims to make the stack non-empty and non-full.
Cleaner processes cannot operate on the stack while Worker processes operate and vice versa.
The stack operations are as follows:
// places the integer x on top of the stack
// Otherwise , err is set to non -zero value if the stack is full
void push( struct stack *, int x, int *err );
// removes and returns the integer on top of the stack
// Otherwise , err is set to non -zero value if the stack is empty
int pop( struct stack *, int *err );
// returns the integer on top of the stack
// Otherwise , err is set to non -zero value if the stack is empty
int peek( struct stack *, int *err );
// returns non -zero value when empty
int is_empty( );
Describe interprocess communication necessary to keep the stack synchronised for N child processes, where child processes can be either Worker or Cleaner types.
You can describe your answer with pseudocode, or C code (most accurate). If your answer is not clear, marks can be deducted.
Useful functions for your reference:
Setting the signal handler
typedef void (*sighandler_t)(int);
sighandler_t signal(int signum, sighandler_t handler);
Sets the disposition of the signal signum to handler, which is either SIG_IGN, SIG_DFL, or the
address of a programmer defined function (a "signal handler").
signal() returns the previous value of the signal handler, or SIG_ERR on error.
Sending the signal
int raise(int sig);
Sends a signal to the calling process or thread. If the signal causes a handler to be called, raise() will return only after the signal handler has returned. raise() returns 0 on success, and nonzero for failure.
Setting up the pipe
int pipe(int pipefd[2]);
creates a pipe, a unidirectional data channel that can be used for interprocess communication.
The array pipefd is used to return two file descriptors referring to the ends of the pipe.
pipefd[0] refers to the read end of the pipe. pipefd[1] refers to the write end of the pipe.
On success, zero is returned. On error, -1 is returned.