LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   I want to run a ftp from c++ asynchronously perhaps using bash (https://www.linuxquestions.org/questions/programming-9/i-want-to-run-a-ftp-from-c-asynchronously-perhaps-using-bash-741626/)

DEF. 07-21-2009 04:04 AM

I want to run a ftp from c++ asynchronously perhaps using bash
 
I want to run a ftp from c++ asynchronously. For example:

Using the following bash command:
//ftp -in <ip address> <<HERE
//user <user> <password>
//put <filename>
//bye
//HERE

Inserted into a c++ system call:

system ("ftp -in 1.2.3.4 <<HERE\nuser username password\nput filename\nbye\nHERE&");

Note I want the ftp to run asynchronously thus at the end of the bash command I have '&'.

Will using $? give the the result of the ftp transfer, thus because of the '&' does $? still work? For example can I then just do the following:

system("if [ $? -eq 0 ]; then\n do something\nfi")

Is there a better way?

How do I know the process has finsihed, do I have to monitor the process?

Should the ftp command just be in a phtread?

catkin 07-21-2009 05:43 AM

Hello DEF. :)

Do you want the compiled C++ executable to wait for the FTP process to complete or to come back later and see what happened?

AFAIK, each system( ) call starts a new shell (which may or may not exit, depending on what you make it do); it's not like it there's one shell that you can keep dipping back into.

Thus you can't use
Code:

system("if [ $? -eq 0 ]; then\n do something\nfi")
to access the exit status from the preceding
Code:

system ("ftp -in 1.2.3.4 <<HERE\nuser username password\nput filename\nbye\nHERE&");
You need some inter-process communication between the process (tree) that you kick off from the compiled C++ executable to do the FTP work back to the compiled C++ executable. The simplest mechanism (conceptually and to debug) is to have the process (tree) doing the FTP work to write to a file and then read that file in the compiled C++ executable.

As with all asynchronous processes you need to time-coordinate between them; yes, you will have to "monitor the process" to see if it has finished before you can check how it went.

I don't C++ but guess the system() call returns the PID of the process it initiated. Presumably there's another function you can call from C++ to query a process by giving its PID. Beware the PID may be re-used!

Best

Charles

bigearsbilly 07-21-2009 05:50 AM

I don't suppose you've heard of
netrc

and maybe:

ftp -N netrc

man ftp

catkin 07-21-2009 05:50 AM

P.S. I don't know about phtreads but, if you can multi-thread then setting up a thread to run the system call that starts the FTP work and not using "&" would be a neat way to pick up the exit status, from the system()'s return value.

Asynchronous processes are non-trivial to work with -- way easier to go synchronous if you have the option.

DEF. 07-30-2009 12:12 PM

catkin: Your comments make sense so I have done what you said and used pthreads.

bigearsbilly: I wasn't sure how I am suppose to use this configuration file to solve my problem?


All times are GMT -5. The time now is 06:58 AM.