{"id":1194,"date":"2010-08-24T23:08:45","date_gmt":"2010-08-24T15:08:45","guid":{"rendered":"http:\/\/edsionte.com\/techblog\/?p=1194"},"modified":"2010-09-24T13:48:08","modified_gmt":"2010-09-24T05:48:08","slug":"shell%e7%ae%a1%e9%81%93%e9%87%8d%e5%ae%9a%e5%90%91%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"http:\/\/edsionte.com\/techblog\/archives\/1194","title":{"rendered":"shell\u7ba1\u9053\u91cd\u5b9a\u5411\u5b9e\u73b0"},"content":{"rendered":"<p>\u5982\u679c\u4f60\u5b9e\u73b0\u8fc7my_shell.c\uff0c\u90a3\u4e48\u5bf9\u7ba1\u9053\u91cd\u5b9a\u5411\u5e94\u8be5\u6709\u5370\u8c61\u3002\u4f46\u662f\u672c\u6587\u4e2d\u6240\u8ff0\u7684\u7ba1\u9053\u91cd\u5b9a\u5411\uff0c\u5c06\u91c7\u7528\u6700\u8fd1\u6211\u4eec\u5b66\u4e60\u7684\u7ba1\u9053\u76f8\u5173\u7cfb\u7edf\u8c03\u7528\u51fd\u6570\u6765\u5b9e\u73b0\u3002\u968f\u540e\uff0c\u4e5f\u5c06\u548c\u5927\u5bb6\u4e00\u8d77\u518d\u53bb\u56de\u987e\u5f53\u521dmy_shell.c\u4e2d\u662f\u5982\u4f55\u5b9e\u73b0\u7ba1\u9053\u91cd\u5b9a\u5411\u7684<\/p>\n<p>\u5bf9\u4e8e\u7ba1\u9053\u7b26\u53f7\uff0c\u8fd9\u91cc\u53ea\u505a\u7b80\u5355\u7684\u8bf4\u660e\uff1a\u7ba1\u9053\u7b26\u524d\u547d\u4ee4\u7684\u8f93\u51fa\u4f5c\u4e3a\u7ba1\u9053\u7b26\u540e\u547d\u4ee4\u7684\u8f93\u5165\u3002\u5bf9\u4e8e\u4e00\u822c\u547d\u4ee4\u800c\u8a00\uff0c\u8f93\u5165\u5747\u6765\u81ea\u6807\u51c6\u8f93\u5165\uff0c\u800c\u8f93\u51fa\u5219\u81f3\u6807\u51c6\u8f93\u51fa\u3002\u4f46\u662f\u4e3a\u4e86\u5b9e\u73b0\u7ba1\u9053\u91cd\u5b9a\u5411\uff0c\u6211\u4eec\u5148\u521b\u5efa\u7ba1\u9053\uff0c\u7136\u540e\u5c06\u7ba1\u9053\u5199\u7aef\u91cd\u5b9a\u5411\u5230\u6807\u51c6\u8f93\u51fa\uff0c\u5c06\u7ba1\u9053\u8bfb\u7aef\u91cd\u5b9a\u5411\u5230\u6807\u51c6\u8f93\u5165\u3002\u5f53\u7136\u8fd9\u4e24\u6b21\u91cd\u5b9a\u5411\u5206\u522b\u5728\u4e0d\u540c\u7684\u8fdb\u7a0b\u4e2d\u5b8c\u6210\u3002\u5177\u4f53\u4ee3\u7801\u5b9e\u73b0\u53ef\u53c2\u8003\u4e0b\u9762\u7684\u4ee3\u7801\u7247\u6bb5\u3002<\/p>\n<pre class=\"brush:c\">\tif(pipe(fd)==-1)\r\n\t{\r\n\t\tprintf(\"my_pipe:can't create a pipe\\n\");\r\n\t\texit(1);\r\n\t}\r\n\r\n\tpid=fork();\r\n\tif(pid==0)\r\n\t{\r\n\t\tclose(fd[0]);\r\n\t\tdup2(fd[1],STDOUT_FILENO);\r\n\t\tclose(fd[1]);\r\n\r\n\t\tif(execlp(argv[1],argv[1],NULL)==0)\r\n\t\t{\r\n\t\t\tprintf(\"my_pipe:can't execute the first command in the child process\\n\");\r\n\t\t\texit(1);\r\n\t\t}\r\n\t}\r\n\telse if(pid&gt;\\0)\r\n\t{\r\n\t\tclose(fd[1]);\r\n\t\tdup2(fd[0],STDIN_FILENO);\r\n\t\tclose(fd[0]);\r\n\r\n\t\tif(execlp(argv[2],argv[2],NULL)==0)\r\n\t\t{\r\n\t\t\tprintf(\"my_pipe:can't execute the second command in the parent process\\n\");\r\n\t\t\texit(1);\r\n\t\t}\r\n\t}\r\n\telse\r\n\t{\r\n\t\tprintf(\"my_pipe:can't create a new process\\n\");\r\n\t\texit(1);\r\n\t}<\/pre>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u6240\u5b9e\u73b0\u7684\u7ba1\u9053\u91cd\u5b9a\u5411\u53ea\u652f\u6301\u6ca1\u6709\u53c2\u6570\u7684\u547d\u4ee4\uff0c\u6bd4\u5982.\/my_pipe ls wc\uff08\u76f8\u5f53\u4e8e\u547d\u4ee4ls | wc\uff09\u3002\u5982\u679c\u60f3\u8981\u4f7f\u7528\u547d\u4ee4\u53c2\u6570\uff0c\u53ef\u4ee5\u5728\u6b21\u57fa\u7840\u4e0a\u7ee7\u7eed\u5b8c\u5584\u3002<\/p>\n<p>\u53ef\u4ee5\u53d1\u73b0\uff0c\u7ba1\u9053\u662f\u4f5c\u4e3a\u524d\u540e\u4e24\u4e2a\u547d\u4ee4\u7684\u6570\u636e\u7f13\u51b2\u533a\uff0c\u5404\u547d\u4ee4\u7684\u76f8\u5173\u8f93\u5165\u6216\u8f93\u51fa\u53ea\u662f\u201c\u79c1\u4e0b\u201d\u64cd\u4f5c\u3002\u65e2\u7136\u7406\u89e3\u4e86\u4e0a\u8ff0\u4ee3\u7801\uff0c\u90a3\u4e48my_shell.c\u4e2d\u7684\u7ba1\u9053\u7684\u5b9e\u73b0\u4ee3\u7801\u4e5f\u5c31\u4e00\u76ee\u4e86\u7136\u4e86\u3002<\/p>\n<p>\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a\u5b50\u8fdb\u7a0b\uff0c\u7136\u540e\u5728\u5b50\u8fdb\u7a0b\u4e2d\u521b\u5efa\u5b50\u5b50\u8fdb\u7a0b(pid2=fork())\u3002\u5176\u5b9e\u65e0\u975e\u662f\u8ba9\u8fd9\u4e24\u4e2a\u5b50\u8fdb\u7a0b\u53bb\u5206\u522b\u6267\u884c\u7ba1\u9053\u7b26\u524d\u540e\u7684\u547d\u4ee4\u3002\u8fd9\u91cc\u5e94\u8be5\u6ce8\u610f\u7684\u662f\u6b64\u6bb5\u4ee3\u7801\u662f\u5982\u4f55\u201c\u6a21\u62df\u201d\u7ba1\u9053\u7684\u3002\u6211\u4eec\u53ef\u4ee5\u53d1\u73b0\uff0c\u5176\u5b9e\u5e76\u6ca1\u6709\u771f\u6b63\u7684pipe\u4e00\u4e2a\u7ba1\u9053\uff0c\u800c\u662f\u521b\u5efa\u4e86\u4e00\u4e2a\u5b9e\u5b9e\u5728\u5728tempfile\u6587\u4ef6\u800c\u5df2\uff0c\u6700\u540e\u4f7f\u7528\u5b8c\u6bd5\u518d\u6084\u6084\u5220\u9664\u6b21\u6587\u4ef6\u3002<\/p>\n<p>\u518d\u770b\u5177\u4f53\u662f\u5982\u4f55\u91cd\u5b9a\u5411\u7684\uff0c\u5728\u5b50\u5b50\u8fdb\u7a0b\u4e2d\uff0c\u4ee5\u5199\u65b9\u5f0f\u6253\u5f00&#8221;\u7ba1\u9053&#8221;\uff0c\u5c06tempfile\u5b9a\u5411\u5230\u6807\u51c6\u8f93\u51fa\u3002\u5728\u5b50\u8fdb\u7a0b\u4e2d\uff0c\u4ee5\u8bfb\u65b9\u5f0f\u6253\u5f00&#8221;\u7ba1\u9053&#8221;\uff0c\u5c06tempfile\u5b9a\u5411\u5230\u6807\u51c6\u8f93\u5165\u3002<\/p>\n<p>\u770b\u5230\u8fd9\u91cc\uff0c\u4f60\u5e94\u8be5\u5df2\u7ecf\u53d1\u73b0\uff0c\u8fd9\u91cc\u6240\u7528\u5230\u7684\u539f\u7406\u5176\u5b9e\u548c\u4e00\u5f00\u59cb\u6211\u4eec\u6240\u8ff0\u7684\u65b9\u6cd5\u662f\u4e00\u81f4\u7684\u3002<\/p>\n<pre class=\"brush:c\">\t\t\tif(pid==0)\/\/child_process\r\n\t\t\t{\r\n\t\t\t\tint pid2;\r\n\t\t\t\tint status2;\r\n\t\t\t\tint fd2;\r\n\r\n\t\t\t\tif((pid2=fork())&lt;\\0)\r\n\t\t\t\t{\r\n\t\t\t\t\tprintf(\"fork2 error\\n\");\r\n\t\t\t\t\treturn;\r\n\t\t\t\t}\r\n\t\t\t\telse if(pid2==0)\/\/child_child_process\r\n\t\t\t\t{\r\n\t\t\t\t\tif(!(find_command(arg[0])))\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tprintf(\"%s: command not found\\n\",arg[0]);\r\n\t\t\t\t\t\texit(0);\r\n\t\t\t\t\t}\r\n\t\t\t\t\tfd2=open(\"\/tmp\/tempfile\",O_WRONLY|O_CREAT|O_TRUNC,0644);\r\n\t\t\t\t\tdup2(fd2,1);\r\n\t\t\t\t\texecvp(arg[0],arg);\r\n\t\t\t\t\texit(0);\r\n\t\t\t\t}\r\n\r\n\t\t\t\tif(waitpid(pid2,&amp;status2,0)==-1)\/\/waiting for child_child_process\r\n\t\t\t\t{\r\n\t\t\t\t\tprintf(\"wait for child_child process error\\n\");\r\n\t\t\t\t}\r\n\r\n\t\t\t\tif(!(find_command(argnext[0])))\r\n\t\t\t\t{\r\n\t\t\t\t\tprintf(\"%s:command not found\\n\",argnext[0]);\r\n\t\t\t\t\texit(0);\r\n\t\t\t\t}\r\n\r\n\t\t\t\tfd2=open(\"\/tmp\/tempfile\",O_RDONLY);\r\n\t\t\t\tdup2(fd2,0);\r\n\t\t\t\texecvp(argnext[0],argnext);\r\n\r\n\t\t\t\tif(remove(\"\/tmp\/tempfile\"))\r\n\t\t\t\t{\r\n\t\t\t\t\tprintf(\"remove error\\n\");\r\n\t\t\t\t}\r\n\t\t\t\texit(0);\r\n\t\t\t}<\/pre>\n<pre class=\"brush:c\"><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u679c\u4f60\u5b9e\u73b0\u8fc7my_shell.c\uff0c\u90a3\u4e48\u5bf9\u7ba1\u9053\u91cd\u5b9a\u5411\u5e94\u8be5\u6709\u5370\u8c61\u3002\u4f46\u662f\u672c\u6587\u4e2d\u6240\u8ff0\u7684\u7ba1\u9053\u91cd\u5b9a\u5411\uff0c\u5c06\u91c7\u7528\u6700\u8fd1\u6211\u4eec\u5b66\u4e60\u7684 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[140],"tags":[109,139,108,125,138,402],"class_list":{"0":"post-1194","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"hentry","6":"category-140","7":"tag-c","8":"tag-ipc","9":"tag-linux","11":"tag-138","12":"tag-402"},"views":10181,"_links":{"self":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/1194","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/comments?post=1194"}],"version-history":[{"count":0,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/1194\/revisions"}],"wp:attachment":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/media?parent=1194"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/categories?post=1194"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/tags?post=1194"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}