Code:
#include <stdio.h> /* For printing output */
#include <signal.h> /* for kill() */
#include <unistd.h> /* for getppid() and sleep() */
#include <sys/types.h> /* for pid_t variable type */
int main(int argc, char *argv[])
{
int i;
pid_t parent;
/* To make this program exit from the shell it was
* started from, we need to kill the parent
* process of the process that is running this
* program.
*
* Our parent process is the process (probably
* a shell) that started this program.
*/
/* get the process ID (PID) of our parent */
parent = getppid();
/* Countdown */
printf("\nLogging out in 5 seconds: ");
for (i = 5; i > 0; --i) {
fflush(stdout);
sleep(1);
printf("%d ", i);
}
fflush(stdout);
printf(" GO!\n");
sleep(1);
/* Use USR1 signal, because the shell ignores the
* QUIT and TERM. As KILL and STOP are to strong
* for this purpose, and QUIT or ABRT would cause
* a core dump, we best chose USR1, USR2 or HUP
*/
if (kill(parent, SIGHUP) < 0) {
/* If kill() returns -1, we were not able
* to kill the parent. So print an error
* message to stderr.
*/
perror(argv[0]); /* also mention program name */
return 1;
}
return 0;
}