Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using this code as a reference, complete the assignment above #include #include curr_time.h /* Declaration of currTime() */ #include tlpi_hdr.h #define SYNC_SIG SIGUSR1 /* Synchronization

image text in transcribed

Using this code as a reference, complete the assignment above

#include #include "curr_time.h" /* Declaration of currTime() */ #include "tlpi_hdr.h"

#define SYNC_SIG SIGUSR1 /* Synchronization signal */

static void /* Signal handler - does nothing but return */ handler(int sig) { }

int main(int argc, char *argv[]) { pid_t childPid; sigset_t blockMask, origMask, emptyMask; struct sigaction sa;

setbuf(stdout, NULL); /* Disable buffering of stdout */

sigemptyset(&blockMask); sigaddset(&blockMask, SYNC_SIG); /* Block signal */ if (sigprocmask(SIG_BLOCK, &blockMask, &origMask) == -1) errExit("sigprocmask");

sigemptyset(&sa.sa_mask); sa.sa_flags = SA_RESTART; sa.sa_handler = handler; if (sigaction(SYNC_SIG, &sa, NULL) == -1) errExit("sigaction");

switch (childPid = fork()) { case -1: errExit("fork");

case 0: /* Child */

/* Child does some required action here... */

printf("[%s %ld] Child started - doing some work ", currTime("%T"), (long) getpid()); sleep(2); /* Simulate time spent doing some work */

/* And then signals parent that it's done */

printf("[%s %ld] Child about to signal parent ", currTime("%T"), (long) getpid()); if (kill(getppid(), SYNC_SIG) == -1) errExit("kill");

/* Now child can do other things... */

_exit(EXIT_SUCCESS);

default: /* Parent */

/* Parent may do some work here, and then waits for child to complete the required action */

printf("[%s %ld] Parent about to wait for signal ", currTime("%T"), (long) getpid()); sigemptyset(&emptyMask); if (sigsuspend(&emptyMask) == -1 && errno != EINTR) errExit("sigsuspend"); printf("[%s %ld] Parent got signal ", currTime("%T"), (long) getpid());

/* If required, return signal mask to its original state */

if (sigprocmask(SIG_SETMASK, &origMask, NULL) == -1) errExit("sigprocmask");

/* Parent carries on to do other things... */

exit(EXIT_SUCCESS); } }

1. Write a program in which the child process waits for parent process to complete some actions. The detailed actions are as follows (refer the "procexec/fork_sig_sync.c" which has reverse roles) : 40 points FILE NAME: hw5 1.c Before fork: a. Use SIGUSR1 and add this signal to the signal mask b. Use "signal()" to call "signal-handler()" that prints out Here is the Signal Handler" Child process c. Suspend child process waiting for a signal from the parent d. Prints out "Child received a signal" e. Restore signal mask from the backup list f. xii. Parent process g. Print out "Parent started h. Sleep 3 seconds i. Print out "Parent about to signal child..." j. Send the SIGUSR1 to the child k. Exit 1. Write a program in which the child process waits for parent process to complete some actions. The detailed actions are as follows (refer the "procexec/fork_sig_sync.c" which has reverse roles) : 40 points FILE NAME: hw5 1.c Before fork: a. Use SIGUSR1 and add this signal to the signal mask b. Use "signal()" to call "signal-handler()" that prints out Here is the Signal Handler" Child process c. Suspend child process waiting for a signal from the parent d. Prints out "Child received a signal" e. Restore signal mask from the backup list f. xii. Parent process g. Print out "Parent started h. Sleep 3 seconds i. Print out "Parent about to signal child..." j. Send the SIGUSR1 to the child k. Exit

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Making Databases Work The Pragmatic Wisdom Of Michael Stonebraker

Authors: Michael L. Brodie

1st Edition

1947487167, 978-1947487161

More Books

Students also viewed these Databases questions

Question

What are transferred-out materials?

Answered: 1 week ago