进程与线程 Processes and Threads

这一周课讲到了进程与线程,并且有相应的教材要读。这一篇目前就作为一个阅读总结,后续可能会搜集网络上的资料进行补充。

阅读材料:Modern Operating Systems – 4th Edition Chapter 2

进程 Process

The Process Model

Four programs in memory

Four independent, sequential processes

At any given instant only one process is actually running.

Process Creation

Four proncipal Events cause processes to be created:

  1. System initialization.
  2. Execution of a process-creation system call by a running process.
  3. A user request to create a new process.
  4. Initiation of a batch job.

Process Termination

Four conditions that the process will terminate:

  1. Normal exit (voluntary).
  2. Error exit (voluntary).
  3. Fatal error (involuntary).
  4. Killed by another process (involuntary).

Process Hierarchies

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.

Process States

Three states:

  1. Running (actually using the CPU at that instant).
  2. Ready (runnable; temporarily stopped to let another process run).
  3. 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

Modeling Multiprogramming

What would happen if an interrupt occurs:

线程 Threads

Thread Usage

  1. They share an address space and all of its data.
  2. They are lighter weight than processes, so they are easier to create and destroy.
  3. 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.
  4. 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.

POSIX Threads

Some of the Pthreads function calls.

About the Author: 雪球

一个在读的工科研究生 一个努力追赶时代脚步的人 Github: https://github.com/xueqiwang0v0 LinkedIn: https://www.linkedin.com/in/xueqi-wang-0939b51a6/

发表评论

邮箱地址不会被公开。 必填项已用*标注