Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

26.4 Creating a Process

The fork function is the primitive for creating a process. It is declared in the header file unistd.h.

— Function: pid_t fork (void)

The fork function creates a new process.

If the operation is successful, there are then both parent and child processes and both see fork return, but with different values: it returns a value of 0 in the child process and returns the child's process ID in the parent process.

If process creation failed, fork returns a value of -1 in the parent process. The following errno error conditions are defined for fork:

EAGAIN
There aren't enough system resources to create another process, or the user already has too many processes running. This means exceeding the RLIMIT_NPROC resource limit, which can usually be increased; see Limits on Resources.
ENOMEM
The process requires more space than the system can supply.

The specific attributes of the child process that differ from the parent process are:

  • The child process has its own unique process ID.
  • The parent process ID of the child process is the process ID of its parent process.
  • The child process gets its own copies of the parent process's open file descriptors. Subsequently changing attributes of the file descriptors in the parent process won't affect the file descriptors in the child, and vice versa. See Control Operations. However, the file position associated with each descriptor is shared by both processes; see File Position.
  • The elapsed processor times for the child process are set to zero; see Processor Time.
  • The child doesn't inherit file locks set by the parent process. See Control Operations.
  • The child doesn't inherit alarms set by the parent process. See Setting an Alarm.
  • The set of pending signals (see Delivery of Signal) for the child process is cleared. (The child process inherits its mask of blocked signals and signal actions from the parent process.)
— Function: pid_t vfork (void)

The vfork function is similar to fork but on some systems it is more efficient; however, there are restrictions you must follow to use it safely.

While fork makes a complete copy of the calling process's address space and allows both the parent and child to execute independently, vfork does not make this copy. Instead, the child process created with vfork shares its parent's address space until it calls _exit or one of the exec functions. In the meantime, the parent process suspends execution.

You must be very careful not to allow the child process created with vfork to modify any global data or even local variables shared with the parent. Furthermore, the child process cannot return from (or do a long jump out of) the function that called vfork! This would leave the parent process's control information very confused. If in doubt, use fork instead.

Some operating systems don't really implement vfork. The GNU C library permits you to use vfork on all systems, but actually executes fork if vfork isn't available. If you follow the proper precautions for using vfork, your program will still work even if the system uses fork instead.


 
 
  Published under the terms of the GNU General Public License Design by Interspire