[Robelle] [SmugBook] [Index] [Prev] [Next]

Creating a Process

In modern operating systems, processes and threads are the minimum units of execution.

On MPE you call the CREATEPROCESS routine. The CREATEPROCESS routine can both load and initiate execution of a new process. CREATEPROCESS allocates completely new data structures for the son process, including stack, heap, and links to the code in a specific program file. The son inherits few of the attributes of the father process.

In UNIX and POSIX you call fork() and then exec() to create a process. When you fork it clones a copy of your current process, including all data, code, environment variables, and open files. This child process is a duplicate of the parent (except for a few details). fork() returns the process ID of the child to the parent process and a zero to the child process (remember, both are now executing). The child may then call exec() to replace itself with the code of a different program (if that was your goal). The new child process will have the same files open as the parent, except those whose close-on-exec flag was set with fcntl.

In pseudo-code, this is:

    result := fork()
    if result < 0
       #fork() failed
    elseif result > 0
       #this is the Parent
    elseif
       #result=0, this is the Child
       #call exec() to "replace myself" with another prog
       exec()
       #control will Never return here from exec()
       #instead if goes to the new program code
    endif

In Windows NT, you can use the POSIX fork/exec calls to create a new process. Alternatively, you can use the CREATEPROCESS call which loads a new program and starts execution. This function is also available in Windows 95.

Process creation tends to be fast in UNIX and POSIX and slower in Windows NT and MPE. Program designs for UNIX often use new processes for many tasks which would be function calls on MPE. As well, the UNIX fork function allows the new process to inherit all the open files and resources of the parent, which the MPE CREATEPROCESS does not.

UNIX, POSIX, and Windows NT have a form of "light process" known as a Thread. A thread is an independent unit of execution within a process. MPE is acquiring threads too, as part of the DCE project. A sequential process has a single flow of control, a sequence of instructions executed by the process. In a multithreaded process, there are multiple scheduled flows of control called threads. By splitting a problem into smaller pieces, a thread can run each piece concurrently, if you have a Symmetric Multiprocessor machine. Without threads the programming strategy is to use multiple processes, communicate using some form of IPC (Inter-Process Communication). With threads you don't have the overhead of IPCs because all the threads of a process share the same data space and system resources.


[Robelle] [SmugBook] [Index] [Unix] [Prev] [Next]