LINUX TIPS AND TRICKS --- April 21, 2000

Published by ITworld.com, the IT problem-solving network
http://www.itworld.com/newsletters

*********************************************************************

Implementing Multiprocessing with fork()
By Danny Kalev

This column continues last week's discussion by showing how to launch a 
child process from a running process while keeping the parent process 
running. Before we do that, however, here's some background on 
concurrency in Linux.

Concurrency comes in two flavors: threads and multiprocessing. Threads 
are lightweight code sections that execute concurrently, sharing a 
single process. They have common address space, data, and resource 
limits. In multiprocessing, two or more processes run concurrently. They 
usually interact with each other using some form of interprocess 
communication, such as shared memory or sockets. 

To achieve rudimentary multiprocessing, you need to make the parent 
process stick around while its newly spawned child process executes. For 
this purpose, you have to use the fork() function. (In contrast, the 
exec() function launches a new process from a running process, but 
replaces the running process.) 

The fork() function has the following prototype:

pid_t fork(void);

fork() creates a new process and returns its pid to the parent process.
The newly spawned process is an exact copy of the parent process but it
has its own address space. The child process immediately calls one of
the exec() functions to run a new program while the parent process
continues to run. Usually the two steps are implemented as a single if
statement whose condition has a fork() call. The body of the if
statement includes an exec() call. The trick is that the parent skips
the if statement's body and proceeds to the next statement, whereas the
child process executes the commands in body of the if statement. 

Here's a concrete example of when to use multiprocessing. Suppose you 
have a Web browser that loads a Web page containing a mailto: link. When 
a visitor clicks on this link, the Web browser launches a mail client 
while the browser keeps loading the Web page.  

#include <unistd.h>
#include <process.h>
#include <stdio.h>
int main()
{
  /* Web browser application */
  if (fork())
  { 
  /* this block is executed by child process; skipped by parent */
    execl("mail", "mail", email_address, NULL);
    printf("error, could not launch mail client");   
  }
  get_web_page(); /* executed by the parent process */
} 

About the author
----------------
Danny Kalev is a system analyst and software engineer with more
than 10 years of experience, specializing in C++ and
object-oriented analysis and design on various platforms including
VMS, DOS, Windows, Unix, and Linux. His technical interests involve
code optimization, networking, and distributed computing. He is
also a member of the ANSI C++ standardization committee and the
author of ANSI/ISO C++ Professional Programmer's Handbook (Que,
1999). Contact him at linuxnl@excite.com.