00 Ipc 5 Berkery cs162 Pipe Socket
00 Ipc 5 Berkery cs162 Pipe Socket
00 Ipc 5 Berkery cs162 Pipe Socket
Recall: Creating Processes with fork() Recall: Key Unix I/O Design Concepts
• pid_t fork() – copy the current process int status; • Uniformity – Everything Is a File!
– State of original process duplicated in pid_t tcpid; – file operations, device I/O, and interprocess communication through open, read/write,
Parent and Child! … close
cpid = fork(); – Allows simple composition of programs
– Address Space (Memory), File Descriptors, etc…
if (cpid > 0) {
• Return value from fork(): pid (like an integer) mypid = getpid();
» find | grep | wc …
– When > 0: printf("[%d] parent of [%d]\n", mypid, cpid); • Open before use
tcpid = wait(&status); – Provides opportunity for access control and arbitration
» Running in (original) Parent process printf("[%d] bye %d(%d)\n",mypid,tcpid,status); – Sets up the underlying machinery, i.e., data structures
» return value is pid of new child } else if (cpid == 0) {
mypid = getpid(); • Byte-oriented
– When = 0:
printf("[%d] child\n", mypid); – Even if blocks are transferred, addressing is in bytes
» Running in new Child process exit(42); • Kernel buffered reads
– When < 0: }
… – Streaming and block devices looks the same, read blocks yielding processor to other task
» Error! Must handle somehow
• Kernel buffered writes
» Running in original process – Completion of out-going transfer decoupled from the application, allowing it to continue
• WHY FORK? • Explicit close
– (mostly true) without fork(), you cannot create new processes!
– Fork was the original mechanism for creating concurrency in UNIX (long before Linux!)
– See, however, Linux clone() which gives you more flexibility
9/14/20 Kubiatowicz CS162 © UCB Fall 2020 Lec 5.3 9/14/20 Kubiatowicz CS162 © UCB Fall 2020 Lec 5.4
Putting it together: web server Putting it together: web server
Kernel buffer
4. parse request 9. format reply 4. parse request reads 9. format reply
Server Server
Process request reply Process request reply
buffer buffer buffer buffer
Kernel wait syscall RTU syscall syscall RTU Kernel syscall RTU syscall syscall RTU
wait
11. kernel copy 11. kernel copy
from user buffer from user buffer
to network buffer to network buffer
interrupt interrupt
interrupt 2. copy arriving 12. format outgoing 6. disk interrupt 2. copy arriving 12. format outgoing 6. disk
packet (DMA) 7. disk data packet (DMA) 7. disk data
packet and DMA request packet and DMA request
(DMA) (DMA)
Hardware Hardware
Network Network
Disk interface Disk interface
interface interface
Request Reply Today: Network Communication Request Reply Today: Network Communication
9/14/20 Kubiatowicz CS162 © UCB Fall 2020 Lec 5.5 9/14/20 Kubiatowicz CS162 © UCB Fall 2020 Lec 5.6
9/14/20 Kubiatowicz CS162 © UCB Fall 2020 Lec 5.9 9/14/20 Kubiatowicz CS162 © UCB Fall 2020 Lec 5.10
9/14/20 Kubiatowicz CS162 © UCB Fall 2020 Lec 5.21 9/14/20 Kubiatowicz CS162 © UCB Fall 2020 Lec 5.22
Out Out
9/14/20 Kubiatowicz CS162 © UCB Fall 2020 Lec 5.23 9/14/20 Kubiatowicz CS162 © UCB Fall 2020 Lec 5.24
When do we get EOF on a pipe? EOF on a Pipe
• After last “write” descriptor is closed, pipe is effectively closed: Process 1 Process 2
– Reads return only “EOF” pipe(…)
• After last “read” descriptor is closed, writes generate SIGPIPE signals: fork() Thread’s Thread’s
– If process ignores, then the write fails with an “EPIPE” error close(3) Regs Address Regs Address
… Space … Space
close(4)
close(4)
(Memory) (Memory)
User Space
Kernel Space File Descriptors File Descriptors
3
4 In
Pipe
Out
EOF
9/14/20 Kubiatowicz CS162 © UCB Fall 2020 Lec 5.25 9/14/20 Kubiatowicz CS162 © UCB Fall 2020 Lec 5.26
9/14/20 Kubiatowicz CS162 © UCB Fall 2020 Lec 5.27 9/14/20 Kubiatowicz CS162 © UCB Fall 2020 Lec 5.28
Web Server Client-Server Protocols: Cross-Network IPC
Client 1
Request
Client 2 Server
***
Reply
9/14/20 Kubiatowicz CS162 © UCB Fall 2020 Lec 5.29 9/14/20 Kubiatowicz CS162 © UCB Fall 2020 Lec 5.30
9/14/20 Kubiatowicz CS162 © UCB Fall 2020 Lec 5.31 9/14/20 Kubiatowicz CS162 © UCB Fall 2020 Lec 5.32
The Socket Abstraction: Endpoint for Communication Sockets: More Details
• Key Idea: Communication across the world looks like File I/O • Socket: An abstraction for one endpoint of a network connection
write(wfd, wbuf, wlen); – Another mechanism for inter-process communication
– Most operating systems (Linux, Mac OS X, Windows) provide this, even if they
Process Socket don’t copy rest of UNIX I/O
Process – Standardized by POSIX
Socket
n = read(rfd, rbuf, rmax); • First introduced in 4.2 BSD (Berkeley Standard Distribution) Unix
– This release had some huge benefits (and excitement from potential users)
– Runners waiting at release time to get release on tape and take to businesses
• Sockets: Endpoint for Communication
– Queues to temporarily hold results • Same abstraction for any kind of network
– Local (within same machine)
• Connection: Two Sockets Connected Over the network IPC over network!
– The Internet (TCP/IP, UDP/IP)
– How to open()?
– Things “no one” uses anymore (OSI, Appletalk, IPX, …)
– What is the namespace?
– How are they connected in time?
9/14/20 Kubiatowicz CS162 © UCB Fall 2020 Lec 5.33 9/14/20 Kubiatowicz CS162 © UCB Fall 2020 Lec 5.34
9/14/20 Kubiatowicz CS162 © UCB Fall 2020 Lec 5.35 9/14/20 Kubiatowicz CS162 © UCB Fall 2020 Lec 5.36
Simple Example: Echo Server Echo client-server example
void client(int sockfd) {
Client (issues requests) Server (services requests) int n;
fgets(sndbuf,bufsize,stdin); char sndbuf[MAXIN]; char rcvbuf[MAXOUT];
while (1) {
write(sockfd,sndbuf,strlen(sndbuf)+1); n = read(sockfd,reqbuf,…); fgets(sndbuf,MAXIN,stdin); /* prompt */
write(sockfd, sndbuf, strlen(sndbuf)+1); /* send (including null terminator) */
wait memset(rcvbuf,0,MAXOUT); /* clear */
n=read(sockfd, rcvbuf, MAXOUT); /* receive */
write(STDOUT_FILENO, rcvbuf, n); /* echo */
}
n = read(sockfd,rcvbuf, …);
Server print
Client
wait Socket Socket
write(sockfd,reqbuf,…); void server(int consockfd) {
char reqbuf[MAXREQ];
int n;
while (1) {
memset(reqbuf,0, MAXREQ);
len = read(consockfd,reqbuf,MAXREQ); /* Recv */
if (n <= 0) return;
write(STDOUT_FILENO, reqbuf, n);
print write(consockfd, reqbuf, n); /* echo*/
}
}
9/14/20 Kubiatowicz CS162 © UCB Fall 2020 Lec 5.37 9/14/20 Kubiatowicz CS162 © UCB Fall 2020 Lec 5.38
9/14/20 Kubiatowicz CS162 © UCB Fall 2020 Lec 5.41 9/14/20 Kubiatowicz CS162 © UCB Fall 2020 Lec 5.42
9/14/20 Kubiatowicz CS162 © UCB Fall 2020 Lec 5.45 9/14/20 Kubiatowicz CS162 © UCB Fall 2020 Lec 5.46
How Could the Server Protect Itself? Sockets With Protection (each connection has own process)
Client Server
• Handle each connection in a separate process Create Server Socket
Accept syscall()
9/14/20 Kubiatowicz CS162 © UCB Fall 2020 Lec 5.49 9/14/20 Kubiatowicz CS162 © UCB Fall 2020 Lec 5.50
9/14/20 Kubiatowicz CS162 © UCB Fall 2020 Lec 5.51 9/14/20 Kubiatowicz CS162 © UCB Fall 2020 Lec 5.52
Server Address: Itself Client: Getting the Server Address
struct addrinfo *setup_address(char *port) { struct addrinfo *lookup_host(char *host_name, char *port) {
struct addrinfo *server; struct addrinfo *server;
struct addrinfo hints;
struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_UNSPEC;
hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE; int rv = getaddrinfo(host_name, port_name,
&hints, &server);
getaddrinfo(NULL, port, &hints, &server);
if (rv != 0) {
return server; printf("getaddrinfo failed: %s\n", gai_strerror(rv));
} return NULL;
}
• Accepts any connections on the specified port return server;
}
9/14/20 Kubiatowicz CS162 © UCB Fall 2020 Lec 5.53 9/14/20 Kubiatowicz CS162 © UCB Fall 2020 Lec 5.54
Close Connection
Close Client Socket Close Server Socket
Socket
9/14/20 Kubiatowicz CS162 © UCB Fall 2020 Lec 5.55 9/14/20 Kubiatowicz CS162 © UCB Fall 2020 Lec 5.56
Thread Pools Conclusion
• Problem with previous version: Unbounded Threads • Interprocess Communication (IPC)
– When web-site becomes too popular – throughput sinks – Communication facility between protected environments (i.e. processes)
• Instead, allocate a bounded “pool” of worker threads, representing the
maximum level of multiprogramming • Pipes are an abstraction of a single queue
– One end write-only, another end read-only
– Used for communication between multiple processes on one machine
queue
Master
– File descriptors obtained via inheritance
Thread