2017-05-05 10 views
0

私はユーザレベルのスレッドシステムを実装しています。バイナリセマフォ の実装(以下に説明するようにアップ/ダウン機能)を使用して、カウントセマフォの実装に関する助けが必要です。ここ は、バイナリセマフォの私の実装のインタフェースである:バイナリセマフォを使用してカウントセマフォを実装する

typedef enum BinSemStatus{ 
    locked, 
    unlocked 
} BinSemStatus; 


struct semaphore { 
BinSemStatus status; 
int bid; 
}; 

int bsem_alloc();//allocate a new binary semaphore,return its descriptor 
void bsem_free(int id); 
void bsem_down(int id); 
void bsem_up(int id); 

そしてここでは、カウンティングセマフォインタフェースのインタフェースである:

struct counting_semaphore* counting_alloc(uint value); 
counting_free(struct counting_semaphore* sem); 

// If the value representing the count of 
// the semaphore variable is not negative, decrement it by 1. If the 
// semaphore variable is now 
// negative, the thread executing acquire is blocked until the value is 
// greater or equal to 1. 
// Otherwise, the thread continues execution. 
void up(struct counting_semaphore* sem); 
// Increments the value of semaphore 
// variable by 1. 
void down(struct counting_semaphore* sem); 

私は何をしようとしたことはボイドアップである(構造体counting_semaphore * sem) は値をロックします。しかし、以下では十分ではありません。問題のあるケースでコメントを追加しました。

struct counting_semaphore { 
int binary_descriptor; 
int value; 
}; 

void down(struct counting_semaphore *sem){ 
    bsem_down(sem->binary_descriptor); 
    if (sem->value > 0){ 
    sem->value--; 
    } 
    else{ 
    //not sure what to do here, maybe use anather semaphore in some way? 
    } 
    bsem_up(sem->binary_descriptor); 
} 
void up(struct counting_semaphore *sem){ 
    bsem_down(sem->binary_descriptor); 
    sem->value++; 
    bsem_up(sem->binary_descriptor); 
} 
+0

'counting_free(構造体counting_semaphore * SEM);' - > '無効counting_free(構造体counting_semaphore * SEM);' – 4386427

+0

あなたはまだコードを書いて試していないことがありますか? – 4386427

+0

質問は何ですか? –

答えて

0

クリティカルセクションはsem-> binary_descriptor1によって保護され、ダウン(S)およびアップ(S)のそれぞれの操作は、sem.valueの値を正しく更新するために使用されます 両方の操作を更新した後、sem-> binary_descriptor2値が正の場合にのみセマフォー 下位(S)を実行するプロセスがsem-> binary_descriptor2でブロックされるため、S.valueは負ではありません。

struct counting_semaphore* counting_semaphore_alloc(int value) { 

    struct counting_semaphore* sem = malloc(sizeof(struct counting_semaphore)); 
    sem->value = value; 
    sem->binary_descriptor1= bsem_alloc();//locks the value 
    sem->binary_descriptor2= bsem_alloc();//locks the sing of the value 
    if (value <= 0) { 
     bsem_down(sem->binary_descriptor2); 
    } 
    return sem; 
    } 

void down(struct counting_semaphore *sem){ 
    bsem_down(sem->binary_descriptor2); 
    bsem_down(sem->binary_descriptor1); 
    sem->value--; 
    if (sem->value>0){ 
     bsem_up(sem->binary_descriptor2); 
    } 
    bsem_up(sem->binary_descriptor1); 
} 


void up(struct counting_semaphore* sem) { 
    bsem_down(sem->binary_descriptor1); 
    sem->value++; 
    if (sem->value == 1) { 
     bsem_up(sem->binary_descriptor2); 
    } 
    bsem_up(sem->binary_descriptor1); 
} 
0

sem->valueが0、スレッドブロックに達し、再スケジュールする時間です。スケジュールコードを表示していないので、具体的なアドバイスはできません。おそらくスケジューラは、スレッドの代わりにbsem_up(sem->binary_descriptor);を呼び出す必要があります。

関連する問題