# Console Application 2

For this, we have to use something called event handlers. Event handlers are functions that is bound to an event and it runs whenever that event is fired within that process.

We bind the the event handler function to the event by passing the function pointer of that function to the object of a **sigaction** structure.

```c
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
//#define _XOPEN_SOURCE 700

int ANS = 0, SANS = 0;

// random shit no jutsu
int getRandom(int lower,int upper){
    srand(time(NULL));
    return (rand() % (upper - lower + 1)) + lower;
}

void showAnswer();
int generateQuestion();

void signal_handler(int signo){
    if (signo == SIGUSR1)
        printf("\nThis is very easy");
}

int main(int argc, char *argv[]){
    pid_t id = fork();
    if (id != 0){
        while(1){
            sleep(5);
            kill(id, SIGUSR1);
        }
    }else{
        struct sigaction sa = {0};
        sa.sa_flags = SA_RESTART;
        sa.sa_handler = &signal_handler;
        sigaction(SIGUSR1, &sa, NULL);
        
        ANS = generateQuestion();
        scanf("%d", &SANS);
        showAnswer();

        kill(getppid(), SIGINT);
    }
    return 0;
}

int generateQuestion(){
    int x = getRandom(15,20);
    int y = getRandom(5,10);
    int c = getRandom(0,2);
    char op[3] = {'+','-','*'};
    printf("Enter the answer of the following problem\n%d %c %d = ",x,op[c],y);
    switch (c){
        case 0: return x+y;
        case 1: return x-y;
        case 2: return x*y;
    }
}

void showAnswer(){
    printf("\nAnswer submitted = %d\n", SANS);
    printf("Correct answer = %d", ANS);
    printf("\n%s answer submitted within time !!!",(SANS == ANS)?"Correct":"Wrong");
}
```

## Outputs

```
Enter the answer of the following problem
18 + 8 = 22

Answer submitted = 22
Correct answer = 26
Wrong answer submitted within time !!!
```

```
Enter the answer of the following problem
16 - 6 = 10

Answer submitted = 10
Correct answer = 10
Correct answer submitted within time !!!
```

```
Enter the answer of the following problem
20 * 10 = 200

Answer submitted = 200
Correct answer = 200
Correct answer submitted within time !!!
```

```
Enter the answer of the following problem
16 - 6 = 
This is very easy
This is very easy
This is very easy
This is very easy
This is very easy
10
This is very easy
Answer submitted = 10
Correct answer = 10
Correct answer submitted within time !!!
```

```
Enter the answer of the following problem
17 * 7 = 
This is very easy
This is very easy
This is very easy
This is very easy
114

This is very easy
Answer submitted = 114
Correct answer = 119
Wrong answer submitted within time !!!
```

Compile and run

```
gcc question.c -o qs && ./qs
```

Memory leak check with&#x20;

```
valgrind ./qs --leak-check=full
```

{% file src="/files/HdkMM0ZonozS1D5LWmdF" %}
**Check logs for debug info**
{% endfile %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://octave-1.gitbook.io/c-unix/applications/console-application-2.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
