Professional Documents
Culture Documents
interfaces protocols
applications
Socket API
Examples
Opens TCP connection to port 80 (default http server port) at cis.poly.edu. Anything typed in sent to port 80 at cis.poly.edu
By typing this in (hit carriage return twice), you send this minimal (but complete) GET request to http server
mail server 3
mail server 4 5 6
because it only defines interactive communication but not the details of the service accessed
service at the same time. It is important yet challenging to provide concurrent services
Client-server model
Typical network app has two pieces: client and server Client:
initiates contact with server
application transport network data link physical
request
(speaks first) typically requests service from server, for Web, client is implemented in browser; for e-mail, in mail reader
reply
application transport network data link physical
Server:
provides requested service to client
10
Servers always up and running Operating systems normally start servers automatically when they start
More differences Servers access system resources => special privileges Servers sometimes handle security issues
11
Examples
12
13
A short identifier allocated by the server, stored in servers database and sent to the client
14
shopping carts
15
risks
Tracking browsing patterns Loss of confidential information (e.g. credit card number, ssn)
16
Concurrent Processing
Concurrent Processing:
Concurrency in clients
Concurrency in servers
17
unit of computation An address space and at least one thread of execution (instruction pointer)
Thread Light-weight process
Program vs process
Static concept
18
Sharing of Variables
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) process creates a copy of global variables, but multiple threads within a single process share the processs global variables
19
Procedure Calls
Procedure Call Rule - When multiple threads execute a piece of code concurrently, each has its own stack of procedure activation records Example - Putting the rules together!
20
21
Sequential C Example
/* seq.c - A conventional C program that sums integers from 1 to 5 */ #include <stdlib.h> #include <stdio.h> 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);
}
22
Concurrent C Example
/* concurr.c - A concurrent C program that sums integers from 1 to 5 */ #include <stdlib.h> #include <stdio.h> 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);
}
23
/* sum.c - A concurrent C program that performs different tasks for two processes */ #include <stdlib.h> #include <stdio.h> 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); } 24
Concurrency in I/O
Let us take a browser as an example It needs to respond to input from users
How to handle this scenario? Asynchronous I/O (aka non-blocking I/O) Concurrency in I/O
25
Lab Exercises
Use telnet to send an email to yourself
Concurrent processing Modify concurr.c so that the messages printed from parent and child processes are different Observe how the messages from parent and child are interleaved in the output Write and compile the summation procedure as a separate program and use execv() to call it in the child process
26