{"id":1388,"date":"2016-10-30T02:29:25","date_gmt":"2016-10-29T18:29:25","guid":{"rendered":"http:\/\/cn.hostease.com\/xueyuan\/?p=1388"},"modified":"2016-10-30T02:29:25","modified_gmt":"2016-10-29T18:29:25","slug":"linuxunix%e4%b8%8bpid%e6%96%87%e4%bb%b6%e4%bd%9c%e7%94%a8%e6%b5%85%e6%9e%90","status":"publish","type":"post","link":"https:\/\/cn.hostease.com\/xueyuan\/jishu\/linux\/linuxunix%e4%b8%8bpid%e6%96%87%e4%bb%b6%e4%bd%9c%e7%94%a8%e6%b5%85%e6%9e%90\/","title":{"rendered":"Linux\/Unix\u4e0bpid\u6587\u4ef6\u4f5c\u7528\u6d45\u6790"},"content":{"rendered":"<div>\n<p>(1) pid\u6587\u4ef6\u7684\u5185\u5bb9\uff1apid\u6587\u4ef6\u4e3a\u6587\u672c\u6587\u4ef6\uff0c\u5185\u5bb9\u53ea\u6709\u4e00\u884c, \u8bb0\u5f55\u4e86\u8be5\u8fdb\u7a0b\u7684ID\u3002<\/p>\n<p>\u7528cat\u547d\u4ee4\u53ef\u4ee5\u770b\u5230\u3002<\/p>\n<p>(2) pid\u6587\u4ef6\u7684\u4f5c\u7528\uff1a\u9632\u6b62\u8fdb\u7a0b\u542f\u52a8\u591a\u4e2a\u526f\u672c\u3002\u53ea\u6709\u83b7\u5f97pid\u6587\u4ef6(\u56fa\u5b9a\u8def\u5f84\u56fa\u5b9a\u6587\u4ef6\u540d)\u5199\u5165\u6743\u9650(F_WRLCK)\u7684\u8fdb\u7a0b\u624d\u80fd\u6b63\u5e38\u542f\u52a8\u5e76\u628a\u81ea\u8eab\u7684PID\u5199\u5165\u8be5\u6587\u4ef6\u4e2d\u3002\u5176\u5b83\u540c\u4e00\u4e2a\u7a0b\u5e8f\u7684\u591a\u4f59\u8fdb\u7a0b\u5219\u81ea\u52a8\u9000\u51fa\u3002<\/p>\n<p>(3) \u7f16\u7a0b\u6280\u5de7\uff1a<\/p>\n<p>\u8c03\u7528fcntl\u8bbe\u7f6epid\u6587\u4ef6\u7684\u9501\u5b9aF_SETLK\u72b6\u6001\uff0c\u5176\u4e2d\u9501\u5b9a\u7684\u6807\u5fd7\u4f4dF_WRLCK\u3002<\/p>\n<p>\u5982\u679c\u6210\u529f\u9501\u5b9a\uff0c\u5219\u5199\u5165\u8fdb\u7a0b\u5f53\u524dPID\uff0c\u8fdb\u7a0b\u7ee7\u7eed\u5f80\u4e0b\u6267\u884c\u3002<\/p>\n<p>\u5982\u679c\u9501\u5b9a\u4e0d\u6210\u529f\uff0c\u8bf4\u660e\u5df2\u7ecf\u6709\u540c\u6837\u7684\u8fdb\u7a0b\u5728\u8fd0\u884c\u4e86\uff0c\u5f53\u524d\u8fdb\u7a0b\u7ed3\u675f\u9000\u51fa\u3002<br \/>\nlock.l_type = F_WRLCK;<br \/>\nlock.l_whence = SEEK_SET;<\/p>\n<p>if (fcntl(fd, F_SETLK, &amp;lock) &lt; 0){<br \/>\n\/\/\u9501\u5b9a\u4e0d\u6210\u529f, \u9000\u51fa&#8230;&#8230;<br \/>\n}<br \/>\nsprintf (buf, &#8220;%d\\n&#8221;, (int) pid);<br \/>\npidsize = strlen(buf);<br \/>\nif ((tmp = write (fd, buf, pidsize)) != (int)pidsize){<br \/>\n\/\/\u5199\u5165\u4e0d\u6210\u529f, \u9000\u51fa&#8230;&#8230;<br \/>\n}<\/p>\n<p>(4) \u4e00\u4e9b\u6ce8\u610f\u4e8b\u9879\uff1a<br \/>\ni) \u5982\u679c\u8fdb\u7a0b\u9000\u51fa\uff0c\u5219\u8be5\u8fdb\u7a0b\u52a0\u7684\u9501\u81ea\u52a8\u5931\u6548\u3002<br \/>\nii) \u5982\u679c\u8fdb\u7a0b\u5173\u95ed\u4e86\u8be5\u6587\u4ef6\u63cf\u8ff0\u7b26fd\uff0c \u5219\u52a0\u7684\u9501\u5931\u6548\u3002(\u6574\u4e2a\u8fdb\u7a0b\u8fd0\u884c\u671f\u95f4\u4e0d\u80fd\u5173\u95ed\u6b64\u6587\u4ef6\u63cf\u8ff0\u7b26)<br \/>\niii) \u9501\u7684\u72b6\u6001\u4e0d\u4f1a\u88ab\u5b50\u8fdb\u7a0b\u7ee7\u627f\u3002\u5982\u679c\u8fdb\u7a0b\u5173\u95ed\u5219\u9501\u5931\u6548\u800c\u4e0d\u7ba1\u5b50\u8fdb\u7a0b\u662f\u5426\u5728\u8fd0\u884c\u3002<br \/>\n(Locks are associated with processes. A process can only have one kind of lock set for each byte of a given file. When any file descriptor for that file is closed by the process, all of the locks that process holds on that file are released, even if the locks were made using other descriptors that remain open. Likewise, locks are released when a process exits, and are not inherited by child processes created using fork.)<\/p>\n<p>(5) \u53c2\u8003\u8d44\u6599\uff1a<br \/>\nfcntl(\u6587\u4ef6\u9501)<br \/>\n\u8868\u5934\u6587\u4ef6 #include<br \/>\n#include<br \/>\n\u51fd\u6570\u5b9a\u4e49int fcntl(int fd, int cmd, struct flock *lock);<br \/>\n\u51fd\u6570\u89e3\u91cafd:\u6587\u4ef6\u63cf\u5199\u7b26<br \/>\n\u8bbe\u7f6e\u7684\u6587\u4ef6\u63cf\u5199\u7b26\uff0c\u53c2\u6570cmd\u4ee3\u8868\u6b32\u5784\u65ad\u7684\u53f7\u53ec<br \/>\nF_DUPFD<br \/>\n\u590d\u5236\u53c2\u6570fd\u7684\u6587\u4ef6\u63cf\u5199\u7b26\uff0c\u5389\u884c\u83b7\u80dc\u5219\u5f52\u6765\u65b0\u590d\u5236\u7684\u6587\u4ef6\u63cf\u5199\u7b26\uff0c<br \/>\nF_GETFD<br \/>\n\u83b7\u5f97close-on-exec\u7b26\u53f7\uff0c\u82e5\u4e9b\u7b26\u53f7\u7684FD_CLOEXEC\u4f4d\u4e3a0\uff0c\u4ee3\u8868\u5728\u8c03\u7528<br \/>\nexec()\u76f8\u5e72\u51fd\u6570\u65f6\u6587\u4ef6\u5c06\u4e0d\u4f1a\u5173\u95ed<br \/>\nF_SETFD \u8bbe\u7f6eclose-on-exec\u7b26\u53f7\uff0c\u8be5\u7b26\u53f7\u4ee5\u53c2\u6570arg\u7684 FD_CLOEXEC\u4f4d\u5b9a\u593a<br \/>\nF_GETFL\u83b7\u5f97open()\u8bbe\u7f6e\u7684\u7b26\u53f7<br \/>\nF_SETFL\u6539\u6362open()\u8bbe\u7f6e\u7684\u7b26\u53f7<br \/>\nF_GETLK\u83b7\u5f97\u6587\u4ef6\u9501\u5b9a\u7684\u4e8b\u6001\uff0c\u4f9d\u636elock\u7684\u63cf\u5199\uff0c\u5b9a\u593a\u662f\u5426\u4e0a\u6587\u4ef6\u9501<br \/>\nF_SETLK\u8bbe\u7f6e\u6587\u4ef6\u9501\u5b9a\u7684\u4e8b\u6001\uff0c\u6b64\u523bflcok\uff0c\u6784\u9020\u7684l_tpye\u503c\u5b9a\u7136\u662fF_RDLCK\u3001F_WRLCK\u6216F_UNLCK\uff0c<br \/>\n\u4e07\u4e00\u65e0\u6cd5\u53d1\u751f\u9501\u5b9a\uff0c\u5219\u5f52\u6765-1<br \/>\nF_SETLKW \u662fF_SETLK\u7684\u963b\u585e\u7248\u672c\uff0c\u5728\u65e0\u6cd5\u83b7\u5f97\u9501\u65f6\u4f1a\u8fdb\u53bb\u7761\u7720\u4e8b\u6001\uff0c\u4e07\u4e00\u80fd\u591f\u83b7\u5f97\u9501\u53ef\u80fd\u6349\u62ff\u5230\u4fe1\u53f7\u5219\u5f52\u6765<br \/>\n\u53c2\u6570lock\u6307\u9488\u4e3aflock\u6784\u9020\u6307\u9488\u5b9a\u4e49\u5982\u4e0b<br \/>\nstruct flock {<br \/>\n&#8230;<br \/>\nshort l_typejngaoy.com;<br \/>\nshort l_whence;<br \/>\noff_t l_start; \u9501\u5b9a\u533a\u57df\u7684\u5f00\u5173\u4f4d\u7f6e<br \/>\noff_t l_len; \u9501\u5b9a\u533a\u57df\u7684\u5927\u5c0f<br \/>\npid_t l_pid; \u9501\u5b9a\u52a8\u4f5c\u7684\u5386\u7a0b<br \/>\n&#8230;<br \/>\n};<br \/>\n1_type\u6709\u4e09\u79cd\u4e8b\u6001\uff1a<br \/>\nF_RDLCK\u8bfb\u53d6\u9501\uff08\u5206\u4eab\u9501\uff09<br \/>\nF_WRLCK\u5199\u5165\u9501\uff08\u6392\u65a5\u9501\uff09<br \/>\nF_UNLCK\u89e3\u9501<br \/>\nl_whence\u4e5f\u6709\u4e09\u79cd\u63aa\u65bd<br \/>\nSEEK_SET\u4ee5\u6587\u4ef6\u5f00\u59cb\u4e3a\u9501\u5b9a\u7684\u8d77\u59cb\u4f4d\u7f6e<br \/>\nSEEK_CUR\u4ee5\u73b0\u5728\u6587\u4ef6\u8bfb\u5199\u4f4d\u7f6e\u4e3a\u9501\u5b9a\u7684\u8d77\u59cb\u4f4d\u7f6e<br \/>\nSEEK_END\u4ee5\u6587\u4ef6\u5c3e\u4e3a\u9501\u5b9a\u7684\u8d77\u59cb\u4f4d\u7f6e<br \/>\n\u5f52\u6765\u503c \u83b7\u80dc\u5219\u5f52\u67650\uff0c\u82e5\u6709\u8bb9\u8c2c\u5219\u5f52\u6765-1<br \/>\nl_len:\u52a0\u9501\u533a\u7684\u957f\u5ea6<br \/>\nl_pid:\u5177\u6709\u963b\u585e\u76ee\u524d\u5386\u7a0b\u7684\u9501\uff0c\u5176\u6301\u6709\u5386\u7a0b\u7684\u5386\u7a0b\u53f7\u50a8\u85cf\u5728l_pid\u4e2d\uff0c\u7531F_GETLK\u5f52\u6765<br \/>\n\u7b49\u95f2\u662f\u5c06l_start\u8bbe\u7f6e\u4e3a0,l_whence\u8bbe\u7f6e\u4e3aSEEK_SET,l_len\u8bbe\u7f6e\u4e3a0<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>(1) pid\u6587\u4ef6\u7684\u5185\u5bb9\uff1apid\u6587\u4ef6\u4e3a\u6587\u672c\u6587\u4ef6\uff0c\u5185\u5bb9\u53ea\u6709\u4e00\u884c, \u8bb0\u5f55\u4e86\u8be5\u8fdb\u7a0b\u7684ID\u3002 \u7528cat\u547d\u4ee4\u53ef\u4ee5\u770b\u5230\u3002  &#8230; <a title=\"Linux\/Unix\u4e0bpid\u6587\u4ef6\u4f5c\u7528\u6d45\u6790\" class=\"read-more\" href=\"https:\/\/cn.hostease.com\/xueyuan\/jishu\/linux\/linuxunix%e4%b8%8bpid%e6%96%87%e4%bb%b6%e4%bd%9c%e7%94%a8%e6%b5%85%e6%9e%90\/\" aria-label=\"\u9605\u8bfb Linux\/Unix\u4e0bpid\u6587\u4ef6\u4f5c\u7528\u6d45\u6790\">\u9605\u8bfb\u66f4\u591a<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[49],"tags":[],"class_list":["post-1388","post","type-post","status-publish","format-standard","hentry","category-linux"],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/posts\/1388","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/comments?post=1388"}],"version-history":[{"count":1,"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/posts\/1388\/revisions"}],"predecessor-version":[{"id":1389,"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/posts\/1388\/revisions\/1389"}],"wp:attachment":[{"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/media?parent=1388"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/categories?post=1388"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/tags?post=1388"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}