0

Each time timer expires the flag will be raised, and a thread will be created to do some special task, and let the main task continue its task without any interruption (as I have shown in Expected Output:). But as you can see in the Actual Output, when the thread is created the main task is stopped, though the thread takes very large time (more than 20 sec) to complete. Since the main thread is in an infinite loop (though it is switching between functions), I don't think I should use pthread_join(). What is Happening, and what should I do to get the expected output?

int global_flag, thread_exist;
void main() {
    //background time, on timer expire, execute "timer_signal_handler" 
    create_timer(tim_value=60sec, tim_interval=60sec);
    int val = 0;
    while(1){
        val = random_integer_generator();
        function_fun(val);
    }
    return; 
}
void function_fun(int val) {
    if(global_flag == 1 && thread_exist == 0) {
        global_flag = 0;
        pthread_t pid_port_scan;
            if ((pthread_create (&pid_port_scan, NULL, portScanRoutine, NULL)) != 0) {
            perror("pthread_create: ");
            thread_exist = 0;
            exit(EXIT_FAILURE);
            }
            else thread_exist = 1;
    }
    printf("value %d\n", val);
    return;
}
void *portScanRoutine(void* ptr) {
    printf("started scanning...\n");
    printf("scanning in progress...\n");
    scan_for_ports_whick_takes_around_20_to_30_seconds;
    printf("done scanning...\n");
    thread_exist = 0;
    return;
}
void timer_signal_handler(int sigId) {
    printf("timer expied\n");
    global_flag = 1;//set to indicate
}

actual Output:
value x//x is random value
value x
value x
value x
value x
value x
timer expied
started scanning...
scanning in progress...
done scanning...
value x
value x
value x
value x
value x
timer expied
started scanning...
scanning in progress...
done scanning...
value x
value x
value x



expected output:
value x//x is random value
value x
value x
value x
value x
value x
timer expied
started scanning...
scanning in progress...
value x
value x
value x
value x
value x
value x
done scanning...
value x
value x
value x
reddi hari
  • 71
  • 8
  • `printf` is not signal safe – Ben Voigt Apr 13 '18 at 04:45
  • On Linux, `void main()` is unconditionally wrong. You can sort of get away with it on Windows; on Linux, it is just wrong. See [What should `main()` return in C and C++](https://stackoverflow.com/questions/204476/) for the details. – Jonathan Leffler Apr 13 '18 at 04:50
  • You have `create_timer(tim_value=60sec, tim_interval=60sec);` This is not valid C. You also have not declared functions before you use them — in the last millennium, you were allowed to be sloppy about that, but not in the current millennium (C90 allowed it; C99 and C11 do not. You should be coding in C11, not C90.) – Jonathan Leffler Apr 13 '18 at 04:53
  • Since `global_flag` is not set to `1` until the first time the timer expires, you'll never see any thread created by the `if` clause in `function_fun()` until the timer expires. The first thing the code does is then set `global_flag` back to 0, so everything goes quiet again until the time expires again. You never collect your dead threads with `pthread_join()` — maybe you should make them detached? You should consider reporting when a thread is busy. Since both the main thread and the child thread access `thread_exist`, you should have a mutex to protect it. – Jonathan Leffler Apr 13 '18 at 05:06
  • thank you for the response, and please do not consider the syntax errors and function prototypes, this is pseudo code to make you understand. And it is a small part of a larger project. Even the outputs is pseudo. – reddi hari Apr 13 '18 at 09:27
  • For timer to expire next time it takes 60 seconds (at least 10 min in real), so, by the time, the thread will complete its task and return (thread exits) and hence where is the point in using pthread_join. And even if the timer expires before thread exits, the variable thread_exist is 1 hence new thread not created until the old existing thread exits. But my problem is when the thread created, what makes the main thread to stop doing its task of printing the val. – reddi hari Apr 13 '18 at 09:52

0 Answers0