user18827168
user18827168

Reputation:

use the alarm function

The program implements several alarms to allow a process to define a number of timers (using the alarm function). The problem is that when I compile I get a few warnings, how do I get rid of them? Also, I don't think the implementation is too good, I would like to receive feedback on this as well

warnings:

main.c:16:55: warning: implicit declaration of function ‘getppid’ [-Wimplicit-function-declaration]
   16 |     printf("sending kill SIGALRM to the parent %d\n", getppid());
      |                                                       ^~~~~~~
main.c: In function ‘set_alarm’:
main.c:26:9: warning: implicit declaration of function ‘fork’ [-Wimplicit-function-declaration]
   26 |     if( fork() == 0 ){
      |         ^~~~
main.c:28:9: warning: implicit declaration of function ‘alarm’ [-Wimplicit-function-declaration]
   28 |         alarm(secs);
      |         ^~~~~
main.c:30:9: warning: implicit declaration of function ‘pause’ [-Wimplicit-function-declaration]
   30 |         pause();
      |         ^~~~~

code:

#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
/*
    The OPTIONAL parent SIGALRM handler. Does nothing useful apart from logging the SIGALRM received from the child.
*/
void parent_alrm_handler(int signo) {
    printf("Parent proc caught alrm signal\n");
    return;
}

/*
    The child SIGALRM handler.Replays the SIGALRM back to the parent when it receives it.
*/
void child_alrm_handler(int signo){
    printf("sending kill SIGALRM to the parent %d\n", getppid());
    kill(getppid(), SIGALRM);
}

/*
    Since each proc has single timer. I create child proceses when the req for new alarms is made.
*/
void set_alarm(int secs){
    signal(SIGALRM, parent_alrm_handler);
    if( fork() == 0 ){
        signal(SIGALRM, child_alrm_handler);
        alarm(secs);
        printf ("setting an alarm\n");
        pause();
        exit(0);        
    }   
}

int
main(void) {
    set_alarm(1); //alarm 1     
    set_alarm(4); //alarm 2     
    for(;;)
        pause();
    return 0;
}

Upvotes: 3

Views: 212

Answers (1)

Rillian Grant
Rillian Grant

Reputation: 98

You didn't include unistd.h in which those functions are defined.

Upvotes: 1

Related Questions