{"id":56,"date":"2024-03-09T15:05:31","date_gmt":"2024-03-09T07:05:31","guid":{"rendered":"http:\/\/104.46.213.183\/?p=56"},"modified":"2024-03-09T15:05:31","modified_gmt":"2024-03-09T07:05:31","slug":"lab-multithreading","status":"publish","type":"post","link":"https:\/\/www.yewpo.top\/index.php\/56\/","title":{"rendered":"Lab Multithreading"},"content":{"rendered":"<h1>Lab Multithreading<\/h1>\n<blockquote>\n<p>\u5b9e\u9645\u8fd0\u7528\u591a\u7ebf\u7a0b<\/p>\n<\/blockquote>\n<h2>1 Uthread: switching between threads (moderate)<\/h2>\n<blockquote>\n<p>\u5728Xv6\u91cc\u5b9e\u73b0\u7ebf\u7a0b\u8c03\u5ea6<\/p>\n<\/blockquote>\n<h3>1.1 struct thread<\/h3>\n<p>\u5728\u8be5\u5b9e\u9a8c\u4e2d\uff0c\u5df2\u7ecf\u5b9a\u4e49\u597d\u4e86\u7ebf\u7a0b\u7ed3\u6784\u4f53\uff1a<\/p>\n<pre><code class=\"language-C\">struct thread {\n  char       stack[STACK_SIZE]; \/* the thread&#039;s stack *\/\n  int        state;             \/* FREE, RUNNING, RUNNABLE *\/\n};<\/code><\/pre>\n<p>stack\u4e3a\u8be5\u7ebf\u7a0b\u8fd0\u884c\u65f6\u7684\u6808\uff0c\u800cstatus\u8868\u793a\u8be5\u7ebf\u7a0b\u7684\u72b6\u6001\uff0c<code>FREE, RUNNING, RUNABLE<\/code>\u3002<\/p>\n<p><strong>\u8be5\u5b9e\u9a8c\u5e0c\u671b\u4f60\u5728\u8be5stack\u4e0a\u8fd0\u884c\u8be5\u7ebf\u7a0b<\/strong><\/p>\n<h3>1.2 create thread<\/h3>\n<p>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u7ebf\u7a0b\u7ed3\u6784\u4f53\uff0c\u5c06\u8be5\u7ebf\u7a0b\u7684\u72b6\u6001\u8bbe\u7f6e\u4e3afree\uff1b<\/p>\n<h3>1.3 Switch thread<\/h3>\n<p>\u5982\u540c\u8fdb\u7a0b\u4e00\u6837\uff0c\u7ebf\u7a0b\u4e5f\u6709\u81ea\u5df1\u7684\u72b6\u6001\uff0c\u5728\u5207\u6362\u4e4b\u524d\uff0c\u6211\u4eec\u8981\u4fdd\u5b58\u4e0a\u4e00\u4e2a\u7ebf\u7a0b\u7684\u8f6c\u6001\uff0c\u7136\u540e\u5c06\u8fdb\u7a0b\u8f6c\u6001\u5207\u6362\u81f3\u4e0b\u4e00\u4e2a\u7ebf\u7a0b\u72b6\u6001\u3002<\/p>\n<h4>1.3.1 save registers<\/h4>\n<p>\u6211\u4eec\u9700\u8981\u4fdd\u7559<code>ra<\/code>\u5bc4\u5b58\u5668\uff0c<code>ra<\/code>\u4f5c\u4e3a\u4e86\u51fd\u6570\u8fd4\u56de\u65f6\u7684\u76ee\u6807\u6307\u4ee4\u5730\u5740\uff0c\u6211\u4eec\u9700\u8981\u4fdd\u5b58ra\u3002<\/p>\n<p>\u5728\u662f\u5426\u9700\u8981\u4fdd\u5b58\u8fd9\u4e2a\u5bc4\u5b58\u5668\u7684\u65f6\u5019\uff0c\u6211\u60f3\u4e86\u8fd9\u4e9b\u95ee\u9898\uff1a<\/p>\n<ul>\n<li>\u4fdd\u5b58\u8be5\u5bc4\u5b58\u5668\u7684\u64cd\u4f5c\u662f\u5728<code>thread_schedule<\/code>\u51fd\u6570\u4e2d\u8fdb\u884c\u7684\uff0c\u5373\u4f7f\u4fdd\u5b58\u4e86ra\u5bc4\u5b58\u5668\uff0c\u5230\u65f6\u5019\u51fd\u6570\u8fd4\u56de\u65f6\u8fd8\u662f\u56de\u5230\u4e86\u8be5\u51fd\u6570\uff0c\u597d\u50cf\u6ca1\u6709\u8d77\u4f5c\u7528\u5440\uff1f<\/li>\n<\/ul>\n<p>\u4f46\u662f\u7ebf\u7a0b\u7684\u6808\u7528\u7684\u662f\u81ea\u5df1\u7684\u6808\uff0c\u5207\u6362\u7ebf\u7a0b\u4e5f\u4f1a\u5207\u6362\u6808\uff0c\u6240\u4ee5\u5373\u4f7f\u56de\u5230\u8fd9\u4e2a\u51fd\u6570\u4e0a\u7ee7\u7eed\u6267\u884c\uff0c\u5728\u6808\u4e0a\u4fdd\u5b58\u7684\u51fd\u6570\u8fd4\u56de\u5730\u5740\u5c31\u4f1a\u5f15\u5bfc\u8be5\u51fd\u6570\u56de\u5230\u6211\u4eec\u5e0c\u671b\u7ebf\u7a0b\u6267\u884c\u7684\u51fd\u6570\u4e2d\u3002<\/p>\n<ul>\n<li>\u7ebf\u7a0b\u5f00\u59cb\u65f6\u5982\u4f55\u8bbe\u7f6era\u3002<\/li>\n<\/ul>\n<p>\u7b2c\u4e00\u4e2a\u7ebf\u7a0b\u51fd\u6570\u4f1a\u5728\u7b2c\u4e00\u6b21\u8c03\u7528<code>thread_schedule<\/code>\u51fd\u6570\u65f6\u8fdb\u884c\u7ebf\u7a0b\u5207\u6362\u7684\u65f6\u5019\u5c31\u8fd4\u56de\u5230ra\u4fdd\u5b58\u7684\u5730\u5740\u4e0a\u3002\u6240\u4ee5\uff0c\u6211\u4eec\u5728\u521b\u5efa\u8fdb\u7a0b\u7684\u65f6\u5019\uff0c\u5c31\u5c06\u7ebf\u7a0b\u4fdd\u5b58\u7684ra\u5bc4\u5b58\u5668\u503c\u8bbe\u7f6e\u4e3a\u51fd\u6570\u7684\u8d77\u59cb\u5730\u5740\u3002<\/p>\n<p>\u7efc\u4e0a\uff0c\u4fdd\u5b58ra\u5bc4\u5b58\u5668\u662f\u6709\u5fc5\u8981\u7684\u3002<\/p>\n<h4>1.3.2 save callee registers<\/h4>\n<p>\u9996\u5148\u770b\u770b<code>callee register<\/code>\u548c<code>caller register<\/code>\u7684\u82f1\u6587\u63cf\u8ff0<\/p>\n<pre><code>Caller-saved registers (AKA volatile registers, or call-clobbered) are used to hold temporary quantities that need not be preserved across calls.<\/code><\/pre>\n<p>\u8c03\u7528\u8005\u4fdd\u7559\u5bc4\u5b58\u5668\u5b58\u653e\u7684\u6682\u65f6\u7684\u503c\uff0c\u8fd9\u4e9b\u503c\u4e0d\u4f1a\u5728\u8c03\u7528\u4e4b\u95f4\u4f20\u9012\uff0c\u610f\u601d\u4f1a\u88ab\u7834\u574f\uff0c\u6240\u4ee5<code>caller-saved registers<\/code>\u6211\u4eec\u53ef\u4ee5\u4e0d\u4fdd\u5b58\u3002<\/p>\n<pre><code class=\"language-\\\">Callee-saved registers (AKA non-volatile registers, or call-preserved) are used to hold long-lived values that should be preserved across calls.<\/code><\/pre>\n<p>\u88ab\u8c03\u7528\u8005\u4fdd\u7559\u5bc4\u5b58\u5668\u662f\u88ab\u8c03\u7528\u4fdd\u62a4\u7684\uff0c\u4ed6\u4f1a\u5728\u8c03\u7528\u4e4b\u95f4\u4fdd\u5b58\uff0c\u5e76\u4e14\u957f\u65f6\u95f4\u7684\u5b58\u5728\u3002\u6240\u4ee5\uff0c\u6211\u4eec\u8981\u4fdd\u7559\u8fd9\u4e9b\u5bc4\u5b58\u5668\u3002<\/p>\n<p>\u4e3a\u4e86\u4fdd\u7559\u8fd9\u4e9b\u5bc4\u5b58\u5668\uff0c\u6211\u4eec\u5b9a\u4e49\u4e00\u4e2a<code>pthread_context<\/code>\u7ed3\u6784\u4f53\uff0c\u5e76\u4e14\u5728\u7ebf\u7a0b\u7ed3\u6784\u4f53\u4e2d\u58f0\u660e\u4e00\u4e2a\u3002<\/p>\n<pre><code class=\"language-c\">struct thread_context {\n  uint64 ra;\n  uint64 sp;\n  uint64 s0;\n  uint64 s1;\n  uint64 s2;\n  uint64 s3;\n  uint64 s4;\n  uint64 s5;\n  uint64 s6;\n  uint64 s7;\n  uint64 s8;\n  uint64 s9;\n  uint64 s10;\n  uint64 s11;\n};<\/code><\/pre>\n<h4>1.3.4 switch.S<\/h4>\n<p>\u5728\u6c47\u7f16\u7ea7\u522b\u4e0a\uff0c\u6211\u4eec\u5b9e\u73b0\u5bf9\u5bc4\u5b58\u5668\u7684\u4fdd\u7559\u3002<\/p>\n<pre><code class=\"language-assembly\">.text\n\n# thread_switch(struct thread_context *old, struct thread_context *new);\n\n.globl thread_switch\nthread_switch:\n    sd ra, 0(a0)\n    sd sp, 8(a0)\n    sd s0, 16(a0)\n    sd s1, 24(a0)\n    sd s2, 32(a0)\n    sd s3, 40(a0)\n    sd s4, 48(a0)\n    sd s5, 56(a0)\n    sd s6, 64(a0)\n    sd s7, 72(a0)\n    sd s8, 80(a0)\n    sd s9, 88(a0)\n    sd s10, 96(a0)\n    sd s11, 104(a0)\n\n    ld ra, 0(a1)\n    ld sp, 8(a1)\n    ld s0, 16(a1)\n    ld s1, 24(a1)\n    ld s2, 32(a1)\n    ld s3, 40(a1)\n    ld s4, 48(a1)\n    ld s5, 56(a1)\n    ld s6, 64(a1)\n    ld s7, 72(a1)\n    ld s8, 80(a1)\n    ld s9, 88(a1)\n    ld s10, 96(a1)\n    ld s11, 104(a1)\n\n    ret # return ra;<\/code><\/pre>\n<h3>1.4 init sp<\/h3>\n<p>\u5728\u521b\u5efa\u7ebf\u7a0b\u7684\u65f6\u5019\uff0c\u5c06sp\u8d4b\u503c\u4e3astack\u7684\u5730\u5740\u3002<\/p>\n<p><strong>its buggy<\/strong><\/p>\n<h3>1.5 run &amp; debug<\/h3>\n<p>\u8fd0\u884c\u8be5\u7a0b\u5e8f\uff0c\u7136\u540e\u53d1\u73b0\u628a100\u6b21\u7684C\u7ebf\u7a0b\u8fd0\u884c\u5b8c\u4e86\uff0c\u4ee5\u4e3a\u6b63\u786e\uff0c\u7136\u540e\u53d1\u73b0a\uff0cb\u7ebf\u7a0b\u6839\u672c\u6ca1\u8dd1\u5b8c\u3002\u4f46\u662f\u600e\u4e48\u56de\u4e8b\u5462\uff1f<\/p>\n<p>\u4e3a\u4e86\u65b9\u4fbf\u6211\u4eec\u8c03\u8bd5\uff0c\u6211\u4eec\u8bbe\u7f6e<code>CPUS=1<\/code>\u7684\u53c2\u6570\u3002<\/p>\n<p>\u8c03\u8bd5\u540e\u53d1\u73b0\uff0c\u5f53C\u7ebf\u7a0b\u6267\u884c\u5b8c\u5faa\u73af\u7684\u7b2c\u4e00\u6b65\u7684\u64cd\u4f5c\u4e4b\u540e\uff0c\u7ebf\u7a0bA\u548cB\u7684status\u88ab\u4fee\u6539\u4e86\u3002<\/p>\n<p>\u7136\u540e\u8c03\u8bd5\u53d1\u73b0\uff0c\u5f53\u7ebf\u7a0b\u5728\u8fd0\u884c\u7684\u65f6\u5019\uff0c\u8fd0\u884c\u7684\u65f6\u5019\u6808\u7684\u5185\u5bb9\u5e76\u6ca1\u6709\u771f\u6b63\u7684\u4fdd\u5b58\u5728\u7ebf\u7a0b\u6240\u5728\u7684\u6808\u4e2d\u3002<\/p>\n<h4>1.5.1 \u6ca1\u6709\u5728\u81ea\u5df1\u7684\u6808\u91cc\u8dd1\u7684\u539f\u56e0<\/h4>\n<p>\u4f17\u6240\u5468\u77e5\uff0c\u7a0b\u5e8f\u7684\u6808\u6307\u9488\u662f\u5411\u8d1f\u65b9\u5411\u589e\u957f\u7684\uff0c\u538b\u6808\u662f\u505a\u5f39\u64cd\u4f5c\uff0c\u5f39\u6808\u662f\u52a0\u64cd\u4f5c\u3002\u6240\u4ee5\uff0c\u51fa\u95ee\u9898\u7684\u5730\u65b9\u5728\u8fd9\u91cc\uff1a<\/p>\n<p>\u7ebf\u7a0b\u521d\u59cb\u5316\u7684\u65f6\u5019\uff0c\u6307\u9488\u521d\u59cb\u5316\u7684\u4f4d\u7f6e\u5728\u7ebf\u7a0b\u6808\u7684\u5f00\u5934\u3001\u53c8\u56e0\u4e3a\u5728C\u8bed\u8a00\u4e2d\uff0c\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\u662f\u5730\u5740\u589e\u52a0\u7684\u884c\u4e3a\uff0c\u548c\u6808\u6307\u9488\u884c\u4e3a\u6070\u597d\u76f8\u53cd\uff0c\u6240\u4ee5\u6808\u6307\u9488\u5728\u505a\u51cf\u6cd5\u7684\u65f6\u5019\u5c31\u8bbf\u95ee\u5230\u4e86\u5176\u4ed6\u7ebf\u7a0b\u7ed3\u6784\u4f53\u4e2d\u7684\u5143\u7d20\u4e86\u30020.0<\/p>\n<h4>1.5.2 \u4fee\u6539<\/h4>\n<p>\u5c06\u6808\u6307\u9488\u521d\u59cb\u5316\u4e3a\u6570\u7ec4\u7684\u672b\u5c3e\u3002<\/p>\n<h3>1.6 finish<\/h3>\n<p>\u8fd0\u884c\u7a0b\u5e8f\uff0c\u80fd\u5f97\u5230\u6b63\u786e\u7684\u7ed3\u679c\u3002<\/p>\n<h2>2 Using threads (moderate)<\/h2>\n<blockquote>\n<p>\u6dfb\u52a0\u7ebf\u7a0b\u9501\uff0c\u9632\u6b62\u591a\u7ebf\u7a0b\u8bbf\u95ee\u5171\u4eab\u53d8\u91cf\u7ade\u4e89<\/p>\n<\/blockquote>\n<p>\u5f53\u591a\u4e2a\u7ebf\u7a0b\u8981\u4fee\u6539\u540c\u4e00\u4e2a\u94fe\u8868\u7684\u65f6\u5019\uff0c\u5c31\u4f1a\u4ea7\u751f\u7ade\u4e89\uff0c\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u7ade\u4e89\uff0c\u4e3a\u6bcf\u4e2a\u54c8\u5e0c\u6876\u51c6\u5907\u4e00\u4e2a\u9501\uff0c\u5f53\u53d1\u751f\u5199\u6216\u8005\u662f\u8bfb\u64cd\u4f5c\u65f6\uff0c\u4f7f\u8be5\u7ebf\u7a0b\u62e5\u6709\u8fd9\u4e2a\u9501\uff0c\u5f53\u64cd\u4f5c\u7ed3\u675f\u7684\u65f6\u5019\uff0c\u91ca\u653e\u8fd9\u4e2a\u9501\uff0c\u4ee5\u4fbf\u5176\u4ed6\u7ebf\u7a0b\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e2a\u54c8\u5e0c\u6876\u3002<\/p>\n<pre><code class=\"language-C\">#include &lt;stdlib.h&gt;\n#include &lt;unistd.h&gt;\n#include &lt;stdio.h&gt;\n#include &lt;assert.h&gt;\n#include &lt;pthread.h&gt;\n#include &lt;sys\/time.h&gt;\n\n#define NBUCKET 5\n#define NKEYS 100000\n\nstruct entry {\n  int key;\n  int value;\n  struct entry *next;\n};\nstruct entry *table[NBUCKET];\nint keys[NKEYS];\nint nthread = 1;\n\npthread_mutex_t lock[NBUCKET];\n\ndouble\nnow()\n{\n struct timeval tv;\n gettimeofday(&amp;tv, 0);\n return tv.tv_sec + tv.tv_usec \/ 1000000.0;\n}\n\nstatic void\ninsert(int key, int value, struct entry **p, struct entry *n)\n{\n  struct entry *e = malloc(sizeof(struct entry));\n  e-&gt;key = key;\n  e-&gt;value = value;\n  e-&gt;next = n;\n  *p = e;\n}\n\nstatic\nvoid put(int key, int value)\n{\n  int i = key % NBUCKET;\n\n  pthread_mutex_lock(&amp;lock[i]);\n\n  \/\/ is the key already present?\n  struct entry *e = 0;\n  for (e = table[i]; e != 0; e = e-&gt;next) {\n    if (e-&gt;key == key)\n      break;\n  }\n  if(e){\n    \/\/ update the existing key.\n    e-&gt;value = value;\n  } else {\n    \/\/ the new is new.\n    insert(key, value, &amp;table[i], table[i]);\n  }\n\n  pthread_mutex_unlock(&amp;lock[i]);\n}\n\nstatic struct entry*\nget(int key)\n{\n  int i = key % NBUCKET;\n\n  pthread_mutex_lock(&amp;lock[i]);\n\n  struct entry *e = 0;\n  for (e = table[i]; e != 0; e = e-&gt;next) {\n    if (e-&gt;key == key) break;\n  }\n\n  pthread_mutex_unlock(&amp;lock[i]);\n\n  return e;\n}\n\nstatic void *\nput_thread(void *xa)\n{\n  int n = (int) (long) xa; \/\/ thread number\n  int b = NKEYS\/nthread;\n\n  for (int i = 0; i &lt; b; i++) {\n    put(keys[b*n + i], n);\n  }\n\n  return NULL;\n}\n\nstatic void *\nget_thread(void *xa)\n{\n  int n = (int) (long) xa; \/\/ thread number\n  int missing = 0;\n\n  for (int i = 0; i &lt; NKEYS; i++) {\n    struct entry *e = get(keys[i]);\n    if (e == 0) missing++;\n  }\n  printf(&quot;%d: %d keys missing\\n&quot;, n, missing);\n  return NULL;\n}\n\nint\nmain(int argc, char *argv[])\n{\n  pthread_t *tha;\n  void *value;\n  double t1, t0;\n\n  if (argc &lt; 2) {\n    fprintf(stderr, &quot;Usage: %s nthreads\\n&quot;, argv[0]);\n    exit(-1);\n  }\n  nthread = atoi(argv[1]);\n  tha = malloc(sizeof(pthread_t) * nthread);\n  srandom(0);\n  assert(NKEYS % nthread == 0);\n  for (int i = 0; i &lt; NKEYS; i++) {\n    keys[i] = random();\n  }\n\n  \/\/initalize lock;\n  for (int i = 0; i &lt; NBUCKET; ++i) {\n      pthread_mutex_init(&amp;lock[i], NULL);\n  }\n\n  \/\/\n  \/\/ first the puts\n  \/\/\n  t0 = now();\n  for(int i = 0; i &lt; nthread; i++) {\n    assert(pthread_create(&amp;tha[i], NULL, put_thread, (void *) (long) i) == 0);\n  }\n  for(int i = 0; i &lt; nthread; i++) {\n    assert(pthread_join(tha[i], &amp;value) == 0);\n  }\n  t1 = now();\n\n  printf(&quot;%d puts, %.3f seconds, %.0f puts\/second\\n&quot;,\n         NKEYS, t1 - t0, NKEYS \/ (t1 - t0));\n\n  \/\/\n  \/\/ now the gets\n  \/\/\n  t0 = now();\n  for(int i = 0; i &lt; nthread; i++) {\n    assert(pthread_create(&amp;tha[i], NULL, get_thread, (void *) (long) i) == 0);\n  }\n  for(int i = 0; i &lt; nthread; i++) {\n    assert(pthread_join(tha[i], &amp;value) == 0);\n  }\n  t1 = now();\n\n  printf(&quot;%d gets, %.3f seconds, %.0f gets\/second\\n&quot;,\n         NKEYS*nthread, t1 - t0, (NKEYS*nthread) \/ (t1 - t0));\n}<\/code><\/pre>\n<p>\u6d4b\u8bd5\uff0c\u7136\u540e\u901a\u8fc7<\/p>\n<h2>3 barrier (moderate)<\/h2>\n<blockquote>\n<p>\u5b9e\u9645\u8fd0\u7528<code>thread_cond_t<\/code>\uff0c\u5b9e\u73b0\u6761\u4ef6\u9501\uff0c\u5f53\u6240\u6709\u7ebf\u7a0b\u90fd\u8fd0\u884c\u8be5\u6761\u4ef6\u70b9\u7684\u65f6\u5019\uff0c\u91cd\u65b0\u6fc0\u6d3b\u6240\u6709\u7ebf\u7a0b\u3002<\/p>\n<\/blockquote>\n<p>\u67e5\u8be2<code>pthread_cond<\/code>\u6587\u6863\uff0c\u83b7\u5f97\u4e0b\u9762\u4e24\u4e2a\u51fd\u6570\u7684\u7528\u6cd5\uff1a<\/p>\n<ul>\n<li>pthread_cond_wait\uff1a\u8be5\u51fd\u6570\u4f1a\u963b\u585e\u6761\u4ef6\u53d8\u91cf\uff0c\u5e76\u91ca\u653e\u8be5\u7ebf\u7a0b\u62e5\u6709\u7684\u9501\uff0c\u4ee5\u4fbf\u5176\u4ed6\u7ebf\u7a0b\u53ef\u4ee5\u4f7f\u7528\u3002\u5982\u679c\u8981\u6062\u590d\uff0c\u5fc5\u987b\u8981\u7ebf\u7a0b\u6765\u5524\u9192\u4ed6\u3002<\/li>\n<li>pthread_cond_signal\u6216pthread_cond_broadcast\uff1a\u4e24\u4e2a\u51fd\u6570\u7684\u4f5c\u7528\u90fd\u662f\u89e3\u9664\u88abwait\u963b\u585e\u7684\u7ebf\u7a0b\uff08\u6761\u4ef6\u9501\u76f8\u540c\uff09\uff0c\u5e76\u4e14\u6062\u590d\u8be5\u7ebf\u7a0b\u62e5\u6709\u7684\u9501\u3002\u533a\u522b\u662f\uff0csignal\u53ea\u662f\u91ca\u653e\u4e00\u4e2a\u6216\u8005\u591a\u4e2a\u7ebf\u7a0b\uff0c\u800cbroadcast\u4f1a\u91ca\u653e\u5168\u90e8\u7ebf\u7a0b\u3002<\/li>\n<\/ul>\n<p>\u865a\u5047\u5524\u9192\uff1a\u5f53\u4f7f\u7528\u6761\u4ef6\u53d8\u91cf\u7684\u65f6\u5019\uff0c\u53ef\u80fd\u5b58\u5728\u865a\u5047\u5524\u9192\u7684\u60c5\u51b5\u3002\u4f8b\u5b50\uff0c\u6709\u4e00\u4e2a\u751f\u4ea7\u8005\uff0c\u751f\u4ea7\u4e00\u4e2a\u4ea7\u54c1\u540e\u8fdb\u884c\u4e86\u4e00\u6b21\u5e7f\u64ad\uff0c\u4f46\u662f\u6b64\u65f6\u6709\u4e09\u4e2a\u6d88\u8d39\u8005\uff0c\u8fd9\u4e09\u4e2a\u6d88\u8d39\u90fd\u88ab\u5524\u9192\u4e86\uff0c\u4f46\u662f\u53ea\u80fd\u6709\u4e00\u4e2a\u4eba\u80fd\u4e70\u8fd9\u4e2a\u4ea7\u54c1\uff1b\u6240\u4ee5\uff0c\u6211\u4eec\u9700\u8981\u7528<code>while<\/code>\u8bed\u53e5\u6765\u68c0\u67e5\u5f53\u524d\u5524\u9192\u662f\u5426\u6ee1\u8db3\u5524\u9192\u6761\u4ef6\u3002<\/p>\n<p>\u4e86\u89e3\u4e86\u8fd9\u4e24\u4e2a\u51fd\u6570\u7684\u989d\u4f5c\u7528\u4e4b\u540e\uff0c\u4ee3\u7801\u4e5f\u975e\u5e38\u597d\u5199\uff0c\u903b\u8f91\u8fc7\u7a0b\u5c31\u4e0d\u518d\u8d58\u8ff0\u4e86\uff0c\u5173\u952e\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-c\">static void\nbarrier()\n{\n  pthread_mutex_lock(&amp;bstate.barrier_mutex);\n  bstate.nthread++;\n  if (bstate.nthread == nthread) {\n    bstate.round++;\n    bstate.nthread = 0;\n    pthread_cond_broadcast(&amp;bstate.barrier_cond);\n  } else {\n    int round = bstate.round;\n    while (round == bstate.round) {\n      pthread_cond_wait(&amp;bstate.barrier_cond, &amp;bstate.barrier_mutex);\n    }\n  }\n  pthread_mutex_unlock(&amp;bstate.barrier_mutex);\n}<\/code><\/pre>\n<p>\u6d4b\u8bd5\u901a\u8fc7\u3002<\/p>\n<h2>4 END<\/h2>\n<p>\u65f6\u9694\u591a\u5929\u65ad\u65ad\u7eed\u7eed\u5b8c\u6210\u4e86\u8fd9\u4e2a\u5b9e\u9a8c\uff0c\u6d4b\u8bd5\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<pre><code>== Test uthread ==\n$ make qemu-gdb\nuthread: OK (4.7s)\n== Test answers-thread.txt == answers-thread.txt: OK\n== Test ph_safe == make[1]: Entering directory &#039;\/home\/ubuntu\/program\/xv6-labs-2022&#039;\ngcc -o ph -g -O2 -DSOL_THREAD -DLAB_THREAD notxv6\/ph.c -pthread\nmake[1]: Leaving directory &#039;\/home\/ubuntu\/program\/xv6-labs-2022&#039;\nph_safe: OK (16.2s)\n== Test ph_fast == make[1]: Entering directory &#039;\/home\/ubuntu\/program\/xv6-labs-2022&#039;\nmake[1]: &#039;ph&#039; is up to date.\nmake[1]: Leaving directory &#039;\/home\/ubuntu\/program\/xv6-labs-2022&#039;\nph_fast: OK (37.4s)\n== Test barrier == make[1]: Entering directory &#039;\/home\/ubuntu\/program\/xv6-labs-2022&#039;\ngcc -o barrier -g -O2 -DSOL_THREAD -DLAB_THREAD notxv6\/barrier.c -pthread\nmake[1]: Leaving directory &#039;\/home\/ubuntu\/program\/xv6-labs-2022&#039;\nbarrier: OK (3.0s)\n== Test time ==\ntime: OK\nScore: 60\/60<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>xv6\u64cd\u4f5c\u7cfb\u7edf\u5b9e\u9a8c6\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[14],"class_list":["post-56","post","type-post","status-publish","format-standard","hentry","category-os","tag-xv6"],"_links":{"self":[{"href":"https:\/\/www.yewpo.top\/index.php\/wp-json\/wp\/v2\/posts\/56","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yewpo.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yewpo.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yewpo.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yewpo.top\/index.php\/wp-json\/wp\/v2\/comments?post=56"}],"version-history":[{"count":0,"href":"https:\/\/www.yewpo.top\/index.php\/wp-json\/wp\/v2\/posts\/56\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yewpo.top\/index.php\/wp-json\/wp\/v2\/media?parent=56"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yewpo.top\/index.php\/wp-json\/wp\/v2\/categories?post=56"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yewpo.top\/index.php\/wp-json\/wp\/v2\/tags?post=56"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}