ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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)
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.