I am looking for C code to use on a Linux based system to start another process asynchronously. The second process should continue, even if the first ends. I've looked through the "fork" and "system" and "exec" options, but don't see anything that will spawn a peer process that's not communicating with or a child of the original process.

Can this be done?

有帮助吗?

解决方案 2

In Linux (and Unix), every process is created by an existing process. You may be able to create a process using fork and then, kill the parent process. This way, the child will be an orphan but still, it gets adopted by init. If you want to create a process that is not inherited by another, I am afraid that may not be possible.

其他提示

Certainly you can. In the parent fork() a child, and in that child first call daemon() (which is an easy way to avoid setsid etc.), then call something from the exec family.

You do a fork (man 2 fork) followed by an execl (man 2 execl)

For creates a new process of the same image as the calling process (so a perfect twin), where execl replaces one of the twins with a new image.

If you search google for "fork execl" you will find many text book examples -- including how to use correctly fork() and exec()

The most common fork-execl you will still have the new process associated to the terminal -- to create a perfect background process you need to create what is called a daemon process -- the template for that can be fornd in this answer here Creating a daemon in Linux

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top