Run external program in background


 
Thread Tools Search this Thread
Top Forums Programming Run external program in background
# 1  
Old 03-01-2009
Run external program in background

Hi,
in my program i need to run an external program in background.I am aware that there are at least 2 alternatives for this:
1)fork+exec
2)system("program &");

I have read several posts about this,and they all tend to suggest to use fork+exec (and that's what i am doing now).
I have some questions though:
a) I read somewhere that "system" actually uses fork+exec (and the man page for "system" implicitly seems to confirm it).Can you confirm this?

b)in the original source code i started working on,the external program was run like this:
Code:
int status=system("program &");
printf("exited with status %d",WEXITSTATUS(status));

I don't think that's correct for ay least 2 reasons:
a)it doesn't check WIFEXITED before checking WEXITSTATUS
b)since i am interested in the exit value of the external program,i think that this code is wrong since it wil output only the "system" exit code,and not the external program's one(consider that the external program will run for a while,so its exit code will be outputted much later).
Am i correct?
# 2  
Old 03-01-2009
Oops i forgot the 3rd question Smilie

If i use fork+exec,i get a zombie process when external program exits,which i treat with wait.
This didn't happen when i used "system" to run it.Why?
# 3  
Old 03-03-2009
I would read your man page - the original code is correct - system does return the lower 8 bits (just like you get for wait) of the the exit status code of "process"

system returns -1 if fork() fails, otherwise it returns the exit status.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Dialog with an external program using named pipes

Dear community, I communicate with an external program (maxima) using named pipes. If I use a text file to capture the output (maxima > out.txt) i can see the programs answer directly after the input written into the file. But if bypass the output into a named pipe (maxima > pipe) and capture it... (1 Reply)
Discussion started by: Pustekuchen
1 Replies

2. Shell Programming and Scripting

how to invoke external program and capture its output

Hi all, I am using an external binary to view memory starting from a specific address and i want to automate this via PERL however there are problems. Hope you can help me ..thx The output of the programme is like below: bash-3.2$ mem_disp 12B21D20 100 Opening RO Data Memory File scp.ro... (4 Replies)
Discussion started by: ekckabatop
4 Replies

3. Programming

Run interactive program in background and grep it

I need to run an interactive program in the background so I can grep it for specific data to use for some logic in a script. There is no quiet or background mode on the program so I plan to redirect a document with commands in it and then to grep the output. This is almost working, except... (1 Reply)
Discussion started by: doonan_79
1 Replies

4. Shell Programming and Scripting

For loop to run external program

Hi, I was hoping for help with a for loop to run a program (vina) repeatedly using all the files in a folder as input. Currently my code looks like this: #!/bin/bash FILES=/home/afalk/Desktop/battest/*.pdbqt for f in $FILES do vina --config /home/afalk/Desktop/A.txt --ligand "$f".pdbqt done... (5 Replies)
Discussion started by: oldmanwinter
5 Replies

5. Shell Programming and Scripting

Passing answers to external program from KSH

I have asked this before but I haven't had any luck so far getting this to work. I use RCS(revision control system). When it runs if I pass the value 'unlock' to $3 its reassigned to $unlock. When I run the command (rcs -u'version number' 'filename') ti will ask me 1-(Do you want to break the lock... (5 Replies)
Discussion started by: pjones006
5 Replies

6. Shell Programming and Scripting

Background program exits prematurely?

Hello! I am currently attempting to build a very, very, basic program that attempts to act like a calendar for a "server." Using this program I need to have the client program connect to the server program, have the client wait for the server to respond through a socket, then exit and the server... (2 Replies)
Discussion started by: Entelexia
2 Replies

7. Shell Programming and Scripting

How to process and run a program in the background in perl?

Hi, I have a query about processing and running Perl program at the background. I have HTML file called Userform.html which accepts input from the user. As soon as input is given the contol goes to get.cgi (get.cgi does some processing and computing tasks). Actually get .cgi takes more... (0 Replies)
Discussion started by: vanitham
0 Replies

8. Programming

Problem with external program launch

Hello, in the application i'm writing i need to launch "recordmydesktop" to capture the screen,but i'm having a problem: when the recording stops,and the encoding of the saved file starts,the entire system hangs until the completion of the encoding.This happens if i launch recordmydesktop from my... (7 Replies)
Discussion started by: Zipi
7 Replies

9. Shell Programming and Scripting

File path with space as external input to the program

Hello I am getting error when the file (Folder or Application) path having space is given as external input to the shell program. It works fine for the files which has no spaces in the file name Thans, (5 Replies)
Discussion started by: keshav.murthy@r
5 Replies

10. Shell Programming and Scripting

Passing value to an external program problem...

This code is in my 'case' statement and it all else works fine. The problem I have is that the value in 'procno' is not passed on to the external program (fireit). It is passing all zeros instead of the actual process number. By the time I get to this case statement, I know the "Number" and... (1 Reply)
Discussion started by: giannicello
1 Replies
Login or Register to Ask a Question