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

Slides:



Advertisements
Similar presentations
Echo server The client reads a line of text from its standard input and writes the line to the server The server reads the line from its network input.
Advertisements

Elementary TCP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer.
1 Elementary TCP Sockets socket function connect function bind function listen function accept function fork and exec functions Concurrent servers close.
CSCE 515: Computer Network Programming TCP Details Wenyuan Xu Department of Computer Science and Engineering.
Global Employee Location Server Jeff Zhou 3/19/2011 CS4516 HELP Session 1 Modified based on CS4514 B05 slides.
Client 1 Client 2 Server Child Process1 Child Process2 (1) (2) (3) serverbase.txt Note: each child process keeps a separate copy of the DB.
1 TCP Sockets Computer Network Programming. 2 TCP Echo Server We will write a simple echo server and client –client read a line of text from standard.
Client 1 Client 2 Server Child Process1 Child Process2 (1) (2) (3) serverbase.txt Note: each child process keeps a separate copy of the DB.
1 Processes and Pipes COS 217 Professor Jennifer Rexford.
CS 311 – Lecture 14 Outline Process management system calls Introduction System calls  fork()  getpid()  getppid()  wait()  exit() Orphan process.
Concurrent TCP connections A look at design-changes which permit a TCP server to handle multiple clients without delays.
CS4514 HELP Session 3 Concurrent Server Using Go-Back-N Song Wang 12/08/2003.
Concurrent Server Using Selective Repeat Data Link Layer Mingzhe Li 12/05/2005 CS4514 HELP Session 3.
Fork Fork is used to create a child process. Most network servers under Unix are written this way Concurrent server: parent accepts the connection, forks.
Daemon Processes and inetd Superserver
Concurrent vs. iterative servers
CS4514 HELP Session 3 Concurrent Server Using Go-Back-N Song Wang 02/17/2004.
Threads© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science Department.
Chapter 5. TCP Client-Server Example. Contents –Introduction –TCP Echo Server –TCP Echo Client –Normal Startup and Termination –Posix Signal Handling.
Networking S04, Recitation, Section A
Server Design Discuss Design issues for Servers Review Server Creation in Linux.
CS4516: Medical Examiner Client/Server Evan Frenn 1.
1 TCP Client-Server Example TCP echo server: main and str_echo TCP echo client: main and str_cli Normal startup and termination POSIX signal handling Handling.
Elementary TCP Sockets
Chapter 8 Elementary UDP Socket. Contents u recvfrom and sendto Function u UDP Echo Server( main, de_echo Function) u UDP Echo Client( main, de_cli Function)
Server Sockets: A server socket listens on a given port Many different clients may be connecting to that port Ideally, you would like a separate file descriptor.
Cli/Serv.: procs/51 Client/Server Distributed Systems v Objectives –look at how to program UNIX processes , Semester 1, Processes.
Elementary TCP Sockets
Programming with TCP – III 1. Zombie Processes 2. Cleaning Zombie Processes 3. Concurrent Servers Using Threads  pthread Library Functions 4. TCP Socket.
Recitation 12 (Nov. 29) Outline Socket programming Lab 7: part 1 Reminder Lab 7: Due next Thursday Minglong Shao Office hours: Thursdays.
Concurrency. Readings r Tanenbaum and van Steen: r Coulouris: Chapter 6 r cs402 web page links r UNIX Network Programming by W. Richard Stevens.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Processes.
Elementary TCP Sockets –The presentation will provide sufficient information to build a COMPLETE TCP client and server. –In addition the topic of concurrency.
TELE 402 Lecture 3: Elementary … 1 Overview Last Lecture –TCP/UDP and Sockets introduction This Lecture –Elementary TCP sockets –Source: Chapter 4 of Stevens’
1 Concurrent Programming. 2 Outline Concurrent programming –Processes –Threads Suggested reading: –12.1, 12.3.
Shell (Addendum). Example r What if we want to support something like this: m ps –le | sort r One process should execute ps –le and another should execute.
Chapter 14 Unix domain protocol. contents Introduction unix domain socket address structure socketpair socket function unix domain stream client-server.
System calls for Process management
I/O Multiplexing. TCP Echo Client: I/O operation is sequential !! tcpcliserv/tcpcli01.c: lib/str_cli.c: TCP Client TCP Server stdin stdout fgets fputs.
1 Daemons & inetd Refs: Chapter Daemons A daemon is a process that: –runs in the background –not associated with any terminal Unix systems typically.
Processes CSCI 4534 Chapter 4. Introduction Early computer systems allowed one program to be executed at a time –The program had complete control of the.
Consider Letting inetd Launch Your Application. inetd daemon  Problems starting with /etc/rc(without inet daemon)  All the servers contains nearly identical.
Threads and Locking Ioctl operations. Threads Lightweight processes What’s wrong with processes? –fork() is expensive – 10 to 100 times slower –Inter.
Threads Chapter 26. Threads Light-weight processes Each process can have multiple threads of concurrent control. What’s wrong with processes? fork() is.
Introduction A Simple Daytime Client A Simple Daytime Server
Intro to Socket Programming CS 360. Page 2 CS 360, WSU Vancouver Two views: Server vs. Client Servers LISTEN for a connection and respond when one is.
TCP Client-Server Example
Concurrent Servers. Idea Behind Concurrent Servers Server Client 1 Server 1 X.
Zombie and orphan processes. Zombie process (from wikipedia) When a process ends, all of the memory and resources associated with it are deallocated so.
System calls for Process management Process creation, termination, waiting.
Dsh: A Devil Shell COMPSCI210 Recitation 14 Sep 2012 Vamsi Thummala.
ECE 297 Concurrent Servers Process, fork & threads ECE 297.
Chapter 5. TCP Client-Server Example
Extending echo server HTTP Broken pipe error Feedback and evaluation
Threads Threads.
Concurrent vs. iterative servers
Concurrent Programming November 13, 2008
CH5 TCP Client - Server Example:
Chapter4 Elementary TCP Socket
Concurrent Server Using Go-Back-N
CHAPTER 8 ELEMENTARY UDP SOCKETS
Recitation 11 – 4/29/01 Outline Sockets Interface
Chapter 5 (part 1) TCP Client /Server Example By: Lim Meng Hui.
Advanced Network Programming spring 2007
Network Programming CSC- 341
Elementary UDP Sockets connectionless, unreliable, datagram
Concurrent Server (in APP) Using Go-Back-N (in DLL)
Concurrent Programming November 13, 2008
TCP Client-Server Example
Concurrent Programming
Presentation transcript:

Fork(), Concurrent Server, Normal Termination ( 금 ) 김 희 준

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

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

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 를 가짐

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()

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 } Close(connfd);/* parent closes connected socket */ 21 } Concurrent Servers (1/2)

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

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

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

tcpserv01.c / str_echo.c

tcpcli01.c / str_cli.c

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

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

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