ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: Debian testing 64bit at home, EL5 32/64bit at work.
Posts: 196
Rep:
How to fork a program and tell when it's done?
I have a backup command that I need to run from a program that I'm writing using GTK/Glib. The problem is that I'm using the system() call ... which causes my GUI to feeze up until the backup command is finished.
How can I properly fork this command and have my gtk_progress_bar just run back and forth until the backup command is finished?
use fork(), then, in the child use execve/execp to run your program and set up a signal handler for SIGCHLD in the parent.
Very short answer, but that's it - consult the man pages of the respective command for details.
Distribution: Debian testing 64bit at home, EL5 32/64bit at work.
Posts: 196
Original Poster
Rep:
Thanks for the note ...
I was using fork with a command inside of it. Ex: fork("find /home/tony"). Here's the code I came up with today:
Code:
pid = fork();
if (pid == -1) {
g_error("Forking Failed");
exit(-1);
} else if (pid == 0) {
execlp("find", "find", "/home/tony/", NULL);
/* I guess there's something wrong if I get this far? */
g_error("I'm past execlp");
exit(-1);
}
I need to know two things: How can I tell when the example find command is finished and how do I make a gtk progress bar start when the find command starts and stop when the find command stops?
I'm not into GTK programming but I think your program has some sort of main loop.
Then you could poll for process termination as well as setting up a signal handler, e.g. like this
Code:
// your main loop
while( true ) {
...
// begin your posted code
pid = fork();
if (pid == -1) {
g_error("Forking Failed");
exit(-1);
} else if (pid == 0) {
execlp("find", "find", "/home/tony/", NULL);
/* I guess there's something wrong if I get this far? */
g_error("I'm past execlp");
exit(-1);
}
// end you posted code
// your parent process continues here, so start you progress bar at this point
// then poll for process termination:
int child_stat = 0;
int process_status = waitpid((pid_t)pid, &child_stat, WNOHANG);
if( process_status == 0 ) {
// process is still running
}
else if ( process_status == pid ) {
// process exited with exit status WEXITSTATUS( child_stat )
// stop your progress bar here
}
else {
// error, examine errno to tell what happened
}
...
// end of your main loop
}
You may want to add some if() conditions which I left out
Hope it helps,
bruce
Last edited by bruce ford; 08-30-2004 at 02:24 AM.
Distribution: Debian testing 64bit at home, EL5 32/64bit at work.
Posts: 196
Original Poster
Rep:
Answer
Thanks for the great reply! With your help I've finally worked through the answer to my original question: How to fork a program and tell when it's done. Here's the working code that I'm using.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <glib.h>
int main ()
{
pid_t pid;
gboolean test = TRUE;
gint child_stat = 0;
gint process_status;
g_print("\nSTART OF MAIN PROGRAM\n");
pid = fork();
if (pid == -1) {
g_error("Forking Failed");
exit(1);
return EXIT_FAILURE;
} else if (pid == 0) {
// start progress bar and run the command
g_print("Start progress bar\n");
if (execlp("find", "find", "/home/tony/glib/docs", NULL) < 0) {
g_error("*** Something wrong with the find command *** \n");
exit(1);
}
} else {
while (test) {
process_status= waitpid((pid_t)pid, &child_stat, WNOHANG);
if (process_status == 0) {
// process is still running
// increment the progress bar after doing some sort of timeout thingy
test = TRUE;
} else if (process_status == pid) {
// stop progress bar
g_print("Process stopped so stop progress bar\n");
test = FALSE;
} else {
g_error("Some sort of error that needs to be examined");
test = FALSE;
return EXIT_FAILURE;
}
}
}
g_print("\nEND OF MAIN PROGRAM\n");
return EXIT_SUCCESS;
}
Now I need to figure out how to make the progress bar run back and forth.
--Tony
Last edited by tonyfreeman; 09-01-2004 at 10:34 PM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.