Perform other functions while untarring file using "system" command in a C program
Jerry Feldman
gaf at blu.org
Wed Jan 31 15:43:18 EST 2001
The system(2) function by defnition waits until the command completes.
As John mentioned, you could use the popen() function or you could do a
fork()/exec() yourself. Also note that the GNU tar command with the z
option forks a gzip(or gunzip) which then pipes the output file to tar.
As a quick example:
/* signal handler */
void sighand(int sig)
{
if (sig == SIGCHLD) {
finished = 1;
}
}
pid_t chld = fork();
finished = 0;
set up signal to catch the SIGCHLD signal;
if (chld == 0) {
exec(tar command);
return 0; /* or exit() */
} else if (chld == -1) {
perror("fork");
exit(-1);
} else { /* parent */
do {
/* calculate percent */
printf("%d %% Completed", percent);
} while(! finished);
}
/* done */
On 31 Jan 2001, at 13:04, John Abreau wrote:
> On Wed, 31 Jan 2001, zoqix wrote:
>
> > Hi all,
> > I'm using the "system" command to do an untarring of a file.tar.gz file
> > in my C program. However, I would like to display some status
> > information like "10 % Completed". I've written some code to calculate
> > the percentage. However, the code could only be runned after the
> > "system" command finished untarring my zipped file. e.g.
> >
> > system("tar zxf file.tar.gz");
> > do
> > {
> > printf("%d %% Completed", percent);
> > }while (!finished)
> >
> > Therefore, the printf will always print 100 % Completed. Is there any
> > way to let the unzipping continue at background and return to print the
> > completion status?
> >
>
> A look through the info files for GNU tar shows the following:
>
> `--checkpoint'
> This option directs `tar' to print periodic checkpoint
> messages as it reads through the archive. Its intended
> for when you want a visual indication that `tar' is still
> running, but don't want to see `--verbose' output.
>
> It doesn't give a percentage, and it's not immediately clear what the
> number it does give actually represents, but it's probably a good starting
> point.
>
> To use its output, you'd have to replace the "system" call with a call to
> popen that reads from the stdout of the "tar" process. You'd also want to
> change the buffering of data in the pipe so you can immediately read the
> output from tar at the newline, rather than waiting for a complete block
> to be buffered.
>
> --
> John Abreau / Executive Director, Boston Linux & Unix
> ICQ#28611923 / AIM abreauj / Email jabr at blu.org
>
> -
> Subcription/unsubscription/info requests: send e-mail with
> "subscribe", "unsubscribe", or "info" on the first line of the
> message body to discuss-request at blu.org (Subject line is ignored).
Jerry Feldman <gaf at blu.org>
Associate Director
Boston Linux and Unix user group
http://www.blu.org
-
Subcription/unsubscription/info requests: send e-mail with
"subscribe", "unsubscribe", or "info" on the first line of the
message body to discuss-request at blu.org (Subject line is ignored).
More information about the Discuss
mailing list