Question

Linux & C code help with creating a child process and kills itself with SIGNAL. I...

Linux & C code help with creating a child process and kills itself with SIGNAL. I provided most of the code, I need some help to finish. Please provide output screen shot as well. Thank you!

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).
Your output should look like....

hello 0

hello 1

hello 2

hello 3

hello 4

[... etc .]

Alarm clock [may or may not appear]

Here is my code......

Use alarm to kill the process.

Hello world printed out every second for 5 seconds, then terminate.

MUST use system call alarm, not EXIT.

#include <unistd.h>

Unsigned int alarm

Sleep ()m

Takes 1 input. Make calling process wait seconds…

#include <unistd.h>

Main (..){

Int n;

N = user input; // user input = convert string 5 to # 5…

(How long hello will be printed out)

Int pid;

If ((pid = fork() < 0){ // error)

Else if (pid > 0){

//parent process

Only child prints hello, parent process does nothing

Wait();

Printf(“this is parent so wait \n”);

}

Else if (pid == 0){ //this is child

// print hello “time” or n times

//once per second

//kill process after n seconds

// must use alarm

Int count = 0;

Alarm(?); // give alarm a #

//alarm doesn’t ring, then print out while loop

While (1){

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include
#include
#include
#include
#include
#include
#include

pid_t pid;

void alarm_handler(int sig)
{
    printf("%s" , "Time limit exceeded");
    kill(pid, 9);

}

void main(int argc, char *argv[])
{

  
   int j,n,i;
   int status = 0;
   int count;


   srand(time(NULL));
   pid=fork();
   if (pid == -1){
      printf("Error in forking....\n");
      exit(0);
   }

   if (pid == 0)
   {
       count = 0;
       while (1) {
         printf("hello %d\n",count);
         count++;
         sleep(1);
       }
   }
   else{
     signal(SIGALRM, alarm_handler);   
     alarm(5);
     while( wait(NULL) > 0 );
   }
   exit(0);
}

Add a comment
Know the answer?
Add Answer to:
Linux & C code help with creating a child process and kills itself with SIGNAL. I...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write code that forks into two processes: a parent process, and a child process. Your code...

    Write code that forks into two processes: a parent process, and a child process. Your code will be called with command-line arguments consisting of negative integers. Do not worry about bad command-line arguments such as "xyz". Your code will not be tested in this way. The parent process will take the arguments to main(), convert them into ints by calling atoi(), and send those ints one at a time to the child process through a pipe (one call to write()...

  • Write code that forks into two processes: a parent process, and a child process. Same as...

    Write code that forks into two processes: a parent process, and a child process. Same as the Regular version, except that your code must also be able to handle negative integers input from the command-line. If I call your code this way:     a03 -3 5 -7 The parent process should print out: Assignment 3     sum = -5 The sums produced from the test input I use will be in the range [-128 .. 127] -------------------------------------------------------------------------------------------------------------------- // Numbers from...

  • Directions: use only the signal mechanism system calls don’t use ( wait() or pipe() )in this...

    Directions: use only the signal mechanism system calls don’t use ( wait() or pipe() )in this problem. You can still read/write from/to a file. You must use ( kill() and pause() ) system calls. rewrite code below to do kill and pause system calls #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/wait.h> // Function ptototypes int readX(); void writeX(int); int main() { int pid; // pid: used to keep track of the child process int x...

  • Write a “C” program that creates a child process that prints out its pid and new...

    Write a “C” program that creates a child process that prints out its pid and new line and then calls pause() system call. Once it returns from the pause system call the child program must exit with code 5. The parent program must wait for the child process and print out the pid of the child process and the exit status with the following format: “childpid=%d,exitstatus=%d\n”.

  • I have the following code....from the previous lab....the above needs to be added to what is...

    I have the following code....from the previous lab....the above needs to be added to what is already existing. ALSO MODIFY SEMAPHORES TO USE pthreads instead of the pipe constructs P() & V() #include <stdio.h> #include <string.h> #include <sys/types.h> #include <unistd.h> #include <sys/wait.h> #include <stdlib.h> #include <sys/stat.h> void printStat(char *filename); //Main int main(int argc, char *argv[]) { //Process Id (storing)    pid_t pid;    int j;    //printf("Welcome to Project Three\n”);    // For loop*/    for (j = 1; j...

  • modify the code for timer_test_02.c to allow the time delay between events to be pseudo- random...

    modify the code for timer_test_02.c to allow the time delay between events to be pseudo- random exponential, with a mean time between arrivals of 0.1 second. Change the limit in the time_stamps() function from 5 time-stamps to 10, so that the mean run-time will be about 10*0.1 = 1.0 seconds. Once this is working, you should be able to generate 10 events with a pseudo-random exponential arrival process. The code is: #include <stdio.h> #include <stdint.h> #include <time.h> #include <unistd.h> #include...

  • C programm , ´hello i need your help -Given the C program primes.c with the following main method: int main() { int num, res; char buffer[11]; bool finished = false; while (!finished)...

    C programm , ´hello i need your help -Given the C program primes.c with the following main method: int main() { int num, res; char buffer[11]; bool finished = false; while (!finished) { printf("Enter n > 0 or quit\n"); scanf("%10s", buffer); if (strcmp(buffer, "quit") == 0) { finished = true; } else { // Convert input to number and compute n-th prime num = atoi(buffer); if (num > 0) { res = nth_prime(num); printf("Prime #%d is %d\n", num, res); }...

  • Source code to modify: #include <stdio.h> #include <unistd.h> #include <sys/types.h> int main() { pid_t pid; /*fork...

    Source code to modify: #include <stdio.h> #include <unistd.h> #include <sys/types.h> int main() { pid_t pid; /*fork a child process*/ pid = fork(); if (pid<0){ /*error occured*/ fprintf(stderr,"Fork failed\n"); return 1; } else if (pid == 0) { /*child process */ printf("I am the child %d\n",pid); execlp("/bin/ls","ls",NULL); } else { /*parent process */ /*parent will wait for the child to complete*/ printf("I am the parent %d\n",pid); wait(NULL); printf("Child Complete\n"); } return 0; } 1. Write program codes for 3.21 a and...

  • C program To develop a C program to implement a process system call. PROBLEM You are...

    C program To develop a C program to implement a process system call. PROBLEM You are to use the Ubuntu operating system to write a C program that creates a process to determine the identification of the current user of your computer. I mean if joe is the login of the current user of your computer your solution should return joe. Your solution must also provide the pid of both the parent and the child processes. You may use the...

  • The following code is meant to capture the output of an executable in a file and...

    The following code is meant to capture the output of an executable in a file and then read in and print out the first 100 bytes of that file. Select all of the bugs in the code. Assume that all system calls succeed and that sizeof(char)-1. pid_t pid = fork(); if (pid == 0) { int in-open("file.txt", READONLY) dup2(STDOUT FILENO, in); execl("./something", "something", "hello!"); l else char buffer[100] int in = open("file.txt'. READ-ONLY): read(buffer, in, 100); The parent does not...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT