EECS 485 Lecture 14
OS and Parallelism
John Kloosterman
Copyright By PowCoder代写 加微信 powcoder
Learning Objectives
• Identify the role of the operating system in abstracting hardware resources
• Differentiate processes and threads, and identify when to use each
• Identify situations that need synchronization between threads and implement synchronization using locks
• Use sockets to perform network communication in Python
Processes and Threads
Project 4: MapReduce
• MapReduce: divide computation among many computers
• P4: simulate this on one computer
• Multiple programs (processes): manager and workers
• Multiple threads: do computation at same time as waiting for network
• Process: a running program
The process abstraction
• The process is the OS abstraction for execution • Also sometimes called a job or a task
• A process is a program in execution
• Programs are static entities with potential for execution
• Process consists of:
• A unique process ID (PID)
• An address space (memory)
• 1 or more threads (sequences of computation)
• Some other resources (file handles, open sockets,…)
Process address space
Static Data
When are processes useful?
• Multiple things happening at once
• Different programs running on the same machine • Same program running on different machines
• Multiple functions in a program running simultaneously
Say hi to partner
Thread Picture
def thread_body():
print(“Thread”)
t = Thread(target=thread_body)
print(“Main thread”)
# exercise:
# which order will the print statements run in?
• jkloosterman.net/485: Lecture 14 Exercise 1
• Change num_threads, see what happens to execution time
repl.it picture
Process with multiple threads
Static Data
Thread 1 Thread 2
Operating systems scheduling
• Each CPU core can run one thread at a time • Every ~1-10ms, the OS can switch
When are threads useful?
• Multiple things happening at once • Within one program
• May need to share data in memory
• Usually some slow resource • Network server
• Controlling a physical system (e.g., airplane controller) • Window system
• Parallel programming
Web server example
• Web server example
• Receives multiple simultaneous requests
• Reads web pages from disk to satisfy each request
When to use processes vs. threads
• Threads have lower overhead than processes
• Often used when threads share data with one another
• Processes provide separate address space • Useful when there is not complete trust
• Useful to limit the damage caused by buggy code • Needed when threads are on different computers
Synchronization
Why synchronization: repl.it
• jkloosterman.net/485: Lecture 14 Exercise 2
• Run the code multiple times. What is happening? What is an explanation?
repl.it drawing
• Change num_increments to 100 • Do you see the same behavior? • Why or why not?
Fix: synchronization
• Synchronization: threads agree on ordering constraints
• Don’t allow read and write to be separated • atomic operation
• Lock: make a region where only one thread can be inside at a time
l = threading.Lock()
def thread_body():
for i in range(num_increments):
l.acquire()
l.release()
Fixing repl.it 2
• jkloosterman.net/485: Lecture 14 Exercise 3
• Multiple ways of locking
• acquire_loop_release: lock during entire loop
• acquire_inc_release: lock during each increment
• acquire_inc1000_release: lock during groups of 1000 increments
• What order are their speeds, and why?
repl.it 3 drawing
• How do we share the network?
• One (or a few) network interface cards/IP addresses • But many applications sending/receiving data
• A network socket provides virtual NIC for a process • Specify sender or receiver by both IP address and port #
• TCP establishes connection between two sockets • UDP sends packets from one socket to another
• Socket API is provided by the operating system, and lets application programs use network sockets
• A process sends cess sends/receives
messages to its socket ssages to/from its
host or server
TCP with buffers, variables
controlled by app developer
host or server
TCP with buffers, variables
controlled by OS
ket • A process receives messages from its
ket analogous to door socket
ding process shoves ssages out the door
ding process relies on nsport infrastructure on
other side of the door to liver message to the socket
e the receiver process
Sockets at the command line
Server Client
$ nc –l localhost 8000
hello world
$ nc –l –p 8000
$ echo “hello world” |
nc localhost 8000
$ echo “hello world” |
nc localhost 8000
Socket example
At the client
• Translate localhost into IP address 127.0.0.1 • Decide to use the TCP protocol
• Create a socket
• connect to 127.0.0.1 at port 8000 •sendthe data
• Close the connection
$ nc –l localhost 8000
hello world
Socket example
At the receiver (server)
• Note: receiver was started first
• Create socket
• Decide to use TCP
• bind the socket to port 8000 •listenon the socket
•acceptthe connection request •recvthe packets (until connection closed) • Close the socket
Socket server in Python
import socket
if __name__ == “__main__”:
sock = socket.socket( socket.AF_INET,
socket.SOCK_STREAM,
sock.setsockopt(
socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((“localhost”, 8000))
Create socket
IPv4 (AF_INET6 for IPv6)
TCP (SOCK_DGRAM for UDP)
Avoid “[ERROR] Address already in use”
bind the socket to port 8000
Socket server in Python
sock.listen(5)
clientsocket, address = sock.accept()
print(“Connection from”, address)
message_chunks = [] while True:
data = clientsocket.recv(4096)
if not data:
break message_chunks.append(data)
listen on the socket, queue up as many as 5 connect requests before refusing outside connections
accept a connection request
recv() returns up to 4096 bytes.
Loop to receive all data. Assumes sender cleanly closes connection.
Socket server in Python
clientsocket.close()
message_bytes = b”.join(message_chunks) message_str = message_bytes.decode(“utf-8”) print(message_str)
Close the socket
Join together chunks of bytes, then interpret as a UTF-8 string
Socket server in Python
Server Client
$ python3 test_server.py
Server: connection from
(‘127.0.0.1’, 60980)
hello world
$ echo “hello world” |
nc localhost 8000
Socket server in Python
• When a TCP server accepts a new client, it returns a new socket to communicate with the client
• Allows the server to communicate with multiple clients
$ python3 test_server.py
Server: connection from
(‘127.0.0.1’, 60980) hello world
Socket client in Python
import socket
if __name__ == “__main__”:
# create an INET, STREAMing socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# connect to the server s.connect((“localhost”, 8000))
# send a message
message = “hello world!” sock.sendall(message.encode(‘utf-8’)) s.close()
Socket client in Python
Server Client
$ nc –l localhost 8000
hello world
$ python3 test_client.py
Socket client/server in Python
Server Client
$ python3 test_server.py
Server: connection from
(‘127.0.0.1’, 60980)
hello world
$ python3 test_client.py
Sockets and project 4
• Project 4 uses processes, threads and sockets to implement a Map Reduce server
• We need to do multiple things in parallel • Example: a manager and N workers
• N+1 processes
• Example: a worker’s task (a map function) and a worker’s heartbeat (“I’m alive” message)
• 2 threads
• Communicate between several machines
• Manager communicates with workers using sockets
Learning Objectives
• Identify the role of the operating system in abstracting hardware resources
• Differentiate processes and threads, and identify when to use each
• Identify situations that need synchronization between threads and implement synchronization using locks
• Use sockets to perform network communication in Python
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com