【正文】
段代碼的修改都可能影響全局; ? 正確性難以保證:操作系統(tǒng)或并發(fā)程序通常很大,很難保證這樣一個(gè)復(fù)雜的系統(tǒng)沒有邏輯錯(cuò)誤; 2. 管程的引入 1973年, Hoare和 Hanson所提出;其基本思想是把信號(hào)量及其操作原語封裝在一個(gè)對象內(nèi)部。 其中: 信號(hào)量 mutex用于互斥,保證任意時(shí)刻只能有一個(gè)進(jìn)程讀寫緩沖區(qū)和相關(guān)的變量,保證了臨界區(qū)、臨界資源的合理安全使用。 consumer_item ( item ) 。 up( amp。 up ( amp。mutex ) 。full ) 。 } } Void consumer(void) { int item。 up( amp。 up ( amp。mutex ) 。empty ) 。item ) 。 Void producer(void) { int item。 semaphore full=0。define N 100 //緩沖區(qū)內(nèi)槽數(shù) Typedef int semaphore。 } V原語通常喚醒進(jìn)程等待隊(duì)列中的頭一個(gè)進(jìn)程 利用信號(hào)量實(shí)現(xiàn)互斥 為臨界資源設(shè)置一個(gè)互斥信號(hào)量 mutex(MUTual Exclusion),其初值為 1;在每個(gè)進(jìn)程中將臨界區(qū)代碼置于 P(mutex)和 V(mutex)原語之間 必須成對使用 P和 V原語:遺漏 P原語則不能保證互斥訪問,遺漏 V原語則不能在使用臨界資源之后將其釋放(給其他等待的進(jìn)程); P、 V原語不能次序錯(cuò)誤、重復(fù)或遺漏 V(mutex)。 //表示釋放一個(gè)資源; if ( = 0) //表示有進(jìn)程處于阻塞狀態(tài); { 從等待隊(duì)列 P。 阻塞調(diào)用進(jìn)程 。 if ( 0) //表示沒有空閑資源 。 } P原語 wait(s) 。 } } Struct binary_semaphore { enmu ( zero , one ) value。 else { remove a process P from 。 block this process。 二元信號(hào)量的 P V操作 Void waitB (binary_semaphore s) { if ( == 1 ) =0。 place process P on ready list。 } } Void signal(semaphore s) { + + 。 if ( 0 ) { place this process in 。 queueType queue。 除了以上三種操作以外,沒有任何其他地方可以檢查或操作信號(hào)量。 為了通過信號(hào)量 s接收信號(hào),進(jìn)程可執(zhí)行原語 wait (s)。 原理:兩個(gè)或多個(gè)進(jìn)程可以通過簡單的信號(hào)進(jìn)行合作,一個(gè)進(jìn)程可以被迫在某一個(gè)位置停止,直到他接受到一個(gè)特定的信號(hào)。 導(dǎo)致的問題:類 Spooler目錄問題 緩沖區(qū)為空,消費(fèi)者檢查,得到 count=0,但未睡覺的時(shí)候,被調(diào)度程序掛起,此時(shí)它將保留一個(gè)局部變量來存儲(chǔ) count的值,此時(shí)生產(chǎn)者開始工作并向緩沖區(qū)內(nèi)放入資源,當(dāng)資源等于 1后,向消費(fèi)者發(fā)送喚醒信號(hào),而此時(shí)消費(fèi)者沒有睡眠,等消費(fèi)者被恢復(fù)以后,檢查自己存儲(chǔ)過的 count,發(fā)覺它等于零,則開始睡眠,而生產(chǎn)者則不停生產(chǎn),當(dāng)緩沖區(qū)滿了以后自己也開始睡眠,這時(shí)候他們就都處于睡眠狀態(tài)。 count=count1。 if (count==0) sleep ( )。 count=count+1。 if (count== N) sleep ( )。 等待使用資源的消費(fèi)者 共享的緩沖區(qū) 生產(chǎn)者 define N 100 int count=0。 生產(chǎn)者和消費(fèi)者問題: 問題描述: 若干進(jìn)程通過有限的共享緩沖區(qū)交換數(shù)據(jù)。 **進(jìn)程的優(yōu)先級有差別:當(dāng)有兩個(gè)進(jìn)程, A、 B, A的優(yōu)先級高,處于就緒狀態(tài),而此時(shí), B在臨界區(qū)內(nèi),由于優(yōu)先級低,故無法被調(diào)度,也就無法離開臨界區(qū),那 A就只能夠在臨界區(qū)外等待。這個(gè)讀寫的過程是個(gè)原子操作,是不可分割的。 硬件手段 ( TSLtest and set lock ) 原子操作:這種操作在運(yùn)行的過程中,是不能也不允許被打斷的,它是操作的最底層的部分,是不可分割的,稱其為原子操作。 /*noncritical area*/ } } Flag [0] Flag [1] 初始值為 false Peterson算法是正確的算法 turn=j。amp。 turn=1。turn==0) /*wait*/ /*critical area*/ flag[0]=false。 while(flag[1]amp。 Void P0 ( ) { while( TRUE) { flag[0]= TRUE。 flag[1] = false。 turn = 0。 critical section。 flag[1] = true。 while (turn = = 0)。 flag[1] = false。 flag[1] = true。參考別的書籍。 異常 : (此時(shí)雖然不會(huì)競爭,但浪費(fèi)了資源 ) 如果進(jìn)程 A的非臨界區(qū)很快能執(zhí)行完,而進(jìn)程 B的非臨界區(qū)卻非常慢,則會(huì): turn=1 turn=0 turn=1 Waiting ( turn ) = 1 A B t t t 此時(shí), 兩個(gè)進(jìn)程的完成時(shí)間的衡量就應(yīng)該按照慢的那個(gè)來衡量 ,而且,如果任何一個(gè)進(jìn)程死掉了,則進(jìn)程則會(huì)永遠(yuǎn)堵塞下去,不管是在臨界區(qū)內(nèi)還是在臨界區(qū)外。故稱作忙等待 busy waiting turn==1 正常情況 假如兩個(gè)進(jìn)程: 首先 A進(jìn)程進(jìn)入,使用臨界區(qū)后,修改 turn的值,然后進(jìn)入非臨界區(qū),然后 B進(jìn)程進(jìn)入,根據(jù)判斷,發(fā)覺可以進(jìn)入,就進(jìn)入臨界區(qū),執(zhí)行完后,修改臨界區(qū)的值,在然后進(jìn)入自己的非臨界區(qū)。 } 初始值: turn=0。 turn=0。 } While ( TRUE ) { while ( turn !=1 )。 turn=1。 While ( TRUE ) { while ( turn !=0 )。 臨界區(qū) 鎖 Process 1 Process 2 Process 3 但是矛盾依然有,跟 Spooler目錄一樣。 !! We shouldn’t let the process keep on waiting out of the critical area. Exclusion of busy waiting 忙等待形式的互斥 ** Close interrupt(關(guān)中段) the simplest way, when the process entered the critical area. It will close the interrupt, when it is finished, it will open the interrupt again。 !! We shouldn’t do any supposition to CPU’s speed and number。 互斥的定義: 用某種手段,避免多個(gè)進(jìn)程訪問同一個(gè)臨界區(qū)的操作叫做互斥。我們把這類資源稱作臨界資源,使用臨界資源的那一部分程序稱作成程序的臨界區(qū)。 In+1=8, B will think his job is finished。 2. Interruption: A blocked, and B came in。 We must make sure the processes can’t effect each other when they are visiting the critical area。 To B: these threads share the same address space And these threads can write the information to the same place ,the same file. So the table will can be used to threads,then the threads can be restore、 block 、 ready …. Meaning : all the program can use the resources more efficiently, improved the OS’s performance. To different OS, the problems are different. 1) OS don’t know the existence of threads The control of threads will by in user space. ? Then many threads packages are there. eg Pthreads 2) OS know the existence of threads the kernel will create the table to maintain the threads, threads table. The difference of the two kind of OS The first one The second Threads switched quickly 線程完全在用戶空間進(jìn)行管理 Threads switched slow, the kernel has to judge the process and the other process One thread blocked , the kernel may be blocked. (to flashget, when the work is broken, all the threads of this process will waste a lot of resource. One thread blocked , the kernel may choose another one to run. Conclusion: Get them together, adopt them two. 3) The new problems !! Parentprocess amp。 A B To A: when sever is busy , it will stopped, the writing will stopped. save the destination to …. To B: when server is busy, other threads will still try.. the writing won’t stopped. use flashget ant 進(jìn)程和線程的區(qū)別 每個(gè)進(jìn)程項(xiàng) 每個(gè)線程項(xiàng) 地址空間 全局變量 打開文件 子進(jìn)程 定時(shí)器 信號(hào)和信號(hào)處理程序 統(tǒng)計(jì)信息 程序計(jì)數(shù)器 寄存器 堆棧 狀態(tài) 允許在同一個(gè)進(jìn)程環(huán)境中有多個(gè)執(zhí)行流(線程),這些流在很大程度上相對獨(dú)立,但共享相同的地址空間。 site: the images folder lies D:\site\images process: build up one connection threads: build up more connection 在瀏覽器內(nèi)設(shè)立多個(gè)進(jìn)程,同時(shí)請求傳輸多幅圖像,可節(jié)省建立和釋放鏈接的時(shí)間。 Running Blocked Ready 1 2 3 4 1. Process blocks for input 2. Scheduler picks another process 3. Scheduler picks this process 4. Input bees available Three States of process, and four transition are there 1 2 3 … … n3 n2 n1 … Scheduler User process Disk process Terminal process In this model, we don’t care about the interrupt Implementation of Processes **P