Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Do it in LINUX ONLY A Process creates a child process. Child process will print

ID: 3605378 • Letter: D

Question

Do it in LINUX ONLY

A Process creates a child process. Child process will print out "hello 0" "hello 1" ..... 1 line per second. After a certain number of seconds (user input), it kills itself WITH signal mechanism ONLY, don't use exit() on the child. You can use alarm() which may show the "Alarm clock". (Alarm clock may not appear depending on your environment. It is not required).

Output should look like

Thank You :)

hello 0 hello 1 hello 2 hello 3 hello 4 [... etc.] Alarm clock [may or may not appear ]

Explanation / Answer

Desired command with comments are given below:

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>

int main()
{
int pid;
int time = 1;
int timeDuration;
printf("Enter the time duration ");
scanf("%d", &timeDuration);
pid=fork(); //doing fork to get child
if(pid<0)
{
printf(" An error occured");
exit(1);
}
else if(pid==0) //in child
{
       while(time <=timeDuration)
       {
       printf("hello %d",time);
       sleep(1);
       time++;
       }  
      
       kill (getpid (), SIGUSR1);

}

}