The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
launch a command only if Minguccio75 UNIX for Advanced & Expert Users 10 02-16-2007 03:48 AM
launch an executable from a C++ program nadiamihu High Level Programming 2 11-03-2006 01:59 AM
how to Launch an independent process. deepthi UNIX for Dummies Questions & Answers 1 03-10-2005 06:12 AM
How do you launch an .exe from my app? kapolani High Level Programming 1 01-03-2005 02:00 PM
launch & monitor process on remote host mrx Shell Programming and Scripting 1 10-15-2004 07:20 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-05-2008
Registered User
 

Join Date: May 2008
Posts: 2
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
How to launch a new process

Hi, I have a situation where a C main program needs to call another C main program, my background is mostly Windows and I'm new to UNIX programming. I've read about system(), fork(), and exec() and I'm a little confused as to what the sequence of steps should be to launch another process. By the way the parent program would have to suspend execution until the child program returns and parent program would need to read the return or exit code of the child program. Thank you in advance for your help.

Last edited by hr94131; 05-05-2008 at 09:48 AM.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 05-05-2008
Registered User
 

Join Date: Apr 2008
Location: European Union/Germany
Posts: 182
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Should your program 1 run, while programm 2 is running? -> fork(), exec()

Else use system(), you can call any program through it.
Reply With Quote
  #3 (permalink)  
Old 05-05-2008
Registered User
 

Join Date: Oct 2007
Location: USA
Posts: 383
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
The sequence is pipe() / fork() / exec() / waitpid(). Last call reads the exit status of the child process. Use of system() is discouraged as it's costly and resource intensive.
Reply With Quote
  #4 (permalink)  
Old 05-05-2008
Registered User
 

Join Date: Apr 2008
Location: European Union/Germany
Posts: 182
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
system() costs mainly the memory for a shell instance. It's secure when the programmer considers the environement (variables) to be unreliable/hostile.

One has to consider what's more important: simplicity of code (system()) or efficiency (fork(), exec(), ...)
Reply With Quote
  #5 (permalink)  
Old 05-05-2008
Registered User
 

Join Date: Dec 2007
Location: Virginia, USA.
Posts: 211
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Last time I checked system() was deprecated in that it uses /bin/sh -c and is a security risk due to set(u|g)id privilege escalation and environment manipulation attacks.

There's a good case that spawning a shell to execute a command is not a good idea (as shamrock noted) if a simple exec will suffice.
Reply With Quote
  #6 (permalink)  
Old 05-05-2008
Registered User
 

Join Date: May 2008
Posts: 2
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Hi, thank you all for your quick reply.
Hec
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes


The 50 most popular UNIX and Linux searches.
Google Search Cloud for The UNIX and Linux Forums
"inappropriate ioctl for device" 421 service not available, remote server has closed connection ^m arg list too long ascii eof autosys awk trim bash eval bash exec bash for loop boot: cannot open kernel/sparcv9/unix close_wait command copy/move folder in unix curses.h dead.letter export display find grep grep multiple lines grep or grep recursive grep unique inappropriate ioctl for device logrotate.conf lynx javascript mailx attachment mget mtime perl array length ping port read awk output into multiple variables replace space by comma , perl script scp recursive segmentation fault(coredump) sftp script snoop unix stale nfs file handle syn_sent tar exclude unix unix .profile unix com unix forum unix forums unix interview questions unix memory usage unix mtime unix simulator unix.com vi tab size while loop within while loop shell script


All times are GMT -7. The time now is 03:51 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101