codekim@ai.hannam.ac.kr - PowerPoint PPT Presentation

1 / 34
About This Presentation
Title:

codekim@ai.hannam.ac.kr

Description:

Title: OS Author: shkim Last modified by: Young-Chul, Hwang Created Date: 10/4/2001 12:42:35 PM Document presentation format: Company: hannam univ – PowerPoint PPT presentation

Number of Views:59
Avg rating:3.0/5.0
Slides: 35
Provided by: shk5
Category:
Tags: codekim | hannam | sigint

less

Transcript and Presenter's Notes

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
????? ??
  • fork.c

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
???? ?? ????
  • zombie.c

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
???? ?? ????
  • wait.c

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
???? ?? ????
  • waitpid.c

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
??? ??? ?? ????
  • sigint.c

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
??? ??? ?? ????
  • sigint2.c

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??? ?? ???
????? ?????? ??? ????.
27
fork??? ??? ???? ????
  • ?? ?? ??

1. ????
Echo Server
Echo Client
2. ???? ??
3. ????
Process
Echo Client
Process
?? ?? ?? ??
28
fork??? ??? ???? ????
  • echo_multiserv.c

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))
29
fork??? ??? ???? ????
  • ?? ??

SERVER
Client 1
Client 2
30
TCP ? ?? ?? ????
  • ? ?? ??

Echo Client
Echo Server
?? ????
READ
socket
fork
WRITE
?? ????
?? ?? ?? ??
31
TCP ? ?? ?? ????
  • ??? ??? ??? ??

Client
Server
Client
Server
???
?? ???
?? ???? ???
??? ????? ??
??? ??? ??? ??
32
fork??? ??? ???? ????
  • echo_multiclnt.c

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 ?? ?????, ??? ?

34
Q A
Write a Comment
User Comments (0)
About PowerShow.com