Linux-Bulgaria.ORG
навигация

 

начало

пощенски списък

архив на групата

семинари ...

документи

как да ...

 

 

Предишно писмо Следващо писмо Предишно по тема Следващо по тема По Дата По тема (thread)

Re: lug-bg: fork, programirane


  • Subject: Re: lug-bg: fork, programirane
  • From: kaloian@xxxxxxxxxx (Kaloian Doganov)
  • Date: Thu, 04 May 2000 14:43:29 +0300



viz dali dolnite obiasnenia niama da ti svurshat rabota:

*********************************************************************
fork() Revisited
By Danny Kalev

Several readers have contacted me regarding the newsletter "Implementing
Multiprocessing with fork()". Unfortunately, a tiny -- yet crucial --
typo crept into the code. Furthermore, many readers have asked me to
explain in greater detail what's going on under the hood when a child is
spawned. Therefore, I would like to discuss this topic again, hopefully
making things clearer and more intuitive.

The most important thing is to understand how the function fork()
behaves. If fork() fails, it returns -1 to the parent process. If it
succeeds, it launches a child process, whose attributes are the same as
the parent's. The most peculiar thing about fork() is that it returns 0
in the child process whereas in the parent process, it returns the
child's pid (which is never 0). Thus, by examining whether the return
value equals 0, a process can tell whether it's a child or a parent, and
behave accordingly. For example:

 #include <unistd.h>
 #include <stdio.h>
 int main()
 {
  if (fork() == 0)
  {
    printf("inside child process");
    exit(0);  /* child terminates here */
  }
  printf("inside parent process");
  return 0;
 }

After launching a child process, there are two identical processes
executing the same program. Normally, you don't want the two process to
do the same thing. Therefore, you write your program so that it behaves
differently when it executes as a child process.
Now back to my original example. A Web browser application spawns a mail
client program when the user clicks on a mailto: link. Here are the
necessary steps for achieving this. First, call fork() to spawn a new
process. The child and the parent are identical but now we know how to
distinguish between them:

 if (fork()==0) /* in child? */
 {/* do child stuff */}

The body of the if-statement executes only in the child process because
the condition is never true in the parent. Next, replace the child
process with another one -- the mail client program. Use the execl()
function for this purpose. A successful execl() call never returns.
Therefore, the following printf() statement executes only if execl()
failed:

if (fork()==0)
{
/* this block is executed by child; skipped by parent */
  execl("mail", "mail", email_address, NULL);
  printf("error, could not launch mail client");
}

The parent process skips the if statement's body and proceeds to the
next statement, which retrieves a Web page. Here's the complete program:

#include <unistd.h>
#include <process.h>
#include <stdio.h>
int main()
{
 /* Web browser application */
 if (fork()==0)
 {
 /* this block is executed by child process; skipped by parent */
   execl("mail", "mail", email_address, NULL);
   printf("error, could not launch mail client");
 }
 get_web_page(); /* executed only by the parent process */
}

==================================================================
A mail-list of Linux Users Group - Bulgaria (bulgarian linuxers)
Otpiswaneto RABOTI !!! : Majordomo@xxxxxxxxxxxxxxxxxx UNSUBSCRIBE LUG-BG
http://www.linux-bulgaria.org/ Hosted by Internet Group Ltd. - Stara Zagora



 

наши приятели

 

линукс за българи
http://linux-bg.org

FSA-BG
http://fsa-bg.org

OpenFest
http://openfest.org

FreeBSD BG
http://bg-freebsd.org

KDE-BG
http://kde.fsa-bg.org/

Gnome-BG
http://gnome.cult.bg/

проект OpenFMI
http://openfmi.net

NetField Forum
http://netField.ludost.net/forum/

 

 

Linux-Bulgaria.ORG

Mailing list messages are © Copyright their authors.