{"id":195097,"date":"2020-07-11T10:00:40","date_gmt":"2020-07-11T02:00:40","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=195097"},"modified":"2020-06-28T08:57:36","modified_gmt":"2020-06-28T00:57:36","slug":"linux-private-transparently","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/linux-private-transparently.html","title":{"rendered":"\u5927\u795e\u6559\u4f60C++\u5199\u65f6\u62f7\u8d1d\u5b9e\u73b0\u539f\u7406\u53ca\u5b9e\u4f8b\u89e3\u6790"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4ecb\u7ecd\u4e86C++\u5199\u65f6\u62f7\u8d1d\u5b9e\u73b0\u539f\u7406\u53ca\u5b9e\u4f8b\u89e3\u6790,\u6587\u4e2d\u901a\u8fc7\u793a\u4f8b\u4ee3\u7801\u4ecb\u7ecd\u7684\u975e\u5e38\u8be6\u7ec6\uff0c\u5bf9\u5927\u5bb6\u7684\u5b66\u4e60\u6216\u8005\u5de5\u4f5c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u5b66\u4e60\u4ef7\u503c,\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e0b<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u4e00\u3001\u4ec0\u4e48\u662f\u5199\u65f6\u62f7\u8d1d<\/strong><\/div>\n

\u5199\u5165\u65f6\u590d\u5236\u662f\u4e00\u79cd\u8ba1\u7b97\u673a\u7a0b\u5e8f\u8bbe\u8ba1\u9886\u57df\u7684\u4f18\u5316\u7b56\u7565\u3002\u5176\u6838\u5fc3\u601d\u60f3\u662f\uff0c\u5982\u679c\u6709\u591a\u4e2a\u8c03\u7528\u8005\u540c\u65f6\u8bf7\u6c42\u76f8\u540c\u8d44\u6e90\uff08\u5982\u5185\u5b58\u6216\u78c1\u76d8\u4e0a\u7684\u6570\u636e\u5b58\u50a8\uff09\uff0c\u4ed6\u4eec\u4f1a\u5171\u540c\u83b7\u53d6\u76f8\u540c\u7684\u6307\u9488\u6307\u5411\u76f8\u540c\u7684\u8d44\u6e90\uff0c\u76f4\u5230\u67d0\u4e2a\u8c03\u7528\u8005\u8bd5\u56fe\u4fee\u6539\u8d44\u6e90\u7684\u5185\u5bb9\u65f6\uff0c\u7cfb\u7edf\u624d\u4f1a\u771f\u6b63\u590d\u5236\u4e00\u4efd\u4e13\u7528\u526f\u672c\uff08private copy\uff09\u7ed9\u8be5\u8c03\u7528\u8005\uff0c\u800c\u5176\u4ed6\u8c03\u7528\u8005\u6240\u89c1\u5230\u7684\u6700\u521d\u7684\u8d44\u6e90\u4ecd\u7136\u4fdd\u6301\u4e0d\u53d8\u3002<\/p>\n

\u8fd9\u4e2a\u8fc7\u7a0b\u5bf9\u5176\u4ed6\u7684\u8c03\u7528\u8005\u662f\u900f\u660e\u7684\uff08transparently\uff09\u3002<\/p>\n

\u6b64\u4f5c\u6cd5\u7684\u4e3b\u8981\u4f18\u70b9\u662f\u5982\u679c\u8c03\u7528\u8005\u6ca1\u6709\u4fee\u6539\u8be5\u8d44\u6e90\uff0c\u5c31\u4e0d\u4f1a\u6709\u526f\u672c\u88ab\u5efa\u7acb\uff0c\u56e0\u6b64\u591a\u4e2a\u8c03\u7528\u8005\u53ea\u662f\u8bfb\u53d6\u64cd\u4f5c\u662f\u53ef\u4ee5\u5171\u4eab\u540c\u4e00\u4efd\u8d44\u6e90\u3002<\/p>\n

\u5199\u65f6\u62f7\u8d1d\u6280\u672f\u662f\u4e00\u79cd\u5f88\u91cd\u8981\u7684\u4f18\u5316\u624b\u6bb5\uff0c\u6838\u5fc3\u662f\u61d2\u60f0\u5904\u7406\u5b9e\u4f53\u8d44\u6e90\u8bf7\u6c42\uff0c\u5728\u591a\u4e2a\u5b9e\u4f53\u8d44\u6e90\u4e4b\u95f4\u53ea\u662f\u5171\u4eab\u8d44\u6e90\uff0c\u8d77\u521d\u662f\u5e76\u4e0d\u771f\u6b63\u5b9e\u73b0\u8d44\u6e90\u62f7\u8d1d\uff0c\u53ea\u6709\u5f53\u5b9e\u4f53\u6709\u9700\u8981\u5bf9\u8d44\u6e90\u8fdb\u884c\u4fee\u6539\u65f6\u624d\u771f\u6b63\u4e3a\u5b9e\u4f53\u5206\u914d\u79c1\u6709\u8d44\u6e90\u3002\u4f46\u5199\u65f6\u62f7\u8d1d\u6280\u672f\u6280\u672f\u4e5f\u6709\u5b83\u7684\u4f18\u70b9\u548c\u7f3a\u70b9\uff1a<\/p>\n

1\u3001\u5199\u65f6\u62f7\u8d1d\u6280\u672f\u53ef\u4ee5\u51cf\u5c11\u5206\u914d\u548c\u590d\u5236\u5927\u91cf\u8d44\u6e90\u65f6\u5e26\u6765\u7684\u77ac\u95f4\u5ef6\u65f6\uff0c\u4f46\u5b9e\u9645\u4e0a\u662f\u5c06\u8fd9\u79cd\u5ef6\u65f6\u9644\u52a0\u5230\u4e86\u540e\u7eed\u7684\u64cd\u4f5c\u4e4b\u4e2d\u3002<\/p>\n

2\u3001\u5199\u65f6\u62f7\u8d1d\u6280\u672f\u53ef\u4ee5\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u8d44\u6e90\u5206\u914d\u3002\u6bd4\u5982fork\u8fdb\u7a0b\u65f6\uff0c\u5e76\u4e0d\u662f\u6240\u6709\u7684\u9875\u9762\u90fd\u9700\u8981\u590d\u5236\uff0c\u7236\u8fdb\u7a0b\u7684\u4ee3\u7801\u6bb5\u548c\u53ea\u8bfb\u6570\u636e\u6bb5\u90fd\u4e0d\u88ab\u5141\u8bb8\u4fee\u6539\uff0c\u6240\u4ee5\u65e0\u9700\u590d\u5236\u3002<\/p>\n

\u4e8c\u3001\u5199\u65f6\u62f7\u8d1d\u6280\u672f\u7684\u5e94\u7528<\/strong><\/div>\n
1\u3001\u865a\u62df\u5185\u5b58\u7ba1\u7406\u4e2d\u7684\u5199\u65f6\u590d\u5236<\/strong><\/span><\/div>\n

\u4e00\u822c\u628a\u8fd9\u79cd\u88ab\u5171\u4eab\u8bbf\u95ee\u7684\u9875\u9762\u6807\u8bb0\u4e3a\u53ea\u8bfb\u3002\u5f53\u4e00\u4e2atask\u8bd5\u56fe\u5411\u5185\u5b58\u4e2d\u5199\u5165\u6570\u636e\u65f6\uff0c\u5185\u5b58\u7ba1\u7406\u5355\u5143\uff08MMU\uff09\u629b\u51fa\u4e00\u4e2a\u5f02\u5e38\uff0c\u5185\u6838\u5904\u7406\u8be5\u5f02\u5e38\u65f6\u4e3a\u8be5task\u5206\u914d\u4e00\u4efd\u7269\u7406\u5185\u5b58\u5e76\u590d\u5236\u6570\u636e\u5230\u6b64\u5185\u5b58\uff0c\u91cd\u65b0\u5411MMU\u53d1\u51fa\u6267\u884c\u8be5task\u7684\u5199\u64cd\u4f5c\u3002<\/p>\n

\u6bd4\u5982Linux\u7684fork()\u4f7f\u7528\u5199\u65f6\u62f7\u8d1d\u9875\u6765\u5b9e\u73b0\u65b0\u8fdb\u7a0b\u7684\u521b\u5efa\uff0c\u5b83\u662f\u4e00\u79cd\u53ef\u63a8\u8fdf\u751a\u81f3\u907f\u514d\u6570\u636e\u62f7\u8d1d\u7684\u6280\u672f\uff0c\u5f00\u59cb\u65f6\u5185\u6838\u5e76\u4e0d\u4f1a\u590d\u5236\u6574\u4e2a\u5730\u5740\u7a7a\u95f4\uff0c\u800c\u662f\u8ba9\u7236\u5b50\u8fdb\u7a0b\u5171\u4eab\u5730\u5740\u7a7a\u95f4\uff0c\u53ea\u6709\u5728\u5199\u65f6\u624d\u590d\u5236\u5730\u5740\u7a7a\u95f4\uff0c\u4f7f\u5f97\u7236\u5b50\u8fdb\u7a0b\u90fd\u62e5\u6709\u72ec\u7acb\u7684\u5730\u5740\u7a7a\u95f4\uff0c\u5373\u8d44\u6e90\u7684\u590d\u5236\u662f\u5728\u53ea\u6709\u9700\u8981\u5199\u5165\u65f6\u624d\u4f1a\u53d1\u751f\u3002\u5728\u6b64\u4e4b\u524d\u90fd\u662f\u4ee5\u8bfb\u7684\u65b9\u5f0f\u53bb\u548c\u7236\u8fdb\u7a0b\u5171\u4eab\u8d44\u6e90\uff0c\u8fd9\u6837\uff0c\u5728\u9875\u6839\u672c\u4e0d\u4f1a\u88ab\u5199\u5165\u7684\u573a\u666f\u4e0b\uff0cfork()\u7acb\u5373\u6267\u884cexec()\uff0c\u65e0\u9700\u5bf9\u5730\u5740\u7a7a\u95f4\u8fdb\u884c\u590d\u5236\uff0cfork()\u7684\u5b9e\u9645\u5f00\u9500\u5c31\u662f\u590d\u5236\u7236\u8fdb\u7a0b\u7684\u4e00\u4e2a\u9875\u8868\u548c\u4e3a\u5b50\u8fdb\u7a0b\u521b\u5efa\u4e00\u4e2a\u8fdb\u7a0b\u63cf\u8ff0\u7b26\uff0c\u4e5f\u5c31\u662f\u8bf4\u53ea\u6709\u5f53\u8fdb\u7a0b\u7a7a\u95f4\u4e2d\u5404\u6bb5\u7684\u5185\u5b58\u5185\u5bb9\u53d1\u751f\u53d8\u5316\u65f6\uff0c\u7236\u8fdb\u7a0b\u624d\u5c06\u5176\u5185\u5bb9\u590d\u5236\u4e00\u4efd\u4f20\u7ed9\u5b50\u8fdb\u7a0b\uff0c\u5927\u5927\u63d0\u9ad8\u4e86\u6548\u7387\u3002<\/p>\n

2\u3001\u6570\u636e\u5b58\u50a8\u4e2d\u7684\u5199\u65f6\u590d\u5236<\/strong><\/span><\/div>\n

Linux\u7b49\u7684\u6587\u4ef6\u7ba1\u7406\u7cfb\u7edf\u4f7f\u7528\u4e86\u5199\u65f6\u590d\u5236\u7b56\u7565\u3002<\/p>\n

\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u6bd4\u5982\u6211\u4eec\u6709\u4e2a\u7a0b\u5e8f\u8981\u5199\u6587\u4ef6\uff0c\u4e0d\u65ad\u5730\u6839\u636e\u7f51\u7edc\u4f20\u6765\u7684\u6570\u636e\u5199\uff0c\u5982\u679c\u6bcf\u4e00\u6b21fwrite\u6216\u662ffprintf\u90fd\u8981\u8fdb\u884c\u4e00\u4e2a\u78c1\u76d8\u7684I\/O\u64cd\u4f5c\u7684\u8bdd\uff0c\u90fd\u7b80\u76f4\u5c31\u662f\u6027\u80fd\u4e0a\u5de8\u5927\u7684\u635f\u5931\uff0c<\/p>\n

\u56e0\u6b64\u901a\u5e38\u7684\u505a\u6cd5\u662f\uff0c\u6bcf\u6b21\u5199\u6587\u4ef6\u64cd\u4f5c\u90fd\u5199\u5728\u7279\u5b9a\u5927\u5c0f\u7684\u4e00\u5757\u5185\u5b58\u4e2d\uff08\u78c1\u76d8\u7f13\u5b58\uff09\uff0c\u53ea\u6709\u5f53\u6211\u4eec\u5173\u95ed\u6587\u4ef6\u65f6\uff0c\u624d\u5199\u5230\u78c1\u76d8\u4e0a\uff08\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48\u5982\u679c\u6587\u4ef6\u4e0d\u5173\u95ed\uff0c\u6240\u5199\u7684\u4e1c\u897f\u4f1a\u4e22\u5931\u7684\u539f\u56e0\uff09<\/p>\n

3\u3001\u8f6f\u4ef6\u5e94\u7528\u4e2d\u7684\u5199\u65f6\u590d\u5236<\/strong><\/span><\/div>\n

\u5728\u6211\u4eec\u7ecf\u5e38\u4f7f\u7528\u7684STL\u6807\u51c6\u6a21\u677f\u5e93\u4e2d\u7684string\u7c7b\uff0c\u4e5f\u662f\u4e00\u4e2a\u5177\u6709\u5199\u65f6\u624d\u62f7\u8d1d\u6280\u672f\u7684\u7c7b\u3002\u4e3a\u4e86\u63d0\u9ad8\u6027\u80fd\uff0cSTL\u4e2d\u7684\u8bb8\u591a\u7c7b\u90fd\u91c7\u7528\u4e86\u5199\u65f6\u62f7\u8d1d\u6280\u672f\u3002\u4f46\u662f\u5728C++11\u6807\u51c6\u4e2d\u4e3a\u4e86\u63d0\u9ad8\u5e76\u884c\u6027\u53d6\u6d88\u4e86\u8fd9\u4e00\u7b56\u7565<\/p>\n

\r\nclass String\r\n{\r\npublic:\r\n  \/\/\u6784\u9020\u51fd\u6570\uff08\u5206\u5b58\u5185\u5b58\uff09\r\n  String(char* tmp)\r\n  {\r\n    _Len = strlen(tmp);\r\n    _Ptr = new char[_Len + 1 + 1];\r\n    strcpy(_Ptr, tmp);\r\n    \/\/ \u5728\u6570\u7ec4\u5c3e\u90e8\u8bbe\u7f6e\u5f15\u7528\u8ba1\u6570\r\n    _Ptr[_Len + 1] = 0; \r\n  }\r\n  \/\/\u6790\u6784\u51fd\u6570\r\n  ~String()\r\n  {\r\n    \/\/\u5f15\u7528\u8ba1\u6570\u51cf\u4e00\r\n    _Ptr[_Len + 1]--; \r\n    \/\/ \u5f15\u7528\u8ba1\u6570\u4e3a0\u65f6\uff0c\u91ca\u653e\u5185\u5b58\r\n    if (_Ptr[_Len + 1] == 0)\r\n    {\r\n      delete[] _Ptr;\r\n    }\r\n  }\r\n \r\n  \/\/\u62f7\u8d1d\u6784\u9020\uff08\u5171\u4eab\u5185\u5b58\uff09\r\n  String(string& str)\r\n  {\r\n    if (this->_Ptr != str)\r\n    {\r\n      \/\/\u5171\u4eab\u5185\u5b58,.data()\u8fd4\u56de\u7684\u662f\u5c06string\u7684\u7c7b\u578b\u8f6c\u6362\u6210char\u7c7b\u578b\u7684\u6307\u9488\r\n      const char *p = str.c_str();\r\n      char* pp;\r\n      strcmp(pp, p);\r\n      this->_Ptr = pp; \r\n      this->_Len = str.size();\r\n      this->_Ptr[_Len + 1] ++; \/\/\u5f15\u7528\u8ba1\u6570\u52a0\u4e00\r\n \r\n    }\r\n  }\r\n \r\n  \/\/\u5bf9[]\u7b26\u8fdb\u884c\u91cd\u8f7d\uff0c\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u64cd\u4f5c\u7684\u65f6\u5019\uff0c\u5f00\u59cb\u5199\u65f6\u590d\u5236\r\n  char& operator[](unsigned int idx)\r\n  {\r\n    if (idx > _Len || _Ptr == 0)\r\n    {\r\n      static char nullchar = 0;\r\n      return nullchar;\r\n    }\r\n    \/\/\u5f15\u7528\u8ba1\u6570\u51cf\u4e00\r\n    _Ptr[_Len + 1]--; \r\n \r\n    char* tmp = new char[_Len + 1 + 1];\r\n \r\n    strncpy(tmp, _Ptr, _Len + 1);\r\n \r\n    _Ptr = tmp;\r\n    \/\/ \u8bbe\u7f6e\u65b0\u7684\u5171\u4eab\u5185\u5b58\u7684\u5f15\u7528\u8ba1\u6570\r\n    _Ptr[_Len + 1] = 0; \r\n    return _Ptr[idx];\r\n  }\r\n \r\nprivate:\r\n  int _Len;\r\n  char* _Ptr;\r\n};<\/pre>\n

\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u5199\u5165\u65f6\u590d\u5236\u662f\u4e00\u79cd\u8ba1\u7b97\u673a\u7a0b\u5e8f\u8bbe\u8ba1\u9886\u57df\u7684\u4f18\u5316\u7b56\u7565\u3002\u5176\u6838\u5fc3\u601d\u60f3\u662f\uff0c\u5982\u679c\u6709\u591a\u4e2a\u8c03\u7528\u8005\u540c\u65f6\u8bf7\u6c42\u76f8\u540c\u8d44\u6e90\uff08\u5982\u5185\u5b58\u6216\u78c1\u76d8\u4e0a\u7684 […]<\/p>\n","protected":false},"author":1482,"featured_media":4213,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-195097","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/195097","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\/1482"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=195097"}],"version-history":[{"count":5,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/195097\/revisions"}],"predecessor-version":[{"id":195102,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/195097\/revisions\/195102"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/4213"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=195097"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=195097"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=195097"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}