forked from yihong0618/2020
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a3b5283
commit f6571a8
Showing
6 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"cells": [], | ||
"metadata": {}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"cells": [], | ||
"metadata": {}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import os | ||
|
||
a = 2 | ||
pid = os.fork() | ||
pid = os.fork() | ||
print(a, pid, os.getpid()) | ||
# import os | ||
# import time | ||
|
||
# for i in range(2): | ||
# print("I'm about to be a dad!") | ||
# time.sleep(5) | ||
# pid = os.fork() | ||
# if pid == 0: | ||
# print("I'm {}, a newborn that knows to write to the terminal!".format(os.getpid())) | ||
# else: | ||
# print("I'm the dad of {}, and he knows to use the terminal!".format(pid)) | ||
# os.waitpid(pid, 0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import socket | ||
import time | ||
import os | ||
|
||
RESPONSE = """\ | ||
HTTP/1.1 200 OK | ||
<H1>Hello world{}</H1>""".replace("\n", "\r\n") | ||
|
||
|
||
with socket.socket() as s: | ||
s.bind(("127.0.0.1", 5434)) | ||
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | ||
s.listen(5) | ||
while True: | ||
conn, addr = s.accept() | ||
pid = os.fork() | ||
if pid == 0: | ||
s.close() | ||
conn.sendall(RESPONSE.format(os.getpid()).encode()) | ||
time.sleep(10) | ||
conn.close() | ||
os._exit(0) | ||
else: | ||
conn.close() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
########################################################################### | ||
# Concurrent server - webserver3c.py # | ||
# # | ||
# Tested with Python 2.7.9 & Python 3.4 on Ubuntu 14.04 & Mac OS X # | ||
# # | ||
# - Child process sleeps for 60 seconds after handling a client's request # | ||
# - Parent and child processes close duplicate descriptors # | ||
# # | ||
########################################################################### | ||
import os | ||
import socket | ||
import time | ||
|
||
SERVER_ADDRESS = (HOST, PORT) = '', 8888 | ||
REQUEST_QUEUE_SIZE = 5 | ||
|
||
|
||
def handle_request(client_connection): | ||
request = client_connection.recv(1024) | ||
print( | ||
'Child PID: {pid}. Parent PID {ppid}'.format( | ||
pid=os.getpid(), | ||
ppid=os.getppid(), | ||
) | ||
) | ||
print(request.decode()) | ||
http_response = b"""\ | ||
HTTP/1.1 200 OK | ||
Hello, World! | ||
""".replace(b"\n", b"\r\n") | ||
client_connection.sendall(http_response) | ||
time.sleep(60) | ||
|
||
|
||
def serve_forever(): | ||
listen_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
listen_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | ||
listen_socket.bind(SERVER_ADDRESS) | ||
listen_socket.listen(REQUEST_QUEUE_SIZE) | ||
print('Serving HTTP on port {port} ...'.format(port=PORT)) | ||
print('Parent PID (PPID): {pid}\n'.format(pid=os.getpid())) | ||
|
||
while True: | ||
client_connection, client_address = listen_socket.accept() | ||
pid = os.fork() | ||
if pid == 0: # child | ||
listen_socket.close() # close child copy | ||
handle_request(client_connection) | ||
client_connection.close() | ||
os._exit(0) # child exits here | ||
else: # parent | ||
client_connection.close() # close parent copy and loop over | ||
|
||
if __name__ == '__main__': | ||
serve_forever() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import os | ||
|
||
print(os.fork()) |