Processes and Threads

Author Avatar
GeniusFunny 12月 09, 2018
  • 在其它设备中阅读本文章

Process and Thread are most important concepts in Operating System.

Processes and Threads

Process

Concept

In process model, all the runnable software on the computer, sometimes include operating system, is organized into a number of sequential process.

A process is just an instance of an executing program, including the current vlaues of program counter, registers, and variables.

A CPU can really run only one process at a time.

In traditional operating systems, each process has an address space and a single thread of control.

In many situations , it is desirable to have multiple threads of control in the same address space running in quasi-parallel, as though they were (almost) separate processes(except for the shared address spaces).

Process Creation

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

Daemons: Processes that stay in the background to handle some activity.

Fork: this system call creates an exact clone of the calling process. After the fork, the two processes, the parent and the child, have the same memory image, the same environment strings, and the same open files.

Process Termination

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

Process Hierarchies

In UNIX, a process and all of its children and further descendants together from a process group.

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

  1. Running.(actually using the CPU at that instant)
  2. Ready. (runnable; temporatily stopped to let another process run)
  3. Blocked.(unable to run until some external event happens)

Implementation of Processes

Process Table: contains per process, each 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 infomation, 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.

Skeleton of what the lowest level of the operating system does when an interrupt occurs:

  1. Hardware stacks program counter, etc.
  2. Hardware loads new program counter from interrupt vector.
  3. Assembly-language procedure saves registers.
  4. Assembly-language procedure sets up new stack.
  5. C interrupt service runs (typically reads and buffers input).
  6. Scheduler decides which process is to run next.
  7. C procedure returns to the assembly code.
  8. Assembly-language procedure starts up new current process.

A process may be interrputed thousands of times during its execution, but the key idea is that after each interrupt the interrupted process returns to precisely the same state it was in before the interrupt occurred.

Thread

Concept

A thread can be reguard as a miniprocess.

Why would anyone want to have threads?

  1. Only now with threads we add a element: the ability for parallel entities to share an address space and all of its data, which is why having multiple processes (with their separate address spaces) will not work.
  2. Threads are lighter weight than processes, they are easier to create and destory than process.
  3. Threads yield no performance gain when all of then are CPU bound, but when where is substantial computing and also substantial I/O, having threads allows these activities to overlap, thus speeding up the application.

The Classical Thread Model

The process model is based on two independent concepts: resource grouping and execution.

A process has a thread of execution, usually shortened to just thread.

Processes are used to group resources together; threads are the entities scheduled for executing on the CPU.

Implementing Threads in User Space

Advantage :

  1. When a thread is finished running for the moment, for example, when it calls thread yield, the code of
    thread yield can save the thread’s information in the thread table itself. Furthermore, it can then call the thread scheduler to pick another thread to run. The procedure that saves the thread’s state and the scheduler are just local procedures, so invoking them is much more efficient than making a kernel call. Among other issues, no trap is needed, no context switch is needed, the memory cache need not be flushed, and so on. This makes thread scheduling very fast.
  2. They allow each process to have its own customized scheduling algorithm.

Problem:

  1. How implement blocking system call?
  2. if a thread starts running, no other thread in that process will ever run unless the first thread voluntarily gives up the CPU.

Implementing Threads in the Kernel

  1. All calls that might block a thread are implemented as system calls, at consid-
    erably greater cost than a call to a run-time system procedure.
  2. Kernel threads do not require any new, nonblocking system calls.

While kernel threads solve some problems, they do not solve all problems.

Hybird Implementations

Using kernel-level threads and then multiplex user-level threads onto some or all of them.With this approach, the kernel is aware of only the kernel-level threads and schedules those. Some of those threads may have multiple user-level threads multiplexed on top of them.

Scheduler Activations

The goals of the scheduler activation work are to mimic the functionality of kernel threads, but with the better performance and greater flexibility usually associated with threads packages implemented in user space.

When scheduler activations are used, the kernel assigns a certain number of virtual processors to each process and lets the (user-space) run-time system allocate threads to processors.

The basic idea that makes this scheme work is that when the kernel knows that a thread has blocked (e.g., by its having executed a blocking system call or caused a page fault), the kernel notifies the process’ run-time system, passing as parameters on the stack the number of the thread in question and a description of the event that occurred. The notification happens by having the kernel activate the run-time system at a known starting address, roughly analogous to a signal in UNIX. This mechanism is called an upcall.