Title: codekim@ai.hannam.ac.kr
1? 10? ????? ??? ????
TCP/IP Socket Programming
- ???????
- ?? 2?? ???
- codekim_at_ai.hannam.ac.kr
2??
- ?? ?? ??? ?? ???
- ????? ??
- ???? ?? ????
- ??? ??? ?? ????
- Fork ??? ??? ?? ?? ??? ??
- TCP ??? ?? ????
3?? ?? ??? ?? ???
- ?? ?? ??? ?? ??
- ???? ??? ?? ????? ??? ??
- select ??? ?? ????? ??? ??
- ???? ???? ?? ?? ??? ??? ??
4????? ??
- ???? ID
- ???? ???
- ?????? ???? ??? ??(2 32786)
???? ??? ?? ps ?? ??
5????? ??
- fork ?? ??? ?? ???? ??
- fork ?? ??
- fork? ??? ????? ??? ??
- ?? ??? ??? ??? ??
- ??? ????? ??? ??? ????? ??
- fork??? ?? ???? ???.
- ?? ???? ??? ?? ???? ID
- ?? ???? ??? 0
6????? ??
includeltstdio.hgt includeltunitd.hgt includeltsys/t
ypes.hgt int main( ) pid_t pid int
data 10 pid fork() if(pid
-1) printf(fork??, ???? id d\n,
pid) printf(fork??, ???? id d\n, pid)
if(pid 0) // ?? ???? data 10
else // ?? ???? data -
10 printf(data d\n, data) return
0
????
7???? ?? ????
- ?? ????
- ??? ?? ???? ?? ???? ?? ????
- ????? ???? ? ? ?? ????? ?? ??? ?? ????? ????
- ???? ??? ??? ???? ?? ??? ?? ?? ????? ?? ??? ??
????.
8???? ?? ????
?? ???? ?? ??
9???? ?? ????
includeltstdio.hgt includeltunitd.hgt includeltsys/t
ypes.hgt int main( ) pid_t pid int
data 10 pid fork() if(pid
-1) printf(fork??, ???? id d\n,
pid) printf(fork??, ???? id d\n, pid)
if(pid 0) // ?? ???? data 10
else // ?? ???? data - 10
sleep(20) // 20??? ?? ??
printf(data d\n, data) return 0
?? ???? ?? ??? sleep(20)??? ??? 20??? ?????
???. ?? ????? ?? ??? ?? ?? ????? 20? ?? ????.
20??? ?? ?? ?? ????? ??? ??? ?? ? ? ??.
10???? ?? ????
11???? ?? ????
- wait ??? ??
- wait ?? ??
- ?? ??? ?? ????? ???, ???? ?????.
- ??? ?? ????? ???, ??? ?? ????? ?????? ?? ?????
??? ??? ??. - wait??? ??? ???? ???? ??? ???? ?? ??
??? ?? ?? ?
WIFEXITED(status) ?? ??? ?? ?? 0? ????.
WEXITSTATUS(status) ???? return ??? exit??? ??? ??? ?? ????.
12???? ?? ????
includeltstdio.hgt includeltunitd.hgt includeltsys/t
ypes.hgt int main( ) pid_t pid, child
int data 10 int state pid
fork() if(pid -1) printf(fork??,
???? id d\n, pid) printf(fork??, ????
id d\n, pid)
if(pid 0) // ?? ???? data 10
else // ?? ???? data - 10
child wait(state) // ?? ???? ????
printf(?????? IDd\n, child)
printf(???d\n, WEXITSTATUS(state))
sleep(20) // 20??? ?? ?? printf(data
d\n, data) return 0
wait??? ???? ??? ??????? ????? ????.
WEXITSTATUS ??? ??? ???? ??????? ??? ???? ???.
wait??? ???? ??? ???? ?? ? ??.
13???? ?? ????
14???? ?? ????
- waitpid ??? ??
- waitpid ?? ??
- pid ?? ??? ??? ?? ????? ID??.
- status ??? ?? ???? ???? ????
- options WNOHANG ??? ??? ???? ?? ?? ???
??????? ??? ?? ??? ???? ?? ?? ???? ??.
15???? ?? ????
includeltstdio.hgt includeltunitd.hgt includeltsys/t
ypes.hgt int main( ) pid_t pid, child
int data 10 int state pid
fork() if(pid -1) printf(fork??,
???? id d\n, pid) printf(fork??, ????
id d\n, pid)
if(pid 0) // ?? ???? data 10
sleep(10) // ??? 10? ?? else
// ?? ???? data - 10 do
sleep(3) puts(3? ??)
child waitpid(-1, state, WNOHANG)
while(child 0) printf(Chid process
idd, return value d
\n,child,WEXITSTATUS(state)) printf(data
d\n, data) return 0
waitpid??? ???? ??? ??????? ????? ????.
WNOHANG??? ??? ?? ??? ??? ??? ?? ??? ???? ?? ??
????.
16???? ?? ????
17??? ??? ?? ????
1. ??????
2. ??? ??
Operatinn System
3. ??? ?? ?? ??
signal
Process
??? ??? ??
??? ???? ?? ??? ????? , ?? ??? ?? ????? ????
??? ??? ??? ???? ???? ?? ?? ??? ??? ???
???, ?? ??? ???? ????? ??
18??? ??? ?? ????
??? ?? ??
SIGALRM ??? ??? ?? ? ??? ??? ?? ????.
SIGINT ???? ??? ???. ??? ????? CtrlC ? ?? ?? ????.
SIGCHLD ?? ????? ??? ?? ????.
19??? ??? ?? ????
- signal ??? ??? ??? ???
- signal ?? ??
- ???? ?????? ??? ??? ??? ???? ???? ????
- int signum ??? ??
- func ??? ??? ??
20??? ??? ?? ????
includeltstdio.hgt includeltunitd.hgt includeltsigna
l.hgt void handler(int sig) int main( )
int state int num 0 signal(SIGINT,
handler) // ??? ?? while(1)
printf(d ???\n, num) sleep(2)
if(numgt5) break
return 0 void handler(int sig) // ???
??? ?? signal(SIGINT, handler)
printf(??? ???? d\n, sig)
21??? ??? ?? ????
SIGINT(cltr c) ??? ??? ??? ??? ?? ???? ??
22??? ??? ?? ????
- sigaction ??? ??? ??? ???
- sigaction ?? ??
- signum signal ??? ????? ?? ??? ?? ???? ??? ???
??????. - act ?? ??? ??? ??? ??? ???? sigaction ??? ???
???? ??? ???? ??. - oldact ??? ????? ??? ???? ???? ??? ?? ???? ??
????.
23??? ??? ?? ????
includeltstdio.hgt includeltunitd.hgt includeltsigna
l.hgt void handler(int sig) int main( )
int state int num 0 struct sigaction
act act.sa_handler handler
sigemptyset(act.sa_mask) act.sa_flags
0 state sigaction(SIGINT, act, 0)
if(state ! 0) puts(sigaction()
error) exit(1)
while(1) printf(d ???\n,
num) sleep(2) if(numgt5)
break return 0 void
handler(int sig) // ??? ??? ?? printf(???
???? d\n, sig)
24??? ??? ?? ????
SIGINT(cltr c) ??? ??? ??? ??? ?? ???? ??
25??? ??? ?? ????
- ???? ?? ?? ????? ??(zombie_handler.c)
includeltstdio.hgt includeltunitd.hgt includeltsigna
l.hgt includeltsys/types.hgt includeltsys/wait.hgt v
oid z_handler(int sig) int main( ) pid_t
pid int state, i struct sigaction
act act.sa_handler z_handler
sigemptyset(act.sa_mask) act.sa_flags
0 state sigaction(SIGCHLD, act, 0)
if(state ! 0) puts(sigaction()
error) exit(1)
pid fork() if(pid 0)
printf(?? ???? ?? d\n, getpid())
exit(3) else sleep(3)
return 0 void z_handler(int sig) // ???
??? ?? pid_t pid int rtn
while((pidwaitpid(-1, rtn, WNOHANG)) gt 0)
printf(?????????ID d\n, pid)
printf(?????? dn,WEXITSTATUS(rtn))
26??? ??? ?? ????
SIGCHLD ??? ?? ??? ??? ??? waitpid??? ?? ???
????? ?????? ??? ????.
27fork??? ??? ???? ????
1. ????
Echo Server
Echo Client
2. ???? ??
3. ????
Process
Echo Client
Process
?? ?? ?? ??
28fork??? ??? ???? ????
while(1) addr_size sizeof(clnt_addr)
clnt_sock accept(serv_sock, (struct sockaddr)
, addr_size)
if(clnt_sock -1) continue
if((pidfork()) -1)
close(clnt_sock) continue else
if(pid gt 0) // ?? ???? puts(????)
close(clnt_sock) else // ?? ????
close(serv_sock) while((str_lenread(
clnt_sock, message,
BUFSIZE)) ! 0)
write(clnt_sock, message, str_len)
write(1, message, str_len)
puts(????) close(clnt_sock)
exit(0) return
0 void z_handler(int sig) pid_t
pid int rtn pidwaitpid(-1, rtn,
WNOHANG) printf(?????????ID d\n, pid)
printf(?????? dn,WEXITSTATUS(rtn))
29fork??? ??? ???? ????
SERVER
Client 1
Client 2
30TCP ? ?? ?? ????
Echo Client
Echo Server
?? ????
READ
socket
fork
WRITE
?? ????
?? ?? ?? ??
31TCP ? ?? ?? ????
Client
Server
Client
Server
???
?? ???
?? ???? ???
??? ????? ??
??? ??? ??? ??
32fork??? ??? ???? ????
pid fork() If(pid 0) // ?????? (??? ??)
while(1) fputs(??? ???? ????? ,
stdout) fgets(message, BUFSIZE,
stdin) if(!strcmp(message, q\n))
shutdown(sock, SHUT_WR)
close(sock) exit(0)
write(sock, message, strlne(message))
else // ?? ???? ??? ?? while(1)
str_len read(sock, message, BUFSIZE)
if(str_len 0) exit(0)
messagestr_len 0
printf(????? ??? ???s\n, message)
close(sock) return 0
33????
- UNIX Network Programming, W.Richard Stevens
- TCP/IP ?? ?????, ??? ?
34Q A