Programming with TCP – III 1. Zombie Processes 2. Cleaning Zombie Processes 3. Concurrent Servers Using Threads  pthread Library Functions 4. TCP Socket.

Slides:



Advertisements
Similar presentations
Process Management.
Advertisements

1 CS345 Operating Systems Φροντιστήριο Άσκησης 1.
CS Lecture 4 Programming with Posix Threads and Java Threads George Mason University Fall 2009.
Threads Programming Thread creation Synchronization.
15-213/ Intro to Computer Systems by btan with reference to Spring 10’s slides.
Threads Lab اللهم علمنا ما ينفعنا،،، وانفعنا بما علمتنا،،، وزدنا علماً
UNIX Process Control Bach 7 Operating Systems Course Hebrew University Spring 2007.
CS 311 – Lecture 14 Outline Process management system calls Introduction System calls  fork()  getpid()  getppid()  wait()  exit() Orphan process.
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.
Lecture 18 Threaded Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Process Control in Unix Operating Systems Hebrew University Spring 2004.
Process in Unix, Linux and Windows CS-3013 C-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
Unix Threads operating systems. User Thread Packages pthread package mach c-threads Sun Solaris3 UI threads Kernel Threads Windows NT, XP operating systems.
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.
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.
1 Concurrent Programming Andrew Case Slides adapted from Mohamed Zahran, Jinyang Li, Clark Barrett, Randy Bryant and Dave O’Hallaron.
Process in Unix, Linux, and Windows CS-3013 A-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
Introduction to Processes CS Intoduction to Operating Systems.
More on Socket API. How to Place Timeouts on Sockets (1)  Using SIGALRM signal Connection timeout 기간의 축소 Response timeout advio/dgclitimeo3.clib/connect_timeo.c.
Concurrency. Readings r Tanenbaum and van Steen: r Coulouris: Chapter 6 r cs402 web page links r UNIX Network Programming by W. Richard Stevens.
June-Hyun, Moon Computer Communications LAB., Kwangwoon University Chapter 26 - Threads.
Threads and Thread Control Thread Concepts Pthread Creation and Termination Pthread synchronization Threads and Signals.
ECE 297 Concurrent Servers Process, fork & threads ECE 297.
7.9 TCP Socket Options 7.10 SCTP Socket Options 7.11 fcntl Function 7.12 Summary 報告者 : 梁凱鈞.
1 Concurrent Programming. 2 Outline Concurrent programming –Processes –Threads Suggested reading: –12.1, 12.3.
Linux Processes Travis Willey Jeff Mihalik. What is a process? A process is a program in execution A process includes: –program counter –stack –data section.
Threads CSCE Thread Motivation Processes are expensive to create. Context switch between processes is expensive Communication between processes.
S -1 Posix Threads. S -2 Thread Concepts Threads are "lightweight processes" –10 to 100 times faster than fork() Threads share: –process instructions,
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.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Processes and Threads.
UNIX Socket Programming CS 6378 Project Reference Book: Unix Network programming: Networking APIs: Sockets and XTI (2nd edition), Prentice Hall >> Threads.
Concurrent Processes Processes can concurrently run same program. Processes can concurrently run same program. Processes can start other processes. Processes.
Concurrency & Context Switching Process Control Block What's in it and why? How is it used? Who sees it? 5 State Process Model State Labels. Causes of.
Operating Systems Process Creation
Chapter 6 P-Threads. Names The naming convention for a method/function/operation is: – pthread_thing_operation(..) – Where thing is the object used (such.
CSC 360, Instructor: Kui Wu Thread & PThread. CSC 360, Instructor: Kui Wu Agenda 1.What is thread? 2.User vs kernel threads 3.Thread models 4.Thread issues.
TCP Client-Server Example
Concurrent Servers. Idea Behind Concurrent Servers Server Client 1 Server 1 X.
Fork(), Concurrent Server, Normal Termination ( 금 ) 김 희 준
S -1 Processes. S -2 wait and waitpid (11.2) Recall from a previous slide: pid_t wait( int *status ) wait() can: (a) block; (b) return with status; (c)
Thread S04, Recitation, Section A Thread Memory Model Thread Interfaces (System Calls) Thread Safety (Pitfalls of Using Thread) Racing Semaphore.
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
2.2 Threads  Process: address space + code execution  There is no law that states that a process cannot have more than one “line” of execution.  Threads:
ECE 297 Concurrent Servers Process, fork & threads ECE 297.
Carnegie Mellon Recitation 11: ProxyLab Fall 2009 November 16, 2009 Including Material from Previous Semesters' Recitations.
Chapter 5. TCP Client-Server Example
Lesson One – Creating a thread
Threads Threads.
Copyright ©: Nahrstedt, Angrave, Abdelzaher
Concurrent Programming November 13, 2008
Unix Process Management
CH5 TCP Client - Server Example:
UNIX PROCESSES.
Tarek Abdelzaher Vikram Adve Marco Caccamo
Concurrent Server Using Go-Back-N
Processes in Unix, Linux, and Windows
Concurrent Servers Topics Limitations of iterative servers
Chapter 5 (part 1) TCP Client /Server Example By: Lim Meng Hui.
Processes in Unix, Linux, and Windows
Concurrent Programming : Introduction to Computer Systems 23rd Lecture, Nov. 13, 2018
Concurrent Programming CSCI 380: Operating Systems
Processes in Unix, Linux, and Windows
Concurrent Programming November 13, 2008
Instructor: Brian Railing
Concurrent Programming
Presentation transcript:

Programming with TCP – III 1. Zombie Processes 2. Cleaning Zombie Processes 3. Concurrent Servers Using Threads  pthread Library Functions 4. TCP Socket Options

Zombie Processes What is a zombie process?  Let’s follow through the steps involved in the normal termination of our TcpEchoClient & TcpConcurrentEchoServer1. 1.Client exits (user types ‘ quit ’). 2.Client process closes its socket descriptor by calling close. This sends FIN to the server’s end of the TCP to close the socket. 3.The server’s child process’ recv ( read ) returns 0 and closes its own end of the connection by calling close.

Zombie Processes cont.  What happens next? 4.The server’s child process terminates by calling exit. 5.A SIGCHLD signal is delivered to the parent when the server child terminates. This occurs in our example, but we do not catch the signal in our code and the default action the signal is to be ignored. Thus the child enters the zombie state.

Zombie Processes cont.  What is the purpose of zombie states?  To maintain information about the child, for the parent to fetch at some time later.  This information includes processID of the child, its termination status and information on the resource utilization of the child (CPU time, memory, etc.)

Zombie Processes cont.  What is the purpose of zombie states?  If a parent terminates and that parent has children in the zombie state, the parent process ID of all the zombie children is set to 1 (the init process) which will inherit the children and clean them up (i.e., init will wait for them which removes the zombie)  Obviously we do not want to leave zombies around. So whenever we fork a child, we must wait for it to prevent them from becoming zombies.  How?

Cleaning Zombie Processes  How to clean zombie processes?  The parent (the main server) establishes a signal handler to receive SIGCHLD signals.  Within the signal handler, the parent calls wait or waitpid to get the status of the child process, which removes the child process from the kernel.

Example Signal Handler  Here is an example signal handler:  Recall that delivery of a signal interrupts a system call (e.g. signal ). So the main loop of the server must be rewritten accordingly. void sig_chld(int signo){ pid_tpid; int signo; while((pid=waitpid(-1, &stat, WNOHANG))>0) printf(" Child %d terminated\n", pid); return; }

Main Loop of the Server  TcpConcurrentEchoServer2.c while(1){ connfd = accept(listenfd,...); if(connfd<0){ if(errno == EINTR) continue; else{ perror("accept"); exit(1); } if(fork()==0){ close(listenfd); doit(connfd); exit(0); } close(connfd); }

Concurrent Servers Using Threads  Here is the outline of a typical concurrent server using threads  ThreadCreation.c, TcpConcurrentEchoServer3.c void * doit(void *arg){ memcpy(connfd, arg, 4); function to be... executed by a close(connfd); worker thread } listenfd = socket(...); bind(listenfd,...); listen(listenfd,...); while(1){ connfd = accept(listenfd,...); pthread_create(..., NULL, doit, &connfd); }

Concurrent Servers Using Threads main thread worker threads User Kernel ref count = 1 TCP Buffers... For listenfd ref count = 1 TCP Buffers... For connfd listenfdconnfd Server’s PCB Socket Data Structure

1. pthread_create fork 2. pthread_join waitpid 3. pthread_t pthread_self(void); getpid ◦ Returns thread ID of the calling thread 4. int pthread_detach(pthread_t tid); ◦ Returns 0 if OK, positive Exxx value on error. ◦ A thread is joinable (default) or detached. When a joinable thread terminates, its threadID and exit status are retained until another thread calls pthread_join. But a detached thread is like a daemon process: When it terminates, all its resources are released and we cannot wait for it to terminate. If one thread needs to know when another thread terminates, it is best to leave the thread as joinable. Otherwise, make the thread detached. ◦ This function is commonly called by the thread that wants to detach it self: pthread_detach(pthread_self()); Other pthread Library Functions #include

5. void pthread_exit(void *status); ◦ One way for a thread to exit is to call this function. ◦ If the thread is not detached, its threadID and exit status are retained for a later pthread_join by some other thread in the calling process. ◦ The pointer « status» must NOT point to a local object (i.e., a local variable stored in stack) as the object disappears when the thread terminates. Other pthread Library Functions #include

void pthread_exit(void *status);  There are 2 other ways for a thread to terminate. 1.The function that started the thread returns. Since that function must be declared to return a pointer to void, that return value is the exit status the thread. 2.The main thread of the process terminates or if any other thread calls EXIT to terminate the process, which terminates ALL threads. Other pthread Library Functions #include

1. TCP_MAXSEG ◦ Allows to fetch or set the MSS for a TCP connection. 2. TCP_NODELAY ◦ If set, this option disables TCP’s Nagle Algorithm. By default this algorithm is enabled. Recall that the purpose of the Nagle Algorithm is to reduce the number of small packets on the network. ◦ The definition of small packet is any packet smaller than MSS. ◦ The algorithm states that if TCP has outstanding data (i.e., data that our TCP has sent, and for which it is currently awaiting an ACK), then no small packets will be sent on the connection in response to a user write until the existing data is ACKed. ◦ Two common apps that suffer from Nagle’s algorithm are rlogin & telnet, since they send each keystroke as a separate packet. TCP Socket Options

H 0 Telnet Example for Nagle’s Algorithm Here is how a telnet session will behave with/without Nagle algorithm: ( Assume an RTT of 600 ms & user types a char every 250 ms) E 250 L 500 L 750 O 1000 ! H EL LO ! H 0 E 250 L 500 L 750 O 1000 ! With Nagle Disabled every char will be in a packet.  TcpOptions.c Nagle Enabled (default) Nagle Disabled