01-23-2006
try this code for fork
#include <stdio.h>
void main()
{
int i ;
printf("The pid of the parent is :%d \n",getpid());
i = fork();
if ( i == 0 )
{
printf("The child pid is :%d and the parent pid is :%d \n",getpid(),getppid());
}
else
{
printf("The parent pid is :%d\n",getpid());
}
}
when a fork is called then the successfull return value of the fork is 0 , the 0 return value is the child process , you will see that when the return value is compared with 0 the child part of the code is executed which is inside the if loop , otherwise the parent is executed( which is the else part)
The child process after fork will always get the value 0.
If you have used fork then child and parent is created.
If inside the child part of code you have written waitpid("parent pid"){waitpid(getppid())} then the child will wait for the parent to be finish. When the parent will complete then the child will start to execute.
If you used like waitpid("child pid") inside the parent part of code then the parent will wait till the child is finished.
Rajesh
9 More Discussions You Might Find Interesting
1. UNIX for Dummies Questions & Answers
i.e configuration of C compiler :confused: (4 Replies)
Discussion started by: atiato
4 Replies
2. Programming
alright, i just got this assignment. since im very new to unix programming, i would like to have some ideas about it. honestly, i dont even know what the hell he is asking for. plz see the attached pdf and help me out. thank you :( (2 Replies)
Discussion started by: ligerdave
2 Replies
3. UNIX for Dummies Questions & Answers
how i can download the game plz tell me ????? ty :) :) :) (3 Replies)
Discussion started by: Sora21
3 Replies
4. Shell Programming and Scripting
I am a newbie to Unix, I am using vi editor. I was able to print output to the screen using simple scripts. But when I tried a midlevel script I don't see anything on the screen and Cursor is just blinking when I type ./test11 filename. I have used chmod 755 also, but of no use. Any help would... (3 Replies)
Discussion started by: Mandab
3 Replies
5. Solaris
hi
I have been given a new Sun fire V120 which i had no use for but my firend wished to host a maple story sever on it so i was happy to let him use it but i have run in to the problem that i have no clue how to configure it so he can use it if anyone can help we would realy appreciate it. Am... (1 Reply)
Discussion started by: rdsars
1 Replies
6. Shell Programming and Scripting
this script reads source.txt and move
and random choice 6 files among 25 txt file
and renmame it..
but it is not executed..
whats problem?
#!/bin/ksh
while read line
do
cd $line/radmin
num=0
/bin/ls *.txt |
while read file_nm ; do
(( check = $RANDOM % 2 ))
if ; then
continue... (1 Reply)
Discussion started by: topic32428285
1 Replies
7. UNIX for Dummies Questions & Answers
plz help me with this question :
1111 | aaaa | pppp | 23/9/87
3333 | dddd | oooo | 6/9/8
4444 | gggg | rrrr | 5/8/9
5555 | qqqq | eeee
ans:
5555 | qqqq | eeee
depending upon how many "|" are in the question
be quick.. (8 Replies)
Discussion started by: adityamitra
8 Replies
8. Shell Programming and Scripting
hi...
i want to print the below format by using shell scripting...plz help me
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Thanking u in advance...
regards
ram (1 Reply)
Discussion started by: ram_pl
1 Replies
9. Shell Programming and Scripting
can you help me to answer these questions ...
Q1 - Write shell script to print the following pattern:
1
22
333
4444
55555
Q2-
Write shell script to compare the strings, check and see if a command
line argument matched the string "net321" print on screen "** string
matches ** " if... (3 Replies)
Discussion started by: last exile
3 Replies
LEARN ABOUT BSD
pthread_atfork
PTHREAD_ATFORK(3) Library Functions Manual PTHREAD_ATFORK(3)
NAME
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 <Xavier.Leroy@inria.fr>
SEE ALSO
fork(2), pthread_mutex_lock(3), pthread_mutex_unlock(3).
LinuxThreads PTHREAD_ATFORK(3)