|
Home ► Lab Exercise ► Network Lab Exercise Programs► Demonstration to restart server by capturing SIGHUP signal▼
Demonstration to restart server by capturing SIGHUP signal: |
Algorithm Steps:
Step 1: start
Step 2: If fork returns zero than call the child function
Step 3: Initialize the socket and set its attributes assign any port number as desired.
Step 4: Call the sigaction function and check the return value.
Step 5: If it is not equal to zero display the message.
Step 6: repeate the process upto system becomes off.
C Program To Restart Server By Capturing SIGHUP signal
#include
#include
#include
#include
#include
#include
#include
#include
int isfirst;
char **command_args;
void sig_hangup(int signum)
{
if ( isfirst )
{
isfirst = 0;
fprintf(stderr, "Parent died\n");
}
else /* Restart! */
{
fprintf(stderr, "Restarting...\n");
/*** Kill all existing child processes ***/
execv(command_args[0], command_args);
fprintf(stderr, "Could not restart!!!\n");
abort();
}
}
void child(void)
{ struct sigaction act;
bzero(&act, sizeof(act));
act.sa_handler = sig_hangup;
act.sa_flags = SA_RESTART;
if ( sigaction(SIGHUP, &act, 0) != 0 )
perror("Can't capture SIGHUP");
for (;;)
{
fprintf(stderr, "[pid=%d] I'm still here\n", getpid());
sleep(1);
}
exit(0);
}
int main(int count, char *strings[])
{
isfirst = 1;
command_args = strings;
if ( fork() == 0 )
child();
sleep(1);
return 0;
}
SAMPLE INPUT OUTPUT:
$gcc restart-example -o restart.exe
$ [pid=2556] I'm still here
$ [pid=2556] I'm still here
$
$ [pid=2556] I'm still here
$ [pid=2556] I'm still here
$ [pid=2556] I'm still here
$ [pid=2556] I'm still here
$ [pid=2556] I'm still here
$ [pid=2556] I'm still here
|
|
⇓Student Projects⇓
⇑Student Projects⇑
|