Linux_Unix Network Programming Technology
Shared by: hcj
-
Stats
- views:
- 15
- posted:
- 5/9/2012
- language:
- English
- pages:
- 47
Document Sample


Linux/Unix Network
Programming Technology
明新技術學院資管系助理教授
清華大學資訊工程學系博士
林文宗
E-mail: wtlin@mis.mhit.edu.tw or
wtlin@simon.cs.nthu.edu.tw
WebPage: http://mars.cs.nthu.edu.tw/wtlin
(C) All rights are reserved by Asistant Professor Wen-Tsung Lin. 1
課程大綱
OSI Model, Client/Server Model, Process
I/O, Signals and Process Control
Inter-process Communication
Sockets, Transport Layer Interface
Library Routines
Case Studies
(C) All rights are reserved by Asistant Professor Wen-Tsung Lin. 2
相關技術與知識
C 語言
TCP/IP 網路基礎
Linux/Unix 基本指令
基本系統管理
(C) All rights are reserved by Asistant Professor Wen-Tsung Lin. 3
OSI Model
應用層 應用層
表現層 表現層
會議層 會議層
傳輸層 傳輸層
網路層 網路層
鏈結層 鏈結層
實體層 實體層
網路實體
(C) All rights are reserved by Asistant Professor Wen-Tsung Lin. 4
TCP/IP Layering Model
應用層 (Telnet, FTP…)
傳輸層 (TCP, UDP)
網路層 (IP, ICMP)
實體層 (Ethernet, Token Ring)
(C) All rights are reserved by Asistant Professor Wen-Tsung Lin. 5
Simplified 4-Layer Model
Process layer protocols
Process Process
Transport layer protocols Transport
Transport
Network Network layer protocols Network
Data Link Data-link layer protocols Data Link
(physical connection)
(C) All rights are reserved by Asistant Professor Wen-Tsung Lin. 6
Client-Server Model (I)
request Server
client response request
Network
response
request
response
client
(C) All rights are reserved by Asistant Professor Wen-Tsung Lin. 7
Client-Server Model (II)
Standard model for network applications.
Server
A process that is waiting to be connected by a
client process so that the server can do something
for the client.
Typical scenarios
The server process is started on some computer system.
It initialize itself, then goes to sleep waiting for a client
process to contact it requiring some service.
A client process is started, then the client process sends
a request across the network to the server requiring
service of some form.
When the server has finished providing its service to the
client, the server goes back to sleep, waiting for the next
client request to arrive.
(C) All rights are reserved by Asistant Professor Wen-Tsung Lin. 8
基本專有名詞
Process (or task)
A program that is being executed by the computer’s OS.
Inter-communication between 2 computers
Two processes, one running on each computer, are in
communication with each other.
IPC – Inter-Process Communication
Processes on a single computer to exchange information.
RPC – Remote Procedure Call
A process on the local system invokes a procedure on a remote
system.
System call
A set of direct entry points through which an active process can
obtain services from the kernel.
(C) All rights are reserved by Asistant Professor Wen-Tsung Lin. 9
C start-up and termination
functions
User functions or
Library functions
call return
User‘s main
function
call return
C startoff exit C exit _exit C _exit
routine routine routine
program invocation: exec system call
kernel
(C) All rights are reserved by Asistant Professor Wen-Tsung Lin. 10
Argument List
The main function is declared as:
main(argc, argv)
int argc;
char *argv[]; // char **argc;
{
}
argv argv[0] C string
argv[1] C string
.
.
.
argv[argc-1] C string
(C) All rights are reserved by Asistant Professor Wen-Tsung Lin. 11
Environment List (I)
The main function is declared as:
main(argc, argv, envp)
int argc;
char *argv[]; // char **argc;
char *envp[]; // char **envp;
{
int i
for (i=0; envp[i] != (char*) 0; i++)
printf(“%s\n”, envp[i]);
exit(0);
}
HOME=/usr1/stevens
SHELL=/bin/csh
TERM=att630
USER=stevens
PATH=/usr1/stevens/bin:usr/local/bin:/bin:/usr/bin
EXINIT=set optimize redraw shell=/bin/csh
(C) All rights are reserved by Asistant Professor Wen-Tsung Lin. 12
Environment List (II)
The C start-up function provides an external
variable named environ that can be used to
access the environment list.
main(argc, argv)
int argc;
char *argv[]; // char **argc;
{
int i
external char **environ;
for (i=0; environ[i] != (char*) 0; i++)
printf(“%s\n”, environ[i]);
exit(0);
}
(C) All rights are reserved by Asistant Professor Wen-Tsung Lin. 13
Environment List (III)
C library provides the function getenv.
main()
{
char *ptr, *getenv();
if ((ptr = getenv(“HOME”) == (char *) 0)
printf(“HOME is not defined\n”);
else
printf(“HOME=%s\n”, ptr);
exit (0);
}
(C) All rights are reserved by Asistant Professor Wen-Tsung Lin. 14
Typically arrangement of a
user process (I)
user context kernel context
stack kernel data
heap
un-initialized
data
initialized
read-write data
initialized read from program file
read-only data when program is executed
text
(C) All rights are reserved by Asistant Professor Wen-Tsung Lin. 15
Typically arrangement of a
user process (II)
Text portion:
contains the actual machine instructions that are executed by
the hardware.
Data portion:
Contains the program’s data.
Heap portion:
is used while a program is running to allocate more data space
dynamically to the process.
Stack portion:
is used dynamically while the process is running to contain the
stack frames that are used by many programming languages.
(C) All rights are reserved by Asistant Professor Wen-Tsung Lin. 16
Example Text segment
int debug = 1; machine instructions
char *progname; main, printf, strlen, strcpy
main(argc, argv)
int argc;
Read-only data
char argv[]; “argc = %d\n”
{ “%s\n”
int i;
char *ptr, *malloc();
Initialized read-write variable
progname = argv[0]; debug
printf(“argc = %d\n”, argc);
for (i=1; i < argc; i++) Stack
prt = malloc(strlen(argv[i] + 1)
strcopy(ptr, argv[i]);
i, ptr
if (debug)
printf(“%s\n”, ptr); Heap
} storage space allocated by malloc
}
function
(C) All rights are reserved by Asistant Professor Wen-Tsung Lin. 17
Process
PID – Process ID
Every process has a unique integer process ID.
0 ~ 30000
0: special kernel process termed either “swapper” or
“scheduler”.
1: init process
2: a kernel process termed the “pagedaemon”
The kernel assigns the PID when a process is
created and a process can obtain using the getpid
system call.
int getpid();
Parent Process ID
Every process has a parent process and a
corresponding parent process ID.
(C) All rights are reserved by Asistant Professor Wen-Tsung Lin. 18
Process-Related Information
Real User ID
/etc/password
unsigned short getuid();
Real Group ID
/etc/group
unsigned short getgid();
Effective User ID
unsigned short geteuid();
Effective Group ID
unsigned short getegid();
main()
{
printf(“pid = %d, ppid = %d\n”, getpid(), getppid());
exit (0);
}
(C) All rights are reserved by Asistant Professor Wen-Tsung Lin. 19
Processes 的關係
當 Unix 系統起動為 multi-user 模式時,kernel
會執行 /etc/init 程式做為起動事情。此 init
process 會以 user ID = 0 來執行,所以它具有
superuser 的權限。而 init 的動作會取決於 Unix
系統。
在 4.3BSD 系統下,init 會執行 /etc/rc 的 shell script,此檔案
會做一些正常的雜事,然後啟動一些正常的 daemon processes。
之後讀取 /etc/ttys 檔案來決定哪些 terminals (終端機) 將被建構
起來以做為 multi-user 操作模式。
在Unix System V 系統下,init 會讀取 /etc/inittab (指定在不同環
境下的動作)。對於正常的 multi-user 操作模式下,/etc/rc 會被
執行,此程式會起動大部份的 daemon processes。當此程式結束,
/etc/inittab 檔案會指定那些 terminals 將被建構起來以做為
multi-user 操作模式。
(C) All rights are reserved by Asistant Professor Wen-Tsung Lin. 20
Processes 的關係
process ID 1
init
fork fork
fork
init init ... init
exec exec exec
getty getty getty
/bin/sh (1)fork /bin/sh
(3)exit (2)exec
/bin/date
(C) All rights are reserved by Asistant Professor Wen-Tsung Lin. 21
Daemon Process
Daemon process 是一個在背景執行的 process,
定期在等待某事件的發生、或等待執行某些特定
的工作。一個daemon process 可能在許多不同的
情況下被執行起動:
當系統起動時。大部份的 system daemons 是被initialization
script /etc/rc 檔案藉由 /etc/init 的執行進入 mutli-user 模式的。
當一個 daemon 經由 /etc/rc 6起動後,它將擁有 superuser 的權
限。
以定期為基礎的從系統中的 /usr/lib/crontab 檔案中起動,以此方
法起動的daemons 可以用有 superuser 權限,因為 cron 程式本
身以 superuser 身份來執行。
以定期為基礎的從使用者中的 crontab 檔案中起動,一般使用者
可以建立他自己專屬的 crontab 檔案(4.3BSD不提供此功能)。
從使用者終端機中啟動,做為前景或背景 job。這通常是用來做
為測試 daemon 用。
(C) All rights are reserved by Asistant Professor Wen-Tsung Lin. 22
System Daemon
基本的 system daemon 會有下列的特性:
它們只被起動一次,即當系統起動時。
它們的生命時間即為系操作的全部時間,通常
是不會死亡而且之後不會重新起動。
它們花費大部份的時間來等待某事見的發生,
並執行其服務。
它們經常繁殖出其它的 processes 來處理服務
的要求。
(C) All rights are reserved by Asistant Professor Wen-Tsung Lin. 23
Job Control
一個Job 是一個或多個共享相同 process group
ID 的 processes。Job control 的特徵之一就是在
許多 job 中有能力去分享terminal。Job Control
需要 terminal driver、signal mechanism、shell
的支援。
(C) All rights are reserved by Asistant Professor Wen-Tsung Lin. 24
Job Control – An example
login
/bin/sh process group leader
fork
/bin/sh /bin/sh
exec exec
vi make
fork
make
exec
make
(C) All rights are reserved by Asistant Professor Wen-Tsung Lin. 25
Job Control – An example
main()
{
printf("pid = %d, pgrp = %d\n", getpid(), getppid());
exit(0);
}
實例觀察
a.out
a.out & a.out &
(a.out & a.out &)
換 sh 觀察看看。
(C) All rights are reserved by Asistant Professor Wen-Tsung Lin. 26
Password File
/etc/passwd file
login-name:encrypted-passwd:user-id:group-id:misc:login-
directory:shell
2 function in C library to search the /etc/passwd file,
looking for a matching user ID or login name.
Shadow Password
/etc/shadow
#include <pwd.h>
struct passwd *getpwuid(int uid)
struct passwd *getpwnam(char *name)
(C) All rights are reserved by Asistant Professor Wen-Tsung Lin. 27
Group File
/etc/group file
group-name:encrypted-passwd:group-id:user-list
The function in C library used to initialize the
group access list.
int initgroups(char *name, int basegid);
Change the real group ID
newgrp group-name
#include <grp.h>
struct group *getgrgid(int gid)
struct group *getgrname(char *name)
(C) All rights are reserved by Asistant Professor Wen-Tsung Lin. 28
Files
Attributes(0 for success, -1 for others)
#include <sys/types.h>
#include <sys/stat.h>
int stat(char *pathname, struct stat *buf);
Int fstat(int fildes, struct stat *buf);
File access permission
File mode creation mask
int umask(int cmask);
Umask 022 -> actual mode 644
(C) All rights are reserved by Asistant Professor Wen-Tsung Lin. 29
常用的
Unix System Call
(C) All rights are reserved by Asistant Professor Wen-Tsung Lin. 30
fork()
建立一個新的 process. 執行 fork 者為 parent process,
而由 fork 所產生出來的新 process 則為 child process。
每呼叫一次 fork system call, 會傳回兩次(一個傳回給
parent,一個傳給 child)。其兩者唯一的差異為:在
parent process 中傳回的值為新建立的 child process 的
PID;而在 child process 中的回傳值為 0。若呼叫失敗,
則回傳 –1。
int fork();
(C) All rights are reserved by Asistant Professor Wen-Tsung Lin. 31
fork() 的範例
main() 執行結果:
{ parent: child pid = 24445, parent pid = 24444
int childpid; child: child pid = 24445, parent pid = 24444
if ( (childpid = fork()) == -1) {
perror(“can’t fork”);
exit(1);
} else if (childpid == 0) {
/* child process */
printf(“child: child pid = %d, parent pid = %d\n”, getpid(), getppid());
exit(0)
} else {
/* parent process */
pritnf(“parent: child pid = %d, parent pid = %d\n”, childpid, getpid());
exit(0);
}
} (C) All rights are reserved by Asistant Professor Wen-Tsung Lin. 32
exec()
int execlp(char *filename, char *arg0, char *arg1, …, char *argn, (char *) 0);
int execlp(char *pathname, char *arg0, char *arg1, …, char *argn, (char *) 0);
int execle(char *pathname, char *arg0, char *arg1, …, char *argn, (char *) 0,
char **envp););
int execvp(char *filename, char **argv);
int execv(char *pathname, char **argv);
int execve(char *pathname, char **argv, char **envp);
Execlp(file, arg, …, 0) Execlp(file, arg, …, 0) Execlp(file, arg, …, 0)
create argv create argv create argv
convert file add
Execlp(file, arg, …, 0) Execlp(file, arg, …, 0) Execlp(file, arg, …, 0)
to path envp
(C) All rights are reserved by Asistant Professor Wen-Tsung Lin. 33
exec()
exec system call 將會以新的程式取代目前的
process。但 Process ID 並不會改變。呼叫 exec
system call 者稱為 calling process,而被執行的
程式即為 new program。
經由 exec system call 所含括執行的程式會繼承
呼叫此 exec 的process如下的屬性:
process ID、process group ID、
parent process ID
terminal group ID
time left until an alarm clock signal
root directory
current working directory
file mode creation mask
real user ID、real group ID
file locks (C) All rights are reserved by Asistant Professor Wen-Tsung Lin. 34
exec()
當新程式被執行可改變的兩項屬性為:
effective user ID
effective group ID
(C) All rights are reserved by Asistant Professor Wen-Tsung Lin. 35
exit()
一個 process 藉由呼叫 exit system call 結束並終止,而且此 system
call 不會傳回值給呼叫者 (Caller)。當呼叫 exit 時,一整數的 exit
status 值經由 process 傳給 kernel。之後,其 parent process 透過
wait system call 才能取得此 exit status。此exit status 的low-order
8bits 才可被使用,所以只允許一個 process 以 0 ~ 255 範圍間的值
做為 process 終止時的 exit status。一般而言,程式正常終止是傳回
exit status 0 的值,而非零的 exit status 用來表示錯誤的情形。
必須注意的是要區分 exit system call 和標準 C 函式庫中的 exit
function。在標準 C 函式庫中的 exit function 首先允許 I/O 函式庫將
buffer 中清除,然後再執行 exit system call。而在標準 C 函式庫中的
另一個 _exit function 則是直接執行 exit system call。因此,_exit
function 是當 process 欲避免清除standard I/O 時所呼叫的。
(C) All rights are reserved by Asistant Professor Wen-Tsung Lin. 36
wait()
一個 process 可以藉由執行 wait system call 來等待其
child process 結束。
wait 所傳回的值為其已結束的 child process 的 Process
ID。若呼叫wait 的 process 沒有任何 child process,wait
system call 會立即傳回 -1。若呼叫 wait system call 的
process 有一或多個 child processes 未結束,則 kernel
會 將 此 呼 叫 的 process 暫 停 , 直 到 其 中 的 一 個 child
process 程式終止。當一個 child process 已終止而且
wait 回傳時,若 status 引數不為 NULL,則此藉由終止
child process傳給 exit system call 的值會被存放在 status
的變數中。
(C) All rights are reserved by Asistant Professor Wen-Tsung Lin. 37
wait()
以 下 三 種 情 況 , wait system call 會 將
process ID 做為其傳回值。
child process 呼叫 exit。
argument to exit (8 bits) 0x00(8-bits)
child process 被一個 signal 所終止。
0x00 (8 bits) core flag(1 bit) signal number(7bits)
child process 被偵錯,而此 process 已經停止。
signal number (8 bits) 0x7f(8 bits)
(C) All rights are reserved by Asistant Professor Wen-Tsung Lin. 38
wait()
當 parent process 在其 child process 終止前已
經結束終止,將會發生怎樣的情形?此有下列三
種情景必須考量:
child process 終止於其 parent process 之前,當我們正在輸入
commands 到 互動式的shell 時,此為「正常」狀況(假設在 command
未結束前,我們不會 logout)。此是若 parent process 已經執行 wait
system call,則 wait 會傳回已終止的 child process ID 給 parent
process。
若 parent 未執行 wait system call,則 child process 會變成一個 zombie
(殭屍) process。此時,kernel 會釋放其所使用的資源,但最少會保留其
的 exit status,直到其 parent process 執行 wait system call 等待此
process。
若 parent process 在其 child processes 終止前結束並終止,因為所有的
child processes 其 parent ID 已經不存在,此時 Unix 會去尋找所有的這
些 child processes,並將其 parent process ID 設為 1(亦即 init process)。
也就是說,init process 會成為這些即將成為「orphan (孤兒)」
processes 的 parent process。
(C) All rights are reserved by Asistant Professor Wen-Tsung Lin. 39
傳輸層界面 –
Socket
(C) All rights are reserved by Asistant Professor Wen-Tsung Lin. 40
Socket 的連線
對 TCP/IP 而言, 一個通訊端點位址的定義
為: (IP address, Port Number)
連線關係
(IP address, Port Number) <-> (IP address, Port Number)
(C) All rights are reserved by Asistant Professor Wen-Tsung Lin. 41
開啟 Socket
socket()
開啟通訊端點, socket() 呼叫系統產生一個新的 socket,
也就是向系統註冊, 通知細統建立一個通訊端點. 如同
對檔案讀寫時須先對檔案作 open() 動作一般.
socket() 功能呼叫執行成功後會得到一個整數值, 稱為
socket ID, 或稱 descriptor, 為 socket 的識別碼.
#include <sys/types.h>
#include <sys/socket.h>
int socket(domain, type, protocol)
int domain, type, protocol
(C) All rights are reserved by Asistant Professor Wen-Tsung Lin. 42
socket() 的參數
Domain (或稱 Family)
用來選擇哪一種通訊協定家族系列.
AF_UNIX: UNIX Internet Protocol
AF_INET: Internet Protocol
AF_NS: Xerox NS protocol
AF_IMPLINK: IMP link layer
AF_ 字眼表示 Address Family.
Type
對應不同的 Domain 內所使用的一種通訊協定.
Protocol
是在選定的 domain 下, 要使用哪一種通訊協定.
(C) All rights are reserved by Asistant Professor Wen-Tsung Lin. 43
Socket type 和
socket_domain
AF_UNIX AF_INET AF_NS
Socket_STREAM * TCP SPP
SOCK_DGRAM * UDP IDP
SOCK_RAW IP *
SOCK_SEQPACKET SPP
(C) All rights are reserved by Asistant Professor Wen-Tsung Lin. 44
socket 的定址
bind() 呼叫
當您建立一個新 socket 後, 接下來的工
作就是要對該 socket 定址. bind() 呼叫
可以讓您對某一個 socket 給定位址.
#include <sys/types.h>
#include <sys/socket.h>
int bind(sd, myaddr, adrlen)
int sd, adrlen;
struct sockaddr *myaddr;
(C) All rights are reserved by Asistant Professor Wen-Tsung Lin. 45
bind() 的參數
sd
您要定址的 socket 時別碼, 必須是呼叫 socket() 成
功後所得到的數值.
通訊端點根據不同的 domain 有不同的格式, 該位
址由 myaddr 指標所指向, 由呼叫者提供.
myaddr
是一資料結構 socketaddr 指標, 該結構用來告訴系
統, socket 位址為何.
如對 AF_INET 而言, 完整的 socket 位址包含 IP address,
TCP 或 UDP port 號碼.
(C) All rights are reserved by Asistant Professor Wen-Tsung Lin. 46
bind() 的參數
在 UNIX 的 <sys/socket.h> 引入檔中定義
了 sockaddr 的資料結構如下:
struct socket {
u_short sa_family;
char sa_data[14];
}
(C) All rights are reserved by Asistant Professor Wen-Tsung Lin. 47
Get documents about "