如何用Peterson's解決方案讓兩個行程同步執行 ???
如何用Peterson's解決方案讓兩個行程同步執行 ???
請問要如何做才能融合Pi 的架構令其可以與生者者行程與消費者行程的程式碼可以同步執行 !??
不是要來求全部執行程式碼 = =... 只是想懂這個問題要如何解決的觀念 ^^
如果可以請用下面三小段程式碼要怎編排讓兩個行程能同步執行!!
do{
flag=TRUE;
true=j;
while(flag[j] && true==j);
flag=FALSE;
}while(TRUE);
上面小程式碼是 Peterson's 解決方案中行程Pi的架構
while(true){
/*produce an item in nextproduced*/
while(counter==BUFFER_SIZE)
; /* do nothing */
buffer[in] = nextProduced;
in = (in+1) % BUFFER_SIZE;
counter++;
}
上面是生產者行程的程式碼
while(true){
while(counter==0)
; /* do nothing */
nextConsumed = buffer[out];
out = (out+1) % BUFFER_SIZE;
counter--;
/*consume the item in nextConsumed*/
}
上面是使用者行程的程式碼