Compute this in C. 1. Write a program in which the child process waits for paren
ID: 3606762 • Letter: C
Question
Compute this in C. 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. Exit -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
Explanation / Answer
#define _POSIX_SOURCE
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
//Method to indicate child has received signal from parent
void measure_time(int sig)
{
printf("Child [%d] received a signal %d ", getpid(), sig);
}
int main(int argc, char *argv[])
{
int n_task = 4;
pid_t pid;
pid_t pid_array[n_task];
for (int i = 0; i < n_task; i++)
{
pid = fork();
if (pid < 0)
{
perror("fork");
exit(1);
}
if (pid == 0) //child
{
signal(SIGUSR1, measure_time); //measure_time is a function
printf(“Here is the signal handler ”);
sleep(5);
exit(0);
}
//parent
pid_array[i] = pid;
}
//This code is executed from parent only
printf(“Parent started… ”);
sleep(3);
printf(“Parent about to signal child… ”);
for (int i = 0; i < n_task; i++)
{
kill(pid_array[i], SIGUSR1);
}
for (int i = 0; i < n_task; i++)
{
wait(NULL);
}
return (0);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.