A process is an basic element to allocate resource from system.
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.
非正式的讲，进程就是一个执行的程序。 但是 ，进程不只是简单的程序。他包括有当前状态，一个进程通常包括有用来保存临时数据的进程栈，例如函数的参数，返回地址以及局部变量等等）以及包含全局变量的数据区
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
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.
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:
struct process *L
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)
add this process to S.L
void signal(semaphore S)
remove a process P from S.L
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读者写者问题
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.
Semaphore mutex, wrt;
wrting is performed
readign is performed.
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.