{"id":47,"date":"2010-04-17T21:26:08","date_gmt":"2010-04-18T06:26:08","guid":{"rendered":"\/blog\/?p=47"},"modified":"2023-09-21T09:41:22","modified_gmt":"2023-09-21T00:41:22","slug":"%ec%83%81%ec%9a%a9-%ec%84%b8%eb%a7%88%ed%8f%ac%ec%96%b4%ec%99%80-%eb%ae%a4%ed%85%8d%ec%8a%a4%ec%9d%98-%ec%98%88","status":"publish","type":"post","link":"https:\/\/hasu0707.duckdns.org\/blog\/?p=47","title":{"rendered":"\uc0c1\uc6a9 \uc138\ub9c8\ud3ec\uc5b4\uc640 \ubba4\ud14d\uc2a4\uc758 \uc608"},"content":{"rendered":"\n<P>[\uc138\ub9c8\ud3ec\uc5b4] [\ubba4\ud14d\uc2a4]<\/P>\n<P>\ub3d9\uae30\ud654\uc5d0 \ud544\uc694\ud55c \uae30\uc220\ub4e4.<\/P>\n<P>\ub370\ub4dc\ub77d\uc774 \uac78\ub838\uc744 \ub54c \uc0ac\uc6a9\ud558\ub294 \uac83\uc774 \uc544\ub2c8\uace0 \uc138\ub9c8\ud3ec\uc5b4\ub098 \ubba4\ud14d\uc2a4\uc5d0 \uc758\ud574 \ub370\ub4dc\ub77d\uc774 \uac78\ub9b4 \uc218\ub3c4 \uc788\uc2b5\ub2c8\ub2e4. \ub530\ub77c\uc11c \uaf2d \ud544\uc694\ud55c \ubd80\ubd84\uc5d0\ub9cc \uc81c\ub300\ub85c \uc0ac\uc6a9\ud574\uc57c\ud569\ub2c8\ub2e4.<\/P>\n<P><br \/>\ubba4\ud14d\uc2a4\ub780 MUTual EXclusion\uc73c\ub85c \uc6b0\ub9ac\ub9d0\ub85c \ud574\uc11d\ud558\uba74 '\uc0c1\ud638 \ubc30\uc81c'\ub77c\uace0 \ud569\ub2c8\ub2e4. \ub9d0 \uadf8\ub300\ub85c \uc0c1\ud638 \ubc30\uc81c\ud574\uc11c \uc2e4\ud589\ud558\ub294 \uac81\ub2c8\ub2e4. \uc784\uacc4 \uad6c\uc5ed\uc744 \uac00\uc9c4 \uc2a4\ub808\ub4dc\ub4e4\uc774 \ub3d9\uc2dc\uc5d0 \uc2e4\ud589\ub418\uc9c0 \uc54a\uace0 \uc11c\ub85c \ubc30\uc81c\ub418\uc5b4 \uc2e4\ud589\ub418\uac8c \ud558\ub294 \uae30\uc220\uc785\ub2c8\ub2e4. \uc5ec\uae30\uc11c \uc784\uacc4\uad6c\uc5ed(Critical Section)\ub780 \ud504\ub85c\uadf8\ub7a8 \uc0c1\uc5d0\uc11c \ub3d9\uc2dc\uc5d0 \uc2e4\ud589\ub420 \uacbd\uc6b0 \ubb38\uc81c\uc744 \uc77c\uc73c\ud0ac \uc218 \uc788\ub294 \ubd80\ubd84\uc744 \ub9d0\ud569\ub2c8\ub2e4. \ub9cc\uc57d \uc5b4\ub290 \uc2a4\ub808\ub4dc\uc5d0\uc11c \uc784\uacc4\uad6c\uc5ed\uc744 \uc2e4\ud589\ud558\uace0 \uc788\uc73c\uba74 \ub2e4\ub978 \uc2a4\ub808\ub4dc\ub4e4\uc740 \uadf8 \uc784\uacc4 \uad6c\uc5ed\uc5d0 \uc811\uadfc\ud560 \uc218 \uc5c6\uace0 \uc55e\uc758 \uc2a4\ub808\ub4dc\uac00 \uc784\uacc4 \uad6c\uc5ed\uc744 \ubc97\uc5b4\ub098\uae30\ub97c \uae30\ub2e4\ub824\uc57c\ud569\ub2c8\ub2e4. \uc774\ub7f0 \ubc29\ubc95\uc774 \ubba4\ud14d\uc2a4\uc785\ub2c8\ub2e4.<\/P>\n<P>\uadf8\ub9ac\uace0 \uc138\ub9c8\ud3ec\uc5b4\ub780 \uc784\uacc4\uad6c\uc5ed\uc5d0 \uc811\uadfc \ud558\uae30 \uc704\ud55c \uc5f4\uc1e0 \uac19\uc740\uac70\ub77c\uace0 \uc0dd\uac01\ud558\uba74 \ub429\ub2c8\ub2e4. \uc608\ub97c \ub4e4\uc5b4 \ud654\uc7a5\uc2e4\uc774 \uc788\ub2e4\uace0 \ud569\uc2dc\ub2e4. \uc774 \ud654\uc7a5\uc2e4\uc5d0 \ub4e4\uc5b4\uac00\uae30 \uc704\ud574\uc11c\ub294 \ubc18\ub4dc\uc2dc \uc5f4\uc1e0\uac00 \uc788\uc5b4\uc57c \ud558\uace0 \uc774 \uc5f4\uc1e0\ub294 \ub2e8 \ud558\ub098 \ubc16\uc5d0 \uc5c6\uc73c\uba70 \ud654\uc7a5\uc2e4\uc5d0\ub294 \ud55c\uba85\uc529\ub9cc \ub4e4\uc5b4\uac08 \uc218 \uc788\uc2b5\ub2c8\ub2e4. \uc774\ub7f4 \ub54c \uc5ec\ub7ec\uba85\uc758 \uc0ac\ub78c\uc774 \ud654\uc7a5\uc2e4\uc744 \uc774\uc6a9\ud558\ub824\uba74 \uc77c\ub2e8 \ud55c\uba85\uc774 \uc5f4\uc1e0\ub97c \uac00\uc9c0\uace0 \ud654\uc7a5\uc2e4\uc744 \uc774\uc6a9\ud558\uace0 \ub098\uc624\uba74 \uadf8 \uc0ac\ub78c\uc5d0\uac8c \uc5f4\uc1e0\ub97c \ubc1b\uc544 \ub2e4\uc74c \uc0ac\ub78c\uc774 \uc774\uc6a9\ud558\uace0 \uc774\ub807\uac8c \ud55c\uc0ac\ub78c\uc529 \ub3cc\uc544\uac00\uba74\uc11c \ud654\uc7a5\uc2e4\uc744 \uc4f8 \uc218 \uc788\uc2b5\ub2c8\ub2e4. \uc774\ub54c \ud654\uc7a5\uc2e4\uc740 \uc784\uacc4\uad6c\uc5ed\uc774 \ub418\uace0 \uc5f4\uc1e0\ub294 \uc138\ub9c8\ud3ec\uc5b4\uac00 \ub418\ub294\uac81\ub2c8\ub2e4. \uadf8\ub9ac\uace0 \ub204\uad70\uac00 \uc5f4\uc1e0\ub97c \uac00\uc9c0\uace0 \ub3c4\ub9dd\uac00\ubc84\ub9b0\ub2e4\uba74 \uc544\ubb34\ub3c4 \ud654\uc7a5\uc2e4\uc744 \uc0ac\uc6a9\ud558\uc9c0 \ubabb\ud558\ub294 \uc0c1\ud0dc\uac00 \ub418\uc5b4\ubc84\ub9bd\ub2c8\ub2e4. \uc774\ub7f0\uac8c \ubc14\ub85c \ub370\ub4dc\ub77d\uc785\ub2c8\ub2e4. \ub370\ub4dc\ub77d\uc744 CPU \uc2a4\ucf00\uc974\ub9c1\uc5d0\uc11c\ub294 \uad50\ucc29\uc0c1\ud0dc\ub77c\uace0\ud569\ub2c8\ub2e4.<\/P>\n<P>&nbsp;<\/P>\n<P>\uc138\ub9c8\ud3ec\uc5b4(SEMAPHORE)<\/P>\n<P>\ud504\ub85c\uadf8\ub798\ubc0d, \ud2b9\ud788 \uc720\ub2c9\uc2a4\uc2dc\uc2a4\ud15c\uc758 \ud504\ub85c\uadf8\ub798\ubc0d\uc5d0\uc11c \uc138\ub9c8\ud3ec\uc5b4\ub294 \uc6b4\uc601\uccb4\uacc4\uc758 \uc790\uc6d0\uc744 \uacbd\uc7c1\uc801\uc73c\ub85c \uc0ac\uc6a9\ud558\ub294 \ub2e4\uc911 \ud504\ub85c\uc138\uc2a4\uc5d0\uc11c, \ud589\ub3d9\uc744 \uc870\uc815\ud558\uac70\ub098 \ub610\ub294 \ub3d9\uae30\ud654 \uc2dc\ud0a4\ub294 \uae30\uc220\uc774\ub2e4. \uc138\ub9c8\ud3ec\uc5b4\ub294 \uc6b4\uc601\uccb4\uacc4 \ub610\ub294 \ucee4\ub110\uc758 \ud55c \uc9c0\uc815\ub41c \uc800\uc7a5\uc7a5\uce58 \ub0b4 \uac12\uc73c\ub85c\uc11c, \uac01 \ud504\ub85c\uc138\uc2a4\ub294 \uc774\ub97c \ud655\uc778\ud558\uace0 \ubcc0\uacbd\ud560 \uc218 \uc788\ub2e4. \ud655\uc778\ub418\ub294 \uc138\ub9c8\ud3ec\uc5b4\uc758 \uac12\uc5d0 \ub530\ub77c, \uadf8 \ud504\ub85c\uc138\uc2a4\uac00 \uc989\uc2dc \uc790\uc6d0\uc744 \uc0ac\uc6a9\ud560 \uc218 \uc788\uac70\ub098, \ub610\ub294 \uc774\ubbf8 \ub2e4\ub978 \ud504\ub85c\uc138\uc2a4\uc5d0 \uc758\ud574 \uc0ac\uc6a9 \uc911\uc774\ub77c\ub294 \uc0ac\uc2e4\uc744 \uc54c\uac8c\ub418\uba74 \uc7ac\uc2dc\ub3c4\ud558\uae30 \uc804\uc5d0 \uc77c\uc815 \uc2dc\uac04\uc744 \uae30\ub2e4\ub824\uc57c\ub9cc \ud55c\ub2e4. \uc138\ub9c8\ud3ec\uc5b4\ub294 \uc774\uc9c4\uc218 (0 \ub610\ub294 1)\ub97c \uc0ac\uc6a9\ud558\uac70\ub098, \ub610\ub294 \ucd94\uac00\uc801\uc778 \uac12\uc744 \uac00\uc9c8 \uc218\ub3c4 \uc788\ub2e4. \uc138\ub9c8\ud3ec\uc5b4\ub97c \uc0ac\uc6a9\ud558\ub294 \ud504\ub85c\uc138\uc2a4\ub294 \uc73c\ub808 \uadf8 \uac12\uc744 \ud655\uc778\ud558\uace0, \uc790\uc6d0\uc744 \uc0ac\uc6a9\ud558\ub294 \ub3d9\uc548\uc5d0\ub294 \uadf8 \uac12\uc744 \ubcc0\uacbd\ud568\uc73c\ub85c\uc368 \ub2e4\ub978 \uc138\ub9c8\ud3ec\uc5b4 \uc0ac\uc6a9\uc790\ub4e4\uc774 \uae30\ub2e4\ub9ac\ub3c4\ub85d \ud574\uc57c\ud55c\ub2e4. <\/P>\n<P>\uc138\ub9c8\ud3ec\uc5b4\ub4e4\uc740 \uc77c\ubc18\uc801\uc73c\ub85c \uba54\ubaa8\ub9ac \uacf5\uac04\uc744 \uacf5\uc720\ud558\uac70\ub098, \ub610\ub294 \ud30c\uc77c\ub4e4\uc744 \uacf5\uc720 \uc561\uc138\uc2a4\ud558\uae30 \uc704\ud55c, \ub450 \uac00\uc9c0 \uc815\ub3c4\uc758 \ubaa9\uc801\uc744 \uc704\ud574 \uc0ac\uc6a9\ub41c\ub2e4. \uc138\ub9c8\ud3ec\uc5b4\ub4e4\uc740 \ud504\ub85c\uc138\uc2a4\uac04 \ud1b5\uc2e0(IPC)\uc744 \uc704\ud55c \uae30\uc220 \uc911\ud558\ub098\uc774\ub2e4. C \ud504\ub85c\uadf8\ub798\ubc0d \uc5b8\uc5b4\ub294 \uc138\ub9c8\ud3ec\uc5b4\ub4e4\uc744 \uad00\ub9ac\ud558\uae30 \uc704\ud55c \uc77c\ub828\uc758 \uc778\ud130\ud398\uc774\uc2a4 \ub610\ub294 \ud568\uc218\ub4e4\uc744 \uc81c\uacf5\ud55c\ub2e4. <br \/><\/P>\n<P>\uc774 \uc138\ub9c8\ud3ec\uc5b4\ub97c \uac00\uc9c0\uace0 \uacbd\uc7c1\uad00\uacc4\uc5d0 \uc788\ub294 \uc790\uc6d0\ub4e4\uc744 \ucda9\ub3cc\uc5c6\uc774 \uc798 \uc0ac\uc6a9\ud558\uae30 \uc704\ud568\uc785\ub2c8\ub2e4. \uacf5\uc720\uc790\uc6d0\uc758 \uacbd\uc6b0 \ub2e4\ub978 \ud504\ub85c\uc138\uc2a4\uac00 \ub3d9\uc2dc\uc5d0 \uc811\uadfc\uc744 \ud55c\ub2e4\uba74, \uc5b4\ub290 \ud504\ub85c\uc138\uc2a4\uc5d0 \uc758\ud574 \ubcc0\uacbd\ub41c \uc790\ub8cc\uac00 \ub098\uc911\uc5d0 \uc218\uc815\ub41c \uc790\ub8cc\uc778\uc9c0 \ubaa8\ub974\uac8c \ub418\ub294 \ubb38\uc81c\uac00 \uc0dd\uae30\uba70, \uc774\ub85c \uc778\ud574 \ub370\uc774\ud130\uc758 \uac01\uc885 \uc624\ub958\uac00 \uc0b0\uc7ac\ud558\uac8c \ub429\ub2c8\ub2e4. \uadf8\ub798\uc11c \uc138\ub9c8\ud3ec\uc5b4 \ub77c\ub294 \uc77c\uc885\uc758 \ubcc0\uc218\ub97c \ub450\uc5b4 \uc81c\uc5b4\ub97c \ud558\uac8c \ub429\ub2c8\ub2e4.<\/P>\n<P>&nbsp;<\/P>\n<P>\uc138\ub9c8\ud3ec\uc5b4<\/P>\n<P>- \ub3d9\uae30\ud654\uc758 \uc77c\ubc18\uc801\uc778 \ubc29\ubc95\uc778 \uc138\ub9c8\ud3ec\uc5b4 \ubc29\ubc95\uc740 \uc138\ub9c8\ud3ec\uc5b4\ub77c\ub294 \uc815\uc218 \ubcc0\uc218(integer variable), \ud504\ub85c\uc138\uc2a4 \ub300\uae30\uc5f4(process waiting queue), P\uc640 V\uc758 \ub450 \uba85\ub839\uc73c\ub85c \uad6c\uc131\ub41c\ub2e4. \ucd08\uae30 \uc0c1\ud0dc\uc758 \ubcc0\uc218\uac12\uc740 \uc790\uc6d0\uc758 \uc218\uc640 \uac19\uc73c\uba70 \ub300\uae30\uc5f4\uc740 \ube44\uc5b4 \uc788\ub2e4. P\uba85\ub839\uc740 \ubcc0\uc218\uc758 \uac12\uc744 \ud558\ub098 \uc904\uc778 \ud6c4, \ubcc0\uc218\uc758 \uac12\uc774 0\ubcf4\ub2e4 \uc791\uc73c\uba74 \ud504\ub85c\uc138\uc2a4\ub97c \ub300\uae30\uc5f4\ub85c \uc9d1\uc5b4 \ub123\ub294\ub2e4. \ubc18\ub300\ub85c 0\ubcf4\ub2e4 \ud06c\uba74 \uadf8 \ud504\ub85c\uc138\uc2a4\ub294 \uacc4\uc18d \uc9c4\ud589\ub41c\ub2e4. V\uba85\ub839\uc740 \ubcc0\uc218\uc758 \uac12\uc744 \ud558\ub098 \uc99d\uac00\uc2dc\ud0a8\ub2e4. \uadf8 \uacb0\uacfc\uac00 0\ubcf4\ub2e4 \ud06c\uba74 \ud504\ub85c\uc138\uc2a4\ub294 \uacc4\uc18d\ub418\uba70 0\ubcf4\ub2e4 \uc791\uc73c\uba74 \ub300\uae30\uc5f4\uc758 \ud504\ub85c\uc138\uc2a4 \ud558\ub098\ub97c \uc900\ube44 \uc0c1\ud0dc\ub85c \ub9cc\ub4e4\uace0, \ud504\ub85c\uc138\uc2a4\uc758 \uc218\ud589\uc740 \uacc4\uc18d\ub41c\ub2e4.&nbsp; \uacb0\uad6d \ubcc0\uc218\uc758 \uac12\uc740 \uc74c\uc218\uc77c \uacbd\uc6b0\ub294 \ub300\uae30 \uc911\uc778 \ud504\ub85c\uc138\uc2a4\uc758 \uc218\ub97c \ub098\ud0c0\ub0b4\uba70, \uc591\uc218\uc774\uba74 \uc0ac\uc6a9 \uac00\ub2a5\ud55c \uc790\uc6d0\uc758 \uc218\ub97c \uac00\ub9ac\ud0a8\ub2e4. \uc704\uc5d0\uc11c \ub3d9\uae30\ud654\ub780 <FONT color=#006699>\ud504\ub85c\uc138\uc2a4\uc758 \uc2e4\ud589\uc744 \uc2dc\uac04\uc5d0 \ub530\ub77c \uc21c\uc11c\uc801\uc73c\ub85c \ucc98\ub9ac\ud558\ub294 \uac83\uc744 \ub3d9\uae30\ud654\ub77c \ud55c\ub2e4<\/FONT>.<\/P>\n<P>- \uc138\ub9c8\ud3ec\uc5b4\ub294 \ub2e4\uc775\uc2a4\ud2b8\ub77c(E.J.Dijkstra)\uac00 \uc81c\uc548\ud55c \ub3d9\uc2dc\uc5d0 \uc815\ubcf4\ub97c \uacf5\uc720\ud558\uc5ec \uc218\ud589\ub418\ub294 \ub450 \uac1c \uc774\uc0c1\uc758 \ud504\ub85c\uadf8\ub7a8\uc774\ub098 \ud504\ub85c\uc138\uc2a4\uc5d0\uc11c \ud65c\ub3d9 (activity)\uc758 \uc704\uce58(coordination)\ub97c \uc124\uc815\ud574 \uc8fc\ub294 \ub370 \uc0ac\uc6a9\ub418\ub294 \ub3d9\uae30\ud654\ub97c \uc704\ud55c \uae30\ubcf8 \uc870\uc791. \uc774\ub294 \ub450\uac1c \uc774\uc0c1\uc758 \ud504\ub85c\uc138\uc2a4\uc5d0 \uc758\ud574 \uacf5\uc720\ub418\ub294 \uace0\uc720\ubcc0\uc218\ub85c \uc815\uc758\ub418\ub294\ub370, \ubcf4\ud1b5\uc758 \ubc29\ubc95\uc73c\ub85c\ub294 \ub2e4\ub8f0 \uc218 \uc5c6\uace0 \ud56d\uc0c1 P\uc640 V\ub77c\ub294 \uc5f0\uc0b0\uc744 \ud1b5\ud574\uc11c\ub9cc \uc561\uc138\uc2a4\ud560 \uc218 \uc788\ub2e4. \uc138\ub9c8\ud3ec\uc5b4 sem\uc774\ub780 \ub2e4\uc74c\uacfc \uac19\uc740 \uc5f0\uc0b0\uc774 \ud5c8\uc6a9\ub41c \uc815\uc218\ud615 \ubcc0\uc218\ub97c \ub9d0\ud55c\ub2e4. (P\uc640 V\ub780 \uc774\ub984\uc740 wait\uc640 signal\uc774\ub780 \ub9d0\uc758 \ub124\ub35c\ub780\ub4dc\uc5b4\uc5d0\uc11c \ub098\uc628\uac83\uc73c\ub85c \uc774\ub54c signal\uc774\ub780 \ubb3c\ub860 UNIX\uc758 signal\ud638\ucd9c\uacfc\ub294 \ub2e4\ub974\ub2e4.) \ub450 \uc5f0\uc0b0\uc740 \ubaa8\ub450 \uc6d0\uc790\ud654\ub418\uc5b4\uc57c \ud55c\ub2e4. \uc989 sem\uc744 \ubcc0\uacbd\ud560\uc218 \uc788\ub294 \ud504\ub85c\uc138\uc2a4\ub294 \ud55c \uc21c\uac04\uc5d0 \uc624\uc9c1 \ud558\ub098 \ubfd0\uc774\ub2e4. <\/P>\n<P>&nbsp;<\/P>\n<P>\ucef4\ud4e8\ud130\uac00 \uc5ec\ub7ec \ud504\ub85c\uadf8\ub7a8\uc744 \ub3d9\uc2dc\uc5d0 \uc218\ud589\ud558\ub294 \ub2e4\uc911 \ud504\ub85c\uadf8\ub798\ubc0d \uc2dc\uc2a4\ud15c\uc5d0\uc11c\ub294 \ud504\ub85c\uc138\uc2a4\ub4e4\uac04\uc758 \uc0c1\ud638 \ubc30\uc81c\uc640 \ub3d9\uae30\ud654\ub97c \uc704\ud55c \uae30\ubcf8\uc801\uc778 \uc5f0\uc0b0\uc774 \ud544\uc694\ud558\ub2e4. \uc138\ub9c8\ud3ec\uc5b4\ub294 \ub2e4\uc775\uc2a4\ud2b8\ub77c\uac00 \uc81c\uc548\ud55c \ud504\ub85c\uc138\uc2a4 \ub3d9\uae30\ud654\ub97c \uc704\ud55c \uad6c\uc870\ub85c, \uc774\ub294 \uc5ec\ub7ec \ud504\ub85c\uc138\uc2a4\ub4e4\uc5d0 \uc758\ud574 \uacf5\uc720\ub418\ub294 \ubcc0\uc218\ub85c \uc815\uc758\ub41c\ub2e4. \uadf8\ub7f0\ub370 \uc774 \ubcc0\uc218\ub294 \ubcf4\ud1b5\uc758 \ubc29\ubc95\uc73c\ub85c\ub294 \uc561\uc138\uc2a4\ud560 \uc218 \uc5c6\uace0 \uc624\uc9c1 P\uc640 V\ub77c\ub294 \uc5f0\uc0b0\uc73c\ub85c\ub9cc \ub2e4\ub8f0 \uc218 \uc788\ub2e4. P\uc640 V\uc5f0\uc0b0\uc758 \uc815\uc758\ub294 \ub2e4\uc74c\uacfc \uac19\ub2e4. <\/P>\n<P>&nbsp;<\/P>\n<P>procedure P(S) &nbsp; --&gt; \ucd5c\ucd08 S\uac12\uc740 1\uc784<\/P>\n<P>&nbsp; &nbsp; while S=0 do wait&nbsp; --&gt; S\uac00 0\uba74 1\uc774 \ub420\ub54c\uae4c\uc9c0 \uae30\ub2e4\ub824\uc57c \ud568<\/P>\n<P>&nbsp; &nbsp; S := S-1 &nbsp; --&gt; S\ub97c 0\ub85c \ub9cc\ub4e4\uc5b4 \ub2e4\ub978 \ud504\ub85c\uc138\uc2a4\uac00 \ub4e4\uc5b4 \uc624\uc9c0 \ubabb\ud558\ub3c4\ub85d \ud568<\/P>\n<P>end P<\/P>\n<P>&nbsp;<\/P>\n<P>procedure V(S) --&gt; \ud604\uc7ac\uc0c1\ud0dc\ub294 S\uac00 0\uc784<\/P>\n<P>&nbsp; &nbsp; S := S+1 &nbsp; --&gt; S\ub97c 1\ub85c \uc6d0\uc704\uce58\uc2dc\ucf1c \ud574\uc81c\ud558\ub294 \uacfc\uc815. \uc774\uc81c\ub294 \ub2e4\ub978 \ud504\ub85c\uc138\uc2a4\uac00 end V \ub4e4\uc5b4 \uc62c\uc218 \uc788\uc74c<\/P>\n<P>&nbsp;<\/P>\n<P>P\uc640 V\ub294 \ucabc\uac24\uc218 \uc5c6\ub294 \ub2e8\uc77c \uc5f0\uc0b0\uc774\ub2e4. \uc989 \ud55c \ud504\ub85c\uc138\uc2a4\uac00 P\ub098 V\ub97c \uc218\ud589\ud558\uace0 \uc788\ub294 \ub3d9\uc548\uc5d0\ub294 \ud504\ub85c\uc138\uc2a4\uac00 \uc778\ud130\ub7fd\ud2b8\ub97c \ub2f9\ud558\uc9c0 \uc54a\ub294\ub2e4. \uc774\uc81c P\uc640 V\ub97c \uc0ac\uc6a9\ud558\uba74 \ub2e4\uc74c\uacfc \uac19\uc774 \uc704\ud5d8\uc9c0\uc5ed(cirtical section)\uc5d0 \ub300\ud55c \uc0c1\ud638\ubc30\uc81c\ub97c \uad6c\ud604\ud560\uc218 \uc788\ub2e4. <\/P>\n<P>&nbsp;<\/P>\n<P>&nbsp; &nbsp; P(S);<\/P>\n<P>&nbsp; &nbsp; -----------------<br \/>&nbsp; &nbsp; | \uc704 \ud5d8 \uc9c0 \uc5ed &nbsp; &nbsp; &nbsp; |<br \/>&nbsp; &nbsp; ----------------- <\/P>\n<P>&nbsp; &nbsp; V(S); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |<br \/><\/P>\n<P>\ucd5c\ucd08\uc5d0 S\uc758 \uac12\uc740 1\uc774\ub2e4. \uc704\uc640 \uac19\uc740 \uc704\ud5d8\uc9c0\uc5ed\uc744 \ud3ec\ud568\ud558\ub294 \ub450\uac1c\uc758 \ud504\ub85c\uc138\uc2a4 A\uc640 B\uac00 \uc788\ub2e4\uace0 \ud558\uc790. A\uc640 B\ub294 \uc11c\ub85c \ub3c5\ub9bd\uc801\uc73c\ub85c \uc218\ud589\ub418\uc9c0\ub9cc, \ub450 \ud504\ub85c\uc138\uc2a4\uac00 \ub3d9\uc2dc\uc5d0 \uc704\ud5d8 \uc9c0\uc5ed\uc73c\ub85c \ub4e4\uc5b4\uac00\uc11c\ub294 \uc548\ub41c\ub2e4. \uc704\uc640 \uac19\uc774 \uc138\ub9c8\ud3ec\uc5b4\ub97c \uc0ac\uc6a9\ud558\uba74 P(S)\ub97c \uba3c\uc800 \uc218\ud589\ud558\ub294 \ud504\ub85c\uc138\uc2a4\uac00 S\ub97c 0\uc73c\ub85c \ud574\ub193\uace0 \uc704\ud5d8\uc9c0\uc5ed\uc5d0 \ub4e4\uc5b4\uac00\ubbc0\ub85c \ub098\uc911\uc5d0 \ub3c4\ucc29\ud558\ub294 \ud504\ub85c\uc138\uc2a4\ub294 P\uc5d0\uc11c \ub354\uc774\uc0c1 \uc9c4\ud589\ub418\uc9c0 \ubabb\ud558\uace0 \uae30\ub2e4\ub9ac\uac8c \ub41c\ub2e4. \uba3c\uc800 \ub4e4\uc5b4\uac14\ub358 \ud504\ub85c\uc138\uc2a4\uac00 V(S)\ub97c \ud574\uc8fc\uc5b4\uc57c \ube44\ub85c\uc11c P(S)\uc5d0\uc11c \uae30\ub2e4\ub9ac\ub358 \ud504\ub85c\uc138\uc2a4\uac00 \uc704\ud5d8\uc9c0\uc5ed\uc5d0 \ub4e4\uc5b4\uac08 \uc218 \uc788\uace0 \ub530\ub77c\uc11c \uc0c1\ud638\ubc30\uc81c\uac00&nbsp; \uc2e4\ud604\ub41c\ub2e4. \uc704\uc758 \uc608\ub294 \uc774\uc9c4 \uc138\ub9c8\ud3ec\uc5b4 (binary semaphore)\ub85c, \ub2e8\uc9c0 \ud558\ub098\uc758 \ud504\ub85c\uc138\uc2a4\ub9cc\uc774 \uc704\ud5d8\uc9c0\uc5ed\uc5d0 \ub4e4\uc5b4\uac08 \uc218 \uc788\ub3c4\ub85d \ud55c\ub2e4. \ud55c\ud3b8 S\uc758 \ucd08\uae30\uac12\uc744 N\uc73c\ub85c \ud558\uba74 \ucd5c\ub300 N\uac1c\uc758 \ud504\ub85c\uc138\uc2a4\uac00 P(S)\ub97c \ud1b5\uacfc\ud560 \uc218 \uc788\uac8c \ub418\ub294\ub370 \uc774\ub7ec\ud55c \uacbd\uc6b0\uc5d0\ub294 \uacc4\uc218 \uc138\ub9c8\ud3ec\uc5b4 (counting semaphore)\ub77c \ud558\uba70 \uc790\uc6d0 \ud560\ub2f9\uc5d0 \uc0ac\uc6a9\ud55c\ub2e4. <\/P>\n<P>UNIX \uc2dc\uc2a4\ud15c V\uc5d0\uc11c \uad6c\ud604\ub41c \uc138\ub9c8\ud3ec\uc5b4\ub294 \uc774\ub7ec\ud55c \uac1c\ub150\uc5d0 \uae30\ucd08\ud588\uc9c0\ub9cc \ubcf4\ub2e4 \uc77c\ubc18\uc801\uc778(\uadf8\ub9ac\uace0 \uc544\ub9c8\ub3c4 \ubcf4\ub2e4 \ubcf5\uc7a1\ud55c)\uae30\ub2a5\uc744 \uc81c\uacf5\ud55c\ub2e4. <\/P>\n<P>\uc6b0\uc120 semget\uacfc semctl\uc744 \uc0b4\ud3b4\ubcf4\uc790.<br \/><\/P>\n<P>&nbsp; &nbsp; &nbsp; &nbsp; &lt;\uc0ac\uc6a9\ubc95&gt;<br \/>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; #include <br \/>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; #include <br \/>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; #include <br \/>&nbsp; &nbsp; <br \/>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; key_t key;<br \/>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; int sem_id, nsems, permflags, command;<br \/>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; int retval, sem_num;<br \/>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; union semun <\/P>\n<P>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; int val;<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; struct semid_ds *stat;<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; ushort *array;<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } ctl_arg;<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .<br \/>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; sem_id = semget(key, nsems, perflags);<br \/>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; retval = semctl(sem_id, sem_num, command, ctl_arg);<\/P>\n<P>&nbsp;<\/P>\n<P>semget \ud638\ucd9c\uc740 msgget(get message queue)\uacfc \uc720\uc0ac\ud558\ub2e4. \uc778\uc218 nsems\ub294 \uc138\ub9c8\ud3ec\uc5b4 \uc9d1\ud569\uc5d0 \ud544\uc694\ud55c \uc138\ub9c8\ud3ec\uc5b4\uc758 \uac2f\uc218\ub97c \ub098\ud0c0\ub0b8\ub2e4. \ub530\ub77c\uc11c UNIX \uc138\ub9c8\ud3ec\uc5b4 \uc5f0\uc0b0\uc740 \uc138\ub9c8\ud3ec\uc5b4 \ud558\ub098\ub9cc\uc774 \uc544\ub2c8\ub77c \ud55c \uc9d1\ud569 \uc804\uccb4\ub97c \ub2e4\ub8e8\uac8c \ub41c\ub2e4. \uc774\ub85c \uc778\ud574\uc11c \ub098\uba38\uc9c0 \uc138\ub9c8\ud3ec\uc5b4 \ub8e8\ud2f4\uc5d0 \ub300\ud55c \uc778\ud130\ud398\uc774\uc2a4\uac00 \ubcf5\uc7a1\ud574\uc9c4\ub2e4. semget\ud638\ucd9c\uc774 \uc131\uacf5\ud558\uba74 \uc138\ub9c8\ud3ec\uc5b4 \uc9d1\ud569 \uc2dd\ubcc4\uc790\ub77c\ub294 \uba54\uc2dc\uc9c0 \ud050 \uc2dd\ubcc4\uc790\uc640 \uc720\uc0ac\ud55c \uc5ed\ud560\uc744 \ud558\ub294 \uac83\uc774 \ub3cc\uc544\uc628\ub2e4. C \uc5b8\uc5b4\uc758 \uad00\uc2b5\uc744 \ub530\ub77c\uc11c \uc138\ub9c8\ud3ec\uc5b4 \uc9d1\ud569\uc5d0 \ub300\ud55c \ucca8\uc790\ub294 0\ubd80\ud130 nsems-1\uae4c\uc9c0 \uc788\uc744 \uc218 \uc788\ub2e4. \uc9d1\ud569 \ub0b4\uc758 \uac01 \uc138\ub9c8\ud3ec\uc5b4\ub294 \ub2e4\uc74c\uacfc \uac19\uc740 \uac12\ub4e4\uc744 \uac16\uac8c \ub41c\ub2e4. <\/P>\n<P>&nbsp; &nbsp; &nbsp;&nbsp; . semval : \uc138\ub9c8\ud3ec\uc5b4\uc758 \uac12\uc73c\ub85c\uc11c \ud56d\uc0c1 \uc591\uc218\uac00 \uc9c0\uc815\ub41c\ub2e4. <br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \uc5ec\uae30\uc5d0 \uac12\uc744 \uc0c8\ub85c \uc9c0\uc815\ud558\ub824\uba74 \ubc18\ub4dc\uc2dc \uc138\ub9c8\ud3ec\uc5b4 \uc2dc\uc2a4\ud15c \ud638\ucd9c\uc744 \ud1b5\ud574\uc57c\ud558\uba70 \ud504\ub85c\uadf8\ub7a8\uc5d0\uc11c \uc77c\ubc18 \uc790\ub8cc\ud615\uc758 \ubcc0\uc218\uc640 \uac19\uc774 \uc9c1\uc811<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \uc811\uadfc \ud560 \uc218\ub294 \uc5c6\ub2e4.<br \/>&nbsp; &nbsp; &nbsp;&nbsp; . sempid : \uc138\ub9c8\ud3ec\uc5b4\uc5d0 \uc811\uadfc\ud588\ub358 \ucd5c\uadfc\uc758 \ud504\ub85c\uc138\uc2a4\uc758 \ud504\ub85c\uc138\uc2a4 \uc2dd\ubcc4\ubc88\ud638\uc774\ub2e4.<br \/>&nbsp; &nbsp; &nbsp;&nbsp; . semncnt : \uc138\ub9c8\ud3ec\uc5b4\uc758 \uac12\uc774 \ud604\uc7ac\ubcf4\ub2e4 \uc99d\uac00\ud558\uae30\ub97c \uae30\ub2e4\ub9ac\ub294 \ud504\ub85c\uc138\uc2a4\uc758 \uac2f\uc218<br \/>&nbsp; &nbsp; &nbsp;&nbsp; . semzcnt : \uc138\ub9c8\ud3ec\uc5b4\uc758 \uac12\uc774 0\uc73c\ub85c \ub418\uae30\uae4c\uc9c0 \uae30\ub2e4\ub9ac\ub294 \ud504\ub85c\uc138\uc2a4\uc758 \uac2f\uc218<\/P>\n<P><br \/>\uc815\uc758\uc5d0\uc11c \uc54c \uc218 \uc788\ub4ef\uc774 \ud568\uc218 semctl\uc740 msgctl\ubcf4\ub2e4 \ud6e8\uc52c \ubcf5\uc7a1\ud558\ub2e4. sem_id\ub294 \uc720\ud6a8\ud55c \uc138\ub9c8\ud3ec\uc5b4 \uc2dd\ubcc4\uc790\ub77c\uc57c \ud55c\ub2e4. command\ub294 msgctl\uc5d0\uc11c\uc640 \uac19\uc774 \uc218\ud589\ud574\uc57c \ud560 \uc815\ud655\ud55c \uae30\ub2a5\uc744 \uba85\uc2dc\ud55c\ub2e4. \uc774 \uae30\ub2a5\uc5d0\ub294 \uc138\uac00\uc9c0 \uc720\ud615\uc774 \uc788\ub2e4. IPC_STAT\uc640 \uac19\uc740 \ud45c\uc900 IPC\uae30\ub2a5, \ub2e8\uc77c \uc138\ub9c8\ud3ec\uc5b4\ub9cc\uc744 \ub2e4\ub8e8\ub294 \uae30\ub2a5, \uc138\ub9c8\ud3ec\uc5b4\uc758 \uc804\uccb4\ub97c \ub2e4\ub8e8\ub294 \uae30\ub2a5\uc774 \uadf8\uac83\uc774\ub2e4. <\/P>\n<P>&nbsp;<\/P>\n<P>\uac00\ub2a5\ud55c \ubaa8\ub4e0 \uae30\ub2a5\uc744 \ub2e4\uc74c\uacfc \uac19\uc774 \uc815\ub9ac\ud558\uc600\ub2e4.<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; semctl(semaphore control operations) \uae30\ub2a5 \ucf54\ub4dc<br \/>&nbsp;&nbsp; -----------------------------------------------------------------------------<br \/>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; \ud45c\uc900 IPC\uae30\ub2a5 (semid_ds\uad6c\uc870\ub294 sem.h\uc5d0 \uc815\uc758\ub418\uc5b4 \uc788\ub2e4.)<br \/>&nbsp;&nbsp; ----------------------------------------------------------------------------- <br \/>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; IPC_STAT&nbsp; \uc0c1\ud0dc\uc815\ubcf4\ub97c ctl_arg.stat\uc5d0 \uc800\uc7a5\ud55c\ub2e4.<br \/>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; IPC_SET &nbsp; ctl_arg.stat\uc5d0 \uc800\uc7a5\ub41c \ud615\ud0dc\ub85c \uc18c\uc720\uad8c\uacfc \uc0ac\uc6a9 \ud5c8\uac00\uad8c\uc744 \uc9c0\uc815\ud55c\ub2e4.<br \/>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; IPC_RMID&nbsp; \uc2dc\uc2a4\ud15c\uc5d0\uc11c \ud574\ub2f9 \uc138\ub9c8\ud3ec\uc5b4\uc758 \uc9d1\ud569\uc744 \uc0ad\uc81c\ud55c\ub2e4.<br \/>&nbsp;&nbsp; ----------------------------------------------------------------------------- <br \/>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; \ub2e8\uc77c \uc138\ub9c8\ud3ec\uc5b4 \uc5f0\uc0b0 (\uc774\ub4e4\uc740 retval\uc5d0\uac8c \ub118\uc5b4\uc628 \uac12 sem_unm\uc744 \uc0ac\uc6a9\ud55c\ub2e4.)<br \/>&nbsp;&nbsp; ----------------------------------------------------------------------------- <br \/>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; GETVAL &nbsp;&nbsp; \uc138\ub9c8\ud3ec\uc5b4\uc758 \uac12 semval\uc744 \ub3cc\ub824\uc900\ub2e4.<br \/>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; SETVAL &nbsp;&nbsp; \uc138\ub9c8\ud3ec\uc5b4 \uac12\uc744 ctl_arg.val\ub85c \uc9c0\uc815\ud55c\ub2e4.<br \/>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; GETPID &nbsp;&nbsp; sempid\uc758 \uac12\uc744 \ub3cc\ub824\uc900\ub2e4.<br \/>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; GETNCNT &nbsp; semncnt\ub97c \ub3cc\ub824\uc900\ub2e4.<br \/>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; GETZCNT &nbsp; semzcnt\ub97c \ub3cc\ub824\uc900\ub2e4.<br \/>&nbsp;&nbsp; ----------------------------------------------------------------------------- <br \/>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; \uc804\uccb4 \uc138\ub9c8\ud3ec\uc5b4 \uc5f0\uc0b0 <br \/>&nbsp;&nbsp; ----------------------------------------------------------------------------- <br \/>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; GETALL &nbsp;&nbsp; \ubaa8\ub4e0 senvals\uc758 \uac12\uc744 ctl_arg.array\uc5d0 \uc800\uc7a5\ud55c\ub2e4.<br \/>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; SETALL &nbsp;&nbsp; ctl_arg.array\uc758 \uac12\uc744 \uc0ac\uc6a9\ud558\uc5ec \ubaa8\ub4e0 semvals\uac12\uc744 \uc9c0\uc815\ud55c\ub2e4.<br \/>&nbsp;&nbsp; ----------------------------------------------------------------------------- <br \/>&nbsp; &nbsp; <br \/>sem_num \uc778\uc218\ub294 semctl\uc758 \ub2e8\uc77c \uc138\ub9c8\ud3ec\uc5b4 \uae30\ub2a5\uc5d0\uc11c \ud2b9\uc815 \uc138\ub9c8\ud3ec\uc5b4\ub97c \uc9c0\uc815\ud574\uc900\ub2e4. \ub9c8\uc9c0\ub9c9 \uc778\uc218\uc778 ctl_arg\ub294 \uc138\uac00\uc9c0 \uad6c\uc131 \uc694\uc18c\uc758 \uacb0\ud569\uc774\ub2e4. \uc774\ub4e4 \uad6c\uc131 \uc694\uc18c\ub4e4\uc740 \uac01\uac01 semctl\uc758 \uc138\uac00\uc9c0 \uae30\ub2a5\uc5d0 \ub300\uc751\ud55c\ub2e4. semctl\uc740 \uc138\ub9c8\ud3ec\uc5b4\uc758 \ucd08\uae30\uac12\uc744 \uc9c0\uc815\ud560 \ub54c \uc694\uae34\ud558\uac8c \uc0ac\uc6a9\ub41c\ub2e4. \uc774 \uae30\ub2a5\uc740 semget\uc5d0\ub294 \uc5c6\uc74c\uc5d0 \uc8fc\uc758\ud558\ub77c. \ub530\ub77c\uc11c semget\uacfc semctl\uc740 \ub450\uac1c \ubaa8\ub450 \uc788\uc5b4\uc57c \ud55c\ub2e4. \ub2e4\uc74c\uc5d0 \uc608\ub85c\uc11c \uc81c\uc2dc\ud558\ub294 \ud568\uc218\ub294 \ud504\ub85c\uadf8\ub7a8\uc774 \ub2e8\uc77c \uc138\ub9c8\ud3ec\uc5b4\ub97c \uc0dd\uc131\ud560\ub54c\ub098 \uc774\uc5d0 \ub300\ud55c \uc138\ub9c8\ud3ec\uc5b4 \uc9d1\ud569 \uc2dd\ubcc4\uc790\ub97c \uc5bb\uace0\uc790 \ud560 \ub54c \uc0ac\uc6a9\ub420 \uc218 \uc788\ub2e4. \uc138\ub9c8\ud3ec\uc5b4\uac00 \uc0dd\uc131\ub418\ub294 \uacbd\uc6b0\uc5d0\ub294 semctl\uc744 \uc0ac\uc6a9\ud558\uc5ec \ucd08\uae30\uac12\uc744 \ubd80\uc5ec\ud558\uac8c \ub41c\ub2e4.<\/P>\n<P>&nbsp;<\/P>\n<P>&nbsp;&nbsp; \/* initsem&nbsp; -- semaphore initialization *\/<br \/>&nbsp; &nbsp; #include \"pv.h\"<br \/>&nbsp; &nbsp; <\/P>\n<P>&nbsp; &nbsp; initsem(semkey) &nbsp;&nbsp; <\/P>\n<P>&nbsp; &nbsp; key_t semkey;<br \/>&nbsp; &nbsp; {<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int status = 0, semid ;<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ((semid = semget(semkey, 1, SEMPERM|IPC_CREAT|IPC_EXCL)) == -1) <\/P>\n<P>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; if (errno == EEXIST)<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; semid = semget(semkey, 1, 0);<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<\/P>\n<P>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else &nbsp; \/* if created... *\/<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; status = semctl(semid, 0, SETVAL, 1);<\/P>\n<P>&nbsp; &nbsp; &nbsp; &nbsp; if ((semid == -1 || status == -1) <\/P>\n<P>&nbsp; &nbsp; &nbsp; &nbsp; {<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; perror(\"initsem failed\");<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; return (-1);<br \/>&nbsp; &nbsp; &nbsp; &nbsp; }<\/P>\n<P>&nbsp; &nbsp; &nbsp; &nbsp; else<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; return semid; &nbsp;&nbsp; \/* all okay&nbsp; *\/<br \/>&nbsp; &nbsp;&nbsp; }<\/P>\n<P>&nbsp;<\/P>\n<P>&nbsp; include \ud654\uc77c pv.h\ub294 \ub2e4\uc74c\uacfc \uac19\ub2e4.<\/P>\n<P>&nbsp; &nbsp; \/* semaphore example header file *\/<br \/>&nbsp; &nbsp; #include <br \/>&nbsp; &nbsp; #include <br \/>&nbsp; &nbsp; #include <br \/>&nbsp; &nbsp; #include <br \/><\/P>\n<P>&nbsp; &nbsp; extern int errno;<br \/><\/P>\n<P>&nbsp; &nbsp; #define SEMPERM 0600<br \/>&nbsp; &nbsp; #define TRUE 1<br \/>&nbsp; &nbsp; #define FALSE 0 <\/P>\n<P>&nbsp;<\/P>\n<P>\uc138\ub9c8\ud3ec\uc5b4 \uc5f0\uc0b0 : semop \ud638\ucd9c<br \/>semop\uc740 \uae30\ubcf8\uc801\uc778 \uc138\ub9c8\ud3ec\uc5b4 \uc5f0\uc0b0\uc744 \uc2e4\uc81c\ub85c \uc218\ud589\ud558\ub294 \uc2dc\uc2a4\ud15c\ud638\ucd9c\uc774\ub2e4. \uc774\ub54c semop\uc740 \uba54\ub274\uc5bc\uc758 \ud56d\ubaa9\uc774 \uc544\ub2c8\ub77c \uc2e4\uc81c \ud568\uc218 \uc774\ub984\uc774\ub2e4.<\/P>\n<P><br \/>&nbsp;&nbsp; \uc0ac\uc6a9\ubc95 <br \/>&nbsp; &nbsp; &nbsp; #include <br \/>&nbsp; &nbsp; &nbsp; #include <br \/>&nbsp; &nbsp; &nbsp; #include <br \/>&nbsp; &nbsp; &nbsp; int retval, sem_id;<br \/>&nbsp; &nbsp; &nbsp; struct sembuf op_array[SOMEVALUE];<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; .<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; .<br \/>&nbsp; &nbsp; &nbsp; retval = semop(sem_id, op_array, SOMEVALUE);<\/P>\n<P>&nbsp;<\/P>\n<P>sem_id\ub294 \uc138\ub9c8\ud3ec\uc5b4 \uc9d1\ud569 \uc2dd\ubcc4\uc790\ub85c\uc11c \uc774\uc804\uc5d0 semget\ud638\ucd9c\uc744 \ud1b5\ud574 \uac12\uc774 \uc9c0\uc815\ub418\uc5b4\uc57c \ud55c\ub2e4. op_array\ub294 sembuf\uad6c\uc870\uc758 \ubc30\uc5f4\ub85c\uc11c sembuf\uad6c\uc870\ub294 sem.h\uc5d0 \uc815\uc758\ub418\uc5b4 \uc788\ub2e4. SOMEVALUE\ub294 \uc784\uc758\uc758 \uc815\uc218\ud615 \uc0c1\uc218\uc774\ub2e4. \uac01\uac01\uc758 sembuf\uad6c\uc870 \ubcc0\uc218\ub294 \uc138\ub9c8\ud3ec\uc5b4\uc5d0 \ub300\ud574 \uc218\ud589\ud560 \uc5f0\uc0b0\uc744 \uc9c0\uc815\ud55c\ub2e4. \ub2e4\uc2dc \uac15\uc870\ud558\uac70\ub2c8\uc640 semop\ud568\uc218\uac00 \uc138\ub9c8\ud3ec\uc5b4 \uc9d1\ud569\uc5d0 \ub300\ud574 \uc218\ud589\ud558\ub294 \uc77c\ub828\uc758 \uc5f0\uc0b0\ub4e4\uc740 \ubaa8\ub450 \uc6d0\uc790\ud654 \ub418\uc5b4\uc57c \ud55c\ub2e4. \uc989, \uadf8\uc911\uc758 \ud55c \uc5f0\uc0b0\uc774\ub77c\ub3c4 \uc218\ud589\ud560 \uc218 \uc5c6\ub2e4\uba74 \uc804\uccb4 \uc5f0\uc0b0\uc774 \ubaa8\ub450 \uc218\ud589\ub418\uc9c0 \ub9d0\uc544\uc57c \ud55c\ub2e4. \uc774 \uacbd\uc6b0\uc5d0\ub294 \ud2b9\ubcc4\ud788 \uba85\uc2dc\ub418\uc9c0 \uc54a\ub294 \ud55c, \ubaa8\ub4e0 \uc5f0\uc0b0\uc774 \ud55c\ubc88\uc5d0 \uc218\ud589\ub420\uc218 \uc788\uc744\ub54c\uae4c\uc9c0 \ud504\ub85c\uc138\uc2a4\uc758 \uc218\ud589\uc774 \uc911\uc9c0\ub41c\ub2e4. sembuf\uad6c\uc870\ub97c \uc880\ub354 \uc790\uc138\ud788 \ubcf4\uba74 \ub2e4\uc74c\uacfc \uac19\uc774 \uad6c\uc131\ub418\uc5b4 \uc788\ub2e4.<br \/>&nbsp; <br \/>&nbsp; &nbsp; &nbsp; --------------------------------------------------------------------<br \/>&nbsp; &nbsp; &nbsp; | &nbsp; &nbsp; struct sembuf { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; |<br \/>&nbsp; &nbsp; &nbsp; | &nbsp; &nbsp; short &nbsp; sem_num; &nbsp; &nbsp; &nbsp;&nbsp; \/* semaphore # *\/ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; |<br \/>&nbsp; &nbsp; &nbsp; | &nbsp; &nbsp; short &nbsp; sem_op; &nbsp; &nbsp; &nbsp; &nbsp; \/* semaphore operation *\/ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; |<br \/>&nbsp; &nbsp; &nbsp; | &nbsp; &nbsp; short &nbsp; sem_flg; &nbsp; &nbsp; &nbsp;&nbsp; \/* operation flags *\/ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; |<\/P>\n<P>&nbsp; &nbsp; &nbsp; | &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |<br \/>&nbsp; &nbsp; &nbsp; --------------------------------------------------------------------<\/P>\n<P>&nbsp;<\/P>\n<P>sem_num\ub294 \uc9d1\ud569 \ub0b4\uc758 \uc138\ub9c8\ud3ec\uc5b4\uc5d0 \ub300\ud55c \ucca8\uc790\ub97c \uc800\uc7a5\ud55c\ub2e4. \ub9cc\uc57d \uc9d1\ud569\uc758 \uc6d0\uc18c\uac00 \ud558\ub098\ubfd0\uc774\ub77c\uba74 sem_num\uc758 \uac12\uc740 0\uc774\uc5b4\uc57c \ud55c\ub2e4.&nbsp; sem_op\ub294 \ud568\uc218 semop\uc774 \uc218\ud589\ud574\uc57c \ud558\ub294 \uae30\ub2a5\uc744 \uc815\uc218\ub85c\uc11c \ub098\ud0c0\ub0b8\ub2e4. <\/P>\n<P>\uc5ec\uae30\uc5d0\ub294 \uc138\uac00\uc9c0 \uacbd\uc6b0\uac00 \uc788\ub2e4.<\/P>\n<P>&nbsp;<\/P>\n<P>\uacbd\uc6b0 1 : sem_op\uac00 \uc74c\uc218\uc77c\ub54c<\/P>\n<P>\uc774 \uacbd\uc6b0\uc5d0\ub294 \uc55e\uc11c \uc18c\uac1c\ud588\ub4ef\uc774 \uc77c\ubc18\uc801\uc778 \uc138\ub9c8\ud3ec\uc5b4 \uba85\ub839 P()\uc640 \uac19\uc774 \uc218\ud589\ub41c\ub2e4. \uc774\ub97c \uc758\uc0ac \ucf54\ub4dc(pseudo-code)\ub85c \ub098\ud0c0\ub0b4\uba74 \ub2e4\uc74c\uacfc \uac19\ub2e4.(ABS\ub294 \ubcc0\uc218\uc758 \uc808\ub300\uac12\uc744 \ub098\ud0c0\ub0b8\ub2e4)<\/P>\n<P>&nbsp;<\/P>\n<P>&nbsp; &nbsp; &nbsp; &nbsp; if (semval &gt;= ABS(sem_op))<\/P>\n<P>&nbsp; &nbsp; &nbsp; &nbsp; {<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; set semval to semval-ABS(sem_op)<br \/>&nbsp; &nbsp; &nbsp; &nbsp; } <\/P>\n<P>&nbsp; &nbsp; &nbsp; &nbsp; else <\/P>\n<P>&nbsp; &nbsp; &nbsp; &nbsp; {<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; if ((sem_flg&amp;IPC_NOWAIT))<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; return-1 immediately<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; else<\/P>\n<P>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; {<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; wait until semval reaches or exceeds<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ABS(sem_op), then subtract<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ABS(sem_op) as above<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br \/>&nbsp; &nbsp; &nbsp; &nbsp; }<br \/><\/P>\n<P>\uae30\ubcf8 \uac1c\ub150\uc740 \ud568\uc218 semop\uc5d0\uc11c sem_num\uc774 \uac00\ub9ac\ud0a4\ub294 \uc138\ub9c8\ud3ec\uc5b4\uc758 \uac12 semval\uc744 \uc870\uc0ac\ud558\ub294 \uac83\uc774\ub2e4. semval\uc758 \uac12\uc774 \ucda9\ubd84\ud788 \ud06c\ub2e4\uba74 \uc989\uc2dc \ud558\ub098 \uac10\uc18c\uc2dc\ud0a8\ub2e4. \uc544\ub2c8\uba74 semval\uc774 \ucda9\ubd84\ud788 \ucee4\uc9c8 \ub54c\uae4c\uc9c0 \ud504\ub85c\uc138\uc2a4\uc758 \uc218\ud589\uc744 \uc911\ub2e8\uc2dc\ud0a8\ub2e4. \uadf8\ub7ec\ub098 sem_flg\uc758 IPC_NOWAIT \ud50c\ub798\uadf8\uc758 \uac12\uc774 1\ub85c \ub418\uc5b4 \uc788\uc73c\uba74 sem_op\uc740 \uc989\uc2dc -1\uc744 \ub418\ub3cc\ub824\uc8fc\uace0 errono\uac12\uc744 EAGAIN\uc73c\ub85c \ud55c\ub2e4. <\/P>\n<P>&nbsp;<\/P>\n<P>\uacbd\uc6b0 2 : sem_op\uc774 \uc591\uc218\uc77c \ub54c<\/P>\n<P>\uc774\ub54c\ub294 \ubcf4\ud1b5\uc758 V()\uc5f0\uc0b0\uacfc \uc720\uc0ac\ud558\ub2e4. \uc989 sem_op\uc758 \uac12\uc744 \ud574\ub2f9 semval\uc5d0 \ub354\ud574\uc900\ub2e4. \uc774\ub54c \ud574\ub2f9 \uc138\ub9c8\ud3ec\uc5b4\uc758 \uac12\uc774 \uc99d\uac00\ud558\uae30\ub97c \uae30\ub2e4\ub9ac\ub294 \ud504\ub85c\uc138\uc2a4\ub4e4\uc774 \uae68\uc5b4\ub098\uac8c \ub41c\ub2e4.<br \/>&nbsp; &nbsp; <br \/>\uacbd\uc6b0 3 : sem_op\uc774 0\uc77c\ub54c<\/P>\n<P>\uc774 \uacbd\uc6b0\ub294 semval\uc744 \ubcc0\ud658\uc2dc\ud0a4\ub294 \uac83\uc774 \uc544\ub2c8\ub77c \uac12\uc774 0\uc774 \ub420\ub54c\uae4c\uc9c0 \uae30\ub2e4\ub9b0\ub2e4. semval\uc774 0\uc774 \uc544\ub2c8\uace0 sem_flg\uc758 IPC_NOWAIT\uac00 1\uc778 \uacbd\uc6b0\uc5d0 semop\ub294 \uc989\uc2dc \uc624\ub958 \uac12\uc744 \ub3cc\ub824\uc8fc\uac8c \ub41c\ub2e4.<br \/>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; <br \/>SEM_UNDO \ud50c\ub798\uadf8<\/P>\n<P>\uc774\uac83\uc740 sembuf\uad6c\uc870\uc758 \uad6c\uc131\uc694\uc18c sem_flg\uc5d0 \uc788\ub294 \ud50c\ub798\uadf8\uc758 \ud558\ub098\uc774\ub2e4. \uc774\ub294 \ud504\ub85c\uc138\uc2a4\uc758 \uc218\ud589\uc774 \ub05d\ub0ac\uc744 \ub54c \uc2dc\uc2a4\ud15c\uc774 \uc218\ud589\ub41c \uc5f0\uc0b0\uc744 \uc790\ub3d9\uc801\uc73c\ub85c \ucde8\uc18c\ud558\ub3c4\ub85d \uc9c0\uc2dc\ud55c\ub2e4. \uc218\ud589\ub41c \uc77c\ub828\uc758 \uc5f0\uc0b0\uc744 \ucd94\uc801\ud558\uae30 \uc704\ud558\uc5ec \uc2dc\uc2a4\ud15c\uc740 \uc138\ub9c8\ud3ec\uc5b4\uc5d0 semadj\ub77c\ub294 \uc815\uc218\ub97c \ub300\uc751\uc2dc\ud0a8\ub2e4. \uc774\ub54c semadj\ubcc0\uc218\ub294 \ud504\ub85c\uc138\uc2a4\ub9c8\ub2e4 \ud560\ub2f9\ub418\uc5b4\uc57c \ud568\uc5d0 \uc8fc\uc758\ud574\ub77c. \ub530\ub77c\uc11c \uc11c\ub85c \ub2e4\ub978 \ud504\ub85c\uc138\uc11c\ub294 \ub3d9\uc77c\ud55c \uc138\ub9c8\ud3ec\uc5b4\uc5d0 \ub300\ud574 \ub3c5\ub9bd\uc801\uc778 semadj\uac12\uc744 \uc720\uc9c0\ud558\uac8c \ub41c\ub2e4. SEM_UNDO\uc758 \uac12\uc744 1\ub85c \ud558\uace0\uc11c semop\uc5f0\uc0b0\uc744 \uc218\ud589\ud558\uba74 semadj\uac12\uc5d0\uc11c sem_num\uac12\uc744 \ube80\ub2e4. \uc774\ub54c sem_num\uc758 \ubd80\ud638\uac00 \uc911\uc694\ud55c\ub370 \uc774\ub294 sem_num\uc758 \uac12\uc774 \uc591\uc218\uc778\uac00 \uc74c\uc218\uc778\uac00\uc5d0 \ub530\ub77c semadj\uc758 \uac12\uc774 \uac10\uc18c\ud558\uac70\ub098 \uc99d\uac00\ud558\uae30 \ub54c\ubb38\uc774\ub2e4. \ud504\ub85c\uc138\uc2a4\uc758 \uc218\ud589\uc774 \ub05d\ub098\uba74 \uc2dc\uc2a4\ud15c\uc740 semadj\uac12\uc744 \ud574\ub2f9 \uc138\ub9c8\ud3ec\uc5b4\uc5d0 \ub354\ud574\uc90c\uc73c\ub85c\uc368 \uc9c0\uae08\uae4c\uc9c0\uc758 \ubaa8\ub4e0 semop\ud638\ucd9c \ud6a8\uacfc\ub97c \uc0c1\uc1c4\uc2dc\ud0a8\ub2e4. \uc77c\ubc18\uc801\uc73c\ub85c \ubcfc\ub54c \ud504\ub85c\uc138\uc2a4\uac00 \uc9c0\uc815\ud55c \uac12\uc774&nbsp; \ud574\ub2f9 \ud504\ub85c\uc138\uc2a4\uc758 \uc885\ub8cc \ud6c4\uc5d0\ub3c4 \ud6a8\ub825\uc744 \uac16\uc9c0 \uc54a\ub294\ub2e4\uba74 SEM_UNDO\uac00 \uc0ac\uc6a9\ub418\uc5b4\uc57c\ub9cc \ud55c\ub2e4.<\/P>\n<P>&nbsp;<\/P>\n<P>\uc138\ub9c8\ud3ec\uc5b4\uc758 \uc608<\/P>\n<P>\uc774\uc81c initsem\ub8e8\ud2f4\uc73c\ub85c \uc2dc\uc791\ud55c \uc608\ub97c \uc644\uc131\ud574 \ubcf4\uc790. \uc5ec\uae30\uc11c\ub294 \uc804\ud1b5\uc801\uc778 \uc138\ub9c8\ud3ec\uc5b4 \uc5f0\uc0b0\uc744 P()\uc640 V()\ub85c \uad6c\ud604\ud558\uc5ec \uc774\ub97c \uc911\uc2ec\uc73c\ub85c \uc0bc\uc558\ub2e4. \uc6b0\uc120 P()\ub97c \ubcf4\uc790. <\/P>\n<P>&nbsp; &nbsp; &nbsp;&nbsp; \/* pc -- semaphore p operation *\/<br \/>&nbsp; &nbsp; &nbsp;&nbsp; #include \"pv.h\"<br \/>&nbsp; &nbsp; &nbsp;&nbsp; p(semid)<br \/>&nbsp; &nbsp; &nbsp;&nbsp; int semid;<br \/>&nbsp; &nbsp; &nbsp;&nbsp; {<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; struct sembuf p_buf;<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; p_buf.sem_num = 0;<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; p_buf.sem_op = -1;<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; p_buf.sem_flg = SEM_UNDO;<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; if (semop(semid, &amp;p_buf, 1) == -1) <\/P>\n<P>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; {<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; perror(\"p(semid) failed\");<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; exit(1);<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; } <\/P>\n<P>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; else<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; return(0);<br \/>&nbsp; &nbsp; &nbsp; &nbsp; } <\/P>\n<P>&nbsp;<\/P>\n<P>\uc774\ub54c SEM_UNDO\ub97c \uc0ac\uc6a9\ud588\uc74c\uc5d0 \uc8fc\uc758\ud558\ub77c. V()\ub294 \ub2e4\uc74c\uacfc \uac19\ub2e4.<br \/>&nbsp; &nbsp; &nbsp; <br \/>&nbsp; &nbsp; &nbsp; &nbsp; \/* v.c -- semaphore v operation *\/<br \/>&nbsp; &nbsp; &nbsp; &nbsp; #include \"pv.h\"<br \/>&nbsp; &nbsp; &nbsp; &nbsp; v(semid)<br \/>&nbsp; &nbsp; &nbsp; &nbsp; int semid;<br \/>&nbsp; &nbsp; &nbsp; &nbsp; {<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; struct sembuf v_buf;<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; v_buf.sem_num = 0; <br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; v_buf.sem_op = 1; <br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; v_buf.sem_flg = SEM_UNDO; <br \/>&nbsp; <br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(semop(semid, &amp;v_buf, 1) == -1) <\/P>\n<P>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; perror(\"v(semid) failed\");<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exit(1);<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } <\/P>\n<P>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return(0);<br \/>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; }<\/P>\n<P>&nbsp;<\/P>\n<P>\uc774\uc81c \ube44\uad50\uc801 \uac04\ub2e8\ud55c \uc774\ub4e4 \ub8e8\ud2f4\uc73c\ub85c \uc0c1\ud638 \ubc30\uc81c\ub97c \uad6c\ud604\ud574\ubcf4\uc790. \ub2e4\uc74c\uc758 \ud504\ub85c\uadf8\ub7a8\uc744 \uc0b4\ud3b4\ubcf4\uc790.<\/P>\n<P>&nbsp;<\/P>\n<P>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; \/* testsem -- test semaphore routines *\/<br \/>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; #include \"pv.h\"<br \/>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; <br \/>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; main()<br \/>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; {<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; key_t semkey = 0x200;<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(fork() == 0)<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; handlesem(semkey);<\/P>\n<P>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(fork() == 0)<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; handlesem(semkey);<\/P>\n<P>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(fork() == 0)<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; handlesem(semkey);<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; <br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; handlesem(skey)<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; key_t skey;<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int semid, pid = getpid();<\/P>\n<P>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if((semid = initsem(skey)) &lt; 0)<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exit(1);<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf(\"\\nprocess %d before critical section\\n\", pid);<br \/>&nbsp; &nbsp; &nbsp; &nbsp; <br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; p(semid);<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf(\"process %d in critical section\\n\", pid);<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; <br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \/* in real life do something interesting *\/<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sleep(10);<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf(\"process %d leaving critical section\\n\", pid);<\/P>\n<P>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; v(semid);<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf(\"process %d exiting\\n\", pid);<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exit(0);<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; }<br \/><\/P>\n<P>testsem\uc740 \uc138\uac1c\uc758 \uc790\uc2dd\ud504\ub85c\uc138\uc2a4\ub97c \uc0dd\uc131\ud558\uace0 , \uc774\ub4e4\uc740 p()\uc640 v()\ub97c \uc0ac\uc6a9\ud558\uc5ec \uc784\uacc4 \uc601\uc5ed\uc5d0\ub294 \ud55c \uc21c\uac04\uc5d0 \ub458 \uc774\uc0c1\uc774 \ub4e4\uc5b4\uc788\uc9c0 \ubabb\ud558\ub3c4\ub85d \ud55c\ub2e4. \ud55c \ucef4\ud4e8\ud130\uc5d0\uc11c testsem\uc744 \uc218\ud589\uc2dc\ud0a8 \uacb0\uacfc\ub294 \ub2e4\uc74c\uacfc \uac19\ub2e4.<\/P>\n<P>&nbsp;<\/P>\n<P>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; process 799 before critical section <br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; process 800 before critical section <br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; process 801 before critical section <\/P>\n<P>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; process 799 in critical section <br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; process 799 leaving critical section <br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; process 799 exiting <br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; process 801 in critical section <br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; process 801 leaving critical section <br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; process 801 exiting<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; process 800 in critical section <br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; process 800 leaving critical section <br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; process 800 exiting<\/P>\n<P>&nbsp;<\/P>\n<P>o Critical secion \ub780 ?<\/P>\n<P>\ub2e4\uc911 \ud504\ub85c\uadf8\ub798\ubc0d \uc6b4\uc601\uccb4\uc81c\uc5d0\uc11c \uc5ec\ub7ec \ud504\ub85c\uc138\uc2a4\uac00 \ub370\uc774\ud0c0\ub97c \uacf5\uc720\ud558\uba74\uc11c \uc218\ud589\ub420 \ub54c \uac01 \ud504\ub85c\uc138\uc2a4\uc5d0\uc11c \uacf5\uc720 \ub370\uc774\ud0c0\ub97c \uc561\uc138\uc2a4\ud558\ub294 \ud504\ub85c\uadf8\ub7a8 \ucf54\ub4dc \ubd80\ubd84\uc744 \uac00\ub9ac\ud0a4\ub294 \ub9d0. \uacf5\uc720\ub370\uc774\ud0c0\ub97c \uc5ec\ub7ec \ud504\ub85c\uc138\uc2a4\uac00 \ub3d9\uc2dc\uc5d0 \uc561\uc138\uc2a4\ud558\uba74 \uc2dc\uac04\uc801\uc778 \ucc28\uc774 \ub54c\ubb38\uc5d0 \uc798\ubabb\ub41c \uacb0\uacfc\ub97c \ub9cc\ub4e4\uc5b4 \ub0bc \uc218 \uc788\uae30 \ub54c\ubb38\uc5d0 \ud55c \ud504\ub85c\uc138\uc2a4\uac00 \uc704\ud5d8 \ubd80\ubd84\uc744 \uc218\ud589\ud558\uace0 \uc788\uc744 \ub54c, \uc989 \uacf5\uc720 \ub370\uc774\ud0c0\ub97c \uc561\uc138\uc2a4\ud558\uace0 \uc788\uc744 \ub54c\ub294 \ub2e4\ub978 \ud504\ub85c\uc138\uc2a4\ub4e4\uc740 \uc808\ub300\ub85c \uadf8 \ub370\uc774\ud0c0\ub97c \uc561\uc138\uc2a4\ud558\uc9c0 \ubabb\ud558\ub3c4\ub85d \ud558\uc5ec\uc57c \ud55c\ub2e4.<\/P>\n<P>o Mutual exclusion (\uc0c1\ud638 \ubc30\uc81c)\ub780(1) ? <\/P>\n<P>- \ud504\ub85c\uc138\uc2a4\uc758 \uc0c1\ud638 \uad50\uc2e0\uc5d0 \ub300\ud55c \uae30\ubcf8\uc801\uc778 \uc870\uce58\ub294 \uacf5\uc6a9 \ubd80\ubd84\uc744 \uc5ec\ub7ec \ud504\ub85c\uc138\uc2a4\uac00 \ub3d9\uc2dc\uc5d0 \uc0ac\uc6a9\ud558\ub294 \uac83\uc744 \ubc30\uc81c\ud558\ub294 \uac83\uc774\uc5c8\ub2e4. \uc2dc\uc2a4\ud15c\uc758 \uc5b4\ub5a0\ud55c \uc790\uc6d0\uc744 \ud55c \uc2dc\uc810\uc5d0\uc11c \ud55c\uac1c\uc758 \ud504\ub85c\uc138\uc2a4\ub9cc\uc774 \uc0ac\uc6a9\ud560 \uc218 \uc788\ub3c4\ub85d \ud558\ub294 \uac83\uc744 \uc0c1\ud638\ubc30\uc81c\ub77c \ud55c\ub2e4. \ub610\ud55c, \ud504\ub85c\uadf8\ub7a8\uc5d0\uc11c \uc774\ub7ec\ud55c \uc790\uc6d0\uc744 \uc0ac\uc6a9\ud558\uac70\ub098 \ud639\uc740 \uadf8 \ub0b4\uc6a9\uc744 \ubcc0\uacbd\ud558\ub294 \ubd80\ubd84\uc744 \uc704\ud5d8\ubd80\ubd84 (critical section)\uc774\ub77c \ud558\uba70, \ub458 \uc774\uc0c1\uc758 \ud504\ub85c\uadf8\ub7a8\uc5d0\uc11c \uc774 \uc704\ud5d8 \ubd80\ubd84\uc774 \ub3d9\uc2dc\uc5d0 \uc218\ud589\ub418\uc9c0 \uc54a\ub3c4\ub85d \ud558\ub294 \uac83\uc774 \uc0c1\ud638 \ubc30\uc81c\uc758 \uae30\ub2a5\uc774\ub2e4.&nbsp; <\/P>\n<P>- \ub2e4\uc911 \ud504\ub85c\uadf8\ub798\ubc0d \uc2dc\uc2a4\ud15c\uc5d0\uc11c \uc5ec\ub7ec \ud504\ub85c\uc138\uc2a4\uac00 \ud558\ub098\uc758 \uacf5\uc720 \ub370\uc774\ud0c0\ub97c \uc561\uc138\uc2a4\ud558\uba74\uc11c \uc791\uc5c5\uc744 \ud560 \ub54c, \ud55c \ud504\ub85c\uc138\uc2a4\uac00 \uadf8 \ub370\uc774\ud0c0\ub97c \uc561\uc138\uc2a4\ud560 \ub54c\ub294 \ub2e4\ub978 \ud504\ub85c\uc138\uc2a4\ub4e4\uc740 \uadf8\uac83\uc744 \uc0ac\uc6a9\ud558\uc9c0 \ubabb\ud558\ub3c4\ub85d \ud558\ub294 \uc6b4\uc601\uccb4\uc81c\uc758 \uae30\ub2a5. \uc608\ub97c \ub4e4\uc5b4 \ud55c \ud504\ub85c\uc138\uc2a4\uac00 \uc5b4\ub5a4 \ud654\uc77c\uc5d0 \ub370\uc774\ud0c0\ub97c \uc4f0\uace0 \uc788\uc744 \ub54c \ub2e4\ub978 \ud504\ub85c\uc138\uc2a4\uac00 \uadf8 \ud654\uc77c\uc744 \uc9c0\uc6d0\ubc84\ub9b0\ub2e4\uba74 \ub9ce\uc740 \ubb38\uc81c\uac00 \ubc1c\uc0dd\ud560 \uac83\uc774\ub2e4. \uc0c1\ud638 \ubc30\uc81c\ub294 \ud55c\ubc88\uc5d0 \ud55c \ud504\ub85c\uc138\uc2a4\ub9cc\uc774 \uacf5\uc720 \ub370\uc774\ud0c0\ub97c \uc561\uc138\uc2a4\ud560 \uc218 \uc788\ub3c4\ub85d \ud574 \uc8fc\ub294 \uac83\uc73c\ub85c, \ub2e4\uc911\ud504\ub85c\uadf8\ub798\ubc0d \uc2dc\uc2a4\ud15c\uc758 \uc6b4\uc601\uccb4\uc81c\uac00 \uaf2d \uac16\ucd94\uc5b4\uc57c \ud560 \uae30\ub2a5\uc774\ub2e4\n","protected":false},"excerpt":{"rendered":"<p>[\uc138\ub9c8\ud3ec\uc5b4] [\ubba4\ud14d\uc2a4] \ub3d9\uae30\ud654\uc5d0 \ud544\uc694\ud55c \uae30\uc220\ub4e4. \ub370\ub4dc\ub77d\uc774 \uac78\ub838\uc744 \ub54c \uc0ac\uc6a9\ud558\ub294 \uac83\uc774 \uc544\ub2c8\uace0 \uc138\ub9c8\ud3ec\uc5b4\ub098 \ubba4\ud14d\uc2a4\uc5d0 \uc758\ud574 \ub370\ub4dc\ub77d\uc774 \uac78\ub9b4 \uc218\ub3c4 \uc788\uc2b5\ub2c8\ub2e4. \ub530\ub77c\uc11c \uaf2d \ud544\uc694\ud55c \ubd80\ubd84\uc5d0\ub9cc \uc81c\ub300\ub85c \uc0ac\uc6a9\ud574\uc57c\ud569\ub2c8\ub2e4. \ubba4\ud14d\uc2a4\ub780 MUTual EXclusion\uc73c\ub85c \uc6b0\ub9ac\ub9d0\ub85c \ud574\uc11d\ud558\uba74 &#8216;\uc0c1\ud638 \ubc30\uc81c&#8217;\ub77c\uace0 \ud569\ub2c8\ub2e4. \ub9d0 \uadf8\ub300\ub85c \uc0c1\ud638 \ubc30\uc81c\ud574\uc11c \uc2e4\ud589\ud558\ub294 \uac81\ub2c8\ub2e4. \uc784\uacc4 \uad6c\uc5ed\uc744 \uac00\uc9c4 \uc2a4\ub808\ub4dc\ub4e4\uc774 \ub3d9\uc2dc\uc5d0 \uc2e4\ud589\ub418\uc9c0 \uc54a\uace0 \uc11c\ub85c \ubc30\uc81c\ub418\uc5b4 \uc2e4\ud589\ub418\uac8c \ud558\ub294 \uae30\uc220\uc785\ub2c8\ub2e4. \uc5ec\uae30\uc11c \uc784\uacc4\uad6c\uc5ed(Critical Section)\ub780 \ud504\ub85c\uadf8\ub7a8 \uc0c1\uc5d0\uc11c [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[18],"tags":[],"class_list":["post-47","post","type-post","status-publish","format-standard","hentry","category-development_language"],"_links":{"self":[{"href":"https:\/\/hasu0707.duckdns.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/47","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hasu0707.duckdns.org\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hasu0707.duckdns.org\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hasu0707.duckdns.org\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hasu0707.duckdns.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=47"}],"version-history":[{"count":0,"href":"https:\/\/hasu0707.duckdns.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/47\/revisions"}],"wp:attachment":[{"href":"https:\/\/hasu0707.duckdns.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=47"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hasu0707.duckdns.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=47"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hasu0707.duckdns.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=47"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}