{"id":25817,"date":"2022-11-21T13:45:03","date_gmt":"2022-11-21T05:45:03","guid":{"rendered":"http:\/\/lrxjmw.cn\/?p=25817"},"modified":"2022-11-21T13:45:44","modified_gmt":"2022-11-21T05:45:44","slug":"linux-always-files","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/linux-always-files.html","title":{"rendered":"\u8be0\u91caLinux\u4e2d\u300e\u4e00\u5207\u90fd\u662f\u6587\u4ef6\u300f\u6982\u5ff5\u548c\u76f8\u5e94\u7684\u6587\u4ef6\u7c7b\u578b"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u5728 Unix \u548c\u5b83\u884d\u751f\u7684\u6bd4\u5982 Linux \u7cfb\u7edf\u4e2d\uff0c\u4e00\u5207\u90fd\u53ef\u4ee5\u770b\u505a\u6587\u4ef6\u3002\u867d\u7136\u5b83\u4ec5\u4ec5\u53ea\u662f\u4e00\u4e2a\u6cdb\u6cdb\u7684\u6982\u5ff5\uff0c\u4f46\u8fd9\u662f\u4e8b\u5b9e\u3002\u5982\u679c\u6709\u4e0d\u662f\u6587\u4ef6\u7684\uff0c\u90a3\u5b83\u4e00\u5b9a\u662f\u6b63\u8fd0\u884c\u7684\u8fdb\u7a0b\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u8981\u7406\u89e3\u8fd9\u70b9\uff0c\u53ef\u4ee5\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u60a8\u7684\u6839\u76ee\u5f55\uff08\/\uff09\u7684\u7a7a\u95f4\u5145\u65a5\u7740\u4e0d\u540c\u7c7b\u578b\u7684 Linux \u6587\u4ef6\u3002\u5f53\u60a8\u521b\u5efa\u4e00\u4e2a\u6587\u4ef6\u6216\u5411\u7cfb\u7edf\u4f20\u8f93\u4e00\u4e2a\u6587\u4ef6\u65f6\uff0c\u5b83\u4f1a\u5728\u7269\u7406\u78c1\u76d8\u4e0a\u5360\u636e\u7684\u4e00\u4e9b\u7a7a\u95f4\uff0c\u800c\u4e14\u662f\u4e00\u4e2a\u7279\u5b9a\u7684\u683c\u5f0f\uff08\u6587\u4ef6\u7c7b\u578b\uff09\u3002<\/p>\n

\u867d\u7136 Linux \u7cfb\u7edf\u4e2d\u6587\u4ef6\u548c\u76ee\u5f55\u6ca1\u6709\u4ec0\u4e48\u4e0d\u540c\uff0c\u4f46\u76ee\u5f55\u8fd8\u6709\u4e00\u4e2a\u91cd\u8981\u7684\u529f\u80fd\uff0c\u90a3\u5c31\u662f\u6709\u7ed3\u6784\u6027\u7684\u5206\u7ec4\u5b58\u50a8\u5176\u5b83\u6587\u4ef6\uff0c\u4ee5\u65b9\u4fbf\u67e5\u627e\u8bbf\u95ee\u3002\u6240\u6709\u7684\u786c\u4ef6\u7ec4\u4ef6\u90fd\u8868\u793a\u4e3a\u6587\u4ef6\uff0c\u7cfb\u7edf\u4f7f\u7528\u8fd9\u4e9b\u6587\u4ef6\u6765\u4e0e\u786c\u4ef6\u901a\u4fe1\u3002<\/p>\n

\u8fd9\u4e9b\u601d\u60f3\u662f\u5bf9 Linux \u4e2d\u7684\u5404\u79cd\u4e8b\u7269\u7684\u91cd\u8981\u9610\u8ff0\uff0c\u56e0\u6b64\u50cf\u6587\u6863\u3001\u76ee\u5f55\uff08Mac OS X \u548c Windows \u7cfb\u7edf\u4e0b\u79f0\u4e4b\u4e3a\u6587\u4ef6\u5939\uff09\u3001\u952e\u76d8\u3001\u76d1\u89c6\u5668\u3001\u786c\u76d8\u3001\u53ef\u79fb\u52a8\u5a92\u4f53\u8bbe\u5907\u3001\u6253\u5370\u673a\u3001\u8c03\u5236\u89e3\u8c03\u5668\u3001\u865a\u62df\u7ec8\u7aef\uff0c\u8fd8\u6709\u8fdb\u7a0b\u95f4\u901a\u4fe1\uff08IPC\uff09\u548c\u7f51\u7edc\u901a\u4fe1\u7b49\u8f93\u5165\/\u8f93\u51fa\u8d44\u6e90\u90fd\u662f\u5b9a\u4e49\u5728\u6587\u4ef6\u7cfb\u7edf\u7a7a\u95f4\u4e0b\u7684\u5b57\u8282\u6d41\u3002<\/p>\n

\u4e00\u5207\u90fd\u53ef\u770b\u4f5c\u662f\u6587\u4ef6\uff0c\u5176\u6700\u663e\u8457\u7684\u597d\u5904\u662f\u5bf9\u4e8e\u4e0a\u9762\u6240\u5217\u51fa\u7684\u8f93\u5165\/\u8f93\u51fa\u8d44\u6e90\uff0c\u53ea\u9700\u8981\u76f8\u540c\u7684\u4e00\u5957 Linux \u5de5\u5177\u3001\u5b9e\u7528\u7a0b\u5e8f\u548c API\u3002<\/p>\n

\u867d\u7136\u5728 Linux \u4e2d\u4e00\u5207\u90fd\u53ef\u770b\u4f5c\u662f\u6587\u4ef6\uff0c\u4f46\u4e5f\u6709\u4e00\u4e9b\u7279\u6b8a\u7684\u6587\u4ef6\uff0c\u6bd4\u5982\u5957\u63a5\u5b57\u548c\u547d\u4ee4\u7ba1\u9053\u3002<\/p>\n

Linux \u7cfb\u7edf\u4e2d\u6709\u4e09\u79cd\u57fa\u672c\u7684\u6587\u4ef6\u7c7b\u578b\uff1a<\/p>\n

    \n
  • \u666e\u901a\/\u5e38\u89c4\u6587\u4ef6<\/li>\n
  • \u7279\u6b8a\u6587\u4ef6<\/li>\n
  • \u76ee\u5f55\u6587\u4ef6<\/li>\n<\/ul>\n
    \u666e\u901a\/\u5e38\u89c4\u6587\u4ef6<\/strong><\/div>\n

    \u5b83\u4eec\u662f\u5305\u542b\u6587\u672c\u3001\u6570\u636e\u3001\u7a0b\u5e8f\u6307\u4ee4\u7b49\u6570\u636e\u7684\u6587\u4ef6\uff0c\u5176\u5728 Linux \u7cfb\u7edf\u4e2d\u662f\u6700\u5e38\u89c1\u7684\u4e00\u79cd\u3002\u5305\u62ec\u5982\u4e0b\uff1a<\/p>\n

      \n
    • \u53ef\u8bfb\u6587\u4ef6<\/li>\n
    • \u4e8c\u8fdb\u5236\u6587\u4ef6<\/li>\n
    • \u56fe\u50cf\u6587\u4ef6<\/li>\n
    • \u538b\u7f29\u6587\u4ef6\u7b49\u7b49<\/li>\n<\/ul>\n
      \u7279\u6b8a\u6587\u4ef6<\/strong><\/div>\n

      \u7279\u6b8a\u6587\u4ef6\u5305\u62ec\u4ee5\u4e0b\u51e0\u79cd\uff1a<\/p>\n

      \u5757\u6587\u4ef6\uff08<\/rp>block<\/rt>\uff09<\/rp><\/ruby><\/strong><\/span><\/div>\n

      \u5757\u6587\u4ef6\uff08<\/rp>block<\/rt>\uff09<\/rp><\/ruby>\uff1a\u8bbe\u5907\u6587\u4ef6\uff0c\u5bf9\u8bbf\u95ee\u7cfb\u7edf\u786c\u4ef6\u90e8\u4ef6\u63d0\u4f9b\u4e86\u7f13\u5b58\u63a5\u53e3\u3002\u5b83\u4eec\u63d0\u4f9b\u4e86\u4e00\u79cd\u901a\u8fc7\u6587\u4ef6\u7cfb\u7edf\u4e0e\u8bbe\u5907\u9a71\u52a8\u901a\u4fe1\u7684\u65b9\u6cd5\u3002<\/p>\n

      \u6709\u5173\u4e8e\u5757\u6587\u4ef6\u4e00\u4e2a\u91cd\u8981\u7684\u6027\u80fd\u5c31\u662f\u5b83\u4eec\u80fd\u5728\u6307\u5b9a\u65f6\u95f4\u5185\u4f20\u8f93\u5927\u5757\u7684\u6570\u636e\u548c\u4fe1\u606f\u3002<\/p>\n

      \u5217\u51fa\u67d0\u76ee\u5f55\u4e0b\u7684\u5757\u6587\u4ef6\uff1a<\/p>\n

      # ls -l \/dev | grep \"^b\"\r\n<\/pre>\n

      \u8f93\u51fa\u4f8b\u5b50<\/p>\n

      brw-rw----  1 root disk        7,   0 May 18 10:26 loop0\r\nbrw-rw----  1 root disk        7,   1 May 18 10:26 loop1\r\nbrw-rw----  1 root disk        7,   2 May 18 10:26 loop2\r\nbrw-rw----  1 root disk        7,   3 May 18 10:26 loop3\r\nbrw-rw----  1 root disk        7,   4 May 18 10:26 loop4\r\nbrw-rw----  1 root disk        7,   5 May 18 10:26 loop5\r\nbrw-rw----  1 root disk        7,   6 May 18 10:26 loop6\r\nbrw-rw----  1 root disk        7,   7 May 18 10:26 loop7\r\nbrw-rw----  1 root disk        1,   0 May 18 10:26 ram0\r\nbrw-rw----  1 root disk        1,   1 May 18 10:26 ram1\r\nbrw-rw----  1 root disk        1,  10 May 18 10:26 ram10\r\nbrw-rw----  1 root disk        1,  11 May 18 10:26 ram11\r\nbrw-rw----  1 root disk        1,  12 May 18 10:26 ram12\r\nbrw-rw----  1 root disk        1,  13 May 18 10:26 ram13\r\nbrw-rw----  1 root disk        1,  14 May 18 10:26 ram14\r\nbrw-rw----  1 root disk        1,  15 May 18 10:26 ram15\r\nbrw-rw----  1 root disk        1,   2 May 18 10:26 ram2\r\nbrw-rw----  1 root disk        1,   3 May 18 10:26 ram3\r\nbrw-rw----  1 root disk        1,   4 May 18 10:26 ram4\r\nbrw-rw----  1 root disk        1,   5 May 18 10:26 ram5\r\n...\r\n<\/pre>\n
      \u5b57\u7b26\u6587\u4ef6\uff08<\/rp>Character<\/rt>\uff09<\/rp><\/ruby><\/strong><\/span><\/div>\n

      \u5b57\u7b26\u6587\u4ef6\uff08<\/rp>Character<\/rt>\uff09<\/rp><\/ruby>\uff1a \u4e5f\u662f\u8bbe\u5907\u6587\u4ef6\uff0c\u5bf9\u8bbf\u95ee\u7cfb\u7edf\u786c\u4ef6\u7ec4\u4ef6\u63d0\u4f9b\u4e86\u975e\u7f13\u51b2\u4e32\u884c\u63a5\u53e3\u3002\u5b83\u4eec\u4e0e\u8bbe\u5907\u7684\u901a\u4fe1\u5de5\u4f5c\u65b9\u5f0f\u662f\u4e00\u6b21\u53ea\u4f20\u8f93\u4e00\u4e2a\u5b57\u7b26\u7684\u6570\u636e\u3002<\/p>\n

      \u5217\u51fa\u67d0\u76ee\u5f55\u4e0b\u7684\u5b57\u7b26\u6587\u4ef6\uff1a<\/p>\n

      # ls -l \/dev | grep \"^c\"\r\n<\/pre>\n

      \u8f93\u51fa\u4f8b\u5b50<\/p>\n

      crw-------  1 root root       10, 235 May 18 15:54 autofs\r\ncrw-------  1 root root       10, 234 May 18 15:54 btrfs-control\r\ncrw-------  1 root root        5,   1 May 18 10:26 console\r\ncrw-------  1 root root       10,  60 May 18 10:26 cpu_dma_latency\r\ncrw-------  1 root root       10, 203 May 18 15:54 cuse\r\ncrw-------  1 root root       10,  61 May 18 10:26 ecryptfs\r\ncrw-rw----  1 root video      29,   0 May 18 10:26 fb0\r\ncrw-rw-rw-  1 root root        1,   7 May 18 10:26 full\r\ncrw-rw-rw-  1 root root       10, 229 May 18 10:26 fuse\r\ncrw-------  1 root root      251,   0 May 18 10:27 hidraw0\r\ncrw-------  1 root root       10, 228 May 18 10:26 hpet\r\ncrw-r--r--  1 root root        1,  11 May 18 10:26 kmsg\r\ncrw-rw----+ 1 root root       10, 232 May 18 10:26 kvm\r\ncrw-------  1 root root       10, 237 May 18 10:26 loop-control\r\ncrw-------  1 root root       10, 227 May 18 10:26 mcelog\r\ncrw-------  1 root root      249,   0 May 18 10:27 media0\r\ncrw-------  1 root root      250,   0 May 18 10:26 mei0\r\ncrw-r-----  1 root kmem        1,   1 May 18 10:26 mem\r\ncrw-------  1 root root       10,  57 May 18 10:26 memory_bandwidth\r\ncrw-------  1 root root       10,  59 May 18 10:26 network_latency\r\ncrw-------  1 root root       10,  58 May 18 10:26 network_throughput\r\ncrw-rw-rw-  1 root root        1,   3 May 18 10:26 null\r\ncrw-r-----  1 root kmem        1,   4 May 18 10:26 port\r\ncrw-------  1 root root      108,   0 May 18 10:26 ppp\r\ncrw-------  1 root root       10,   1 May 18 10:26 psaux\r\ncrw-rw-rw-  1 root tty         5,   2 May 18 17:40 ptmx\r\ncrw-rw-rw-  1 root root        1,   8 May 18 10:26 random\r\n<\/pre>\n
      \u7b26\u53f7\u94fe\u63a5\u6587\u4ef6\uff08<\/rp>Symbolic link<\/rt>\uff09<\/rp><\/ruby><\/strong><\/span><\/div>\n

      \u7b26\u53f7\u94fe\u63a5\u6587\u4ef6\uff08<\/rp>Symbolic link<\/rt>\uff09<\/rp><\/ruby>\u00a0\uff1a \u7b26\u53f7\u94fe\u63a5\u662f\u6307\u5411\u7cfb\u7edf\u4e0a\u5176\u4ed6\u6587\u4ef6\u7684\u5f15\u7528\u3002\u56e0\u6b64\uff0c\u7b26\u53f7\u94fe\u63a5\u6587\u4ef6\u662f\u6307\u5411\u5176\u5b83\u6587\u4ef6\u7684\u6587\u4ef6\uff0c\u90a3\u4e9b\u6587\u4ef6\u53ef\u4ee5\u662f\u76ee\u5f55\u6216\u5e38\u89c4\u6587\u4ef6\u3002<\/p>\n

      \u5217\u51fa\u67d0\u76ee\u5f55\u4e0b\u7684\u7b26\u53f7\u94fe\u63a5\u6587\u4ef6\uff1a<\/p>\n

      # ls -l \/dev\/ | grep \"^l\"\r\n<\/pre>\n

      \u8f93\u51fa\u4f8b\u5b50<\/p>\n

      lrwxrwxrwx  1 root root             3 May 18 10:26 cdrom -> sr0\r\nlrwxrwxrwx  1 root root            11 May 18 15:54 core -> \/proc\/kcore\r\nlrwxrwxrwx  1 root root            13 May 18 15:54 fd -> \/proc\/self\/fd\r\nlrwxrwxrwx  1 root root             4 May 18 10:26 rtc -> rtc0\r\nlrwxrwxrwx  1 root root             8 May 18 10:26 shm -> \/run\/shm\r\nlrwxrwxrwx  1 root root            15 May 18 15:54 stderr -> \/proc\/self\/fd\/2\r\nlrwxrwxrwx  1 root root            15 May 18 15:54 stdin -> \/proc\/self\/fd\/0\r\nlrwxrwxrwx  1 root root            15 May 18 15:54 stdout -> \/proc\/self\/fd\/1\r\n<\/pre>\n

      Linux \u4e2d\u4f7f\u7528 ln \u5de5\u5177\u5c31\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u7b26\u53f7\u94fe\u63a5\u6587\u4ef6\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n

      # touch file1.txt\r\n# ln -s file1.txt \/home\/tecmint\/file1.txt  [\u521b\u5efa\u7b26\u53f7\u94fe\u63a5\u6587\u4ef6]\r\n# ls -l \/home\/tecmint\/ | grep \"^l\"         [\u5217\u51fa\u7b26\u53f7\u94fe\u63a5\u6587\u4ef6]\r\n<\/pre>\n

      \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u9996\u5148\u6211\u4eec\u5728 \/tmp \u76ee\u5f55\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u53eb file1.txt \u7684\u6587\u4ef6\uff0c\u7136\u540e\u521b\u5efa\u7b26\u53f7\u94fe\u63a5\u6587\u4ef6\uff0c\u5c06 \/home\/tecmint\/file1.txt \u6307\u5411 \/tmp\/file1.txt \u6587\u4ef6\u3002<\/p>\n

      \u7ba1\u9053\uff08<\/rp>Pipe<\/rt>\uff09<\/rp><\/ruby>\u548c\u547d\u4ee4\u7ba1\u9053\uff08<\/rp>Named pipe<\/rt>\uff09<\/rp><\/ruby> <\/strong><\/span><\/div>\n

      \u7ba1\u9053\uff08<\/rp>Pipe<\/rt>\uff09<\/rp><\/ruby>\u548c\u547d\u4ee4\u7ba1\u9053\uff08<\/rp>Named pipe<\/rt>\uff09<\/rp><\/ruby> : \u5c06\u4e00\u4e2a\u8fdb\u7a0b\u7684\u8f93\u51fa\u8fde\u63a5\u5230\u53e6\u4e00\u4e2a\u8fdb\u7a0b\u7684\u8f93\u5165\uff0c\u4ece\u800c\u5141\u8bb8\u8fdb\u7a0b\u95f4\u901a\u4fe1\uff08IPC\uff09\u7684\u6587\u4ef6\u3002<\/p>\n

      \u547d\u540d\u7ba1\u9053\u5b9e\u9645\u4e0a\u662f\u4e00\u4e2a\u6587\u4ef6\uff0c\u7528\u6765\u4f7f\u4e24\u4e2a\u8fdb\u7a0b\u5f7c\u6b64\u901a\u4fe1\uff0c\u5c31\u50cf\u4e00\u4e2a Linux \u7ba1\u9053\u4e00\u6837\u3002<\/p>\n

      \u5217\u51fa\u67d0\u76ee\u5f55\u4e0b\u7684\u7ba1\u9053\u6587\u4ef6\uff1a<\/p>\n

      # ls -l | grep \"^p\"\r\n<\/pre>\n

      \u8f93\u51fa\u4f8b\u5b50\uff1a<\/p>\n

      prw-rw-r-- 1 tecmint tecmint    0 May 18 17:47 pipe1\r\nprw-rw-r-- 1 tecmint tecmint    0 May 18 17:47 pipe2\r\nprw-rw-r-- 1 tecmint tecmint    0 May 18 17:47 pipe3\r\nprw-rw-r-- 1 tecmint tecmint    0 May 18 17:47 pipe4\r\nprw-rw-r-- 1 tecmint tecmint    0 May 18 17:47 pipe5\r\n<\/pre>\n

      \u5728 Linux \u4e2d\u53ef\u4ee5\u4f7f\u7528 mkfifo \u5de5\u5177\u6765\u521b\u5efa\u4e00\u4e2a\u547d\u540d\u7ba1\u9053\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n

      # mkfifo pipe1\r\n# echo \"This is named pipe1\" > pipe1\r\n<\/pre>\n

      \u5728\u4e0a\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u53eb pipe1 \u7684\u547d\u540d\u7ba1\u9053\uff0c\u7136\u540e\u4f7f\u7528 echo \u547d\u4ee4 \u52a0\u5165\u4e00\u4e9b\u6570\u636e\uff0c\u8fd9\u4e4b\u540e\u5728\u5904\u7406\u8f93\u5165\u7684\u6570\u636e\u65f6 shell \u5c31\u53d8\u6210\u975e\u4ea4\u4e92\u5f0f\u7684\u4e86\uff08LCTT \u8bd1\u6ce8\uff1a\u88ab\u7ba1\u9053\u5360\u4f4f\u4e86\uff09\u3002<\/p>\n

      \u7136\u540e\uff0c\u6211\u4eec\u6253\u5f00\u53e6\u5916\u4e00\u4e2a shell \u7ec8\u7aef\uff0c\u8fd0\u884c\u53e6\u5916\u7684\u547d\u4ee4\u6765\u6253\u5370\u51fa\u521a\u52a0\u5165\u7ba1\u9053\u7684\u6570\u636e\u3002<\/p>\n

      # while read line ;do echo \"This was passed-'$line' \"; done<pipe1\r\n<\/pre>\n
      \u5957\u63a5\u5b57\u6587\u4ef6\uff08<\/rp>socket<\/rt>\uff09<\/rp><\/ruby>\u00a0<\/strong><\/span><\/div>\n

      \u5957\u63a5\u5b57\u6587\u4ef6\uff08<\/rp>socket<\/rt>\uff09<\/rp><\/ruby>\u00a0\uff1a \u63d0\u4f9b\u8fdb\u7a0b\u95f4\u901a\u4fe1\u65b9\u6cd5\u7684\u6587\u4ef6\uff0c\u5b83\u4eec\u80fd\u5728\u8fd0\u884c\u5728\u4e0d\u540c\u73af\u5883\u4e2d\u7684\u8fdb\u7a0b\u4e4b\u95f4\u4f20\u8f93\u6570\u636e\u548c\u4fe1\u606f\u3002<\/p>\n

      \u8fd9\u5c31\u662f\u8bf4\uff0c\u5957\u63a5\u5b57\u53ef\u4ee5\u4e3a\u8fd0\u884c\u7f51\u7edc\u4e0a\u4e0d\u540c\u673a\u5668\u4e2d\u7684\u8fdb\u7a0b\u63d0\u4f9b\u6570\u636e\u548c\u4fe1\u606f\u4f20\u8f93\u3002<\/p>\n

      \u4e00\u4e2a socket \u8fd0\u884c\u7684\u4f8b\u5b50\u5c31\u662f\u7f51\u9875\u6d4f\u89c8\u5668\u8fde\u63a5\u5230\u7f51\u7ad9\u670d\u52a1\u5668\u7684\u8fc7\u7a0b\u3002<\/p>\n

      # ls -l \/dev\/ | grep \"^s\"\r\n<\/pre>\n

      \u8f93\u51fa\u4f8b\u5b50\uff1a<\/p>\n

      srw-rw-rw-  1 root root             0 May 18 10:26 log\r\n<\/pre>\n

      \u4e0b\u9762\u662f\u4f7f\u7528 C \u8bed\u8a00\u7f16\u5199\u7684\u8c03\u7528 socket() \u7cfb\u7edf\u8c03\u7528\u7684\u4f8b\u5b50\u3002<\/p>\n

      int socket_desc= socket(AF_INET, SOCK_STREAM, 0 );\r\n<\/pre>\n

      \u4e0a\u4f8b\u4e2d\uff1a<\/p>\n