Results 1 to 10 of 14
I am trying to run my function in the background but can't seem to do it.
I can run my app in background like this ;
./myapp &
but I ...
- 01-14-2010 #1Just Joined!
- Join Date
- Jan 2010
- Posts
- 9
Running function in the background in C
I am trying to run my function in the background but can't seem to do it.
I can run my app in background like this ;
./myapp &
but I want to run it in the background when a specific event occurs.
main() {
int a=0;
scanf(" %d ", &a);
if(a==5)
printf("a is 5 \n");
else
run in background??
}
I mean when a is not 5 I want my app to exit from console control but run in bnackground.
Any help would be appriciated.
- 01-14-2010 #2
sounds like you need fork()
fork
- 01-14-2010 #3Just Joined!
- Join Date
- Jan 2010
- Posts
- 9
well, not really.
actually the exact thing I want to do is ;
main() {
int a=0;
scanf(" %d ", &a);
if(a==5)
printf("a is 5 \n");
else
*continue the program flow in background
}
fork will create a child process and main will wait for fork to complete.
this is not I want, I want the app to continue in the background when it reaches *, ready to accept input from the same console.
- 01-14-2010 #4
you need to create and use a thread, I think for c/c++ on linux you would want to use pthread library
- 01-14-2010 #5
- 01-15-2010 #6
... and the fork will complete as soon as child process is created, it doesn't wait till the child process is finished so I think something like
should do it, or maybe I just understood wrong.Code:if(fork()==0) *continue program flow else return 0;
EDIT: Wait... so you want the app exit from the consoles control but still be able to take input from console?
- 01-15-2010 #7Just Joined!
- Join Date
- Jan 2010
- Posts
- 9
no I do not want it to get input from console ;
main() {
int a=0;
scanf(" %d ", &a);
if(a==5)
printf("a is 5 \n");
else
*continue the program flow in background
}
when the program flow comes to *, it will go background but not end the application.
after * there is no user interaction, code will continue working but exit from console like I started the application like ./myapp &
I do not think fork is the solution here becasue if I fork it I will be able to run the child in the background but parent will wait for child to complete so parent will still run normally.
if I couldn't express my goal clearly, let me try to clarify it better ;
user@linuxbox#./myapp
5
a is 5
user@linuxbox#./myapp
4
user@linuxbox# << program running
exactly like this but program will be working in the background, still has a pid and is a main process.
- 01-15-2010 #8
Ok, if I still got it wrong I realise I'm getting pretty annoying for everyone so this will be my last post in this thread
...but lets see...
Ok, so this where we disagree. My opinion is that the parent will NOT wait for the child to complete.
Let's see following code:
Now if I run this and input lets say 4 for example the output will be:Code:#include <stdio.h> #include <unistd.h> int main(void) { int a=0; scanf("%d", &a); if(a==5) printf("a is 5 \n"); else { if(fork()==0) while(1); //keep running forever else { puts("parent finishing..."); return 0; } } return 0; }
djap@djap-laptop:~/koodi$ ./a.out
4
parent finishing...
djap@djap-laptop:~/koodi$ <<the process is still running
- 01-15-2010 #9
djap is correct. fork()'s behaviour is explicitly to return to the parent as soon as the child process is created. From the fork man page:
fork() returns at the same time from both the parent and child.Code:RETURN VALUE 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.
If you wanted the parent to wait for the child, you must explicitly use one of the wait() family of functions. It is very common to see a construct like the following:
In your case, you may simply fork() and then exit the parent, and the child will continue in the background.Code:pid_t childpid = fork(); if(childpid < 0) { perror("fork()"); return 1; } else if(childpid == 0) { // child // do your processing // ... exit(0); } else { // parent // wait for child waitpid(childpid, NULL, 0); } // at this point, the child has ended and the parent continuesDISTRO=Arch
Registered Linux User #388732
- 01-15-2010 #10Just Joined!
- Join Date
- Jan 2010
- Posts
- 7
I'm a little rusty with my linux programming but if i'm not mistaking once the app is in background it's tty isnt the same with the console's anymore so it cant accept anymore input from console. When it enters background the only way to communicate with it is through shared memory or pipes or signals or message queues. Correct me if i'm wrong...


Reply With Quote
