On Wed, 17 Jun 1998, Jeff wrote:
>
> if (!fork()) // what does this line mean? if(fork == 0)?
> {
> send(new_fd, "hello\n", 6, 0);
> close(new_fd);
> while(waitpid(-1,NULL,WNOHANG) > 0);
> }
>
> this came from a socket tutorial, my actual code is a little different
> and includes a recv that prints the first thing it recieves and then
> continues, and it also has some error checking that I left out. I dont
> understand what this is doing. particularly how the fork() function works.
> Could someone explain this?
>
man fork
will give u some idea.
In Un*x all processes (except init) are created by cloning from some other
by fork()/vfork()/clone().
fork() makes an exact copy of the process (with pages initialy shared
wiht the copy-on-write mechanism) so the return value helps u decide where
u are - if fork() returns > 0 then this is the pid of the child process
which u may use for waitpid()
if fork() returns 0 this is the child process, if the return value is -1
an error ocurred.
U can't rely on whether the child or parent process will take a timeslice
first.
The glibc info files will give u more detailed explanation,
Hope this helps...
Marin
-= Why do we need gates in a world without fences? =-