Example 1 – creation/destruction – process/thread

The process model: Here's what we do: in a loop (that executes a total of 60,000 times!), create and destroy a process by calling fork(2) and subsequently exiting. (We take care of details such as clearing any possible zombie by waiting in the parent for the child to die before proceeding in the loop.) The relevant code is as follows (ch14/speed_multiprcs_vs_multithrd_simple/create_destroy/fork_test.c):

For readability, only the relevant parts of the code are displayed in the following code; to view and run it, the entire source code can be found here: https://github.com/PacktPublishing/Hands-on-System-Programming-with-Linux.
...#define NFORKS 60000void do_nothing(){ unsigned long f = 0xb00da; ...

Get Hands-On System Programming with Linux now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.