This program will create a child process, the child process will wait for an ALARM signal, when this signal arrives after 3 seconds, the f function will grab the parent process ID, and send a SIGINT signal to kill it, so the child will kill the parent after 3 seconds
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
void f(int sig)
{
kill(getppid(),SIGINT);
}
main()
{
int f=fork();
if(f==0)
{
signal(SIGALRM,f);
alarm(3);
}
else
{
pause();
}
}
I got this error:
test13.c: In function ‘main’:
test13.c:16:3: warning: passing argument 2 of ‘signal’ makes pointer from integer without a cast
/usr/include/signal.h:101:23: note: expected ‘__sighandler_t’ but argument is of type ‘int’
Stop stomping on f
with your variable.
You've reused the name f
to refer to different things depending upon scope.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With