Solved

making a system command hold until completion

Posted on 2003-11-24
5
305 Views
Last Modified: 2010-04-15
i've got a program that does a couple of ftp's.  it first ftp's a file that is selected by the user.  i am having a problem with this system call because when this file is large, it takes some time, one of the next things i do in the program is create a file and ftp it; so when the first file is large it doesn't complete the ftp before ftp is called again.  i have solved this in the VB version of the same program by using a execute and wait type function.  how do i do the same thing in c??  thanks

Kris
0
Comment
Question by:kris-mayco
  • 2
  • 2
5 Comments
 
LVL 16

Expert Comment

by:imladris
ID: 9813064
Given your reference to VB I will assume you are operating in a windows environment. Given that, you should be able to use a fork or spawn call. For a microsoft compiler you can use one of a number of different versions of it. The first argument is mode, for which you can specify P_WAIT so that the call won't return until the program specified has completed execution. The spawnl has the following definition for my compiler:

int spawnl(int mode,char *cmdname,char *arg0,char *arg1,...char *argn,NULL);

0
 
LVL 45

Expert Comment

by:Kdo
ID: 9813495
Hi kris-mayco,

If you want the program to run on both Windows and Linux/unix systems, you should probably fork() a child process and let it do the work.  The parent process can the sleep() until the child completes the ftp.

Be aware that FTP can be tricky to program because of the signon/password considerations.  The FTP command does not read from stdin so sending the authentication information might be a challenge.  :)

Here's a code snippet to fire of the FTP as a child process:

int ChildPID;
char Command [1024];

ChildPid = fork();
if (ChildPid == -1)
{
  printf ("Fork failed.  Cannot continue.\n");  /* Will probably never happen  */
  exit (0);
}

if (ChildPid)  /*  This is the parent task  */
  sleep (3600);  /*  Set the sleep time to 1 hour.  If the child doesn't finish by then, continue anyway  */
else
{
  sprintf (Command "FTP %s", TargetSystem);
  system (Command);  /*  Start the FTP
/*  Insert additional code for the child process  */
}

This works because when a child task finishes, it sends a signal to the parent task that interrupts the sleep().


Kent
0
 

Author Comment

by:kris-mayco
ID: 9818069
Kent,
   my program is very time sensitive, i.e. i can't afford to sleep for any amount of time after the ftp is complete.  i need a way to come back to the parent as soon as the child is done.  also, i don't neccassarily need this to run on a linux machine, although that would be a bonus, it doesn't look like my project will be running on anything other than windows machines, thanks

Kris
0
 
LVL 45

Accepted Solution

by:
Kdo earned 85 total points
ID: 9818410

When a child task completes, it sends a signal to the parent that interrupts the sleep().  I picked 3600 seconds as a "maximum" amount of time to sleep, but it may certainly not be appropriate for your application.  You can choose any value that works for you.

But the number is an upper limit.  The parent task will sleep until the timer expires OR the child task completes and AUTOMATICALLY sends a signal to the parent that it has completed.  You can trap these signals or (as in my example above) just let the O/S trap the signal and break the sleep().

Here's the same code modified to FTP the same file to 5 different systems:

int ChildPID;
char Command [1024];
char *SystemNames[] = {"System1", "System2", "System3", "System4", "System5"};
int  SystemNumber;

for (SystemNumber = 0; SystemNumber < 5; SystemNumber++)
{
  ChildPid = fork();
  if (ChildPid == -1)
  {
    printf ("Fork failed.  Cannot continue.\n");  /* Will probably never happen  */
    exit (0);
  }

  if (ChildPid)  /*  This is the parent task  */
    sleep (3600);  /*  Set the sleep time to 1 hour.  If the child doesn't finish by then, continue anyway  */
  else
  {
    sprintf (Command "FTP %s", SystemNames[SystemNumber]);
    system (Command);  /*  Start the FTP
  /*  Insert additional code for the child process  */
  }
}


If the transfer times to these systems is 10, 12, 21, 6, and 8 seconds, then the parent will have a life of just under a minute.  (The transfer times add up to 57 seconds.)  This is because when each transfer completes the child task terminates and sends a signal to the parent that breaks the sleep() function.

You can also use the waitpid() function that will cause the parent to "sleep" until the child task completes.  The format would be waitpid(ChildPid, &Status, 0).

The sleep(3600) does exactly the same thing, except that it also says that if the child doesn't terminate in 3600 seconds, break anyway.

Kent
0
 
LVL 16

Expert Comment

by:imladris
ID: 9825394
Did any of those answers help?

If so, it is now time to select one (or more) and grade it to close this off.

If not, perhaps a clarifying question would help.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now