fork()

QNX SDP8.0System ArchitectureDeveloperUser

The fork() function creates a new child process by sharing the same code as the calling process and duplicating the calling process's data to give the child process an exact copy. Most process resources are inherited.

The following resources are explicitly not inherited:
  • process ID
  • parent process ID
  • file locks
  • pending signals and alarms
  • timers
The fork() function is typically used for one of two reasons:
  • to create a new instance of the current execution environment
  • to create a new process running a different program

When creating a new thread, common data is placed in an explicitly created shared memory region. Prior to the POSIX thread standard, this was the only way to accomplish this. With POSIX threads, this use of fork() is better accomplished by creating threads within a single process using pthread_create().

When creating a new process running a different program, the call to fork() is soon followed by a call to one of the exec*() functions. This too is better accomplished by a single call to the posix_spawn() function or the QNX OS spawn() function, both of which combine both operations with far greater efficiency.

Since QNX OS provides better POSIX solutions than using fork(), its use is probably best suited for porting existing code and for writing portable code that must run on a UNIX system that doesn't support the POSIX pthread_create() or posix_spawn() API.

Note:
It's possible to call fork() from a multithreaded process, but it can be very difficult to do so safely, so we recommend that you call it only from single-threaded processes. For more information, see Using fork() in a multithreaded process in the Processes and Threads chapter of Getting Started with the QNX OS.
Page updated: