# 2021-04-06 `ilkclord` ## 測驗 `1` ## 測驗 `2` ```clike 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. */ pthread_cond_wait(&q->non_empty ,&q->head_lock); // prevent empty dequeue /*start poping*/ old_head = q->tail ; q->tail->pre->next = NULL ; fd = old_head->fd pthread_mutex_unlock(q->head_lock); free(old_head); } ``` 1 . greeter 控管連接伺服器的連接 ,並控制單位時間內的連線數 worker_routine 將資料傳輸倒伺服器並寫入檔案 2 . 利用 pthread_cond_wait(,) 和 pthread_cond_signal( , )的組合確保dequeue不會在空元素的狀況下操作 . ## 測驗 `3` ### Struct of Ring Buffer ```clike typedef struct { struct { /** Ring producer status. */ uint32_t watermark; /**< Maximum items before EDQUOT. */ uint32_t size; /**< Size of ring. */ uint32_t mask; /**< Mask (size - 1) of ring. */ volatile uint32_t head, tail; /**< Producer head and tail. */ } prod __attribute__((__aligned__(CACHE_LINE_SIZE))); struct { /** Ring consumer status. */ uint32_t size; /**< Size of the ring. */ uint32_t mask; /**< Mask (size - 1) of ring. */ volatile uint32_t head, tail; /**< Consumer head and tail. */ } cons __attribute__((__aligned__(CACHE_LINE_SIZE))); void *ring[] __attribute__((__aligned__(CACHE_LINE_SIZE))); } ringbuf_t; ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up