0

I'm having this weird issue right now, basically I'm trying to execute two functions in parallel.

The small code snippet that has this issue has this purpose: The child function listens to some events as long as the parent function/process has not finished its own task, in case of a new event happening in the child process/function the child process should terminate the whole program.

Here is the example code snippet:

#include<stdio.h>
#include<stdlib.h>

void send();

int main(void)
{
    send();
}

void send()
{

    if( fork() ) { /*child process */

            keep_checking_status(); /*listens to audio related events, and terminate the whole program when some                specific kind of event occurs */
    }
    else {
        /* Parent Process */
        /* The parent process executes different other functions, lines of code here */
        /* code lines */
        func1();
        func2();
        func3();
        func4(); 
        /* More lines of code */

        /* And when no event occurs in the child which means the program is still running and the parent process almost executed all the code it was supposed to do, so finally exit(0); */      
        exit(0); 
    }

}    

Now the problem is that instead of executing the parent only once, the parent process goes into infinite condition, and doesn't execute all the code to reach to the last line which is func4() or exit(0); and terminate peacefully.

Again i want the child process to listen to some specific audio events that i have taken care of, and the parent process should only execute the lines of code inside it and exit.

Also there is no while loop in the parent process , it is just a code that executes and computes some other things, and that is why it feel very strange.

I did looked into some examples of fork() being used differently , i tried them but still same issue.

Edit: (SOLVED): Ok after renaming the function void send(); to void send11(); , it worked now as I wanted. It seems like the libraries that I'm using i.e libcurl etc have some function like send() or they have included the linux system all send() which was creating this issue for me. My bad. If anyone can answer why this behavior of never terminating , please post your detail answer, because usually the compiler report the conflicting of names but in my case it didn't reported any errors instead the program went into infinity.

0

2 Answers 2

3

The parent process runs forever because:

1) Your comments are backwards. The parent calls keep_checking_status, not the child. The fork function returns zero to the child, so if( fork() ) is true in the parent (where a non-zero value is returned).

2) The child doesn't stop it. The exit function, as the documentation says, terminates the calling process. So the parent will stay in keep_checking_status forever.

The fix is probably to fix the logic so that the parent and child roles are reversed and save the child's PID in the parent so you can kill it when you're done.

Sign up to request clarification or add additional context in comments.

5 Comments

I did this way too i.e 'pid_t child; if( (child=fork()==0 ) { keep_checking_status() } else { /* parent proc */ } ' but still the same issue.
I thin you meant if( (child = fork()) == 0 ). Why do you make things difficult for yourself though? Why not: pid_t child = fork(); if (pid == 0) ... -- that's so easy to understand. It's like you're trying to make your code hard to understand.
Edited the question, please check back. :)
The process never terminates because it's an endless loop. You need to have the parent kill the child when it's done with it. That's why you save the pid.
Sorry I didn't thought it would be more robust if i could reverse the logic as you have said, because it was working for me i used other unnecessary methods to terminate the "child" that i thought previously as to be the "parent",I had 2 issues i guess. Thank you.
1

Your problem is because you have the parent process and child process mixed up, causing the parent process to run forever and not stop.

Quote from the man page of fork:

On success, the PID of the child process is returned in the parent, and 0 is returned in the child. On failure, -1 is returned in the parent, no child process is created, and errno is set appropriately.

So your 0 is the parent, which in your case is running the keep_checking_status() function.

You just need to reverse your logic a bit and you'll be fine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.