匿名使用者
匿名使用者 發問時間: 電腦與網際網路程式設計 · 1 0 年前

OS行程合作的問題(老手,高手或研究生,工程師回答為佳)

我們知道在行程合作方面有生產者和消費者的問題,

擔任生產者的行程產生資訊給擔任消費者的行程消耗.

例如: MP3撥放器中的解碼器是生產者而撥放聲音的模組是消費者.

當生產者和消費者同時執行時,需要緩衝區給生產者存放產品,以提供消費者使用.

兩者同時執行時,生產者與消費者之間的動作必須同步.

以下有三段code:

第一段code:

//buffer(緩衝區)

#define BUF_SIZE n

int iIn = 0, iOut = 0;

user_def_type buffer[BUF_SIZE];

其中緩衝區使用一個大小為BUF_SIZE的陣列buffer,

iIn代表緩衝區內下一個空的位置,即生產者下一個可存放資料的位置,

iOut代表緩衝區內最先可用資料的位置.

當iIn=iOut時,代表緩衝區是空的;而當(iIn+1)%BUF_SIZE=iOut時,代表緩衝區已滿.

第二段code:

//producer(生產者)

do{

...

/*產生一個型別為 user_def_type 的資料並存放於nextp*/

...

while((iIn + 1) % BUF_SIZE == iOut); //若緩衝區已滿則等待

buffer[iIn] = nextp;

iIn = (iIn + 1) % BUF_SIZE;

}

while(FALSE);

其中 nextp 為生產者行程內的區域變數.

第三段code:

//consumer(消費者)

do{

while(iIn == iOut); //若緩衝區為空則等待

nextc = buffer[iOut];

iOut = (iOut + 1) % BUF_SIZE;

...

/*將存於 nextc 的資料消耗掉*/

...

}

while(FALSE);

其中 nextc 為消費者行程內的區域變數.

我想問的問題是為何以上do-while的while的括號中會是FALSE?

這樣的意義是???

已更新項目:

to laog8:

那個......謝謝你的回答,不過我想你誤會了我的問題.

我當然知道大寫的FALSE和小寫false有何不同.

看第二段和第三段code的內容,若把do-while(FALSE)這個迴圈拿掉,執行的結果沒什麼差別,不是嗎?

我想問的問題是那個do-while(FALSE)

在OS中的producer與consumer問題裡是擔任什麼角色呢?

一定有其存在的價值吧,在OS中是怎麼運作的呢?

2 個已更新項目:

to 龍:

我起初和你的想法差不多,可是書本上給的code只有這樣,我想一定是有什麼有關OS的行程合作的概念我不知道.

後面有幾個另外的code也都是do-while(FALSE)中有code.

3 個已更新項目:

若以Linux和UNIX的角度想呢?

4 個已更新項目:

to 龍:

我沒用過java,

看了那個執行緒範例的cubbyhole應該像是buffer還有同步的功用.問一下:

try {

wait();

} catch (InterruptedException e) { }

那個java的wait();和try-catch是做什麼的?和例外中斷有關?

另外notifyAll();的功能?

2 個解答

評分
  • 1 0 年前
    最佳解答

    FALSE is a Microsoft pre-defined macro. In face, if you don't include Windows.h header, your compiler won't even recognize 'FALSE' and 'TRUE'. It is not the same as the boolean 'false' (note the lower case). FALSE basically means '0', while TRUE means '1'. It is just an integer value. Therefore having FALSE or TRUE in the do-while is same as:

    // case (1)

    do

    {

    // some code

    }while(1);

    // case (2)

    do

    {

    // some code

    }while(0);

    The only difference is that the first case will result in infinite loop unless user has a break keyword some where in the loop. Case 2 will only excute the do-while once. So it is unnecessary to use do-while in this case as either case (with or without do-while) result in your code running only once.

    2006-11-15 16:05:26 補充:

    yeah, i misunderstood your questions. sorry about that. but do...while(FALSE) still doesn't make sense. I agree with what 'Dragon' stated about it being a typo, or it is predefined to 1 (which doesn't make sense as well), so do...while(FALSE) == do...while(1).

    2006-11-15 16:06:33 補充:

    i assume the wait() function is same as idle() in Windows, where the OS rest and wait for incoming messages. and if a message has been received during wait(), it invokes the exception and OS gets out of wait() to execute whatever the incoming messages requested.

    2006-11-15 16:07:38 補充:

    for example, when you leave your computer unattended, wait() funciton is being called every frame. when users presses a key on the keyboard, an exception is thrown and caught by the catch(InterruptedException e), then OS moves on to process the key input message, then a letter is printed

    2006-11-15 16:08:08 補充:

    I assume notifyAll() is similar to broadcast function in Windows where a component send out a message to all component who is listening, this case is rare since not all components need to know about this message.

  • Lv 7
    1 0 年前

    看起來那個FALSE應是TRUE才對.不過你的程式碼沒刊全所以不能亂下斷言.比如說iIn,iOut和buffer這些變數應被一個mutex保護.所以當consumer和producer在存取之前應先進入mutex.在用元之後應退出mutex.你若無mutex保護這三個變數的話那整個程式就有問題.

    2006-11-13 22:34:20 補充:

    就像laog8所講的一樣,有do-while(FALSE)跟沒有do-while(FALSE)一樣.從你刊出的程式來看那個do-while(FALSE)應是do-while(TREUE).這樣producer才能一直produce.consumer才能一直consume.

    不過如果這程式碼之間有goto的話那就另當別論了.在不然就是FALSE被重新#define了.

    這裡有個producer和consumer的例子.雖然是用Java寫的.不過原理大致相同.producer會produce出10的item.consumer會consume進10的item.

    2006-11-13 22:34:26 補充:

    http://www.imsc.res.in/Computer/local/Docs/Java/es...

還有問題?馬上發問,尋求解答。