UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » Threads
  • » this alarm program isn't giving desired results

#1 2007-03-18 09:10 PM

dilip
Member
Registered: 2007-03-17
Posts: 4

Re: this alarm program isn't giving desired results

#include <unistd.h>    /* standard unix functions, like alarm()          */
#include <signal.h>    /* signal name macros, and the signal() prototype */

char user[40];		/* buffer to read user name from the user */

/* define an alarm signal handler. */
void catch_alarm(int sig_num)
{
    printf("Operation timed out. Exiting...\n\n");
    exit(0);
}
main()
{
/* set a signal handler for ALRM signals */
signal(SIGALRM, catch_alarm);

/* prompt the user for input */
printf("Username: ");
fflush(stdout);
/* start a 30 seconds alarm */
alarm(30);
/* wait for user input */
gets(user);
/* remove the timer, now that we've got the user's input */
alarm(0);
}

can anyone tell me wat exactly went wrong in this program. so that i could correct it.iam not able to get the proper result atleast in my system. plzzz help me fnds!!!!!

Offline

#2 2007-03-18 09:40 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: this alarm program isn't giving desired results

Your code works fine for me. Though I've edited a bit.  (Always compile with warnings enabled. I use gcc with -Wall -W -Wno-unused -Wstrict-prototypes.)

#include <stdio.h>     /* for printf() */
#include <stdlib.h>    /* for exit() */
#include <unistd.h>    /* standard unix functions, like alarm()          */
#include <signal.h>    /* signal name macros, and the signal() prototype */

char user[40];		/* buffer to read user name from the user */

/* define an alarm signal handler. */
void catch_alarm(int sig_num)
{
    printf("\nOperation timed out. Exiting...\n");
    exit(0);
}

int main(void)
{
	/* set a signal handler for ALRM signals */
	signal(SIGALRM, catch_alarm);

	/* prompt the user for input */
	printf("Username: ");
	fflush(stdout);
	/* start a 3 seconds alarm */
	alarm(3);
	/* wait for user input. Avoid buffer overflow. */
	fgets(user, sizeof(user), stdin);
	/* remove the timer, now that we've got the user's input */
	alarm(0);
	return 0;
}

Offline

#3 2007-03-19 08:54 PM

dilip
Member
Registered: 2007-03-17
Posts: 4

Re: this alarm program isn't giving desired results

hey thanx!!!!
i got it correct, thanx i3839

Offline

#4 2007-03-22 08:33 PM

dilip
Member
Registered: 2007-03-17
Posts: 4

Re: this alarm program isn't giving desired results

hey guyzz in this program, whn at the output  if we mention some username it has to terminate
but it is not getting terminated instead it is saying tht "no such file or directory"
n so if we mention some time say in seconds n we dont mention any username at the output then its showing tht  operation is timed out....well this is fine
but not the username
can anyone correct it

Offline

#5 2007-03-22 09:38 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: this alarm program isn't giving desired results

Huh?  I don't understand what you're saying...  Are you working off some different
code than that posted above?  Because, I see no way either of those could produce
any "No such file or directory" message...

Offline

  • Index
  • » Threads
  • » this alarm program isn't giving desired results

Board footer

Powered by FluxBB