ååãechoãµã¼ãã¼ãæ¸ãã¦ã¿ãããã®ç¶ãã
echoãµã¼ãã¼ãæ¸ãã¦ã¿ãã¨ãã®ã¡ã¢ ãã®1 ã½ã±ããAPIã¨TCP
ãã®echoãµã¼ãã¼ã ã¨åæã«è¤æ°ã®ã¯ã©ã¤ã¢ã³ããæããªããã©ããã¦ãï¼å®éã«è©¦ãã¦è¦ããã¯ã©ã¤ã¢ã³ããã2åtelnetã§ã¤ãªãã§ã¿ãã
ã¯ã©ã¤ã¢ã³ã1
$ telnet 192.168.33.10 30000 Trying 192.168.33.10... Connected to 192.168.33.10. Escape character is '^]'. Hello World!
ã¯ã©ã¤ã¢ã³ã2
$ telnet 192.168.33.10 30000 Trying 192.168.33.10... Connected to 192.168.33.10. Escape character is '^]'.
2ã¤ç®ã®æ¥ç¶ã§ã¯ã Hello World!
ã表示ããã¦ããªãã
ãµã¼ãã¼å´ã§æ¥ç¶ã確èªãã¦ã¿ãã
netstatã§ã¿ãã¨è¤æ°ã®æ¥ç¶ãã§ãã¦ãã
$ netstat -an | grep 30000 tcp 0 0 0.0.0.0:30000 0.0.0.0:* LISTEN tcp 0 0 192.168.33.10:30000 192.168.33.1:64085 ESTABLISHED tcp 0 0 192.168.33.10:30000 192.168.33.1:64084 ESTABLISHED
ããããã½ã±ããã«å¯¾ãããã£ã¹ã¯ãªãã¿ã確èªããã¨ã1ã¤ãããªãï¼64084ãã¼ãã使ã£ã¦ãæ¹ï¼ã
$ lsof -i:30000 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME echo_serv 2316 ubuntu 3u IPv4 52901 0t0 TCP *:30000 (LISTEN) echo_serv 2316 ubuntu 4u IPv4 52902 0t0 TCP 192.168.33.10:30000->192.168.33.1:64084 (ESTABLISHED) $ ll /proc/2316/fd total 0 dr-x------ 2 ubuntu ubuntu 0 Mar 5 03:12 ./ dr-xr-xr-x 9 ubuntu ubuntu 0 Mar 5 03:12 ../ lrwx------ 1 ubuntu ubuntu 64 Mar 5 03:14 0 -> /dev/pts/0 lrwx------ 1 ubuntu ubuntu 64 Mar 5 03:14 1 -> /dev/pts/0 lrwx------ 1 ubuntu ubuntu 64 Mar 5 03:12 2 -> /dev/pts/0 lrwx------ 1 ubuntu ubuntu 64 Mar 5 03:14 3 -> socket:[52901] lrwx------ 1 ubuntu ubuntu 64 Mar 5 03:14 4 -> socket:[52902]
$ ps x | grep echo_server 2316 pts/0 S+ 0:00 ./echo_server
psã®STATã S+
ãªã®ã§å²ãè¾¼ã¿å¯è½ãªã¹ãªã¼ãç¶æ
ã§ãã©ã¢ã°ã©ã¦ã³ãã®ããã»ã¹ã°ã«ã¼ãã«å
¥ã£ã¦ããã¨ããç¶æ
ã
straceã§è¦ã¦ã¿ãã¨ãreadã§ã¹ãªã¼ããã¦ããã
$ sudo strace -p 2316 strace: Process 2316 attached read(4,
ã¯ã©ã¤ã¢ã³ã1ã§æååãå ¥å
$ telnet 192.168.33.10 30000 Trying 192.168.33.10... Connected to 192.168.33.10. Escape character is '^]'. Hello World! hoge hoge Connection closed by foreign host.
æ¥ç¶ããµã¼ãã¼å´ããåããã¦ãã¯ã©ã¤ã¢ã³ã2ã®telnetã« Hello World!
ã表示ãããã
$ telnet 192.168.33.10 30000 Trying 192.168.33.10... Connected to 192.168.33.10. Escape character is '^]'. Hello World!
ãµã¼ãã¼å´ã§ç¢ºèªãã¦ã¿ãã¨æ°ããæ¥ç¶ã«å¤ãã£ã¦ããã¨ããããã
$ lsof -i:30000 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME echo_serv 2316 ubuntu 3u IPv4 52901 0t0 TCP *:30000 (LISTEN) echo_serv 2316 ubuntu 4u IPv4 56445 0t0 TCP 192.168.33.10:30000->192.168.33.1:64085 (ESTABLISHED) ll /proc/2316/fd total 0 dr-x------ 2 ubuntu ubuntu 0 Mar 5 03:12 ./ dr-xr-xr-x 9 ubuntu ubuntu 0 Mar 5 03:12 ../ lrwx------ 1 ubuntu ubuntu 64 Mar 5 03:14 0 -> /dev/pts/0 lrwx------ 1 ubuntu ubuntu 64 Mar 5 03:14 1 -> /dev/pts/0 lrwx------ 1 ubuntu ubuntu 64 Mar 5 03:12 2 -> /dev/pts/0 lrwx------ 1 ubuntu ubuntu 64 Mar 5 03:14 3 -> socket:[52901] lrwx------ 1 ubuntu ubuntu 64 Mar 5 03:14 4 -> socket:[56445]
ã¾ã¨ããã¨ã read
ããå¦çãé²ã¾ããã¯ã©ã¤ã¢ã³ã2ã®æ¥ç¶ã«å¯¾ããacceptãå¼ã°ããªãããã®ãããµã¼ãã¼ã¯ã¯ã©ã¤ã¢ã³ãã1ã¤ãã¤ããå¦çã§ããªãã
I/Oã¢ãã«
ååã«å¼ãç¶ãUNIXãããã¯ã¼ã¯ããã°ã©ãã³ã°ãåèã«ããã
UNIXãããã¯ã¼ã¯ããã°ã©ãã³ã°ãèªãã¨5種é¡ã®I/Oãããã£ã¦æ¸ãã¦ããã
- ããããã³ã°I/O
- éããããã³ã°I/O
- I/Oã®å¤éå
- ã·ã°ãã«é§åI/O
- éåæI/O
Unixãããã¯ã¼ãã³ã°ããã°ã©ãã³ã° P.140
ããã«ãå ¥åæä½ã¯2ã¤ã®ã¹ãããããæãã¨æ¸ãã¦ããã
å ¥åæä½ã¯æ¬¡ã®2段éã§æ§æããã¦ããã
- ãã¼ã¿ã®ç¨æãã§ããã¾ã§å¾ ã¡ã
- ãã®ãã¼ã¿ãã«ã¼ãã«ããããã»ã¹ã«ã³ãã¼ããã
ã½ã±ããã«é¢ããå ¥åã§ã¯ãæåã®ã¹ãããã§ã¯æ®éã®ãããã¯ã¼ã¯ããã®ãã¼ã¿ã®å°çãå¾ ã¤ããã±ãããå°çããã¨ãã«ã¼ãã«å ã®ãããã¡ã«ã³ãã¼ãããã2ã¤ç®ã®ã¹ãããã§ã¯ããã®ãã¼ã¿ãã«ã¼ãã«ã®ãããã¡ããã¢ããªã±ã¼ã·ã§ã³ã®ãããã¡ã«ã³ãã¼ãããã¨ã«ãªãã
Unixãããã¯ã¼ãã³ã°ããã°ã©ãã³ã° P.140
ããããã³ã°I/Oããã³ããããã³ã°I/OãI/Oã®å¤éåãã·ã°ãã«é§åI/Oã®4ã¤ã¯ã¹ããã1ãã©ãæ±ãããç°ãªããã¹ããã2ã«ã¤ãã¦ã¯åããéåæI/Oã ããä»ã¨ç°ãªãæ¹æ³ã§ã¹ããã1,2ãæ±ãã
éåæI/Oã¯ãLinuxã ã¨aioã¨ãã¦æä¾ããã¦ããããã°ã°ã£ã¦è²ã èªãã§ã¿ãæãã ã¨ãã¾ã使ããã¦ãªãã£ã½ãï¼
åæI/Oæä½ã¨éåæI/Oæä½
I/Oã¢ãã«ã¨ã¯å¥ã®è¦³ç¹ã§åæI/Oæä½ã¨éåæI/Oæä½ã¨ããã®ãããã
Posix.1 ã§ã¯ããã2ã¤ã®èªã以ä¸ã®æ§ã«å®ç¾©ãã¦ããã
- åæI/Oæä½ã§ã¯ããããè¦æ±ããããã»ã¹ã¯è¦æ±ããI/Oæä½ãå®äºããã¾ã§ãããã¯ãã
- éåæI/Oæä½ã§ã¯ããããè¦æ±ããããã»ã¹ã¯ãããã¯ããªã
ãã®å®ç¾©ã«ããã°ãæåã®4ã¤ã®I/Oã¢ãã«ãããªãã¡ããããã³ã°ãéããããã³ã°ãå¤éåI/Oããã³ã·ã°ãã«I/Oæä½ã®ãã¹ã¦ã«ããã¦ããã»ã¹ã¯ãããã¯ãããéåæI/Oã¢ãã«ã®ã¿ãéåæI/Oã®å®ç¾©ã«ä¸è´ãã¦ããã
UNIXãããã¯ã¼ãã³ã°ããã°ã©ãã³ã° P.144
æ¸ãã¦ã¿ãechoãµã¼ãã¼ã®I/Oã¢ãã«
æ¸ãã¦ã¿ãechoãµã¼ãã¼ã§ä½¿ã£ã¦ã read
ã¯ããããã³ã°I/Oã§ãããããããã³ã°I/Oã§ã¯ã¹ããã1ã¨2ãçµããã¾ã§å
ã®ããã»ã¹ã«å¦çãæ»ããªãã read
ã§å¦çãæ¢ã¾ã£ã¦ããã®ã¯ã¹ããã1ã§ãã¯ã©ã¤ã¢ã³ãããã®ãã¼ã¿ã®å°éãå¾
ã£ã¦ããããã§ãããã¯ã©ã¤ã¢ã³ãããã®ãã¼ã¿ãå±ãã¾ã§echoãµã¼ãã¼ã®ããã»ã¹ã¯ãããã¯ãããã
è¤æ°ã¯ã©ã¤ã¢ã³ããåæã«æãããã«ãechoãµã¼ãã¼ã以ä¸ã®æ¹æ³ã§æ¸ãæãã¦ã¿ããã¨æãã
- ããããã³ã°I/Oã®ã¾ã¾ãforkã使ã£ã¦è¤æ°ããã»ã¹ãç«ã¡ä¸ãã1ããã»ã¹1ã¯ã©ã¤ã¢ã³ãã§å¯¾å¿ãã
- éããããã³ã°I/Oã使ã£ã¦å¯¾å¿ããã1ããã»ã¹ã§è¤æ°ã¯ã©ã¤ã¢ã³ãã«å¯¾å¿ãã
- I/Oå¤éã使ã£ã¦å¯¾å¿ããã1ããã»ã¹ã§è¤æ°ã¯ã©ã¤ã¢ã³ãã«å¯¾å¿ãã