Processes and Threads

The term task (as in multitasking) refers to a hardware-defined task state. While OS/2 supports multitasking, it does not directly use the concept of a task as defined by the Intel 80386 processor architecture. Instead, OS/2 makes a differentiation between processes and threads.

Processes

A process is most easily defined as a program executing in the system. Since it is possible for a single program to be invoked multiple times in a multitasking system such as OS/2, multiple processes may be executing the same program, and each such process is known as an execution instance of the program. A process owns system resources such as threads, file handles etc, and a memory map that describes the region of memory owned by that process. Since each process owns its own resources and memory map, which are administered by the operating system on behalf of the process, the resources of one process are protected from access by any other process. In situations where communication between processes is required, OS/2 provides a number of architected mechanisms by which they may be achieved. These mechanisms are described in Interprocess Communication and Synchronization.

Threads

A thread is the unit of dispatching for the operating system's scheduler, and therefore equates closely with the notion of an 80386 task as defined by the processor architecture. Each thread is owned by a process, and a single process may have multiple threads. When a process is created, one thread (known as the primary thread ) is always created to run the code specified in the process creation system call. Thus a process always has at least one thread. Secondary threads are often used to perform lengthy operations such as document formatting, remote communications etc, thereby allowing the primary thread to continue interaction with the user. Secondary threads may be created and terminated at any time during execution of a process. When the primary thread of a process is terminated, the process itself terminates.

A thread executes for a short period of time before the operating system's scheduler preempts the thread and gains control. The scheduler may then determine that there is some other thread that ought to run; if so, the scheduler saves the task state of the current thread and dispatches the new thread, which executes for a period of time until it too is preempted and control returns to the scheduler.

OS/2 Version 2.0 supports up to 4096 threads within the system. Note that this limit includes those threads used by the operating system and by applications executing under operating system control, such as the print spooler. The number of threads available to applications will therefore be somewhat less than 4096.

Since each thread is owned by a process, all threads within that process share the resources and memory map belonging to that process, and thus have access to those resources. OS/2 does not protect memory resources from being accessed by multiple threads within the same process; this is the responsibility of the application developer. However, OS/2 provides a number of architected mechanisms to aid the application developer in maintaining the integrity of the application's resources.


[Back: Application Support]
[Next: Interprocess Communication and Synchronization]