<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 多線程編程:復習題 > 原文:<https://github.com/angrave/SystemProgramming/wiki/Multi-threaded-Programming%3A-Review-Questions> > 警告 - 問題編號可能會有變化 ## Q1 以下代碼是否是線程安全的?重新設計以下代碼是線程安全的。提示:如果消息內存對每個調用都是唯一的,則不需要互斥鎖。 ```c static char message[20]; pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; void format(int v) { pthread_mutex_lock(&mutex); sprintf(message, ":%d:" ,v); pthread_mutex_unlock(&mutex); return message; } ``` ## Q2 以下哪一項不會導致進程退出? * 從最后一個運行線程中的 pthread 的啟動函數返回。 * 從 main 返回的原始線程。 * 任何導致分段錯誤的線程。 * 任何調用`exit`的線程。 * 在主線程中調用`pthread_exit`,其他線程仍在運行。 ## Q3 寫下將由以下程序打印的“W”字符數的數學表達式。假設 a,b,c,d 是小的正整數。你的答案可能會使用'min'函數返回其最低值的參數。 ```c unsigned int a=...,b=...,c=...,d=...; void* func(void* ptr) { char m = * (char*)ptr; if(m == 'P') sem_post(s); if(m == 'W') sem_wait(s); putchar(m); return NULL; } int main(int argv, char** argc) { sem_init(s,0, a); while(b--) pthread_create(&tid, NULL, func, "W"); while(c--) pthread_create(&tid, NULL, func, "P"); while(d--) pthread_create(&tid, NULL, func, "W"); pthread_exit(NULL); /*Process will finish when all threads have exited */ } ``` ## Q4 完成以下代碼。以下代碼應該打印交替`A`和`B`。它代表兩個輪流執行的線程。將條件變量調用添加到`func`,以便等待的線程不需要連續檢查`turn`變量。問:是否需要 pthread_cond_broadcast 或者 pthread_cond_signal 是否足夠? ```c pthread_cond_t cv = PTHREAD_COND_INITIALIZER; pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER; void* turn; void* func(void* mesg) { while(1) { // Add mutex lock and condition variable calls ... while(turn == mesg) { /* poll again ... Change me - This busy loop burns CPU time! */ } /* Do stuff on this thread */ puts( (char*) mesg); turn = mesg; } return 0; } int main(int argc, char** argv){ pthread_t tid1; pthread_create(&tid1, NULL, func, "A"); func("B"); // no need to create another thread - just use the main thread return 0; } ``` ## Q5 確定給定代碼中的關鍵部分。添加互斥鎖定以使代碼線程安全。添加條件變量調用,以使`total`永遠不會變為負數或高于 1000.相反,調用應該阻塞,直到可以繼續進行。解釋為什么`pthread_cond_broadcast`是必要的。 ```c int total; void add(int value) { if(value < 1) return; total += value; } void sub(int value) { if(value < 1) return; total -= value; } ``` ## Q6 非線程安全數據結構具有`size()` `enq`和`deq`方法。使用條件變量和互斥鎖來完成線程安全的阻塞版本。 ```c void enqueue(void* data) { // should block if the size() would become greater than 256 enq(data); } void* dequeue() { // should block if size() is 0 return deq(); } ``` ## Q7 您的啟動使用最新的交通信息提供路徑規劃。您的多付實習生創建了一個非線程安全的數據結構,其中包含兩個函數:`shortest`(使用但不修改圖形)和`set_edge`(修改圖形)。 ```c graph_t* create_graph(char* filename); // called once // returns a new heap object that is the shortest path from vertex i to j path_t* shortest(graph_t* graph, int i, int j); // updates edge from vertex i to j void set_edge(graph_t* graph, int i, int j, double time); ``` 為了提高性能,多個線程必須能夠同時調用`shortest`,但是當`shortest`或`set_edge`內??沒有其他線程執行時,只能通過一個線程修改圖形。 使用互斥鎖和條件變量來實現讀寫器解決方案。不完整的嘗試如下所示。雖然這種嘗試是線程安全的(因此足以用于演示日!),但它不允許多個線程同時計算`shortest`路徑并且沒有足夠的吞吐量。 ```c path_t* shortest_safe(graph_t* graph, int i, int j) { pthread_mutex_lock(&m); path_t* path = shortest(graph, i, j); pthread_mutex_unlock(&m); return path; } void set_edge_safe(graph_t* graph, int i, int j, double dist) { pthread_mutex_lock(&m); set_edge(graph, i, j, dist); pthread_mutex_unlock(&m); } ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看