Siva
Siva

Reputation: 1

Signals Using Alarm() in Linux C

  1. When there is timeout the program should move to second question and if time out it should say " your time is over ".

  2. Currently my program runs for the first question but not going to second question.

I have tried many time but it doesn't work.

Int main(){
    int time1
    char q1[20]
    printf("\n")
    printf("Enter the malaysia capital name : ");

    alarm(30);
    scanf("%[^\n]",q1);
    
    if (time != SIGALRM)
    {
        alarm(0);
        printf("The Malaysia capital is : '%s'\n",q1);
        printf("\n");
    }

    int time2
    char q1[20]
    printf("\n")
    printf("Enter the malaysian tallest building : ");

    alarm(30);
    scanf("%s",q2);

    if (time2 != SIGALRM)
    { 
        alarm(0);
        printf("Enter the malaysian tallest building is  : '%s'\n",q2);
        printf("\n");
    }
}

Expected :- After time out should move to second question Actual :- Not moving to second question

Upvotes: 0

Views: 466

Answers (1)

bta
bta

Reputation: 45087

The alarm() function causes SIGALRM to be raised, but you have to use the signal() function first to register a handler for this signal. Your program never registers a handler so there's nothing to run when the signal is raised.

Upvotes: 1

Related Questions