0

私は Java で NACHOS フェーズ 3 プロジェクト (キャッシングと仮想メモリ) を行っています。以下の関数を実装する際にいくつかの混乱があります。

/**
 * Restore the state of this process after a context switch. Called by
 * UThread.restoreState()
 */

public void restoreState() {
    // Invalidate all TLB entries;
    for(int i=0; i < Machine.processor().getTLBSize(); i++){
        Lib.debug(dbgVM, "Invalidating TLB on context switch.");
        TranslationEntry entry = Machine.processor().readTLBEntry(i);
        entry.valid = false;
        Machine.processor().writeTLBEntry(i, entry);
    }

    syncPageTable();
}

/**
 * Called when the process is context switched in. Synchs the process
 * pagetable with the global one so that read/writeVirtualMemory calls
 * can proceed as they would normally in the UserProcess class.
 */
private void syncPageTable(){
    for(TranslationEntry e : pageTable){
        TranslationEntry f = vmk.lookupAddress(super.getPid(), e.vpn);
        if(f == null || f.valid == false){
            e.valid = false;
        }else if(f != null){
            f.valid = true;
        }
    }
}

ここで、vmk = (VMKernel)Kernel.kernel; . syncPageTable()関数を理解していません。for 句内のTranslationEntry e : pageTableの意味と、if-else ブロックによって実際にチェックされているのは何ですか?

4

1 に答える 1

0
for( TranslationEntry e : pageTable ) { /* block */ }

これは「for each」ステートメントと呼ばれます。pageTable 内の各 TranslationEntry を反復処理し、ブロック内のステートメントを実行します。現在のエントリの名前は「e」です。詳細については、javadoc を参照してください。

形式化するには:

for( <type> <variable-name> : <name of the container containing <type> entries> ) {}

ブロック内のステートメントに関して: 関数は aまたはlookupAddress()のいずれかを返すように見えます。最初の部分が true の場合、ステートメントの 2 番目の部分は評価されないため、最初に確認する必要があります。に対してこのテストを省略すると、ヌル ポインタ例外が発生します。TranslationEntrynullnull||nullf.valid

今: else if( f != null ): f が null の場合、または f が無効な場合、最初の if ステートメントは false です。どのケースが発生したかを知るには、その逆をテストする必要があります。テストすることもできますif( f.valid == true )が、前と同様に、null ポインター例外が発生する可能性があります。したがって、これが本当に尋ねるのは次のとおりif( f != null && f.valid == true )です。

于 2015-12-18T11:01:24.747 に答える