3. Process
A process is running instance of a program.
Process memory is divided into four sections
1. The text section comprises the compiled program code, read
in from non-volatile storage when the program is launched.
2. The data section stores global and static variables, allocated
and initialized prior to executing main.
3. The heap is used for dynamic memory allocation, and is
managed via calls to new, delete, malloc, free, etc.
4. The stack is used for local variables. Space on the stack is
reserved for local variables when they are declared ( at
function entrance or elsewhere, depending on the language ),
and the space is freed up when the variables go out of scope.
Note that the stack is also used for function return values, and
the exact mechanisms of stack management may be language
specific.
4. Process State
Processes may be in one of 5 states.
New - The process is in the stage of being created.
Ready - The process has all the resources available that it needs to run, but the CPU is not
currently working on this process's instructions.
Running - The CPU is working on this process's instructions.
Waiting - The process cannot run at the moment, because it is waiting for some resource to
become available or for some event to occur. For example the process may be waiting for
keyboard input, disk access request, inter-process messages, a timer to go off, or a child
process to finish.
Terminated - The process has completed.
5. Process Control Block
For each process there is a Process Control Block, PCB, which stores the following types of
process-specific information
Process State - Running, waiting, etc.
Process ID, and parent process ID.
CPU registers and Program Counter - These need to be saved and restored when
swapping processes in and out of the CPU.
CPU-Scheduling information - Such as priority information and pointers to scheduling
queues.
Memory-Management information - E.g. page tables or
segment tables.
Accounting information - user and kernel CPU time consumed,
account numbers, limits, etc.
I/O Status information - Devices allocated, open file tables,
etc.
6. Threads
A thread is a basic unit of CPU utilization, consisting of a program counter, a stack,
and a set of registers, ( and a thread ID. )
Multi-threaded applications have multiple threads within a single process,
each having their own program counter, stack and set of registers, but sharing
common code, data, and certain structures such as open files.
8. Multithreading Model
There are two types of threads to be managed in a modern system:
User threads
User threads are supported above the kernel, without kernel support. These are the threads that
application programmers would put into their programs.
Kernel threads
Kernel threads are supported within the kernel of the OS itself. All modern OSes support kernel level
threads, allowing the kernel to perform multiple simultaneous tasks and/or to service multiple kernel
system calls simultaneously.
In a specific implementation, the user threads must be mapped to kernel threads, using one of
the following strategies.
Many to One Model
One to One Model
Many to Many Model
11. Linux Threads
Linux does not distinguish between processes and threads
The traditional fork( ) system call completely duplicates a process.
An alternative system call, clone( ) allows for varying degrees of sharing between the parent and child
tasks, controlled by flags such as those shown in the following table:
Calling clone( )with no flags set is equivalent to fork( ). Calling clone( ) with CLONE_FS,
CLONE_VM, CLONE_SIGHAND, and CLONE_FILES is equivalent to creating a thread, as all of
these data structures will be shared.
Linux implements this using a structure task_struct, which essentially provides a level of indirection to
task resources. When the flags are not set, then the resources pointed to by the structure are copied,
but if the flags are set, then only the pointers to the resources are copied, and hence the resources are
shared
Flag Meaning
CLONE_FS File-system information is shared
CLONE_VM The same memory space is shared
CLONE_SIGHAND Signal handlers are shared
CLONE_FILES The set of open files is shared