{"id":264160,"date":"2023-02-15T14:10:19","date_gmt":"2023-02-15T06:10:19","guid":{"rendered":"https:\/\/lrxjmw.cn\/ljczsxhlxhly.html"},"modified":"2023-02-15T14:10:19","modified_gmt":"2023-02-15T06:10:19","slug":"ljczsxhlxhly","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/ljczsxhlxhly.html","title":{"rendered":"\u3010Linux\u57fa\u7840\u77e5\u8bc6\u3011\u4fe1\u53f7\u91cf\u4fe1\u53f7\u91cf\u4e0eSYSTEMV\u4fe1\u53f7\u91cf\u7684\u533a\u522b"},"content":{"rendered":"
\u4e00\u3001\u4fe1\u53f7\u91cf\u7b80\u4ecb<\/p>\n
\u8baf\u53f7\u91cf\u662f\u89e3\u51b3\u8fdb\u7a0b\u4e4b\u95f4\u7684\u540c\u6b65\u4e0e\u4e92\u65a5\u7684IPC\u673a\u5236\uff0c\u4e92\u65a5\u4e0e\u540c\u6b65\u5173\u7cfb\u5b58\u5728\u7684\u75c7\u7ed3\u5728\u4e8e\u4e34\u754c\u8d44\u6e90\u3002\u4e34\u754c\u8d44\u6e90\u662f\u5728\u540c\u4e00\u4e2a\u65f6\u523b\u53ea\u5bb9\u8bb8\u6709\u9650\u4e2a\uff08\u4e00\u822c\u53ea\u6709\u4e00\u4e2a\uff09\u8fdb\u7a0b\u53ef\u4ee5\u8bbf\u95ee\uff08\u8bfb\uff09\u6216\u66f4\u6539\uff08\u5199\uff09\u7684\u8d44\u6e90\uff0c\u4e34\u754c\u8d44\u6e90\u5305\u62ec\uff1a\u786c\u4ef6\u8d44\u6e90\uff08\u5904\u7406\u5668\u3001\u5185\u5b58\u3001\u5b58\u50a8\u5668\u4ee5\u53ca\u5176\u4ed6\u5916\u56f4\u8bbe\u5907\u7b49\uff09\u548c\u8f6f\u4ef6\u8d44\u6e90\uff08\u5171\u4eab\u4ee3\u7801\u6bb5\u3001\u5171\u4eab\u7ed3\u6784\u548c\u53d8\u91cf\u7b49\uff09\u3002<\/p>\n
\u8baf\u53f7\u91cf\u662f\u63cf\u8ff0\u67d0\u4e00\u79cd\u8d44\u6e90\u662f\u5426\u53ef\u7528\u7684\u53d8\u91cf\uff0c\u8baf\u53f7\u91cf\u7684\u503c\u8868\u793a\u5f53\u524d\u53ef\u7528\u7684\u8d44\u6e90\u7684\u6570\u76eelinux\u67e5\u770b\u4fe1\u53f7\u91cf\u547d\u4ee4<\/a><\/strong>\uff0c\u82e5\u8baf\u53f7\u91cf\u7684\u503c\u7b49\u4e8e0\u5219\u610f\u5473\u7740\u76ee\u524d\u6ca1\u6709\u53ef\u7528\u7684\u8d44\u6e90\u3002<\/p>\n \u5bf9\u8baf\u53f7\u91cf\u8fdb\u884c\u7684\u4e24\u4e2a\u539f\u5b50\u64cd\u4f5c(PV\u64cd\u4f5c)<\/p>\n P\u64cd\u4f5c\uff1a\u7b49\u5f85\u3002\u5047\u5982sv\u5c0f\u4e8e0\uff0c\u51cf\u5c11sv\u3002\u5047\u5982sv\u4e3a0\uff0c\u6302\u8d77\u8fd9\u4e2a\u8fdb\u7a0b\u7684\u6267\u884c\u3002<\/p>\n V\u64cd\u4f5c\uff1a\u53d1\u9001\u8baf\u53f7\u3002\u5047\u5982\u6709\u8fdb\u7a0b\u88ab\u6302\u8d77\u7b49\u5f85sv\uff0c\u4f7f\u5176\u6062\u590d\u6267\u884c\u3002\u5047\u5982\u6ca1\u6709\u8fdb\u884c\u88ab\u6302\u8d77\u7b49\u5f85sv\uff0c\u964d\u4f4esv\u3002<\/p>\n Linux<\/a>\u7cfb\u7edf<\/a>\u627f\u7ee7\u4e86Unix\u7cfb\u7edf\u7684\u4e24\u79cd\u8baf\u53f7\u91cf\uff1a<\/p>\n A\u3001\u5185\u6838\u8baf\u53f7\u91cf\uff0c\u7531\u5185\u6838\u63a7\u5236\u8def\u5f84\u4f7f\u7528<\/p>\n B\u3001\u7528\u6237\u6001\u8fdb\u7a0b\u4f7f\u7528\u7684\u8baf\u53f7\u91cf\uff0c\u5206\u4e3aPOSIX\u8baf\u53f7\u91cf\u548cSYSTEMV\u8baf\u53f7\u91cf\u3002POSIX\u8baf\u53f7\u91cf\u53c8\u5206\u4e3a\u6709\u540d\u8baf\u53f7\u91cf\u548c\u65e0\u540d\u8baf\u53f7\u91cf\uff0c\u6709\u540d\u8baf\u53f7\u91cf\uff0c\u5176\u503c\u4fdd\u5b58\u5728\u6587\u4ef6\u4e2d,\u53ef\u7528\u4e8e\u7ebf\u7a0b\u3001\u8fdb\u7a0b\u95f4\u7684\u540c\u6b65\uff1b\u65e0\u540d\u8baf\u53f7\u91cflinux deepin\uff0c\u5176\u503c\u4fdd\u5b58\u5728\u663e\u5b58\u4e2d\u3002<\/p>\n POSIX\u8baf\u53f7\u91cf\u4e0eSYSTEMV\u8baf\u53f7\u91cf\u7684\u533a\u522b\u5982\u4e0b\uff1a<\/p>\n A\u3001POSIX\u8baf\u53f7\u91cf\u662f\u4e2a\u975e\u8d1f\u6574\u6570\uff0c\u5e38\u7528\u4e8e\u7ebf\u7a0b\u95f4\u540c\u6b65\u3002SYSTEMV\u8baf\u53f7\u91cf\u662f\u4e00\u4e2a\u6216\u591a\u4e2a\u8baf\u53f7\u91cf\u7684\u96c6\u5408\uff0c\u662f\u4e00\u4e2a\u8baf\u53f7\u91cf\u7ed3\u6784\u4f53\uff0c\u8baf\u53f7\u91cf\u662f\u5b83\u7684\u4e00\u90e8\u4efd\uff0c\u5e38\u7528\u4e8e\u8fdb\u7a0b\u95f4\u540c\u6b65\u3002<\/p>\n B\u3001POSIX\u8baf\u53f7\u91cf\u7684\u5f15\u7528\u5934\u6587\u4ef6\u662f\uff0c\u800cSYSTEMV\u8baf\u53f7\u91cf\u7684\u5f15\u7528\u5934\u6587\u4ef6\u662f\u3002<\/p>\n C\u3001SystemV\u8baf\u53f7\u91cf\u662f\u590d\u6742\u7684\uff0c\u800cPOSIX\u8baf\u53f7\u91cf\u662f\u7b80\u5355\u7684\u3002<\/p>\n \u4e8c\u3001POSIX\u65e0\u540d\u8baf\u53f7\u91cf1\u3001\u65e0\u540d\u8baf\u53f7\u91cf<\/p>\n \u65e0\u540d\u8baf\u53f7\u91cf\u7684\u521b\u5efa\u4e0e\u666e\u901a\u53d8\u91cf\u4e00\u6837\uff0c\u7533\u660e\u540e\u521d\u59cb\u5316\u5373\u53ef\uff0c\u6bd4\u5982\uff1asem_tsemid=1\u3002\u65e0\u540d\u8baf\u53f7\u91cf\u5e38\u7528\u4e8e\u591a\u7ebf\u7a0b\u95f4\u7684\u540c\u6b65\uff0c\u4e5f\u53ef\u7528\u4e8e\u76f8\u5173\u8fdb\u7a0b\u95f4\u7684\u540c\u6b65\u3002\u65e0\u540d\u8baf\u53f7\u91cf\u5fc5\u987b\u662f\u591a\u4e2a\u8fdb\u7a0b\uff08\u7ebf\u7a0b\uff09\u7684\u5171\u4eab\u53d8\u91cf\uff0c\u65e0\u540d\u8baf\u53f7\u91cf\u8981\u4fdd\u62a4\u7684\u53d8\u91cf\u4e5f\u5fc5\u987b\u662f\u591a\u4e2a\u8fdb\u7a0b\uff08\u7ebf\u7a0b\uff09\u7684\u5171\u4eab\u53d8\u91cf\uff0c\u65e0\u540d\u8baf\u53f7\u91cf\u7684\u503c\u4fdd\u5b58\u5728\u663e\u5b58\u4e2d\u3002<\/p>\n \u5e38\u89c1\u7684\u65e0\u540d\u8baf\u53f7\u91cf\u76f8\u5173\u51fd\u6570\uff1a<\/p>\n \n pshared==0\u7528\u4e8e\u540c\u4e00\u591a\u7ebf\u7a0b\u7684\u540c\u6b65\uff1b<\/p>\n pshared>0\u7528\u4e8e\u591a\u4e2a\u76f8\u5173\u8fdb\u7a0b\u95f4\u7684\u540c\u6b65\uff08\u5373\u7531fork\u5f62\u6210\u7684\uff09<\/p>\n \n \u9500\u6bc1\u8baf\u53f7\u91cf<\/p>\n \n \u62ff\u56de\u8baf\u53f7\u91cfsem\u7684\u5f53\u524d\u503clinux shell\uff0c\u628a\u503c\u4fdd\u5b58\u5230sval\u4e2d\u3002<\/p>\n \u82e5\u67091\u4e2a\u6216\u66f4\u591a\u7684\u7ebf\u7a0b\u6216\u8fdb\u7a0b\u8c03\u7528sem_wait\u963b\u585e\u5728\u8be5\u8baf\u53f7\u91cf\u4e0a\uff0c\u8be5\u51fd\u6570\u8fd4\u56de\u4e24\u79cd\u503c\uff1a<\/p>\n 1)\u8fd4\u56de0<\/p>\n 2)\u8fd4\u56de\u963b\u585e\u5728\u8be5\u8baf\u53f7\u91cf\u4e0a\u7684\u8fdb\u7a0b\u6216\u7ebf\u7a0b\u6570\u91cf<\/p>\n linux\u91c7\u7528\u8fd4\u56de\u7684\u7b2c\u4e00\u79cd\u7b56\u7565\u3002<\/p>\n sem_wait(\u6216sem_trywait)\u76f8\u5f53\u4e8eP\u64cd\u4f5c\uff0c\u5373\u7533\u8bf7\u8d44\u6e90\u3002<\/p>\n \n \u6d4b\u8bd5\u6240\u6307\u5b9a\u8baf\u53f7\u91cf\u7684\u503c,\u5b83\u7684\u64cd\u4f5c\u662f\u539f\u5b50\u7684\u3002<\/p>\n \u82e5sem>0\uff0c\u8fd9\u4e48\u5b83\u51cf1\u5e76\u7acb\u523b\u8fd4\u56de\u3002<\/p>\n \u82e5sem==0\uff0c\u5219\u7761\u7720\u76f4\u81f3sem>0\uff0c\u6b64\u65f6\u7acb\u523b\u51cf1\uff0c\u4e4b\u540e\u8fd4\u56de\u3002<\/p>\n \n \u5176\u4ed6\u7684\u884c\u4e3a\u548csem_wait\u4e00\u6837\uff0c\u4e0d\u4ec5\uff1a<\/p>\n \u82e5sem==0\uff0c\u4e0d\u662f\u7761\u7720\uff0c\u800c\u662f\u8fd4\u56de\u4e00\u4e2a\u9519\u8befEAGAIN\u3002<\/p>\n sem_post\u76f8\u5f53\u4e8eV\u64cd\u4f5c\uff0c\u91ca\u653e\u8d44\u6e90\u3002<\/p>\n \n \u628a\u6307\u5b9a\u7684\u8baf\u53f7\u91cfsem\u7684\u503c\u52a01;<\/p>\n \u547c\u9192\u6b63\u5728\u7b49\u5f85\u8be5\u8baf\u53f7\u91cf\u7684\u4efb\u610f\u7ebf\u7a0b\u3002<\/p>\n 2\u3001\u65e0\u540d\u8baf\u53f7\u91cf\u5728\u591a\u7ebf\u7a0b\u95f4\u7684\u540c\u6b65<\/p>\n \u65e0\u540d\u8baf\u53f7\u91cf\u7684\u5e38\u89c1\u7528\u6cd5\u662f\u5373\u5c06\u4fdd\u62a4\u7684\u53d8\u91cf\u653e\u5230sem_wait\u548csem_post\u4e2d\u95f4\u6240\u4ea7\u751f\u7684\u4e34\u754c\u533a\u5185\u3002<\/p>\n \u4f7f\u7528\u8baf\u53f7\u91cf\u5b9e\u73b0\u591a\u7ebf\u7a0b\u540c\u6b65\uff08\u7ebf\u7a0b\u6267\u884c\u6b21\u5e8f\u968f\u673a\uff09\u5b9e\u4f8b\uff1a<\/p>\n \n <\/p>\n#include \n#include \nint number; \/\/ \u88ab\u4fdd\u62a4\u7684\u5168\u5c40\u53d8\u91cf\nsem_t semid;\nvoid* thread_one(void *arg)\n{\n sem_wait(&semid);\n printf(\"thread_one have the semaphoren\");\n number++;\n printf(\"number = %dn\",number);\n sem_post(&semid);\n}\nvoid* thread_two(void *arg)\n{\n sem_wait(&semid);\n printf(\"thread_two have the semaphore n\");\n number--;\n printf(\"number = %dn\",number);\n sem_post(&semid);\n}\nint main(int argc, char *argv[])\n{\n number = 1;\n pthread_t tid1, tid2;\n sem_init(&semid, 0, 1);\n pthread_create(&tid1,NULL,thread_one, NULL);\n pthread_create(&tid2,NULL,thread_two, NULL);\n pthread_join(tid1,NULL);\n pthread_join(tid2,NULL);\n printf(\"main running...n\");\n return 0;\n}<\/code><\/pre>\n<\/p>\n \u4f7f\u7528\u8baf\u53f7\u91cf\u5b9e\u73b0\u591a\u7ebf\u7a0b\u540c\u6b65\uff08\u7ebf\u7a0b\u6267\u884c\u6b21\u5e8f\u6307\u5b9a\uff09\u5b9e\u4f8b\uff1a<\/p>\n \n <\/p>\nint number; \/\/ \u88ab\u4fdd\u62a4\u7684\u5168\u5c40\u53d8\u91cf\nsem_t semid1, semid2;\nvoid* thread_one(void *arg)\n{\n sem_wait(&semid1);\n printf(\"thread_one have the semaphoren\");\n number++;\n printf(\"number = %dn\",number);\n sem_post(&semid1);\n}\nvoid* thread_two(void *arg)\n{\n sem_wait(&semid2);\n printf(\"thread_two have the semaphore n\");\n number--;\n printf(\"number = %dn\",number);\n sem_post(&semid2);\n}\nint main(int argc, char *argv[])\n{\n number = 1;\n pthread_t tid1, tid2;\n sem_init(&semid1, 0, 1);\n sem_init(&semid2, 0, 0);\n pthread_create(&tid1,NULL,thread_one, NULL);\n pthread_create(&tid2,NULL,thread_two, NULL);\n pthread_join(tid1,NULL);\n pthread_join(tid2,NULL);\n printf(\"main running...n\");\n sem_destroy(&semid1);\n sem_destroy(&semid2);\n return 0;\n}<\/code><\/pre>\n<\/p>\n 3\u3001\u65e0\u540d\u8baf\u53f7\u91cf\u5728\u4eb2\u7f18\u8fdb\u7a0b\u95f4\u7684\u540c\u6b65<\/p>\n \n <\/p>\n#include \n#include \n#include \n#include \n#include \n#include \n#include \nint main(int argc, char **argv)\n{\n int fd, i, nloop=10, zero=0, *ptr;\n sem_t mutex;\n \/\/open a file and map it into memory\n fd = open(\"log.txt\",O_RDWR|O_CREAT,S_IRWXU);\n write(fd,&zero,sizeof(int));\n ptr = mmap( NULL,sizeof(int),PROT_READ |\n PROT_WRITE,MAP_SHARED,fd,0 );\n close(fd);\n \/* create, initialize semaphore *\/\n if( sem_init(&mutex,1,1) < 0) \/\/\n {\n perror("semaphore initilization");\n exit(0);\n }\n if (fork() == 0)\n { \/* child process*\/\n sem_wait(&mutex);\n for (i = 0; i < nloop; i++)\n {\n printf("child: %dn", (*ptr)++);\n }\n sem_post(&mutex);\n exit(0);\n }\n \/* back to parent process *\/\n sem_wait(&mutex);\n for (i = 0; i < nloop; i++)\n <\/p>\n {\n printf(\"parent: %dn\", (*ptr)++);\n }\n sem_post(&mutex);\n exit(0);\n}<\/code><\/pre>\n<\/p>\n \u4e09\u3001POSIX\u6709\u540d\u8baf\u53f7\u91cf<\/p>\n \u6709\u540d\u8baf\u53f7\u91cf\u7684\u7279\u5f81\u662f\u628a\u8baf\u53f7\u91cf\u7684\u503c\u4fdd\u5b58\u5728\u6587\u4ef6\u4e2d\uff0c\u65e2\u53ef\u4ee5\u7528\u4e8e\u7ebf\u7a0b\uff0c\u4e5f\u53ef\u4ee5\u7528\u4e8e\u4eb2\u7f18\u8fdb\u7a0b\u95f4\uff0c\u65e0\u4eb2\u7f18\u8fdb\u7a0b\u95f4\u3002<\/p>\n \n name\u662f\u6587\u4ef6\u7684\u8def\u5f84\u540d\uff0c\u5728linux\u4e2dsem\u90fd\u662f\u521b\u5efa\u5728\/dev\/shm\u76ee\u5f55\u4e0b\u3002name\u53ef\u4ee5\u5199\u6210\u201c\/mysem\u201d\u6216\u201cmysem\u201d\uff0c\u521b\u5efa\u4e0b\u6765\u7684\u6587\u4ef6\u90fd\u662f\u201c\/dev\/shm\/sem.mysem\u201d\uff0c\u5343\u4e07\u4e0d\u8981\u5199\u8def\u5f84\u3002<\/p>\n oflag\u6709O_CREAT\u6216O_CREAT|EXCL\u4e24\u4e2a\u53d6\u503c\uff1b<\/p>\n mode\u63a7\u5236\u65b0\u7684\u8baf\u53f7\u91cf\u7684\u8bbf\u95ee\u6743\u9650\uff1b<\/p>\n value\u6307\u5b9a\u8baf\u53f7\u91cf\u7684\u521d\u59cb\u5316\u503c\u3002<\/p>\n \n \u62ff\u56de\u8baf\u53f7\u91cfsem\u7684\u5f53\u524d\u503c\uff0c\u628a\u503c\u4fdd\u5b58\u5230sval\u4e2d\u3002<\/p>\n \u82e5\u67091\u4e2a\u6216\u66f4\u591a\u7684\u7ebf\u7a0b\u6216\u8fdb\u7a0b\u8c03\u7528sem_wait\u963b\u585e\u5728\u8be5\u8baf\u53f7\u91cf\u4e0a\uff0c\u8be5\u51fd\u6570\u8fd4\u56de\u4e24\u79cd\u503c\uff1a<\/p>\n 1)\u8fd4\u56de0<\/p>\n 2)\u8fd4\u56de\u963b\u585e\u5728\u8be5\u8baf\u53f7\u91cf\u4e0a\u7684\u8fdb\u7a0b\u6216\u7ebf\u7a0b\u6570\u91cf<\/p>\n linux\u91c7\u7528\u8fd4\u56de\u7684\u7b2c\u4e00\u79cd\u7b56\u7565\u3002<\/p>\n sem_wait(\u6216sem_trywait)\u76f8\u5f53\u4e8eP\u64cd\u4f5c\uff0c\u5373\u7533\u8bf7\u8d44\u6e90\u3002<\/p>\n \n \u6d4b\u8bd5\u6240\u6307\u5b9a\u8baf\u53f7\u91cf\u7684\u503c,\u5b83\u7684\u64cd\u4f5c\u662f\u539f\u5b50\u7684\u3002<\/p>\n \u82e5sem>0\uff0c\u8fd9\u4e48\u5b83\u51cf1\u5e76\u7acb\u523b\u8fd4\u56de\u3002<\/p>\n \u82e5sem==0\uff0c\u5219\u7761\u7720\u76f4\u81f3sem>0\uff0c\u6b64\u65f6\u7acb\u523b\u51cf1\uff0c\u4e4b\u540e\u8fd4\u56de\u3002<\/p>\n \n \u5176\u4ed6\u7684\u884c\u4e3a\u548csem_wait\u4e00\u6837\uff0c\u4e0d\u4ec5\uff1a<\/p>\n \u82e5sem==0\uff0c\u4e0d\u662f\u7761\u7720\uff0c\u800c\u662f\u8fd4\u56de\u4e00\u4e2a\u9519\u8befEAGAIN\u3002<\/p>\n sem_post\u76f8\u5f53\u4e8eV\u64cd\u4f5c\uff0c\u91ca\u653e\u8d44\u6e90\u3002<\/p>\n \n \u628a\u6307\u5b9a\u7684\u8baf\u53f7\u91cfsem\u7684\u503c\u52a01;<\/p>\n \u547c\u9192\u6b63\u5728\u7b49\u5f85\u8be5\u8baf\u53f7\u91cf\u7684\u4efb\u610f\u7ebf\u7a0b\u3002<\/p>\n \n \u56db\u3001SYSTEMV\u8baf\u53f7\u91cf<\/p>\n SYSTEMV\u8baf\u53f7\u91cf\u662fSYSTEMVIPC\u7684\u7ec4\u6210\u90e8\u4efd\uff0cSystemV\u8baf\u53f7\u91cf\u5728\u5185\u6838\u4e2d\u7ef4\u62a4\uff0c\u5176\u4e2d\u5305\u62ec\u4e8c\u503c\u8baf\u53f7\u91cf\u3001\u8ba1\u6570\u8baf\u53f7\u91cf\u3001\u8ba1\u6570\u8baf\u53f7\u91cf\u96c6\u3002<\/p>\n \u7cfb\u7edf\u4e2d\u8bb0\u5f55\u8baf\u53f7\u91cf\u7684\u6570\u636e\u7ed3\u6784\uff08structipc_idssem_ids\uff09\u5750\u843d\u5185\u6838\u4e2dlinux\u67e5\u770b\u4fe1\u53f7\u91cf\u547d\u4ee4<\/strong>\uff0c\u7cfb\u7edf\u4e2d\u7684\u6240\u6709\u8baf\u53f7\u91cf\u90fd\u53ef\u4ee5\u5728\u7ed3\u6784sem_ids\u4e2d\u627e\u5230\u8bbf\u95ee\u5165\u53e3\u3002<\/p>\n \n <\/p>\n} ;<\/code><\/pre>\n<\/p>\n \u8baf\u53f7\u91cf\u7f16\u7a0b\u7684\u6d41\u7a0b\uff1a<\/p>\n 1\u3001\u521b\u5efa\u8baf\u53f7\u91cf\u6216\u83b7\u5f97\u5728\u7cfb\u7edf\u5df2\u5b58\u5728\u7684\u8baf\u53f7\u91cf<\/p>\n \u8c03\u7528semget()\u51fd\u6570\uff0c\u4e0d\u540c\u8fdb\u7a0b\u4f7f\u7528\u540c\u4e00\u4e2a\u8baf\u53f7\u91cf\u901a\u914d\u7b26\u6765\u83b7\u5f97\u540c\u4e00\u4e2a\u8baf\u53f7\u91cf<\/p>\n \n \u8fd4\u56de\u503c\u662f\u4e00\u4e2a\u79f0\u4e3a\u8baf\u53f7\u91cf\u6807\u793a\u7b26\u7684\u6574\u6570\uff0csemop\u548csemctl\u51fd\u6570\u5c06\u4f7f\u7528\u5b83\u3002<\/p>\n key\uff1a\u6240\u521b\u5efa\u6216\u6253\u5f00\u8baf\u53f7\u91cf\u96c6\u7684\u901a\u914d\u7b26\u3002\u987b\u8981\u662f\u60df\u4e00\u7684\u975e\u96f6\u6574\u6570\u3002<\/p>\n nsem\uff1a\u521b\u5efa\u7684\u8baf\u53f7\u91cf\u96c6\u4e2d\u7684\u8baf\u53f7\u91cf\u7684\u4e2a\u6570\uff0c\u8be5\u53c2\u6570\u53ea\u5728\u521b\u5efa\u8baf\u53f7\u91cf\u96c6\u65f6\u6709\u6548\u3002\u901a\u5e38\u53d6\u503c\u4e3a1.<\/p>\n oflag\uff1a\u8c03\u7528\u51fd\u6570\u7684\u64cd\u4f5c\u7c7b\u578b\uff0c\u4e5f\u53ef\u7528\u4e8e\u8bbe\u7f6e\u8baf\u53f7\u91cf\u96c6\u7684\u8bbf\u95ee\u6743\u9650\uff0cSEM_R(read)\u548cSEM_A(alter)\uff0c\u4e5f\u53ef\u4ee5\u662fIPC_CREAT\u6216IPC_EXCL<\/p>\n 2\u3001\u521d\u59cb\u5316\u8baf\u53f7\u91cf<\/p>\n \u4f7f\u7528semctl()\u51fd\u6570\u7684SETVAL\u64cd\u4f5c<\/p>\n \u5f53\u4f7f\u7528\u4e8c\u7ef4\u8baf\u53f7\u91cf\u65f6\uff0c\u4e00\u822c\u5c06\u8baf\u53f7\u91cf\u521d\u59cb\u5316\u4e3a1<\/p>\n \n sem_id\u662f\u7531semget\u8fd4\u56de\u7684\u8baf\u53f7\u91cf\u6807\u793a\u7b26\u3002<\/p>\n sem_num\u662f\u8baf\u53f7\u91cf\u96c6\u4e2d\u7684\u67d0\u4e00\u4e2a\u8d44\u6e90<\/p>\n cmd\uff1a\u8868\u793a\u5373\u5c06\u91c7\u53d6\u7684\u52a8\u4f5c\u3002\u6700\u5e38\u7528\u7684\u4e24\u4e2a\u503c\u5982\u4e0b\uff1a<\/p>\n SETVAL\uff1a\u62ff\u6765\u628a\u8baf\u53f7\u91cf\u521d\u59cb\u5316\u4e3a\u4e00\u4e2a\u5df2\u77e5\u7684\u503c\u3002\u8fd9\u4e2a\u503c\u901a\u8fc7unionsemun\u4e2d\u7684val\u6210\u5458\u8bbe\u7f6e\u3002\u5176\u4f5c\u7528\u662f\u5728\u8baf\u53f7\u91cf\u7b2c\u4e00\u6b21\u4f7f\u7528\u4e4b\u524d\u5bf9\u5b83\u8fdb\u884c\u8bbe\u7f6e\u3002<\/p>\n IPC_RMID\uff1a\u7528\u4e8e\u5220\u6389\u4e00\u4e2a\u65e0\u9700\u7ee7\u7eed\u4f7f\u7528\u7684\u8baf\u53f7\u91cf\u6807\u5fd7\u7b26\u3002<\/p>\n \n 3\u3001\u8fdb\u884c\u8baf\u53f7\u91cf\u7684PV\u64cd\u4f5c<\/p>\n \u8c03\u7528semop()\u51fd\u6570<\/p>\n \u5b9e\u73b0\u8fdb\u7a0b\u4e4b\u95f4\u7684\u540c\u6b65\u548c\u4e92\u65a5\u7684\u6838\u5fc3\u90e8\u4efd<\/p>\n \n \u53c2\u6570semid\u662f\u4e00\u4e2a\u901a\u8fc7semget\u51fd\u6570\u8fd4\u56de\u7684\u4e00\u4e2a\u8baf\u53f7\u91cf\u6807\u793a\u7b26<\/p>\n \u53c2\u6570opsptr\u662f\u4e00\u4e2a\u8868\u9488\uff0c\u6307\u5411\u4e00\u4e2a\u8baf\u53f7\u91cf\u64cd\u4f5c\u5b57\u6bb5<\/p>\n \u53c2\u6570nops\u6807\u6ce8\u4e86\u53c2\u6570semoparray\u6240\u6307\u5411\u94fe\u8868\u4e2d\u7684\u5143\u7d20\u4e2a\u6570<\/p>\n \n 4\u3001\u5982\u679c\u4e0d\u987b\u8981\u8baf\u53f7\u91cf\uff0c\u5219\u4ece\u7cfb\u7edf\u4e2d\u5220\u6389\u5b83<\/p>\n \u4f7f\u7528semclt()\u51fd\u6570\u7684IPC_RMID\u64cd\u4f5c\uff0c\u5728\u7a0b\u5e8f\u4e2d\u4e0d\u5e94\u5f53\u51fa\u73b0\u5bf9\u5df2\u88ab\u5220\u6389\u7684\u8baf\u53f7\u91cf\u7684\u64cd\u4f5c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":" \u53ef\u7528\u4e8e\u7ebf\u7a0b\u3001\u8fdb\u7a0b\u95f4\u7684\u540c\u6b65\uff1b\u65e0\u540d\u4fe1\u53f7\u91cf\uff0c\u5176\u503c\u4fdd\u5b58\u5728\u5185\u5b58\u4e2d\u3002\u65e0\u540d\u4fe1\u53f7\u91cf\u5e38\u7528\u4e8e\u591a\u7ebf\u7a0b\u95f4\u7684\u540c\u6b65\uff0c\u4e5f\u53ef\u7528\u4e8e\u76f8\u5173\u8fdb\u7a0b\u95f4\u7684\u540c\u6b65\u3002\u65e0\u540d\u4fe1\u53f7\u91cf\u5fc5\u987b\u662f\u591a\u4e2a\u8fdb\u7a0b\uff08\u7ebf\u7a0b\uff09\u7684\u5171\u4eab\u53d8\u91cf\uff0c\u65e0\u540d\u4fe1\u53f7\u91cf\u8981\u4fdd\u62a4\u7684\u53d8\u91cf\u4e5f\u5fc5\u987b\u662f\u591a\u4e2a\u8fdb\u7a0b\uff08\u7ebf\u7a0b\uff09\u7684\u5171\u4eab\u53d8\u91cf\uff0c\u65e0\u540d\u4fe1\u53f7\u91cf\u7684\u503c\u4fdd\u5b58\u5728\u5185\u5b58\u4e2d\u3002\u8fd4\u56de\u963b\u585e\u5728\u8be5\u4fe1\u53f7\u91cf\u4e0a\u7684\u8fdb\u7a0b\u6216\u7ebf\u7a0b\u6570\u76eemode\u63a7\u5236\u65b0\u7684\u4fe1\u53f7\u91cf\u7684\u8bbf\u95ee\u6743\u9650\uff1b\u8fd4\u56de\u963b\u585e\u5728\u8be5\u4fe1\u53f7\u91cf\u4e0a\u7684\u8fdb\u7a0b\u6216\u7ebf\u7a0b\u6570\u76ee<\/p>\n","protected":false},"author":1,"featured_media":264161,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[988],"tags":[999],"class_list":["post-264160","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tougao","tag-999"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/264160","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=264160"}],"version-history":[{"count":0,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/264160\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/264161"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=264160"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=264160"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=264160"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}int sem_init(sem_t *sem, int pshared, unsigned int value);<\/code><\/pre>\n<\/p>\n
int sem_destroy(sem_t *sem);<\/code><\/pre>\n<\/p>\n
int sem_getvalue(sem_t *sem, int *sval);<\/code><\/pre>\n<\/p>\n
int sem_wait(sem_t *sem); \/\/ \u963b\u585e\u7684\u51fd\u6570<\/code><\/pre>\n<\/p>\n
int sem_trywait(sem_t *sem); \/\/ \u975e\u963b\u585e\u7684\u51fd\u6570<\/code><\/pre>\n<\/p>\n
int sem_post(sem_t *sem);<\/code><\/pre>\n<\/p>\n
#include \n#include \n#include \n
#include \n#include \n#include \n#include \n#include \n
#include \n#include \n
sem_t *sem_open(const char *name, int oflag, mode_t mode , int value);<\/code><\/pre>\n<\/p>\n
int sem_getvalue(sem_t *sem, int *sval);<\/code><\/pre>\n<\/p>\n
int sem_wait(sem_t *sem); \/\/ \u963b\u585e\u7684\u51fd\u6570<\/code><\/pre>\n<\/p>\n
int sem_trywait(sem_t *sem); \/\/ \u975e\u963b\u585e\u7684\u51fd\u6570<\/code><\/pre>\n<\/p>\n
int sem_post(sem_t *sem);<\/code><\/pre>\n<\/p>\n
int sem_close(sem_t *sem);\nint sem_unlink(const char *name);<\/code><\/pre>\n<\/p>\n
struct semid_ds {\n struct ipc_permsem_perm ;\n struct\u00a0sem* sem_base ; \/\/\u4fe1\u53f7\u6570\u7ec4\u6307\u9488\n ushort sem_nsem ; \/\/\u6b64\u96c6\u4e2d\u4fe1\u53f7\u4e2a\u6570\n time_t sem_otime ; \/\/\u6700\u540e\u4e00\u6b21semop\u65f6\u95f4\n time_t sem_ctime ; \/\/\u6700\u540e\u4e00\u6b21\u521b\u5efa\u65f6\u95f4\n
#include \nint semget (key_t key, int nsem, int oflag) ;<\/code><\/pre>\n<\/p>\n
int semctl(int semid, int semnum, int cmd, ...);<\/code><\/pre>\n<\/p>\n
union semun {\n int \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0val; \u00a0\u00a0\u00a0\/* Value for SETVAL *\/\n struct semid_ds *buf; \u00a0\u00a0\u00a0\/* Buffer for IPC_STAT, IPC_SET *\/\n unsigned short \u00a0*array; \u00a0\/* Array for GETALL, SETALL *\/\n struct seminfo \u00a0*__buf; \u00a0\/* Buffer for IPC_INFO *\/\n};<\/code><\/pre>\n<\/p>\n
#include \nint semop (int semid, struct sembuf * opsptr, size_t nops) ;<\/code><\/pre>\n<\/p>\n
struct\u00a0sembuf{\u00a0\u00a0\n \/\/\u9664\u975e\u4f7f\u7528\u4e00\u7ec4\u4fe1\u53f7\u91cf\uff0c\u5426\u5219\u5b83\u4e3a0\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0short\u00a0sem_num;\n \/\/\u4fe1\u53f7\u91cf\u5728\u4e00\u6b21\u64cd\u4f5c\u4e2d\u9700\u8981\u6539\u53d8\u7684\u6570\u636e\uff0c\u901a\u5e38\u662f\u4e24\u4e2a\u6570\uff0c\u4e00\u4e2a\u662f-1\uff0c\u5373P\uff08\u7b49\u5f85\uff09\u64cd\u4f5c\uff0c\n \/\/ \u4e00\u4e2a\u662f+1\uff0c\u5373V\uff08\u53d1\u9001\u4fe1\u53f7\uff09\u64cd\u4f5c\u3002\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0short\u00a0sem_op;\n\u00a0\u00a0\u00a0\u00a0short\u00a0sem_flg;\/\/\u901a\u5e38\u4e3aSEM_UNDO,\u4f7f\u64cd\u4f5c\u7cfb\u7edf\u8ddf\u8e2a\u4fe1\u53f7\uff0c\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0\/\/\u5e76\u5728\u8fdb\u7a0b\u6ca1\u6709\u91ca\u653e\u8be5\u4fe1\u53f7\u91cf\u800c\u7ec8\u6b62\u65f6\uff0c\u64cd\u4f5c\u7cfb\u7edf\u91ca\u653e\u4fe1\u53f7\u91cf\u00a0\u00a0\n};\u00a0\u00a0<\/code><\/pre>\n<\/p>\n