Visit The New, Modern Unix Linux Community

Linux and UNIX Man Pages

Test Your Knowledge in Computers #410
Difficulty: Easy
Trusted XENIX was a variant initially developed by HP-UX, under the name Secure XENIX.
True or False?
Linux & Unix Commands - Search Man Pages

pthread_atfork(3) [freebsd man page]

PTHREAD_ATFORK(3)					   BSD Library Functions Manual 					 PTHREAD_ATFORK(3)

pthread_atfork -- register fork handlers LIBRARY
POSIX Threads Library (libpthread, -lpthread) SYNOPSIS
#include <pthread.h> int pthread_atfork(void (*prepare)(void), void (*parent)(void), void (*child)(void)); DESCRIPTION
The pthread_atfork() function declares fork handlers to be called before and after fork(2), in the context of the thread that called fork(2). The handlers registered with pthread_atfork() are called at the moments in time described below: prepare Before fork(2) processing commences in the parent process. If more than one prepare handler is registered they will be called in the opposite order they were registered. parent After fork(2) completes in the parent process. If more than one parent handler is registered they will be called in the same order they were registered. child After fork(2) processing completes in the child process. If more than one child handler is registered they will be called in the same order they were registered. If no handling is desired at one or more of these three points, a null pointer may be passed as the corresponding fork handler. RETURN VALUES
If successful, the pthread_atfork() function will return zero. Otherwise an error number will be returned to indicate the error. ERRORS
The pthread_atfork() function will fail if: [ENOMEM] Insufficient table space exists to record the fork handler addresses. SEE ALSO
fork(2), pthread(3) STANDARDS
The pthread_atfork() function is expected to conform to IEEE Std 1003.1 (``POSIX.1''). AUTHORS
This manpage was written by Alex Vasylenko <>. BSD
June 21, 2004 BSD

Check Out this Related Man Page

PTHREAD_ATFORK(3)					     Library Functions Manual						 PTHREAD_ATFORK(3)

pthread_atfork - register handlers to be called at fork(2) time SYNOPSIS
#include <pthread.h> int pthread_atfork(void (*prepare)(void), void (*parent)(void), void (*child)(void)); DESCRIPTION
pthread_atfork registers handler functions to be called just before and just after a new process is created with fork(2). The prepare han- dler will be called from the parent process, just before the new process is created. The parent handler will be called from the parent process, just before fork(2) returns. The child handler will be called from the child process, just before fork(2) returns. One or several of the three handlers prepare, parent and child can be given as NULL, meaning that no handler needs to be called at the cor- responding point. pthread_atfork can be called several times to install several sets of handlers. At fork(2) time, the prepare handlers are called in LIFO order (last added with pthread_atfork, first called before fork), while the parent and child handlers are called in FIFO order (first added, first called). To understand the purpose of pthread_atfork, recall that fork(2) duplicates the whole memory space, including mutexes in their current locking state, but only the calling thread: other threads are not running in the child process. The mutexes are not usable after the fork and must be initialized with pthread_mutex_init in the child process. This is a limitation of the current implementation and might or might not be present in future versions. RETURN VALUE
pthread_atfork returns 0 on success and a non-zero error code on error. ERRORS
ENOMEM insufficient memory available to register the handlers. AUTHOR
Xavier Leroy <> SEE ALSO
fork(2), pthread_mutex_lock(3), pthread_mutex_unlock(3). LinuxThreads PTHREAD_ATFORK(3)

15 More Discussions You Might Find Interesting

1. Programming

fork() in for loop

hello, Every time i use fork() in the for loop, my college network(which i work in) either gets slow or hangs up.Can any 1 explain why it is so? Of course there is no use of doing it though. But still i want to clear my doubt. Thanks (2 Replies)
Discussion started by: cyno
2 Replies

2. Programming

signal handler for SIGCHLD

Hi, I have an c++ application which uses the function fork and execvp(). The parent does not wait until the child ends. The parents just creates children and let them do their stuff. You can see the parent program as a batch-manager. I have added a SIGCHLD handler to the program: void... (3 Replies)
Discussion started by: jens
3 Replies

3. Programming

fork multiple shells

Hi, i was trying to play with fork,exec and signal for spawning multiple new shells, but it seems that i'm doing blunder somewhere. <sample code> 1 /* 2 * The idea is to fork multpile(equal to $ULIMIT) childs 3 * and replace their images with process:csh 4 * during this the parent... (3 Replies)
Discussion started by: amit4g
3 Replies

4. UNIX for Advanced & Expert Users

Fork and \n

Hi, I wrote a simple program for understanding the fork command. The code is as below int main(void) { fork(); printf("hi 1 \n"); fork(); printf("hi 2 \n"); fork(); printf("hi 3 \n"); } I am getting a variation in the number of times the printf is called if i remove the \n from each of... (1 Reply)
Discussion started by: xyz123456
1 Replies

5. UNIX for Advanced & Expert Users

handling Infinite fork

how Unix handles as process which forks infinitely. like ....... while(1) fork(); ........ What happens when it is executed and how to avoid it. Thanks, Ashish (3 Replies)
Discussion started by: ashish_uiit
3 Replies

6. UNIX for Dummies Questions & Answers

simple fork() problem

I have this little program ... int main(void){ printf("Before"); fork(); printf("After"); } output is this..... BeforeAfterBeforeAfter Shouldnt it be.....BeforeAfterAfter After parent is forked child receives the copy of program and continues from next statement... (3 Replies)
Discussion started by: joker40
3 Replies

7. Shell Programming and Scripting

invoke fork command

Hi, I have startup shell script called "xxxxx" for Jboss server which is taking more than expected timeline to complete the process, here I want to use the fork command to start the child process for non dependent component I have a scheduler called "yyyy" which is currently getting invoked... (2 Replies)
Discussion started by: harish76
2 Replies

8. Programming

Problem with execution of fork system call if i use \n

hi all, i tried the following source codes: fork1.c: main() { printf("demo of fork\n"); fork(); printf("hello"); } output: demo of fork hello hello fork2.c: main() (3 Replies)
Discussion started by: pnirmala
3 Replies

9. Programming

Parent forking

My question is, how do you fork only the parent processes in unix? For example how would I use the fork function to fork the parent process more than once and leave the children processes alone. This way I do not have children of children. The way I have it set up now it the parent process forks 3... (7 Replies)
Discussion started by: TWhitt24
7 Replies

10. Programming

parent called more times - fork - C language

Hi gurus can you explain following lines of code ? #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> int main(void) { pid_t pid; int rv; switch(pid = fork()) { case -1: ... (7 Replies)
Discussion started by: wakatana
7 Replies

11. Programming

question about fork

i'm experimenting fork function and i found this code #include <stdio.h> #include <sys/stat.h> #include <sys/types.h> #include <wait.h> #include <fcntl.h> #include <unistd.h> int main(void) { int fd; pid_t p; p = fork(); fork(); if (p>0) { fork();} fork(); fork();... (6 Replies)
Discussion started by: blob84
6 Replies

12. Shell Programming and Scripting

help with fork() command???

Hi , help me out Generally If the fork() is executed successfully Unix will create identical copies address spaces and the execution starts from the next statement of the fork() So, in such case output of the following prog must be #include<stdio.h> { printf("\nwelcome to"); fork();... (3 Replies)
Discussion started by: vijay496
3 Replies

13. UNIX for Advanced & Expert Users

Doubt with fork() system call

Hi I wrote a simple fork program to illustrate the fork() system cal. here it is #include<stdio.h> #include<sys/stat.h> #include<sys/types.h> main() { int flag; flag=fork(); if(flag==0) { printf("Child \n"); printf("Process id= %d\n",getpid()); ... (3 Replies)
Discussion started by: badsha6642
3 Replies

14. Shell Programming and Scripting

fork in perl

Can someone tell me perl fork example please. I tried online but could not get proper documentation. (2 Replies)
Discussion started by: Anjan1
2 Replies

15. Ubuntu

Help: how to call fork() in shell script? New to linux

Hi, I'm writing a shell script where I want to call fork(). However I wrote like this "var=fork()" in c style and got this error: "syntax error near unexpected token `(' " How could I call fork() in shell script? Thanks in advance. (2 Replies)
Discussion started by: Xiaoya
2 Replies

Featured Tech Videos