UNIX processes using C/C++
  • 🍴The fork system call
  • ⌚The wait system call
  • 🆔Process ID
  • 🔌Signals in UNIX
    • 🗄️Signal Handlers
  • 🥃Executing system commands
  • 📁Sharing information between processes
    • 🕚Bidirectional pipe
  • 🖥️Applications
    • 💻Console Application 1
    • 💻Console Application 2
  • ⚽Named pipe FIFO
Powered by GitBook
On this page
  1. Applications

Console Application 2

Now we shall make a program which gives the user a hint every 5 secs until the user inputs something or terminates the program (whichever comes first)

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.

#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

valgrind ./qs --leak-check=full
PreviousConsole Application 1NextNamed pipe FIFO

Last updated 2 years ago

🖥️
💻
1KB
signals-log-2.txt
Check logs for debug info