Section 12.9. Threads and fork

team bbl


12.9. Threads and fork

When a thread calls fork, a copy of the entire process address space is made for the child. Recall the discussion of copy-on-write in Section 8.3. The child is an entirely different process from the parent, and as long as neither one makes changes to its memory contents, copies of the memory pages can be shared between parent and child.

By inheriting a copy of the address space, the child also inherits the state of every mutex, readerwriter lock, and condition variable from the parent process. If the parent consists of more than one thread, the child will need to clean up the lock state if it isn't going to call exec immediately after fork returns.

Inside the child process, only one thread exists. It is made from a copy of the thread that called fork in the parent. If the threads in the parent process hold any locks, the locks will also be held in the child process. The problem is that the child process doesn't contain copies of the threads holding the locks, so there is no way for the child to know which locks are held and need to be unlocked.

This problem can be avoided if the child calls one of the exec functions directly after returning from fork. In this case, the old address space is discarded, so the lock state doesn't matter. This is not always possible, however, so if the child needs to continue processing, we need to use a different strategy.

To clean up the lock state, we can establish fork handlers by calling the function pthread_atfork.

[View full width]

 #include <pthread.h> int pthread_atfork(void (*prepare)(void), void  (*parent)(void),                    void (*child)(void)); 

Returns: 0 if OK, error number on failure


With pthread_atfork, we can install up to three functions to help clean up the locks. The prepare fork handler is called in the parent before fork creates the child process. This fork handler's job is to acquire all locks defined by the parent. The parent fork handler is called in the context of the parent after fork has created the child process, but before fork has returned. This fork handler's job is to unlock all the locks acquired by the prepare fork handler. The child fork handler is called in the context of the child process before returning from fork. Like the parent fork handler, the child fork handler too must release all the locks acquired by the prepare fork handler.

Note that the locks are not locked once and unlocked twice, as it may appear. When the child address space is created, it gets a copy of all locks that the parent defined. Because the prepare fork handler acquired all the locks, the memory in the parent and the memory in the child start out with identical contents. When the parent and the child unlock their "copy" of the locks, new memory is allocated for the child, and the memory contents from the parent are copied to the child's memory (copy-on-write), so we are left with a situation that looks as if the parent locked all its copies of the locks and the child locked all its copies of the locks. The parent and the child end up unlocking duplicate locks stored in different memory locations, as if the following sequence of events occurred.

  1. The parent acquired all its locks.

  2. The child acquired all its locks.

  3. The parent released its locks.

  4. The child released its locks.

We can call pthread_atfork multiple times to install more than one set of fork handlers. If we don't have a need to use one of the handlers, we can pass a null pointer for the particular handler argument, and it will have no effect. When multiple fork handlers are used, the order in which the handlers are called differs. The parent and child fork handlers are called in the order in which they were registered, whereas the prepare fork handlers are called in the opposite order from which they were registered. This allows multiple modules to register their own fork handlers and still honor the locking hierarchy.

For example, assume that module A calls functions from module B and that each module has its own set of locks. If the locking hierarchy is A before B, module B must install its fork handlers before module A. When the parent calls fork, the following steps are taken, assuming that the child process runs before the parent.

  1. The prepare fork handler from module A is called to acquire all module A's locks.

  2. The prepare fork handler from module B is called to acquire all module B's locks.

  3. A child process is created.

  4. The child fork handler from module B is called to release all module B's locks in the child process.

  5. The child fork handler from module A is called to release all module A's locks in the child process.

  6. The fork function returns to the child.

  7. The parent fork handler from module B is called to release all module B's locks in the parent process.

  8. The parent fork handler from module A is called to release all module A's locks in the parent process.

  9. The fork function returns to the parent.

If the fork handlers serve to clean up the lock state, what cleans up the state of condition variables? On some implementations, condition variables might not need any cleaning up. However, an implementation that uses a lock as part of the implementation of condition variables will require cleaning up. The problem is that no interface exists to allow us to do this. If the lock is embedded in the condition variable data structure, then we can't use condition variables after calling fork, because there is no portable way to clean up its state. On the other hand, if an implementation uses a global lock to protect all condition variable data structures in a process, then the implementation itself can clean up the lock in the fork library routine. Application programs shouldn't rely on implementation details like this, however.

Example

The program in Figure 12.17 illustrates the use of pthread_atfork and fork handlers.

We define two mutexes, lock1 and lock2. The prepare fork handler acquires them both, the child fork handler releases them in the context of the child process, and the parent fork handler releases them in the context of the parent process.

When we run this program, we get the following output:

     $ ./a.out     thread started...     parent about to fork...     preparing locks...     child unlocking locks...     child returned from fork     parent unlocking locks...     parent returned from fork 

As we can see, the prepare fork handler runs after fork is called, the child fork handler runs before fork returns in the child, and the parent fork handler runs before fork returns in the parent.

Figure 12.17. pthread_atfork example
 #include "apue.h" #include <pthread.h> pthread_mutex_t lock1 = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t lock2 = PTHREAD_MUTEX_INITIALIZER; void prepare(void) {     printf("preparing locks...\n");     pthread_mutex_lock(&lock1);     pthread_mutex_lock(&lock2); } void parent(void) {     printf("parent unlocking locks...\n");     pthread_mutex_unlock(&lock1);     pthread_mutex_unlock(&lock2); } void child(void) {     printf("child unlocking locks...\n");     pthread_mutex_unlock(&lock1);     pthread_mutex_unlock(&lock2); } void * thr_fn(void *arg) {     printf("thread started...\n");     pause();     return(0); } int main(void) {     int         err;     pid_t       pid;     pthread_t   tid; #if defined(BSD) || defined(MACOS)     printf("pthread_atfork is unsupported\n"); #else     if ((err = pthread_atfork(prepare, parent, child)) != 0)         err_exit(err, "can't install fork handlers");     err = pthread_create(&tid, NULL, thr_fn, 0);     if (err != 0)         err_exit(err, "can't create thread");     sleep(2);     printf("parent about to fork...\n");     if ((pid = fork()) < 0)         err_quit("fork failed");     else if (pid == 0) /* child */         printf("child returned from fork\n");     else        /* parent */         printf("parent returned from fork\n"); #endif     exit(0); } 

    team bbl



    Advanced Programming in the UNIX Environment
    Advanced Programming in the UNIX Environment, Second Edition (Addison-Wesley Professional Computing Series)
    ISBN: 0321525949
    EAN: 2147483647
    Year: 2005
    Pages: 370

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net