LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 09-30-2010, 06:17 AM   #1
xDan
LQ Newbie
 
Registered: Sep 2010
Posts: 6

Rep: Reputation: 1
Fork/exec versus posix_spawn


Hello,

Just coming over from Windows here... where I've been using "CreateProcess" which returns a value depending on whether the process was created successfully or not.

Now I'm trying to create a process on linux and I've been learning about fork/exec, and I've been struggling with the fact that there seems to be no easy way to know (within the original parent process) if exec succeeded or not. (without forcing the parent process to hang around polling it or something).

Anyway, I've now just discovered the posix_spawn function, and it seems exactly what I need. However I'm finding the documentation a little hard to understand. In particular, I can't find actual confirmation that it will definitely return an error if creating the process fails (like CreateProcess does on windows). So can anyone confirm that for me?

The text on the documentation states: "If posix_spawn() or posix_spawnp() fail for any of the reasons that would cause fork() or one of the exec family of functions to fail, an error value shall be returned as described by fork() and exec"

This makes me think it uses exec/fork under the hood, and so I wonder if it can be trusted..?

And also, most of the examples on the internet for creating processes use fork/exec, so I wonder if there is some catch with posix_spawn I haven't read about... (since it seems a much simpler way of doing things)
 
Old 09-30-2010, 07:30 AM   #2
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
Obviously you can determine whether fork/exec have succeeded, since both return -1 on failure, but you want to determine this in the parent. Normally the error would be handled by the child.

How the parent should handle an exec error in the child depends on what you are intending. However, it is always more robust to handle it in the child process (more below).

If the parent must handle a potential error in the exec, then it could handle it at the point where it waits for the child to finish.

However, you probably want it to handle the error immediately, in which case you could break up the exec further (eg, opening the file first, then calling fexecve). You would need to communicate from the child to the parent whether the open had succeeded (and wait for this in the parent).

Or you could simply send a signal from the child to the parent when an exec fails (and create a signal handler in the parent). Whatever you do, I would recommend against polling in a multiprocess environment.

I will point out though that CreateProcess has similar issues; the success of the spawning is not a guarantee that the child has succeeded. For example, the spawned process may fail to load a dll, and this will not be seen by the parent but by the child, because the CreateProcess call returns before the child has been initialized. So even in the Windows context you must be prepared for this; it is not as simple as just calling and checking for an error in the parent.

The posix_spawn is only required in place of fork/exec because some embedded environments (eg processors without MMUs) cannot efficiently implement the fork. It is not implemented using fork. In some ways it is much closer to the simplified CreateProcess of Windows, so if you are just porting software and don't want to change any error handling logic, then it might indeed be the easiest path.
 
1 members found this post helpful.
Old 09-30-2010, 08:19 AM   #3
xDan
LQ Newbie
 
Registered: Sep 2010
Posts: 6

Original Poster
Rep: Reputation: 1
Thanks, that's very enlightening. I didn't consider that CreateProcess might have similar problems. fexecve will do me fine.
 
  


Reply



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
gdb, fork(), exec() jason_m Programming 1 08-28-2010 11:03 AM
linux C exec fork karana Linux - Newbie 1 03-21-2009 07:48 AM
Fork exec problem bhupeshchawda Programming 3 04-19-2007 04:58 AM
how to fork/ exec schneidz Programming 10 08-30-2005 11:50 AM
Fork 'n Exec sourceman Linux - General 4 02-14-2002 02:31 AM

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

All times are GMT -5. The time now is 08:09 PM.

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