Docsity

Concurrent Programming, Lecture Slide - Computer Science, Slides of Computer System Design and Architecture

Partial preview of the text

Download Concurrent Programming, Lecture Slide - Computer Science and more Slides Computer System Design and Architecture in PDF only on Docsity!

Concurrent Programming

15-­‐213: Introduc0on to Computer Systems

nd

Lecture, Nov. 11, 2010

Instructors:

Randy Bryant and Dave O’Hallaron

Concurrent Programming is Hard!

 The human mind tends to be sequen9al

 The no9on of 9me is o<en misleading

 Thinking about all possible sequences of events in a

computer system is at least error prone and frequently

impossible

Client /

Server

Session

Itera9ve Echo Server

Client Server

socket socket

bind

listen

rio_readlineb

rio_readlineb rio_writen

rio_writen

Connec9on

request

rio_readlineb

close

close

EOF

Await connec9on

request from

next client

open_listenfd

open_clientfd

connect accept

Itera9ve Servers

 Itera9ve servers process one request at a 9me

client 1 server client 2

connect

accept

connect

write

read

call read

close

accept

write

read

close

Wait for Client 1

call read

write

ret read

write ret read

Fundamental Flaw of Itera9ve Servers

 Solu9on: use concurrent servers instead

 Concurrent servers use mul0ple concurrent flows to serve mul0ple

clients at the same 0me

User goes

out to lunch

Client 1 blocks

waiting for user

to type in data

Client 2 blocks

waiting to read from server

Server blocks

waiting for

data from

Client 1

client 1 server client 2

connect

accept

connect

write

read

call read

write

call read

write ret read

Crea9ng Concurrent Flows

 Allow server to handle mul0ple clients simultaneously

 1. Processes

 Kernel automa0cally interleaves mul0ple logical flows

 Each flow has its own private address space

 2. Threads

 Kernel automa0cally interleaves mul0ple logical flows

 Each flow shares the same address space

 3. I/O mul9plexing with select()

 Programmer manually interleaves mul0ple logical flows

 All flows share the same address space

 Relies on lower-­‐level system abstrac0ons

Review: Itera9ve Echo Server

int main(int argc, char **argv)

int listenfd, connfd;

int port = atoi(argv[1]);

struct sockaddr_in clientaddr;

int clientlen = sizeof(clientaddr);

listenfd = Open_listenfd(port);

while (1) {

connfd = Accept(listenfd, (SA *)&clientaddr, &clientlen);

echo(connfd);

Close(connfd);

exit(0);

 Accept a connec0on request

 Handle echo requests un0l client terminates

int main(int argc, char **argv)

{

int listenfd, connfd;

int port = atoi(argv[1]);

struct sockaddr_in clientaddr;

int clientlen=sizeof(clientaddr);

Signal(SIGCHLD, sigchld_handler);

listenfd = Open_listenfd(port);

while (1) {

connfd = Accept(listenfd, (SA *) &clientaddr, &clientlen);

if (Fork() == 0) {

Close(listenfd); /* Child closes its listening socket */

echo(connfd); /* Child services client */

Close(connfd); /* Child closes connection with client */

exit(0); /* Child exits */

}

Close(connfd); /* Parent closes connected socket (important!) */

}

}

Process-­‐Based Concurrent Server

Fork separate process for

each client

Does not allow any

communication between

different client handlers

Process Execu9on Model

 Each client handled by independent process

 No shared state between them

 Both parent & child have copies of listenfd and connfd

 Parent must close connfd

 Child must close listenfd

Client 1

Server

Process

Client 2

Server

Process

Listening

Server

Process

Connection Requests

Client 1 data Client 2 data

Concurrent Server: accept Illustrated

listenfd(3)

Client

1. Server blocks in accept,

wai4ng for connec4on request

on listening descriptor

listenfd

clientfd

Server

listenfd(3)

Client

clientfd

Server

2. Client makes connec4on request by

calling and blocking in connect

Connec9on

request

listenfd(3)

Client

clientfd

Server

3. Server returns connfd from

accept. Forks child to handle client.

Client returns from connect.

Connec4on is now established between

clientfd and connfd

Server

Child

connfd(4)

View from Server’s TCP Manager

Client 1 Client 2 Server

cl1> ./echoclient greatwhite.ics.cs.cmu.edu 15213

srv> ./echoserverp 15213

srv> connected to (128.2.192.34), port 50437

cl2> ./echoclient greatwhite.ics.cs.cmu.edu 15213

srv> connected to (128.2.205.225), port 41656

Connec9on Host Port Host Port

Listening --- --- 128.2.220.10 15213

cl1 128.2.192.34 50437 128.2.220.10 15213

cl2 128.2.205.225 41656 128.2.220.10 15213

View from Server’s TCP Manager

 Port Demul9plexing

 TCP manager maintains separate stream for each connec0on

 Each represented to applica0on program as socket

 New connec0ons directed to listening socket

 Data from clients directed to one of the connec0on sockets

Connec9on Host Port Host Port

Listening --- --- 128.2.220.10 15213

cl1 128.2.192.34 50437 128.2.220.10 15213

cl2 128.2.205.225 41656 128.2.220.10 15213

Approach #2: Mul9ple Threads

 Very similar to approach #1 (mul9ple processes)

 but, with threads instead of processes

Tradi9onal View of a Process

 Process = process context + code, data, and stack

shared libraries

run-time heap

0

read/write data

Program context:

Data registers

Condition codes

Stack pointer (SP)

Program counter (PC)

Kernel context:

VM structures

Descriptor table

brk pointer

Code, data, and stack

read-only code/data

stack

SP

PC

brk

Process context