桦山涧

桦山涧
Asp.net ---->知识改变命运!
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

关于进程和线程『整理』

Posted on 2006-10-10 20:28  桦林  阅读(450)  评论(0编辑  收藏  举报
程序包含了若干进程,每一个进程包含了一个或多个概念上知执行的线程。

线程是进程的执行单元。当进程被初始化后,主线程就被创建了。对于绝大多数的应用程序来说,通常仅要求有一个主线程。尽管如此进程也可以创建额外的线程。

每一个用户进程拥有自己私有的地址空间,也就是说,进程拥有一定的可被其访问的内存区域。一个用户进程不可以直接访问其他进程的地址空间。另外还有一个特殊的进程,内核进程,它运行在超级用户权限模式。这个进程通常包括两个线程:
Kernel server (内核服务器)线程:是一个初始的进程,在系统启动时就已经存在。它可以在heap执行由核心函数请求的内存分配或内存的重分配。这是系统中具有最高权限的线程。
null (空)线程:当系统中没有其他可运行的线程时这个线程就开始运行,null 线程使处理器处于空闲状态,减少耗电。

线程是独立运行的,它且并不知道进程中还有其他线程存在。线程的执行是抢占式的,也就是说,当前运行的线程在任何时候都可能被挂起,以便另外一个线程可以运行。

每一个线程都设置了优先限权;在任何时候,只要线程已经准备就绪,具有高优先权的线程总是首先运行。如果线程具有相同的悠闲权,则根据时间片进行轮转调度。上下文的切换包括了保存和恢复线程状态。这个状态不仅仅包含了处理器寄存器(进程上下文)而且还包含了线程可访问的地址空间(进程上下文)。只有在重新调度是在两个进程间进行的时候,进程上下文才被切换。

把这个与活动对象比较,活动对象允许在一个线程中实现非强占式的多任务调度。

一个线程可以被挂起,唤醒、异常抛出和结束其他线程。

线程被创建以后,它处于挂起状态。它没有马上进入运行状态,直到它的Resume()成员函数被调用。

线程创建以后,它具有EPriorityNormal的默认优选级。线程被初始化并处于挂起状态,这意味着在线程开始运行前,线程的运行优先级可以被改变(通过调用RThread::SetPriority()) 。

原文如下:
Introduction to threads and processes
Programs consist of a number of processes, each of which contains one or more conceptually concurrent threads of execution.
A thread is the unit of execution within a process. Every time a process is initialised, a primary thread is created. For many applications the primary thread is the only one that the application requires; however, processes can create additional threads.
Each user process has its own private address space, i.e. a collection of memory regions which that process can access. A user process cannot directly address memory areas in the address space of another process. There is also a special process, the Kernel process, whose threads run at supervisor privilege level. This process normally contains two threads:
the Kernel server thread, which is the initial thread whose execution begins at the reset vector, and which is used to implement all Kernel functions requiring allocation or deallocation on the Kernel heap. This is the highest priority thread in the system.
the null thread, which runs only when no other threads are ready to run. The null thread places the processor into idle mode to save power.
Threads execute individually and are unaware of other threads in a process. The scheduling of threads is pre-emptive, i.e. a currently executing thread may be suspended at any time to allow another thread to run.
Each thread is assigned a priority; at any time, the thread running is the highest priority thread which is ready to run. Threads with equal priority are time-sliced on a round-robin basis. Context switching between threads involves saving and restoring the state of threads. This state includes not only the processor registers (the thread context) but also the address space accessible to the thread (the process context). The process context only needs switching if a reschedule is between threads in different processes.
Compare this with active objects which allow non pre-emptive multi-tasking within a single thread.
A thread can suspend, resume, panic and kill another thread.
When a thread is created it is put into a suspended state, it does not begin to run until that thread’s Resume() member function is called.
When a thread is created, it is given the priority EPriorityNormal by default. The fact that a thread is initially put into a suspended state means that the thread priority can be changed (RThread::SetPriority()) before the thread is started (RThread::Resume()).