# 2021-04-06 `Jings1017` contributed by < `Jings1017` > ###### tags: `linux2021` ## 測驗 2 ### dequeue( ) ```c= static void dequeue(queue_t *q, int *fd) { node_t *old_head; pthread_mutex_lock(q->head_lock); /* Wait until signaled that queue is non_empty. * Need while loop in case a new thread manages to steal the queue * element after the waiting thread is signaled, but before it can * re-acquire head_lock. */ // Wait until signaled that queue is non_empty. while (!q->size) pthread_cond_wait(q->non_empty, q->head_lock); old_head = q->head->next; if (!old_head->next) q->tail = q->head; q->head->next = old_head->next; *fd = old_head->fd; q->size--; pthread_mutex_unlock(q->head_lock); free(old_head); } ``` ## 問題 1. 執行緒函式 greeter_routine 和 worker_routine 在上述網頁伺服器中,分別負責什麼功能? greeter_routine : receive the HTTP request worker_routine : parse the request,回傳相對應的訊息。 2. enqueue 和 dequeue 是單向鏈結串列 (singly-linked list) 所實作的佇列 (queue) 裡頭的基本操作,你如何驗證自己的實作能在並行環境正確無誤? 利用 mutex lock 來對每個 queue 進行存取上的限制,以達到並行下可以正確無誤。