阅读材料：Modern Operating Systems – 4th Edition Chapter 2
The Process Model
Four programs in memory
Four independent, sequential processes
At any given instant only one process is actually running.
Four proncipal Events cause processes to be created:
- System initialization.
- Execution of a process-creation system call by a running process.
- A user request to create a new process.
- Initiation of a batch job.
Four conditions that the process will terminate:
- Normal exit (voluntary).
- Error exit (voluntary).
- Fatal error (involuntary).
- Killed by another process (involuntary).
In UNIX, a process and all of its children and further descendants together form a process group. For example, a special process, called init, is present in the boot image. When it starts running, it reads a file telling how many terminals there are. Then it forks off a new process per terminal. These processes wait for someone to log in. If a login is successful, the login process executes a shell to accept commands. These commands may start up more processes, and so forth. Thus, all the processes in the whole system belong to a single tree, with init at the root.
In contrast, Windows has no concept of a process hierarchy. All processes are equal. The only hint of a process hierarchy is that when a process is created, the parent is given a special token (called a handle) that it can use to control the child.
- Running (actually using the CPU at that instant).
- Ready (runnable; temporarily stopped to let another process run).
- Blocked (unable to run until some external event happens).
Transition 1 occurs when the operating system discovers that a process cannot continue right now. Transitions 2 and 3 are caused by the process scheduler, a part of the operating system, without the process even knowing about them. Transition 4 occurs when the external event for which a process was waiting (such as the arrival of some input) happens.
Here the lowest level of the operating system is the scheduler, with a variety of processes on top of it. All the interrupt handling and details of actually starting and stopping processes are hidden away in what is here called the scheduler.
Implementation of Processes
To implement the process model, the operating system maintains process control blocks. This entry contains important information about the process’ state, including its program counter, stack pointer, memory allocation, the status of its open files, its accounting and scheduling infor- mation, and everything else about the process that must be saved when the process is switched from running to ready or blocked state so that it can be restarted later as if it had never been stopped.
Key fields in a typical system
What would happen if an interrupt occurs:
- They share an address space and all of its data.
- They are lighter weight than processes, so they are easier to create and destroy.
- Performance gain. They yield no performance gain when all of them are CPU bound, but when there is a substantial computing and also substantial I/O, having threads allows these activities to overlap, thus speeding up the application.
- They are useful on systems with multiple CPUs.
The Classical Thread Model
Left: Three processes each with one thread
Right: One process with three threads
The following is a list that is divided by whether the item is private to each thread.
What we are trying to achieve with the thread concept is the ability for multiple threads of execution to share a set of resources so that they can work together closely to perform some tasks.
Each thread has its own stack.
Some of the Pthreads function calls.