SlideShare a Scribd company logo
清華大學 工業工程與工程管理學系
王子洋
A server process and one or more client
processes.
Server manages some resources.
Server provides services by manipulating
resource for clients.
Two common paradigms for clients and servers
communication
• Datagrams (UDP protocol, SOCK_DGRAM)
• Connections (TCP protocol, SOCK_STREAM)
Connections are point-to-point, full-duplex (2-
way communication), and reliable (only for TCP).
CLIENT SERVER
Client programs
• Web browsers, ftp, telnet,
ssh
How does a client find the
server?
• IP address
• Port
Servers are long-running
processes
Each server waits for
requests to arrive on a well-
known port associated with
a particular service.
Concurrency is a property of systems in
which several computations are executing
simultaneously, and potentially interacting
with each other.
• Single host, multiple processes
• Single process, multiple threads
大多數的伺服程式都是動態運作的,一收到新
要求就相對建立一個新的 thread。
• Parent_P: 一為接受要求與建立新的 thread 的程式碼
• Child_P: 另一為處理個別要求的程式碼
當 concurrent 伺服程式開始執行時,只有第一
部分會被執行。也就是說伺服程式的Parent_P:
是用來等待新的請求。當要求到達時,
Parent_P:會建立一個新的Child_P來處理此要
求。此Child_P處理完要求後,即可終止。但
Parent_P仍將一直維持伺服程式在執行的狀態,
繼續等待下一個新的要求。
Socket API 原本是 BSD UNIX 作業系統的
一部份。當初是由美國政府出資,由
Berkley Univ. 所負責研發新版 UNIX 中,用
在 TCP/IP 網際網路通訊協定。它是提供應
用程式使用網路通訊的一組程序
(procedure)。
UNIX domain socket
• Local IPC
• AF_LOCAL or AF_UNIX
• Filesystem: path name
Internet domain socket
• Internet communications
• IPv4: AF_INET
• IPv6: AF_INET6
• Internet address: IP + Port
UNIX 將 socket 與其 I/O 的概念相結合,讓
應用程式間的通訊就像在讀寫檔案一般容易。
在應用程式使用通訊協定做通訊時,需先要
求作業系統產生(open)一個 socket,系統將
回傳一 descriptor(sockfd)來代表此 socket。
在後續處理時皆須利用此指標。
Internet-specific socket address (IPv4):
<netinet/in.h> (Posix.1g)
• Must cast (sockaddr_in *) to (sockaddr *) for
connect(), bind(), and accept().
Generic socket address: <sys/socket.h>
• For address arguments to connect(), bind(), and
accept().
Internet-specific socket address (IPv6):
<netinet/in.h> (Posix.1g)
int gethostname(char *name, int namelen)
客戶或伺服程式可以呼叫 gethostname 取
得本機的位址資訊
#include <sys/socket.h>
int socket (int family, int type, int protocol);
• returns: nonnegative integer descriptor if OK, -1 on
error
socket:用來建立一個 socket,回傳一整數的
descriptor (sockfd) family: protocol family
type: communication type
• protocol: 指定此 socket 所使用的傳輸通訊協定。
Specifying a protocol of 0 causes socket() to use an
unspecified default protocol appropriate for the
requested socket type (e.g., TCP/IP).
#include <sys/socket.h>
int connect (int sockfd, const struct sockaddr
*servaddr, socklen_t addrlen);
• returns: 0 of OK, -1 on error
connect:client程式利用 connect 與指定的server
程式建立連線。
• sockfd: 為用來連線的客戶端之 socket descriptor
• *servaddr: 為一指向記錄Server IP和Port之 sockaddr 資料
結構的位址
• addrlen: 為 sockaddr 資料結構的長度
#include <sys/socket.h>
int bind (int sockfd, const struct sockaddr *myaddr,
socklen_t addrlen);
• returns: 0 if OK, -1 on error
bind:當 socket 建置時,並未宣告本機地址和遠
端主機的位址。伺服程式需用 bind 程序來將
socket與本機上的一個 port 相關聯,之後 server
就可以在該port 監聽服務請求。
• sockfd: 參數值為已建置但未指定 port number 的 descriptor
• *myaddr: 為在名本機位址的資料結構之起始位置
• addrlen: 為本機位址的資料結構之長度
#include <sys/socket.h>
int listen (int sockfd, int backlog);
• returns: 0 if OK,-1 on error
listen:當伺服程式宣告其通訊協定 port
number 後 (ex: bind),可要求 OS 將此 socket
設為被動的等待 client 來聯繫的模式。
• sockfd: 參數值為已建置且已指定 port number 的
descriptor
• backlog: 指定此 socket 的服務請求佇列 (request
queue) 的最大值
#include <sys/socket.h>
int accept (int sockfd, struct sockaddr *cliaddr,
socklen_t *addrlen);
• returns: nonnegative descriptor if OK, -1 on error
sockfd: listening socket accept returns:
connected socket (new socket)
cliaddr, addrlen: value-result arguments
• cliaddr: 為 sockaddr 資料結構的起始位置
• addrlen: 為位址的長度
#include <unistd.h>
pid_t fork (void);
• returns: 0 in child, process ID of child in parent, -1 on
error (called-once-return-twice)
A child has only one parent process. It can use
getppid() to get parent’s PID.
If a parent want to get a child’s PID, it has to
record the return value of fork().
新的 process/thread (child) 被建立時,應
從產生它的 parent 身上繼承一份所有已開
啟的 socket。
Socket 使用 reference count 機制,當一個
socket 被建立時,系統就把 socket 的
reference count 設為 1,當這個 reference
count 大於零的時候,socket都會存在。
• 當程式產生新的 process/thread 時,會把 parent 和
child process 的 reference count 都加 1。
Status of client-server before call to accept
returns
Status of client-server after return from
accept
Status of client-server after fork returns
Status of client-server after parent and
child close appropriate sockets
當某一 process/thread 呼叫 socket 的
close 程序時,系統將其 reference count
值減一,並且從 thread 清單去除。
若 thread 在關閉 socket 前結束,系統會把
thread 開啟的所有 socket,執行 close 程
序。
#include <unistd.h>
int close (int sockfd);
• returns: 0 if OK, -1 on error;
close:告知系統結束使用 socket
• socket 參數為欲關閉 socket 的 descriptor
• 若此 socket 是 connection-oriented 的傳輸通訊協
定,close 在關掉 socket 之前會先結束網路的連線。
• socket 關閉後立即停止使用,並將系統資源釋放掉,
避免應用程式送出或收到更多的資料。
send:客戶和伺服程式都需要發送資訊。
若 socket 已連線成功,可用 send 程序來
發送資料。
send (socket, *data, length, flags)
• data 為指向要發送資料的起始位址
• length 為所發送資料的總長度
• flags 為發送資料的型別,flags參數值可能為 0 或
是下面三個值之一,一般設定為 0。
MSG-OOB
MSG_PEEK
MSG_DONTROUTE
recv:客戶和伺服程式都需要接收對方發送
的資料。應用程式能呼叫 recv 從已連線
(connected)的 socket 中接收資料。
recv (socket,*buffer,length,flags)
• buffer 指向存放接收資料的起始位址
• length 為 buffer 總長度
• flags:包含 MSG_OOB,MSG_PEEK
Socket 也允許應用程式使用 read/write 來
傳送/接收資料。
read 與 write 必須與connected-socket 合用。
使用 read/write 程序最大的好處是具有通用
性。
read(int fd, void *buf, size_t count);
write(int fd, const void *buf, size_t count);
socket 程序有許多的參數及 options,
setsockopt 設定 socket option 值,
setsockopt 為讀取 socket option 值。
gethostbyaddr 與 gethostbyname 相反,將
IP 位址轉成對映的電腦名稱。
Signals
• “software interrupts”
• usually occur asynchronously.
• can be sent
by one process to another process (or to itself)
by the kernel to a process.
zombie
wait() && waitpid()
to be notified, by kernel, if one or more I/O
conditions are ready.
Scenarios in networking applications:
• a client handling multiple descriptors (stdio/socket)
• a client handling multiple sockets
• a TCP server handling a listening socket and its
connected sockets
• a server handling both TCP and UDP
• a server handling multiple services and protocols
wait for data; copy data from kernel to user
blocking I/O: blocked all the way
nonblocking I/O: if no data, immediate
returns EWOULDBLOCK
I/O multiplexing (select and poll): blocked
separately in wait and copy
signal driven I/O (SIGIO): nonblocked in wait
but blocked in copy (signaled when I/O can
be initiated)
asynchronous I/O (aio_): nonblocked all the
way (signaled when I/O is complete)
Synchronous I/O
• The requesting process to be blocked until that I/O
operation completes.
• blocking, non-blocking, I/O multiplexing, and
signal-driven I/O
Asynchronous I/O
• Does not cause the requesting process to be
blocked.
• Asynchronous I/O
可以使用select要求kernel測試某個輸入裝
置是否ready for reading
當kernel中對應此裝置的buffer中已有相當
數量(可設定)的輸入資料時,此裝置即是
ready for reading
select return後,由我們的程式自行呼叫其它
的system call將buffer中的資料搬回來(也會
block,但時間有限)。
可以使用select要求kernel測試某個輸 出裝
置是否ready for writing
當kernel中對應此裝置的buffer中已有相 當
空間(可設定)可放置輸出資料時,此裝 置即
是ready for writing
select return後,由我們的程式自行呼 叫其它
的system call將欲輸出資料搬入buffer中(也
會block,但時間有限) 。
close()
• Depend on the reference count (=0)
• Terminate both directions
shutdown()
• Initial termination without considering reference count
• Half-close
I’m sleepy, google it by yourself.

More Related Content

What's hot

第20讲 帧中继
第20讲 帧中继第20讲 帧中继
第20讲 帧中继F.l. Yu
 
gRPC - 打造輕量、高效能的後端服務
gRPC - 打造輕量、高效能的後端服務gRPC - 打造輕量、高效能的後端服務
gRPC - 打造輕量、高效能的後端服務
升煌 黃
 
第18讲 Hdlc和Ppp
第18讲 Hdlc和Ppp第18讲 Hdlc和Ppp
第18讲 Hdlc和PppF.l. Yu
 
嵌入式inux應用專題文件-智慧家庭系統
嵌入式inux應用專題文件-智慧家庭系統嵌入式inux應用專題文件-智慧家庭系統
嵌入式inux應用專題文件-智慧家庭系統
艾鍗科技
 
Juniper ScreenOS 基于Policy的
Juniper ScreenOS 基于Policy的Juniper ScreenOS 基于Policy的
Juniper ScreenOS 基于Policy的mickchen
 
第3讲 Tcpip协议栈
第3讲 Tcpip协议栈第3讲 Tcpip协议栈
第3讲 Tcpip协议栈F.l. Yu
 
[圣思园][Java SE]Network
[圣思园][Java SE]Network[圣思园][Java SE]Network
[圣思园][Java SE]NetworkArBing Xie
 
MPTCP学习
MPTCP学习MPTCP学习
MPTCP学习
hubugui
 
Linux firewall
Linux firewallLinux firewall
Linux firewall
Kenny (netman)
 
MPI use c language
MPI use c languageMPI use c language
MPI use c language
ZongYing Lyu
 
Wbm9000动态库说明L
Wbm9000动态库说明LWbm9000动态库说明L
Wbm9000动态库说明Lguest8eab39bd
 
OpenResty 项目模块化最佳实践
OpenResty 项目模块化最佳实践OpenResty 项目模块化最佳实践
OpenResty 项目模块化最佳实践
Orangle Liu
 
Analysis on tcp ip protocol stack
Analysis on tcp ip protocol stackAnalysis on tcp ip protocol stack
Analysis on tcp ip protocol stack
Yueshen Xu
 
Linux Network Monitoring
Linux Network MonitoringLinux Network Monitoring
Linux Network Monitoring
Kenny (netman)
 
Linux基础
Linux基础Linux基础
Linux基础
zhuqling
 
Linux system security
Linux system securityLinux system security
Linux system security
Kenny (netman)
 
簡介 Linux 的 Network Namespace 功能
簡介 Linux 的 Network Namespace 功能簡介 Linux 的 Network Namespace 功能
簡介 Linux 的 Network Namespace 功能
John L Chen
 
Ryu Learning Guide
Ryu Learning GuideRyu Learning Guide
Ryu Learning Guide
呈 李
 

What's hot (20)

第20讲 帧中继
第20讲 帧中继第20讲 帧中继
第20讲 帧中继
 
gRPC - 打造輕量、高效能的後端服務
gRPC - 打造輕量、高效能的後端服務gRPC - 打造輕量、高效能的後端服務
gRPC - 打造輕量、高效能的後端服務
 
第18讲 Hdlc和Ppp
第18讲 Hdlc和Ppp第18讲 Hdlc和Ppp
第18讲 Hdlc和Ppp
 
嵌入式inux應用專題文件-智慧家庭系統
嵌入式inux應用專題文件-智慧家庭系統嵌入式inux應用專題文件-智慧家庭系統
嵌入式inux應用專題文件-智慧家庭系統
 
Juniper ScreenOS 基于Policy的
Juniper ScreenOS 基于Policy的Juniper ScreenOS 基于Policy的
Juniper ScreenOS 基于Policy的
 
第3讲 Tcpip协议栈
第3讲 Tcpip协议栈第3讲 Tcpip协议栈
第3讲 Tcpip协议栈
 
[圣思园][Java SE]Network
[圣思园][Java SE]Network[圣思园][Java SE]Network
[圣思园][Java SE]Network
 
MPTCP学习
MPTCP学习MPTCP学习
MPTCP学习
 
Linux firewall
Linux firewallLinux firewall
Linux firewall
 
MPI use c language
MPI use c languageMPI use c language
MPI use c language
 
Wbm9000动态库说明L
Wbm9000动态库说明LWbm9000动态库说明L
Wbm9000动态库说明L
 
Is
IsIs
Is
 
OpenResty 项目模块化最佳实践
OpenResty 项目模块化最佳实践OpenResty 项目模块化最佳实践
OpenResty 项目模块化最佳实践
 
Analysis on tcp ip protocol stack
Analysis on tcp ip protocol stackAnalysis on tcp ip protocol stack
Analysis on tcp ip protocol stack
 
Linux Network Monitoring
Linux Network MonitoringLinux Network Monitoring
Linux Network Monitoring
 
network2
network2network2
network2
 
Linux基础
Linux基础Linux基础
Linux基础
 
Linux system security
Linux system securityLinux system security
Linux system security
 
簡介 Linux 的 Network Namespace 功能
簡介 Linux 的 Network Namespace 功能簡介 Linux 的 Network Namespace 功能
簡介 Linux 的 Network Namespace 功能
 
Ryu Learning Guide
Ryu Learning GuideRyu Learning Guide
Ryu Learning Guide
 

Similar to Unix socket

构建ActionScript游戏服务器,支持超过15000并发连接
构建ActionScript游戏服务器,支持超过15000并发连接 构建ActionScript游戏服务器,支持超过15000并发连接
构建ActionScript游戏服务器,支持超过15000并发连接
Renaun Erickson
 
intro syslog syslogng
intro syslog syslogngintro syslog syslogng
intro syslog syslogngjuruntang
 
Syslog Ng
Syslog NgSyslog Ng
Syslog Ngflytod
 
syslog&syslog-ng
syslog&syslog-ngsyslog&syslog-ng
syslog&syslog-ngjurntang
 
高性能并发网络服务器设计与实现
高性能并发网络服务器设计与实现高性能并发网络服务器设计与实现
高性能并发网络服务器设计与实现ideawu
 
PHP 應用之一 socket funion : 偽 WEB Server
PHP 應用之一 socket funion : 偽 WEB ServerPHP 應用之一 socket funion : 偽 WEB Server
PHP 應用之一 socket funion : 偽 WEB Server
志賢 黃
 
Java技术讲座 网络编程
Java技术讲座 网络编程Java技术讲座 网络编程
Java技术讲座 网络编程
xujie
 
高性能并发Web服务器实现核心内幕
高性能并发Web服务器实现核心内幕高性能并发Web服务器实现核心内幕
高性能并发Web服务器实现核心内幕ideawu
 
Os讀書會20170415
Os讀書會20170415Os讀書會20170415
Os讀書會20170415
Jen-Wei Cheng
 
ZeroMQ简介
ZeroMQ简介ZeroMQ简介
ZeroMQ简介
Xu Wang
 
Openstack neutron 原理详解
Openstack neutron 原理详解Openstack neutron 原理详解
Openstack neutron 原理详解
Yong Luo
 
组网实践
组网实践组网实践
组网实践
telab
 
High performance browser network wired network
High performance browser network   wired network High performance browser network   wired network
High performance browser network wired network
柏瑀 黃
 
Rpc原理与实现
Rpc原理与实现Rpc原理与实现
Rpc原理与实现
wavefly
 
Real time web实时信息流推送
Real time web实时信息流推送Real time web实时信息流推送
Real time web实时信息流推送yongboy
 
Real-Time Web实时信息流推送
Real-Time Web实时信息流推送Real-Time Web实时信息流推送
Real-Time Web实时信息流推送
yongboy
 
[若渴計畫2015.8.18] SMACK
[若渴計畫2015.8.18] SMACK[若渴計畫2015.8.18] SMACK
[若渴計畫2015.8.18] SMACK
Aj MaChInE
 
Using armeria to write your RPC
Using armeria to write your RPCUsing armeria to write your RPC
Using armeria to write your RPC
koji lin
 
4, OCP - oracle networking
4, OCP - oracle networking4, OCP - oracle networking
4, OCP - oracle networking
ted-xu
 
实时消息推送系统
实时消息推送系统实时消息推送系统
实时消息推送系统Yi Feng Yang
 

Similar to Unix socket (20)

构建ActionScript游戏服务器,支持超过15000并发连接
构建ActionScript游戏服务器,支持超过15000并发连接 构建ActionScript游戏服务器,支持超过15000并发连接
构建ActionScript游戏服务器,支持超过15000并发连接
 
intro syslog syslogng
intro syslog syslogngintro syslog syslogng
intro syslog syslogng
 
Syslog Ng
Syslog NgSyslog Ng
Syslog Ng
 
syslog&syslog-ng
syslog&syslog-ngsyslog&syslog-ng
syslog&syslog-ng
 
高性能并发网络服务器设计与实现
高性能并发网络服务器设计与实现高性能并发网络服务器设计与实现
高性能并发网络服务器设计与实现
 
PHP 應用之一 socket funion : 偽 WEB Server
PHP 應用之一 socket funion : 偽 WEB ServerPHP 應用之一 socket funion : 偽 WEB Server
PHP 應用之一 socket funion : 偽 WEB Server
 
Java技术讲座 网络编程
Java技术讲座 网络编程Java技术讲座 网络编程
Java技术讲座 网络编程
 
高性能并发Web服务器实现核心内幕
高性能并发Web服务器实现核心内幕高性能并发Web服务器实现核心内幕
高性能并发Web服务器实现核心内幕
 
Os讀書會20170415
Os讀書會20170415Os讀書會20170415
Os讀書會20170415
 
ZeroMQ简介
ZeroMQ简介ZeroMQ简介
ZeroMQ简介
 
Openstack neutron 原理详解
Openstack neutron 原理详解Openstack neutron 原理详解
Openstack neutron 原理详解
 
组网实践
组网实践组网实践
组网实践
 
High performance browser network wired network
High performance browser network   wired network High performance browser network   wired network
High performance browser network wired network
 
Rpc原理与实现
Rpc原理与实现Rpc原理与实现
Rpc原理与实现
 
Real time web实时信息流推送
Real time web实时信息流推送Real time web实时信息流推送
Real time web实时信息流推送
 
Real-Time Web实时信息流推送
Real-Time Web实时信息流推送Real-Time Web实时信息流推送
Real-Time Web实时信息流推送
 
[若渴計畫2015.8.18] SMACK
[若渴計畫2015.8.18] SMACK[若渴計畫2015.8.18] SMACK
[若渴計畫2015.8.18] SMACK
 
Using armeria to write your RPC
Using armeria to write your RPCUsing armeria to write your RPC
Using armeria to write your RPC
 
4, OCP - oracle networking
4, OCP - oracle networking4, OCP - oracle networking
4, OCP - oracle networking
 
实时消息推送系统
实时消息推送系统实时消息推送系统
实时消息推送系统
 

Unix socket

  • 2.
  • 3. A server process and one or more client processes. Server manages some resources. Server provides services by manipulating resource for clients.
  • 4.
  • 5. Two common paradigms for clients and servers communication • Datagrams (UDP protocol, SOCK_DGRAM) • Connections (TCP protocol, SOCK_STREAM) Connections are point-to-point, full-duplex (2- way communication), and reliable (only for TCP).
  • 6.
  • 7. CLIENT SERVER Client programs • Web browsers, ftp, telnet, ssh How does a client find the server? • IP address • Port Servers are long-running processes Each server waits for requests to arrive on a well- known port associated with a particular service.
  • 8.
  • 9. Concurrency is a property of systems in which several computations are executing simultaneously, and potentially interacting with each other. • Single host, multiple processes • Single process, multiple threads
  • 10. 大多數的伺服程式都是動態運作的,一收到新 要求就相對建立一個新的 thread。 • Parent_P: 一為接受要求與建立新的 thread 的程式碼 • Child_P: 另一為處理個別要求的程式碼 當 concurrent 伺服程式開始執行時,只有第一 部分會被執行。也就是說伺服程式的Parent_P: 是用來等待新的請求。當要求到達時, Parent_P:會建立一個新的Child_P來處理此要 求。此Child_P處理完要求後,即可終止。但 Parent_P仍將一直維持伺服程式在執行的狀態, 繼續等待下一個新的要求。
  • 11. Socket API 原本是 BSD UNIX 作業系統的 一部份。當初是由美國政府出資,由 Berkley Univ. 所負責研發新版 UNIX 中,用 在 TCP/IP 網際網路通訊協定。它是提供應 用程式使用網路通訊的一組程序 (procedure)。
  • 12. UNIX domain socket • Local IPC • AF_LOCAL or AF_UNIX • Filesystem: path name Internet domain socket • Internet communications • IPv4: AF_INET • IPv6: AF_INET6 • Internet address: IP + Port
  • 13. UNIX 將 socket 與其 I/O 的概念相結合,讓 應用程式間的通訊就像在讀寫檔案一般容易。 在應用程式使用通訊協定做通訊時,需先要 求作業系統產生(open)一個 socket,系統將 回傳一 descriptor(sockfd)來代表此 socket。 在後續處理時皆須利用此指標。
  • 14.
  • 15. Internet-specific socket address (IPv4): <netinet/in.h> (Posix.1g) • Must cast (sockaddr_in *) to (sockaddr *) for connect(), bind(), and accept().
  • 16. Generic socket address: <sys/socket.h> • For address arguments to connect(), bind(), and accept().
  • 17. Internet-specific socket address (IPv6): <netinet/in.h> (Posix.1g)
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25. int gethostname(char *name, int namelen) 客戶或伺服程式可以呼叫 gethostname 取 得本機的位址資訊
  • 26. #include <sys/socket.h> int socket (int family, int type, int protocol); • returns: nonnegative integer descriptor if OK, -1 on error socket:用來建立一個 socket,回傳一整數的 descriptor (sockfd) family: protocol family type: communication type • protocol: 指定此 socket 所使用的傳輸通訊協定。 Specifying a protocol of 0 causes socket() to use an unspecified default protocol appropriate for the requested socket type (e.g., TCP/IP).
  • 27.
  • 28. #include <sys/socket.h> int connect (int sockfd, const struct sockaddr *servaddr, socklen_t addrlen); • returns: 0 of OK, -1 on error connect:client程式利用 connect 與指定的server 程式建立連線。 • sockfd: 為用來連線的客戶端之 socket descriptor • *servaddr: 為一指向記錄Server IP和Port之 sockaddr 資料 結構的位址 • addrlen: 為 sockaddr 資料結構的長度
  • 29. #include <sys/socket.h> int bind (int sockfd, const struct sockaddr *myaddr, socklen_t addrlen); • returns: 0 if OK, -1 on error bind:當 socket 建置時,並未宣告本機地址和遠 端主機的位址。伺服程式需用 bind 程序來將 socket與本機上的一個 port 相關聯,之後 server 就可以在該port 監聽服務請求。 • sockfd: 參數值為已建置但未指定 port number 的 descriptor • *myaddr: 為在名本機位址的資料結構之起始位置 • addrlen: 為本機位址的資料結構之長度
  • 30. #include <sys/socket.h> int listen (int sockfd, int backlog); • returns: 0 if OK,-1 on error listen:當伺服程式宣告其通訊協定 port number 後 (ex: bind),可要求 OS 將此 socket 設為被動的等待 client 來聯繫的模式。 • sockfd: 參數值為已建置且已指定 port number 的 descriptor • backlog: 指定此 socket 的服務請求佇列 (request queue) 的最大值
  • 31. #include <sys/socket.h> int accept (int sockfd, struct sockaddr *cliaddr, socklen_t *addrlen); • returns: nonnegative descriptor if OK, -1 on error sockfd: listening socket accept returns: connected socket (new socket) cliaddr, addrlen: value-result arguments • cliaddr: 為 sockaddr 資料結構的起始位置 • addrlen: 為位址的長度
  • 32. #include <unistd.h> pid_t fork (void); • returns: 0 in child, process ID of child in parent, -1 on error (called-once-return-twice) A child has only one parent process. It can use getppid() to get parent’s PID. If a parent want to get a child’s PID, it has to record the return value of fork().
  • 33. 新的 process/thread (child) 被建立時,應 從產生它的 parent 身上繼承一份所有已開 啟的 socket。 Socket 使用 reference count 機制,當一個 socket 被建立時,系統就把 socket 的 reference count 設為 1,當這個 reference count 大於零的時候,socket都會存在。 • 當程式產生新的 process/thread 時,會把 parent 和 child process 的 reference count 都加 1。
  • 34. Status of client-server before call to accept returns
  • 35. Status of client-server after return from accept
  • 36. Status of client-server after fork returns
  • 37. Status of client-server after parent and child close appropriate sockets
  • 38. 當某一 process/thread 呼叫 socket 的 close 程序時,系統將其 reference count 值減一,並且從 thread 清單去除。 若 thread 在關閉 socket 前結束,系統會把 thread 開啟的所有 socket,執行 close 程 序。
  • 39. #include <unistd.h> int close (int sockfd); • returns: 0 if OK, -1 on error; close:告知系統結束使用 socket • socket 參數為欲關閉 socket 的 descriptor • 若此 socket 是 connection-oriented 的傳輸通訊協 定,close 在關掉 socket 之前會先結束網路的連線。 • socket 關閉後立即停止使用,並將系統資源釋放掉, 避免應用程式送出或收到更多的資料。
  • 40. send:客戶和伺服程式都需要發送資訊。 若 socket 已連線成功,可用 send 程序來 發送資料。 send (socket, *data, length, flags) • data 為指向要發送資料的起始位址 • length 為所發送資料的總長度 • flags 為發送資料的型別,flags參數值可能為 0 或 是下面三個值之一,一般設定為 0。 MSG-OOB MSG_PEEK MSG_DONTROUTE
  • 41. recv:客戶和伺服程式都需要接收對方發送 的資料。應用程式能呼叫 recv 從已連線 (connected)的 socket 中接收資料。 recv (socket,*buffer,length,flags) • buffer 指向存放接收資料的起始位址 • length 為 buffer 總長度 • flags:包含 MSG_OOB,MSG_PEEK
  • 42. Socket 也允許應用程式使用 read/write 來 傳送/接收資料。 read 與 write 必須與connected-socket 合用。 使用 read/write 程序最大的好處是具有通用 性。 read(int fd, void *buf, size_t count); write(int fd, const void *buf, size_t count);
  • 43. socket 程序有許多的參數及 options, setsockopt 設定 socket option 值, setsockopt 為讀取 socket option 值。 gethostbyaddr 與 gethostbyname 相反,將 IP 位址轉成對映的電腦名稱。
  • 44.
  • 45. Signals • “software interrupts” • usually occur asynchronously. • can be sent by one process to another process (or to itself) by the kernel to a process. zombie wait() && waitpid()
  • 46.
  • 47. to be notified, by kernel, if one or more I/O conditions are ready. Scenarios in networking applications: • a client handling multiple descriptors (stdio/socket) • a client handling multiple sockets • a TCP server handling a listening socket and its connected sockets • a server handling both TCP and UDP • a server handling multiple services and protocols
  • 48. wait for data; copy data from kernel to user blocking I/O: blocked all the way nonblocking I/O: if no data, immediate returns EWOULDBLOCK I/O multiplexing (select and poll): blocked separately in wait and copy signal driven I/O (SIGIO): nonblocked in wait but blocked in copy (signaled when I/O can be initiated) asynchronous I/O (aio_): nonblocked all the way (signaled when I/O is complete)
  • 49.
  • 50. Synchronous I/O • The requesting process to be blocked until that I/O operation completes. • blocking, non-blocking, I/O multiplexing, and signal-driven I/O Asynchronous I/O • Does not cause the requesting process to be blocked. • Asynchronous I/O
  • 51. 可以使用select要求kernel測試某個輸入裝 置是否ready for reading 當kernel中對應此裝置的buffer中已有相當 數量(可設定)的輸入資料時,此裝置即是 ready for reading select return後,由我們的程式自行呼叫其它 的system call將buffer中的資料搬回來(也會 block,但時間有限)。
  • 52. 可以使用select要求kernel測試某個輸 出裝 置是否ready for writing 當kernel中對應此裝置的buffer中已有相 當 空間(可設定)可放置輸出資料時,此裝 置即 是ready for writing select return後,由我們的程式自行呼 叫其它 的system call將欲輸出資料搬入buffer中(也 會block,但時間有限) 。
  • 53.
  • 54.
  • 55.
  • 56. close() • Depend on the reference count (=0) • Terminate both directions shutdown() • Initial termination without considering reference count • Half-close
  • 57. I’m sleepy, google it by yourself.