Process creation and threads

QNX SDP8.0Getting Started with the QNX OSDeveloperUser

Suppose you have a process and you haven't created any threads yet (i.e., you're running with one thread, the one that called main()). When you call fork(), another process is created, also with one thread. This is the simple case.

Now suppose that in your process, you've called pthread_create() to create another thread. When you call fork(), it will now return ENOSYS (meaning that the function is not supported)! Why?

Well, believe it or not, this is POSIX compatible—POSIX says that fork() can return ENOSYS. What actually happens is this: the QNX OS C library isn't currently built to handle the forking of a process with threads. When you call pthread_create(), the function sets a flag, effectively saying, Don't let this process fork(), because I'm not prepared to handle it. Then, in the library fork() function, this flag is checked, and, if set, causes fork() to return ENOSYS.

The reason this is intentionally done has to do with threads and mutexes. Some of the threads might have locked some C library internal mutexes. Since the mutexes are protecting internal library data structures, those data structures would be left in an inconsistent state in the child process. Further, the mutexes would still be locked, and would be left that way.

Note that this restriction may be lifted in a future release.

Page updated: