Sponsored Content
Full Discussion: C fork Confusion :-?
Top Forums Programming C fork Confusion :-? Post 302219232 by ralpheno on Monday 28th of July 2008 01:45:28 PM
Old 07-28-2008
C fork Confusion :-?

Hi,

I was trying to learn forking in C in UNIX. Somehow i still haven't been able to get the concept well. I mean, i do understand that fork creates an exact replica of the parent (other than the fact that parent gets the process id of the child and child gets 0 when fork is called). This is the point till which i am ok.
Now comes the confusion. If the child is an exact replica, the how does it contain the same file handles. For example lets take the case of a simple webserver. This is how the logic goes

1) the program calls socket() function
2) then it calls bind();
3) then it calls listen(on port 80);
4) waits of connection so it calls accept();
5) As soon as it gets a connection from the client, it forks a child and lets the child handle the request.

Since the child is the exact replica of the parent, the child will try to listen on port 80 too. But that port is already blocked by the parent process. So how does forking work in this case?
The parent and child cant be exact replicas as they both cant be listening on the same port and IP address at the same time.
So my question is: when a fork() function is called, will the child process run the program right from the beginning or will it run from after the fork function call

Thanks in advance
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

confusion with export

Hi, I have written the following two scripts. a.ksh ---> FPATH=/users/kushard autoload b b echo "From a.ksh::" $aa b ---> function b { typeset aa aa="TRUE." echo "From b::" $aa export aa } (1 Reply)
Discussion started by: kdipankar
1 Replies

2. UNIX for Dummies Questions & Answers

unix confusion

:confused: some one please tell me where i can possibly find out what is unix 10.2 and the basic system functions of it is. I really need help! (1 Reply)
Discussion started by: tribb24
1 Replies

3. UNIX for Dummies Questions & Answers

ftp confusion

I'm an intern at a company that recently bought out another business. In doing so, they inherited a unix system that contains files which they need to retrieve. No one in the company, including myself, really understands or knows unix so please respond with the true assumption that I'm a unix... (1 Reply)
Discussion started by: intern
1 Replies

4. UNIX for Dummies Questions & Answers

wc command confusion

Can somebody explain it to me that why wc gives more chars suppose Ab.txt have two lines qwer qasd then wc -c ab.txt will give 10.why not 8.okay may be it is taking count one for each line just in case but why echo "qwer"|wc -C gives 5. Ok with \c it is returning 4. :) (6 Replies)
Discussion started by: Dhruva
6 Replies

5. Shell Programming and Scripting

Sed confusion

Hello all, I am trying to delete all the lines in a particular file having a pattern. The problem is that it has special characters and for some reason is not doing the job. For eg. src_file /home/test/filelist.txt :xxxx:ogog /home/test/RCH/ogogogg /home/test/RYHUJ/HHHH... (3 Replies)
Discussion started by: alfredo123
3 Replies

6. UNIX for Dummies Questions & Answers

'tr' confusion

Good day, everyone! Could anybody explain me the following situation. If I'm running similar script: Var="anna.kurnikova" Var2="Anna Kurn" echo $Var | tr -t "$Var" "$Var2" Why the output is : anna KurniKova instead of Anna Kurnikova? :confused: Thank you in advance for any... (2 Replies)
Discussion started by: Nafanja
2 Replies

7. Cybersecurity

LDAP; confusion

Hello, I hope all is well. Two issues that I am grappling with. One: Is this a true statement: (AIX, LDAP configured), even if authentication is configured with LDAP, the system would still need to be authenticated against local (/etc/passwd); incase of network failure? Two: I can log... (0 Replies)
Discussion started by: rsheikh
0 Replies

8. Shell Programming and Scripting

Confusion with PS

Hello All, I have a problem in counting number of process getting run with my current script name.. Here it is ps -ef | grep $0 | grep -v grep This display just one line with the PID, PPID and other details when i print it in the script. But when I want to count the numbers in my... (11 Replies)
Discussion started by: sathyaonnuix
11 Replies

9. Shell Programming and Scripting

confusion in use of exit 0

hi i am new to shell scripting. i was going thru the part option and arguments. on this section i fail to understand the use of exit 0 in below example . #!/bin/sh USAGE="Usage: $0 " case "$1" in -t) TARGS="-tvf $2" ;; -c) TARGS="-cvf $2.tar $2" ;; *) echo "$USAGE" exit 0 ;; esac... (13 Replies)
Discussion started by: scriptor
13 Replies

10. Shell Programming and Scripting

Confusion in hash

Hi folks, If a declare a direct hash , then the hash element works fine. my %test = ("test",1); print %test; print "\n"; Here in the above, the name of the hash is predeclared... Suppose now I need to create the hash elements dynamically in the for loop. $test="hash"; my... (1 Reply)
Discussion started by: scriptscript
1 Replies
PTHREAD_ATFORK(3)					   BSD Library Functions Manual 					 PTHREAD_ATFORK(3)

NAME
pthread_atfork -- register handlers to be called before and after fork() 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. Important: only async-signal-safe functions are allowed on the child side of fork(). See sigaction(2) for details. 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] The system lacked the necessary resources to add another handler to the list. SEE ALSO
fork(2), pthread(3) STANDARDS
The pthread_atfork() conforms to ISO/IEC 9945-1:1996 (``POSIX.1''). BSD
June 21, 2004 BSD
All times are GMT -4. The time now is 12:51 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy