r Some of these slides are from Prof Frank Lin SJSU. r Minor modifications are made. 1
2 Client-server model Typical network app has two pieces: client and server application transport network data link physical application transport network data link physical Client: initiates contact with server ( “ speaks first ” ) r typically requests service from server, r for Web, client is implemented in browser; for , in mail reader Server: r provides requested service to client r e.g., Web server sends requested Web page, mail server delivers e- mail request reply
3 More on Client Server Model r Clients initiate requests r Servers wait for incoming requests and provide services m Servers always up and running m Operating systems normally start servers automatically when they start r More differences m Servers access system resources => special privileges m Servers sometimes handle security issues
4 Connectionless vs. Connection- Oriented Protocols r Connectionless protocols m UDP m Fast m Unreliable r Connection-oriented protocols m TCP m Slower m Reliable r Examples
5 Stateless vs. Stateful Protocols r Stateful protocols m Keeping states in the servers m Reducing the size and number of messages m Applications sometimes need session information r Stateless protocols m Simple m Protocol reliability r example m Stateless file server m Stateful file server
6 How to keep track of clients? Two ways r End points (IP address, port number) m What is wrong? r Handles m A short identifier allocated by the server, stored in server’s database and sent to the client
7 Keeping track of clients: handles r user accounts r shopping carts r Web portals r Advertising m Secretly collecting users’ browsing habits r What to do?
8 More on Stateful Protocols r Keeping correct states is challenging m What happens when clients crash and reboot? m What happens when messages arrive out of order, get duplicated or get lost? r Keeping states sometimes poses security risks m Tracking browsing patterns m Loss of confidential information (e.g. credit card number, ssn)
9 Concurrent Processing Concurrent Processing: r Why is it needed? r Achieving concurrent processing m Time sharing m multiprocessing r Concurrency in clients m no special efforts by client programmers r Concurrency in servers m Special efforts needed by server programmers Multiple processes Multiple threads
10 Processes and Threads r Processes m Fundamental unit of computation m An address space and at least one thread of execution (instruction pointer) r Thread m Light-weight process r Program vs process m Static concept
11 Sharing of Variables r Local Variable Rule - When multiple processes or threads execute a piece of code concurrently, each creates its own independent copy of the variables associated with the code (local variables) r Global Variable Rule - Each process creates a copy of global variables, but multiple threads within a single process share the process’s global variables int x; for (i=1; i<=10; i++) printf (“%d\n”, i+x);
12 Procedure Calls r Procedure Call Rule - When multiple threads execute a piece of code concurrently, each has its own stack of procedure activation records r Example - Putting the rules together!
13 Example of Concurrent Process Creation r Example 1 m Sequential processing r Example 2 m Concurrent version Computationally light Computationally intensive m Both processes do the same job!
14 Sequential C Example /* seq.c - A conventional C program that sums integers from 1 to 5 */ #include int sum; /* global variable */ main() { int i; /* local variable */ sum = 0; for (i=1; i<=5; i++) { /* iterate from 1 to 5 */ printf(“The value of i is %d\n”, i); fflush (stdout); /* flush buffer */ sum += i; } printf (“The sum is %d\n”, sum); exit (0); }
15 Concurrent C Example /* concurr.c - A concurrent C program that sums integers from 1 to 5 */ #include int sum; /* global variable */ main() { int i; /* local variable */ sum = 0; fork(); for (i=1; i<=5; i++) { /* iterate from 1 to 5 */ printf(“The value of i is %d\n”, i); fflush (stdout); /* flush buffer */ sum += i; } printf (“The sum is %d\n”, sum); exit (0); }
16 Concurrent Processes (II) r Processes perform different tasks r Distinguished by the return value of fork() r fork and execve allow newly created processes to execute an independent, separately-compiled program /* sum.c - A concurrent C program that performs different tasks for two processes */ #include main() { int pid; /* local variable */ pid=fork(); if (pid != 0) { /* original process */ printf(“The original process prints this.\n”); } else { /* newly created process */ printf (“The new process prints this.\n”); } exit(0); }
17 Concurrency in I/O Let us take a browser as an example r It needs to respond to input from users m Mouse clicks m Keyboard input r It needs to respond to input from the network m Downloaded web pages, images, … r It needs to send request to the network m Send a HTTP request How to handle this scenario? r Asynchronous I/O (aka non-blocking I/O) r Concurrency in I/O select system call