Programming This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
08-29-2004, 10:55 AM
|
#1
|
|
Member
Registered: Sep 2003
Location: Fort worth, TX
Distribution: Debian testing 64bit at home, EL5 32/64bit at work.
Posts: 187
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?
|
|
|
|
08-29-2004, 11:20 AM
|
#2
|
|
Member
Registered: Jul 2004
Location: Munich, Germany
Distribution: Sun Solaris 8, SuSE 9.0
Posts: 43
Rep:
|
Hi,
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.
Good luck
bruce
|
|
|
|
08-29-2004, 02:33 PM
|
#3
|
|
Member
Registered: Sep 2003
Location: Fort worth, TX
Distribution: Debian testing 64bit at home, EL5 32/64bit at work.
Posts: 187
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?
Any help would be great :-)
--Tony
|
|
|
|
08-30-2004, 02:18 AM
|
#4
|
|
Member
Registered: Jul 2004
Location: Munich, Germany
Distribution: Sun Solaris 8, SuSE 9.0
Posts: 43
Rep:
|
Hi,
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.
|
|
|
|
08-30-2004, 02:30 AM
|
#5
|
|
Member
Registered: Jul 2004
Location: Munich, Germany
Distribution: Sun Solaris 8, SuSE 9.0
Posts: 43
Rep:
|
BTW, if you're interested in the output that your find process generates it is more
convenient to use popen() instead of fork().
so long...
bruce
|
|
|
|
09-01-2004, 10:25 PM
|
#6
|
|
Member
Registered: Sep 2003
Location: Fort worth, TX
Distribution: Debian testing 64bit at home, EL5 32/64bit at work.
Posts: 187
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.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
Similar Threads
|
| Thread |
Thread Starter |
Forum |
Replies |
Last Post |
|
fork() in c++
|
deveraux83 |
Programming |
5 |
11-13-2004 03:12 PM |
|
more on fork()
|
feetyouwell |
Programming |
6 |
09-17-2004 11:18 AM |
|
about fork
|
eshwar_ind |
Programming |
5 |
02-11-2004 03:38 AM |
|
Fork again
|
Avatar33 |
Programming |
13 |
08-22-2003 01:41 PM |
|
Fork
|
Ztyx |
Linux - General |
1 |
08-31-2002 11:25 AM |
All times are GMT -5. The time now is 01:19 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|