Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fork(), Concurrent Server, Normal Termination 2007. 3. 30 ( 금 ) 김 희 준

Similar presentations


Presentation on theme: "Fork(), Concurrent Server, Normal Termination 2007. 3. 30 ( 금 ) 김 희 준"— Presentation transcript:

1 Fork(), Concurrent Server, Normal Termination 2007. 3. 30 ( 금 ) 김 희 준 icemichy@hufs.ac.kr

2 Contents fork() concurrent server simple echo client/server echo server ▲ main() ▲ str_echo() echo client ▲ main() ▲ str_cli() Normal startup Normal termination

3 Unix 에서 새로운 process 를 생성하는 유일한 방법 child process 가 parent process 로 부터 상속받는 것들 ▲ parent process 의 모든 환경 변수 ▲ parent process 가 열고 있는 모든 file descriptor ▲ file offset fork() - 1/3 #include pid_t fork(void); Returns: 0 in child, process ID of child in parent, -1 on error

4 fork() – 2/3 ▲ child process  fork() 는 child process 에서 parent’s process ID 대신 0 을 return  child process 는 단 하나의 parent process 만을 가짐  parent process ID 는 getppid() 를 사용하여 얻을 수 있다. ▲ parent process  child process 의 process ID 를 return  여러 개의 child process 를 가짐

5 fork() – 3/3 Exec() – program file execution ▲ How does a Program Run a Program? Fork() – Copy at all (parent) ▲ How Do We Get a New Process? PID process(PID = 1234) execvp(“ls”, “-l”); PID process(PID = 1234)  PID 유지된다. Before Fork() After fork Before Fork() After Before Fork() After fork Before fork() After fork()

6 1 pid_tpid; 2 intlistenfd, connfd; 3 4 listenfd = Socket(……); 5 6/* fill in sockaddr_in() with server’s well-known port */ 7 Bind(listenfd, ……); 8 Listen(listenfd, LISTENQ); 9 10 for ( ; ; ) { 11connfd = Accept(listenfd, ……);/* probably blocks */ 12 13if ( (pid = Fork()) == 0) { 14Close(listenfd);/* child closes listening socket */ 15doit(connfd);/* process the request */ 16 Close(connfd);/* done with this client */ 17 exit(0);/* child terminates */ 18 } 19 20 Close(connfd);/* parent closes connected socket */ 21 } Concurrent Servers (1/2)

7 listenfd connfd Concurrent Servers (2/2) connect() listenfd connfd connect() listenfd connfd listenfd connfd listenfd connfd clientserver before accept after return from accept listenfd connfd after fork return after close sockets fork() connection request connection

8 Concurrent Servers overall listenfd connfd connect() listenfd connfd listenfd connfd listenfd connfd connect() listenfd connect() connection request connection fork Before accept After return from accept After fork return After close sockets

9 Simple Echo client/server echo ▲ 동작절차  client 는 입력받은 문자열을 server 에게 전송  server 는 client 로 부터 전송받은 문자열을 client 에게 되돌림  client 는 되돌려 받은 문자열을 화면에 출력 ▲ port number : 7 (TCP/UDP) TCP Client TCP Server stdin stdout fgets fputs writen readline writen

10 tcpserv01.c / str_echo.c

11 tcpcli01.c / str_cli.c

12 Normal startup // tcpserv01 // tcpcli01 // SERV_PORT // tcpcli01 은 문자열 입력대기 상태

13 Normal termination (1/2) // ctrl + D : EOF // zombie process

14 Normal termination (2/2) Problems at normal termination ▲ Server processes still remain as zombie processes  Zombie state of process  To maintain information about the child for the parent fetch at some later time such as  Child process ID  termination status  information on the resource utilization  When a child process is terminated, signal SIGCHLD is delivered to the parent (default action of SIGCHLD is to be ignored) Handling Abnormal Termination Cases including ▲ Client host crashes ▲ Client process crashes ▲ Network connectivity is lost, and so on


Download ppt "Fork(), Concurrent Server, Normal Termination 2007. 3. 30 ( 금 ) 김 희 준"

Similar presentations


Ads by Google