4月24号
## 服务端
from threading import Thread
from socket import *
import json, struct, os
class MyTcpServer(Thread):
def __init__(self, conn, addr):
super().__init__()
self.conn = conn
self.addr = addr
def server_head(self, filesize, status):
head_dic = {'filesize': filesize, 'status': status}
head_json = json.dumps(head_dic)
head_json_bytes = head_json.encode('utf-8')
head_struct = struct.pack('i', len(head_json_bytes))
self.conn.send(head_struct)
self.conn.send(head_json_bytes)
def run(self):
while True:
print(f'from client {self.addr}
')
while True:
try:
head_struct = self.conn.recv(4)
if not head_struct: break
head_len = struct.unpack('i', head_struct)[0]
head_json = self.conn.recv(head_len).decode('utf-8')
head_dic = json.loads(head_json)
print(head_dic)
cmd = head_dic['cmd']
if hasattr(self, cmd):
func = getattr(self, cmd)
func(head_dic)
except Exception:
break
break
def get(self, args):
file_path = os.path.normpath(os.path.join('test_tcp', args['filename']))
if os.path.exists(file_path):
filesize = os.path.getsize(file_path)
self.server_head(filesize, True)
with open(file_path, 'rb') as f:
for line in f:
self.conn.send(line)
else:
self.server_head(None, False)
def put(self, args):
file_path = os.path.normpath(os.path.join('test_tcp', args['filename']))
filesize = args['filesize']
recv_size = 0
with open(file_path, 'wb') as f:
while recv_size < filesize:
recv_data = self.conn.recv(4096)
f.write(recv_data)
recv_size += len(recv_data)
if __name__ == '__main__':
server = socket()
server.bind(('127.0.0.1', 8080))
server.listen(5)
while True:
print('等待连接...')
conn, addr = server.accept()
t = MyTcpServer(conn, addr)
t.start()
## 客户端
import json, struct, os
from socket import *
class MyTcpClient:
def __init__(self, ipport):
self.ipport = ipport
self.client = socket(AF_INET, SOCK_STREAM)
try:
self.client_connect()
except Exception:
self.client_close()
def client_connect(self):
self.client.connect(self.ipport)
def client_close(self):
self.client.close()
def download(self, total_size, recv_size):
percent = recv_size / total_size
res = int(50 * percent) * '#'
print('
[%-50s] %d%%' % (res, int(100 * percent)), end='')
def run(self):
while True:
cmd = input("输入指令>>: ").strip()
if not cmd: continue
li = cmd.split()
cmd = li[0]
if hasattr(self, cmd):
func = getattr(self, cmd)
func(li)
def get(self, args):
cmd = args[0]
filename = args[1]
head_dic = {'cmd': cmd, 'filename': filename}
print(head_dic)
head_json = json.dumps(head_dic)
head_json_bytes = head_json.encode('utf-8')
head_struct = struct.pack('i', len(head_json_bytes))
self.client.send(head_struct)
self.client.send(head_json_bytes)
rc_headstruct = self.client.recv(4)
rc_len = struct.unpack('i', rc_headstruct)[0]
rc_json = self.client.recv(rc_len).decode('utf-8')
rc_dic = json.loads(rc_json)
if rc_dic['status']:
total_size = rc_dic['filesize']
recv_size = 0
with open(filename, 'ab') as f:
while recv_size < total_size:
recv_data = self.client.recv(1024)
recv_size += len(recv_data)
self.download(total_size, recv_size)
f.write(recv_data)
print('
文件下载成功')
else:
print("服务器没有此文件")
def put(self, args):
cmd = args[0]
filename = args[1]
if not os.path.isfile(filename):
print('file:%s is not exists' % filename)
return
else:
filesize = os.path.getsize(filename)
head_dic = {'cmd': cmd, 'filename': os.path.basename(filename), 'filesize': filesize}
print(head_dic)
head_json = json.dumps(head_dic)
head_json_bytes = bytes(head_json, encoding='utf-8')
head_struct = struct.pack('i', len(head_json_bytes))
self.client.send(head_struct)
self.client.send(head_json_bytes)
send_size = 0
with open(filename, 'rb') as f:
for line in f:
self.client.send(line)
send_size += len(line)
self.download(filesize, send_size)
else:
print('
upload successful')
client = MyTcpClient(('127.0.0.1',8080))
client.run()
4月22号
## 简述操作系统发展史
1. 真空管和穿孔卡片
优点:
程序员在申请的时间段内独享整个资源,可以即时地调试自己的程序(有bug可以立刻处理)
缺点:
浪费计算机资源,一个时间段内只有一个人用。
2. 晶体管和批处理系统
优点:批处理,节省了机时
缺点:1.整个流程需要人参与控制,将磁带搬来搬去(中间俩小人)
2.计算的过程仍然是顺序计算-》串行
3.程序员原来独享一段时间的计算机,现在必须被统一规划到一批作业中,等待结果和重新调试的过程都需要等同批次的其他程序都运作完才可以(这极大的影响了程序的开发效率,无法及时调试程序)
3. 集成电路芯片和多道程序设计
空间上的复用
时间上的复用
4. 个人计算机
## 简述进程发展史及算法演变
先来先服务(FCFS)调度算法
短作业(进程)优先调度算法
时间片轮转(Round Robin,RR)法
多级反馈队列调度算法
## 简述多道技术
多道技术中的多道指的是多个程序,多道技术的实现是为了解决多个程序竞争或者说共享同一个资源(比如cpu)的有序调度问题,解决方式即多路复用,多路复用分为时间上的复用和空间上的复用。
空间上的复用:将内存分为几部分,每个部分放入一个程序,这样,同一时间内存中就有了多道程序。
当一个程序在等待I/O时,另一个程序可以使用cpu,如果内存中可以同时存放足够多的作业,则cpu的利用率可以接近100%,类似于我们小学数学所学的统筹方法。(操作系统采用了多道技术后,可以控制进程的切换,或者说进程之间去争抢cpu的执行权限。这种切换不仅会在一个进程遇到io时进行,一个进程占用cpu时间过长也会切换,或者说被操作系统夺走cpu的执行权限)
## 简述同步异步阻塞非阻塞概念
同步: 所谓同步,就是在发出一个功能调用时,在没有得到结果之前,该调用就不会返回。
异步: 异步的概念和同步相对。当一个异步功能调用发出后,调用者不能立刻得到结果。当该异步功能完成后,通过状态、通知或回调来通知调用者。
阻塞: 阻塞调用是指调用结果返回之前,当前线程会被挂起(如遇到io操作)。函数只有在得到结果之后才会将阻塞的线程激活。
非阻塞: 非阻塞和阻塞的概念相对应,指在不能立刻得到结果之前也会立刻返回,同时该函数不会阻塞当前线程。
## 书写进程创建的两种方式
from multiprocessing import Process
import time
def task(name):
print('%s is running'%name)
time.sleep(3)
print('%s is over'%name)
if __name__ == '__main__':
# 1 创建一个对象
p = Process(target=task, args=('jason',))
# 容器类型哪怕里面只有1个元素 建议要用逗号隔开
# 2 开启进程
p.start() # 告诉操作系统帮你创建一个进程 异步
print('主')
from multiprocessing import Process
import time
class MyProcess(Process):
def run(self):
print('hello bf girl')
time.sleep(1)
print('get out!')
if __name__ == '__main__':
p = MyProcess()
p.start()
print('主')