LINUX TIPS AND TRICKS --- June 22, 2001

Published by ITworld.com -- changing the way you view IT
http://www.itworld.com/newsletters
________________________________________________________________

Cloning A Process
By Danny Kalev

In a previous newsletter, I discussed the fork() system call. 
fork() launches a new process as a child of the parent process. 
With fork(), the parent and the child have distinct address 
spaces. This is a classic implementation of multiprocessing. In 
a multithreaded application, the main process and its threads 
behave like distinct processes too, except that they share some 
or all of their resources ? virtual memory, open files, signal 
handlers and so on.

The clone() syscall enables the programmer to specify the 
shared resources between the parent and the child processes. 
clone() has the following prototype:

    int clone(int flags);

The return value of clone() is the same as fork's: in the 
parent process, it returns the pid of the newly created 
process or -1 in the event of an error; in the child process 
the function returns 0. In fact, calling clone() isn't much 
different from a fork() call. The only difference is the flags 
argument. This argument must contain the signal that should be 
returned to the parent process when the child exits. Normally, 
it's SIGCHLD although under certain conditions, a different 
value may be used as well. You may combine one or more of the 
following options using the bitwise OR operator:

    CLONE_VM ? The two processes share their virtual memory
     space, including the stack.
    CLONE_FS ? The two processes share file system information
     such as the current directory.
    CLONE_FILES ? Open files are shared.
    CLONE_SIGHAND ? The two processes share the same
     signal handlers.

These constants are declared in the <sched.h> header file. 
Sharing a resource means that the two processes see it 
identically. For instance, when you specify the CLONE_FILES 
option, not only is the set of open files shared between the 
parent and its child, but the current position in each file 
is also shared.

Here is an example of using clone() to launch a child process 
that share's the parent's virtual memory:

  pid_t chld=clone(SIGCHLD|CLONE_VM)
  if (chld>0)
  {
    /*?we're in the parent*/
  }
  else if (chld==0)
  {
    /*?we're in the child*/
  }
  if (chld<0)
  {
    printf("clone() failed");
    exit(1)
  }

Note that using clone() directly in an application isn't 
recommended. Linux has plenty of thread libraries that hide the 
gory details of thread management and provide a fully POSIX 
compliant, higher-level thread implementation. These libraries, 
including the new glibc 2 C library, rely on the clone() 
syscall underneath.


About the author(s)
-------------------
Danny Kalev is a system analyst and software engineer with more 
than 10 years of experience, specializing in C++ and 
object-oriented analysis and design on various platforms 
including VMS, DOS, Windows, Unix, and Linux. His technical 
interests involve code optimization, networking, and 
distributed computing. He is also a member of the ANSI C++ 
standardization committee and the author of ANSI/ISO C++ 
Professional Programmer's Handbook (Que, 1999). Contact him at 
linuxnl@excite.com.
________________________________________________________________

ADDITIONAL RESOURCES

Using the Clone() System Call
http://www2.linuxjournal.com/articles/misc/0033.html

clone() Frequently Asked Questions
http://www.accessone.com/~jql/clone-faq.html

The Linux clone() project
http://www.accessone.com/~jql/clone.html
________________________________________________________________

CUSTOMER SERVICE

SUBSCRIBE/UNSUBSCRIBE:
- Go to: http://reg.itworld.com/cgi-bin/subcontent12.cgi
- Enter your email address under "Current subscriber" to log in
- Uncheck the box next to the newsletter you want to
  unsubscribe from
- Or check the box next to the newsletter you want to
  subscribe to
- Submit

If you have questions, please send email to customer service at:
mailto:support@itworld.com
________________________________________________________________

CONTACTS

* For editorial comments, write Andrew Santosusso, Associate
  Editor, Newsletters at: andrew_santosusso@itworld.com

* For advertising information, write Dan Chupka, Account
  Executive at: dan_chupka@itworld.com

* For recruitment advertising information, write Jamie Swartz,
  Eastern Regional Sales Manager at: jamie_swartz@itworld.com
  or Paul Duthie, Western Regional Sales Manager at:
  paul_duthie@itworld.com

* For all other inquiries, write Jodie Naze, Product Manager, 
  Newsletters at: jodie_naze@itworld.com
________________________________________________________________

PRIVACY POLICY
http://www.itworld.com/Privacy/

Copyright 2001 ITworld.com, Inc., All Rights Reserved.
http://www.itworld.com
