PROCESS[进程]

abstract:
A process is an basic element to allocate resource from system.

Process concept:[进程的基本概念]

Informally, a process is a program in execution. A process is more than the program code, whitch is sometimes known as the text section. it also includes the current activity. In addition, a process generally includes the process stack, which contains temporary data(such as method parameters, return addresses, and local varirables) , and a data section, which contains global variables.
非正式的讲,进程就是一个执行的程序。 但是 ,进程不只是简单的程序。他包括有当前状态,一个进程通常包括有用来保存临时数据的进程栈,例如函数的参数,返回地址以及局部变量等等)以及包含全局变量的数据区

Process State进程的状态
As a process excutes,it changes state. The state of a process is defined in part by the current activity of that process. Each process may be in one of the following states:
New: The process si being created
Running: instructions are being excuted
Wating:The process is wating for some event to occur(such as an I/O completion or reception of a signal)
Ready:The process is waiting to be assigned to a processor
进程在执行的时候,状态会不断改变。进程的状态由进程当前的活动所决定。每个进程具有如下所示的几个状态之一:
新建:进程正在被新建
运行:进行正在运行
阻塞(等待):进程在等待某个事情的发生(例如I/O操作的完成,或者某个信号灯)
就绪:进程获得了除CPU之外的所有必须资源,只在等待CPU调度


Process Synchronization进程同步
A cooperating process is one that can affect or be affected by other processes executing in the system. Coopearting processes may either directly share a logical address space, or be allowed to share data . so hwo can we apply specific mechanism to ensure the orderly execution of cooperating processes that share a logical address space, so that data consistency is maintained.

The Critical-Section Problem 临界区问题

Consider a system consisting of a n processes(p0,p1,p2...p(n-1)). Each process has a segment of code, called a critical section, in which the process may be changing common variables, updating a table, wrting a file, and so on. The important feature of the system is that, when one process is executing in its critica section, no other process is to be allowed to execute in its critical section. Thus, the execution of cirtical sections by the processes is mutually exclusive.
假设一个系统中包含n个进程,p0,p1,...p(n-1)。 每个进程包含一代码,该代码称为临界区。在临界区中,进程能够改变变量的值,更新表,写文件等等操作。在这样的系统中最重要的特征就是,当一个进程在临界区中执行的时候,其他进程不能够进入到临界区。因此,进程对临界区的执行必须是互斥的。

Solutions:解决方案
Semephores senario:信号灯
A semaphore S is an integer variable that, apart from initialization, is accessed only through two standard atomic operations:wait(P) and signal(V)
we can define a semaphore as a  C struct:
typedef struct{
            int value;
            struct process *L
                    }semaphore;

when a process executes the wait operation and finds that the semaphore value is not positive, it must waiting and block itself. the blcok operation places a process into a waiting queue associated with the semaphore, and the state of the process is switched to the waiting state. then ,control is transferred to the CPU scheduler,which selectes another process to execute.
A process that is blocked, waiting on a semaphore S, should be restarted when some other process excutes a signal operation. The process is retarted by a wakeup operation,which changed the process from the wating state to the ready state. the process is then placed in the ready queue.
therefore,the Wait operation should be defined as following:
void wait(semaphore S)
{
S.value--;
if (S.value<0)
    { 
   add this process to S.L
    block();
    }
}

void signal(semaphore S)
{
S.value++;
if(S.value<=0)
    {
        remove a process P from S.L
        wakeup(P);
    }
}

the block operation suspends the process that invokes it. the wakeup(p) operation resumes the execution of a blocked process P. these two operations are provided by the operating system as basic system calls.

Deadlocks and Starvation死锁和饥饿状态
The implementation of a semaphore with a waiting queue may result in a situation where two or more processes are waiting infinitely for an event that can be caused only by one of the waiting processes. when such a state is reached, these processes are said to be deadlocked. Another problem ralated to deadlocks in indefninite blocking or starvation, a situation where processes wait indefinitely within the semaphore. indefinite blocking may occur if we add and remove processes from the list associated with a semaphore in LIFO order

Classic Problems of Synchronization
 1. producer-cosumer problem.生产者消费者问题
2.The Reader-Writer Problem读者写者问题
    Problem definition:
    A data object(such as a file or record) is to be shared among several concurrent processes. Some of these processes may want only to read the content of the shared object, whearas others may want to update the shared object. we distinguish these two types of processes by referring to those processes that are intested in only reading as readers, and to the rest as writers. Obviously, if two readers access the shareddata object simultaneously, no adverse effects will result. However, if a writer sna some other process access the shared object simultaneously,  consistency may be vilated.
     code inplementation:
    Semaphore mutex, wrt;
    int readercount;
    Writer()
    {
    while(1)
        {
        p(wrt)
        ...
        wrting is performed
        ...
        v(wrt);
        ...
        }
    }

    Reader()
    {
        while(1)
        {
            P(mutex)
            readercounter++;
            if(readercount==1)
                P(wrt);
            V(mutex);
            ...
            readign is performed.
            ...
            P(mutex)
            readercount--;
            if(readercount==0)
            V(wrt)
            V(mutex)
        }
    }

3) The Dining-Philosophers Problem哲学家就餐问题
Consider five philosophers who spend their lives thinking and eating. the philosephers share a comman circular table surrouded by five chairs, each belonging to one philosopher. In the center of the table is a bowl of rice, and the table is laid with five single chopsticks. when a philosephor thinks, she doesnot interact with her colleagues. from time to time.  a philosopher gets hungry and tries to pick up the two chopsticks that are closed to her. Obviously, she cannot pick up a chopstick that is already in the hand of a  neighbour. suppose that all five philosophers become hungry simultaneouly, and each grabs her left chopstick, then all philosopher will be starved to death.

【推广】 免费学中医,健康全家人
原文地址:https://www.cnblogs.com/Winston/p/1191035.html