---
# System prepended metadata

title: 2021-04-06 `shanihsu`

---

# 2021-04-06 `shanihsu`
## 測驗 1
```c=
static void run(char *c, int t)
{
    char *redir_stdin = NULL, *redir_stdout = NULL;
    int pipefds[2] = {0, 0}, outfd = 0;
    char *v[99] = {0};
    char **u = &v[98]; /* end of words */
    for (;;) {
        c--;
        if (is_delim(*c)) /* if NULL (start of string) or pipe: break */
            break;
        if (!is_special(*c)) {
            c++;    /* Copy word of regular chars into previous u */
            **(u--) = *c;  /* 先將 u-- ，得到 previous u 的指標的指標，再取兩次值，得到 u 的值，並將 c 的值 assign  給 **u */
        }
        if (is_redir(*c)) { /* If < or > */
            if (*c == '<')
                redir_stdin = *u;
            else
                redir_stdout = *u;
            if ((u - v) != 98)
                u++;
        }
    }
    if ((u - v) == 98) /* empty input */
```

---

## 測驗 2
```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.
     */
    /* delete node in head of queue */
    old_head = q->head;
    q->head = q->head->next;
    q->size--;
    pthread_mutex_unlock(q->head_lock);
    free(old_head);
}
```
1. 執行緒函式 greeter_routine 和 worker_routine 在上述網頁伺服器中，分別負責什麼功能？


2. enqueue 和 dequeue 是單向鏈結串列 (singly-linked list) 所實作的佇列 (queue) 裡頭的基本操作，你如何驗證自己的實作能在並行環境正確無誤？

---

## 測驗 3
```c=
#define ALIGN_FLOOR(val, align) (((val % align) == 0)? (val) : (val + align - (val % align)))
```

---

## 測驗 4