LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 08-29-2004, 10:55 AM   #1
tonyfreeman
Member
 
Registered: Sep 2003
Location: Fort worth, TX
Distribution: Debian testing 64bit at home, EL5 32/64bit at work.
Posts: 196

Rep: Reputation: 30
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?
 
Old 08-29-2004, 11:20 AM   #2
bruce ford
Member
 
Registered: Jul 2004
Location: Munich, Germany
Distribution: Sun Solaris 8, SuSE 9.0
Posts: 43

Rep: Reputation: 15
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
 
Old 08-29-2004, 02:33 PM   #3
tonyfreeman
Member
 
Registered: Sep 2003
Location: Fort worth, TX
Distribution: Debian testing 64bit at home, EL5 32/64bit at work.
Posts: 196

Original Poster
Rep: Reputation: 30
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
 
Old 08-30-2004, 02:18 AM   #4
bruce ford
Member
 
Registered: Jul 2004
Location: Munich, Germany
Distribution: Sun Solaris 8, SuSE 9.0
Posts: 43

Rep: Reputation: 15
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.
 
Old 08-30-2004, 02:30 AM   #5
bruce ford
Member
 
Registered: Jul 2004
Location: Munich, Germany
Distribution: Sun Solaris 8, SuSE 9.0
Posts: 43

Rep: Reputation: 15
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
 
Old 09-01-2004, 10:25 PM   #6
tonyfreeman
Member
 
Registered: Sep 2003
Location: Fort worth, TX
Distribution: Debian testing 64bit at home, EL5 32/64bit at work.
Posts: 196

Original Poster
Rep: Reputation: 30
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:50 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration