In: Computer Science
A process creates a child then it gives it 2 seconds to start in order for the child to register a handler for the SIGTSTP signal and stay alert for receiving the signal. Then the parent reads a command without options from the user and sends it to the child using a pipe then sends SIGTSTP signal to the child. The parent waits after that for the child to terminate then exits. The child handles the SIGTSTP signal by reading a command from the pipe and execute it using execlp system call. Write a C program to implement that logic using the signal handling system not sigaction.
Please go though code and output.
CODE:
#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
int main()
{
int fd[2] = {0}; // pipe fd
pipe(fd); // genrate fd
int pid =0;
pid = fork(); // create child process
if(pid == 0)
{
/* In child process */
char command[32] = {0};
read(fd[0], command,
sizeof(command)); // read from pipe
execlp(command, command, NULL); //
execute command
exit(0); // exit child
}
else
{
char command[32] = {0};
printf("Enter command without
opeion: \n");
scanf("%s",command); // get command
from user
write(fd[1], command,
strlen(command)); // write in pipe
sleep(2); // sleep for 2
seconds
kill(pid, SIGTSTP); // send SIGTSTP
signal to child
wait(0); // wait for child
complition
}
}
OUTPUT: