LinuxQuestions.org
Help answer threads with 0 replies.
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 07-21-2009, 04:04 AM   #1
DEF.
Member
 
Registered: Apr 2009
Posts: 96

Rep: Reputation: 23
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?
 
Old 07-21-2009, 05:43 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
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
 
Old 07-21-2009, 05:50 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
I don't suppose you've heard of
netrc

and maybe:

ftp -N netrc

man ftp
 
Old 07-21-2009, 05:50 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
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.

Last edited by catkin; 07-21-2009 at 05:51 AM. Reason: Typo
 
Old 07-30-2009, 12:12 PM   #5
DEF.
Member
 
Registered: Apr 2009
Posts: 96

Original Poster
Rep: Reputation: 23
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?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
problem with ending requests asynchronously in my block device driver cable_plug2000 Programming 0 12-15-2007 05:12 PM
FTP Bash script Elguapo Programming 2 06-07-2007 01:26 PM
bash script AND ftp bokini Linux - General 2 01-04-2006 04:33 PM
how to while in a ftp bash script dezeque Programming 3 06-12-2004 04:13 PM
BASH: FTP backup... elitecodex Programming 2 03-28-2004 11:30 AM

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

All times are GMT -5. The time now is 05:04 AM.

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