{"id":75520,"date":"2024-01-05T10:29:55","date_gmt":"2024-01-05T02:29:55","guid":{"rendered":"https:\/\/www.techphant.cn\/?p=75520"},"modified":"2024-01-05T10:29:55","modified_gmt":"2024-01-05T02:29:55","slug":"rhhqdqjcidff","status":"publish","type":"post","link":"https:\/\/www.techphant.cn\/blog\/75520.html","title":{"rendered":"\u5982\u4f55\u83b7\u53d6\u5f53\u524d\u8fdb\u7a0bID\u7684\u65b9\u6cd5"},"content":{"rendered":"

  \u5728\u8ba1\u7b97\u673a\u79d1\u5b66\u9886\u57df\uff0c\u83b7\u53d6\u5f53\u524d\u8fdb\u7a0bID\u662f\u4e00\u9879\u975e\u5e38\u5e38\u89c1\u7684\u4efb\u52a1\uff0c\u4eca\u5929\u6211\u4eec\u5c06\u4ecb\u7ecd\u4e00\u4e9b\u5e38\u7528\u7684\u65b9\u6cd5\u548c\u6280\u5de7\u6765\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u3002<\/p>\n

\u4f7f\u7528C\u8bed\u8a00\u83b7\u53d6\u5f53\u524d\u8fdb\u7a0bID<\/h2>\n

  \u4f7f\u7528getpid\u51fd\u6570<\/h3>\n

  \u5728C\u8bed\u8a00\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528getpid\u51fd\u6570\u6765\u83b7\u53d6\u5f53\u524d\u8fdb\u7a0bID\u3002\u8be5\u51fd\u6570\u901a\u5e38\u5728unistd.h\u5934\u6587\u4ef6\u4e2d\u58f0\u660e\uff0c\u5e76\u4e14\u8fd4\u56de\u4e00\u4e2a\u6574\u6570\u503c\uff0c\u4ee3\u8868\u5f53\u524d\u8fdb\u7a0b\u7684ID\u3002\u4f8b\u5982\uff1a<\/p>\n

#include <\/p>\n

   #include <\/p>\n

   int main() {<\/p>\n

   pid_t pid;<\/p>\n

   pid = getpid();<\/p>\n

   printf(\"\u5f53\u524d\u8fdb\u7a0bID\u4e3a\uff1a%dn\", pid);<\/p>\n

   return 0;<\/p>\n

  }<\/p>\n

<\/code><\/p>\n

  \u4f7f\u7528proc\u6587\u4ef6\u7cfb\u7edf<\/h3>\n

  \u5728Linux\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bfb\u53d6\/proc\/self\/status\u6587\u4ef6\u6765\u83b7\u53d6\u5f53\u524d\u8fdb\u7a0bID\u3002\u8be5\u6587\u4ef6\u5305\u542b\u4e86\u5927\u91cf\u5173\u4e8e\u5f53\u524d\u8fdb\u7a0b\u7684\u4fe1\u606f\uff0c\u5305\u62ec\u8fdb\u7a0bID\u3002\u4f8b\u5982\uff1a<\/p>\n

#include <\/p>\n

   int main() {<\/p>\n

   FILE *fp;<\/p>\n

   char str[100];<\/p>\n

   fp = fopen(\"\/proc\/self\/status\", \"r\");<\/p>\n

   while (fgets(str, 100, fp) != NULL) {<\/p>\n

   if (strncmp(str, \"Pid:\", 4) == 0) {<\/p>\n

   printf(\"\u5f53\u524d\u8fdb\u7a0bID\u4e3a\uff1a%s\", str + 5);<\/p>\n

   break;<\/p>\n

   }<\/p>\n

   }<\/p>\n

   fclose(fp);<\/p>\n

   return 0;<\/p>\n

  }<\/p>\n

<\/code><\/p>\n

\u4f7f\u7528Python\u83b7\u53d6\u5f53\u524d\u8fdb\u7a0bID<\/h2>\n

  \u4f7f\u7528os\u6a21\u5757<\/h3>\n

  \u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528os\u6a21\u5757\u6765\u83b7\u53d6\u5f53\u524d\u8fdb\u7a0bID\u3002os\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2a\u540d\u4e3agetpid\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u76f4\u63a5\u8fd4\u56de\u5f53\u524d\u8fdb\u7a0bID\u3002\u4f8b\u5982\uff1a<\/p>\n

import os<\/p>\n

   pid = os.getpid()<\/p>\n

   print(\"\u5f53\u524d\u8fdb\u7a0bID\u4e3a\uff1a\", pid)<\/p>\n

  <\/code><\/p>\n

  \u4f7f\u7528multiprocessing\u6a21\u5757<\/h3>\n

  \u5982\u679c\u9700\u8981\u5728Python\u4e2d\u83b7\u53d6\u5176\u4ed6\u8fdb\u7a0b\u7684ID\uff0c\u53ef\u4ee5\u4f7f\u7528multiprocessing\u6a21\u5757\u3002\u8be5\u6a21\u5757\u63d0\u4f9b\u4e86Process\u7c7b\uff0c\u53ef\u4ee5\u521b\u5efa\u65b0\u7684\u8fdb\u7a0b\uff0c\u5e76\u83b7\u53d6\u5176\u8fdb\u7a0bID\u3002\u4f8b\u5982\uff1a<\/p>\n

from multiprocessing import Process<\/p>\n

   import os<\/p>\n

   def f():<\/p>\n

   print(\"\u5b50\u8fdb\u7a0bID\u4e3a\uff1a\", os.getpid())<\/p>\n

   p = Process(target=f)<\/p>\n

   p.start()<\/p>\n

   p.join()<\/p>\n

  <\/code><\/p>\n

\u603b\u7ed3<\/h2>\n

  \u83b7\u53d6\u5f53\u524d\u8fdb\u7a0bID\u662f\u7f16\u5199\u7cfb\u7edf\u7ea7\u7a0b\u5e8f\u548c\u8fdb\u884c\u8fdb\u7a0b\u95f4\u901a\u4fe1\u7684\u5173\u952e\u6b65\u9aa4\u3002<\/strong>\u65e0\u8bba\u662f\u5728C\u8bed\u8a00\u8fd8\u662fPython\u4e2d\uff0c\u6211\u4eec\u90fd\u53ef\u4ee5\u8f7b\u677e\u5730\u83b7\u53d6\u5f53\u524d\u8fdb\u7a0bID\uff0c\u4ece\u800c\u5b9e\u73b0\u5404\u79cd\u529f\u80fd\u3002\u901a\u8fc7\u672c\u6587\u4ecb\u7ecd\u7684\u65b9\u6cd5\uff0c\u8bfb\u8005\u53ef\u4ee5\u66f4\u597d\u5730\u7406\u89e3\u8fdb\u7a0bID\u7684\u6982\u5ff5\uff0c\u5e76\u4e14\u638c\u63e1\u83b7\u53d6\u5f53\u524d\u8fdb\u7a0bID\u7684\u6280\u5de7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u5728\u8ba1\u7b97\u673a\u79d1\u5b66\u9886\u57df\uff0c\u83b7\u53d6\u5f53\u524d\u8fdb\u7a0bID\u662f\u4e00\u9879\u975e\u5e38\u5e38\u89c1\u7684\u4efb\u52a1\uff0c\u672c\u6587\u5c06\u4ecb\u7ecd\u4e00\u4e9b\u5e38\u7528\u7684\u65b9\u6cd5\u548c\u6280\u5de7\u6765\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u3002\u4f7f\u7528C\u8bed\u8a00\u83b7\u53d6\u5f53\u524d\u8fdb\u7a0bID\u4f7f\u7528getpid\u51fd\u6570<\/p>\n","protected":false},"author":1,"featured_media":64183,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-global-header-display":"","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":"","footnotes":""},"categories":[36],"tags":[305],"class_list":["post-75520","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","tag-dzjs"],"_links":{"self":[{"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/posts\/75520","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/comments?post=75520"}],"version-history":[{"count":0,"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/posts\/75520\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/media\/64183"}],"wp:attachment":[{"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/media?parent=75520"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/categories?post=75520"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/tags?post=75520"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}