并发编程(练习题)

1、简述计算机操作系统中的“中断”的作用?

中断是指在计算机执行期间,系统内发生任何非寻常的或非预期的急需处理事件,使得CPU暂时中断当前正在执行的程序而转去执行相应的事件处理程序。待处理完毕后又返回原来被中断处继续执行或调度新的进程执行的过程。
它是计算机可以更好更快利用有限的系统资源解决系统响应速度和运行效率的一种控制技术。
实时响应,系统调度

2、简述计算机内存中的“内核态”和“用户态”;

操作系统位于计算机硬件与应用软件之间,本质也是一个软件。操作系统由操作系统的内核(运行于内核态,管理硬件资源)以及系统调用(运行于用户态,为应用程序员写的应用程序提供系统调用接口)两部分组成

3、进程间通信方式有哪些?

1.管道:速度慢,容量有限,只有父子进程能通讯    

2.FIFO:任何进程间都能通讯,但速度慢    

3.消息队列:容量受到系统限制,且要注意第一次读的时候,要考虑上一次没有读完数据的问题    

4.信号量:不能传递复杂消息,只能用来同步    

5.共享内存区:能够很容易控制容量,速度快,但要保持同步,比如一个进程在写的时候,另一个进程要注意读写的问题,相当于线程中的线程安全,当然,共享内存区同样可以用作线程间通讯,不过没这个必要,线程间本来就已经共享了同一进程内的一块内存

4、简述你对管道、队列的理解;

消息队列,是消息的链接表,存放在内核中。一个消息队列由一个标识符(即队列ID)来标识。

1、特点

  1. 消息队列是面向记录的,其中的消息具有特定的格式以及特定的优先级。

  2. 消息队列独立于发送与接收进程。进程终止时,消息队列及其内容并不会被删除。

  3. 消息队列可以实现消息的随机查询,消息不一定要以先进先出的次序读取,也可以按消息的类型读取。

管道,通常指无名管道,是 UNIX 系统IPC最古老的形式。

1、特点:

  1. 它是半双工的(即数据只能在一个方向上流动),具有固定的读端和写端。

  2. 它只能用于具有亲缘关系的进程之间的通信(也是父子进程或者兄弟进程之间)。

  3. 它可以看成是一种特殊的文件,对于它的读写也可以使用普通的read、write 等函数。但是它不是普通的文件,并不属于其他任何文件系统,并且只存在于内存中

5、请列举你知道的进程间通信方式;

6、什么是同步I/O,什么是异步I/O?

  •    同步I/O操作(synchronous I/O operation):导致请求进程阻塞,直到I/O操作完成。
  •    异步I/O操作(asynchronous I/O operation): 不导致请求进程阻塞。

7、请问multiprocessing模块中的Value、Array类的作用是什么?举例说明它们的使用场景

通常,进程之间彼此是完全孤立的,唯一的通信方式是队列或管道。但可以使用两个对象来表示共享数据。
其实,这些对象使用了共享内存(通过mmap模块)使访问多个进程成为可能。
python 多进程通信Queue Pipe Value Array
queue和pipe用来在进程间传递消息;

Value + Array 是python中共享内存 映射文件的方法,速度比较快。

8、请问multiprocessing模块中的Manager类的作用是什么?与Value和Array类相比,Manager的优缺点是什么?

Python multiprocessing.Manager(进程间共享数据):Python中进程间共享数据,除了基本的queue,pipe和value+array外,
还提供了更高层次的封装,使用multiprocessing.Manager可以简单地使用这些高级接口。
Manager支持的类型有list,dict,Namespace,Lock,RLock,Semaphore,BoundedSemaphore,Condition,Event,Queue,Value和Array。

9、写一个程序,包含十个线程,子线程必须等待主线程sleep 10秒钟之后才执行,并打印当前时间;

from  threading import  Thread,currentThread,Event

def task():
    event.wait(10)
    print('%s is  running time is  '%currentThread().getName(),time.ctime())

if __name__ == '__main__':
    event = Event()

    for i in range(10):
        p = Thread(target=task,)
        p.start()
    print('')
    time.sleep(10)
    event.set()

#输出
主
Thread-1 is  running time is   Tue May 29 11:05:11 2018
Thread-4 is  running time is   Tue May 29 11:05:11 2018
Thread-3 is  running time is   Tue May 29 11:05:11 2018
Thread-7 is  running time is   Tue May 29 11:05:11 2018
Thread-5 is  running time is   Tue May 29 11:05:11 2018
Thread-10 is  running time is   Tue May 29 11:05:11 2018
Thread-6 is  running time is   Tue May 29 11:05:11 2018
Thread-8 is  running time is   Tue May 29 11:05:11 2018
Thread-2 is  running time is   Tue May 29 11:05:11 2018

10、写一个程序,包含十个线程,同时只能有五个子线程并行执行;

import time, os
from concurrent.futures import ThreadPoolExecutor,ProcessPoolExecutor
from  threading import  currentThread

def task():
    print('%s is  running time is  ' % currentThread().getName(), time.ctime())


if __name__ == '__main__':
    executor = ThreadPoolExecutor(max_workers=5)
    for i in range(10):
        p =  executor.submit(task)
    print('')


#输出

<concurrent.futures.thread.ThreadPoolExecutor object at 0x00000193975E7128>_0 is  running time is   Tue May 29 11:46:24 2018
<concurrent.futures.thread.ThreadPoolExecutor object at 0x00000193975E7128>_0 is  running time is   Tue May 29 11:46:24 2018
<concurrent.futures.thread.ThreadPoolExecutor object at 0x00000193975E7128>_1 is  running time is   Tue May 29 11:46:24 2018
<concurrent.futures.thread.ThreadPoolExecutor object at 0x00000193975E7128>_0 is  running time is   Tue May 29 11:46:24 2018
<concurrent.futures.thread.ThreadPoolExecutor object at 0x00000193975E7128>_1 is  running time is   Tue May 29 11:46:24 2018<concurrent.futures.thread.ThreadPoolExecutor object at 0x00000193975E7128>_2 is  running time is   Tue May 29 11:46:24 2018
<concurrent.futures.thread.ThreadPoolExecutor object at 0x00000193975E7128>_3 is  running time is   Tue May 29 11:46:24 2018
<concurrent.futures.thread.ThreadPoolExecutor object at 0x00000193975E7128>_3 is  running time is   Tue May 29 11:46:24 2018
<concurrent.futures.thread.ThreadPoolExecutor object at 0x00000193975E7128>_3 is  running time is   Tue May 29 11:46:24 2018
<concurrent.futures.thread.ThreadPoolExecutor object at 0x00000193975E7128>_3 is  running time is   Tue May 29 11:46:24 2018

11、写一个程序,要求用户输入用户名和密码,要求密码长度不少于6个字符,且必须以字母开头,如果密码合法,则将该密码使用md5算法加密后的十六进制概要值存入名为password.txt的文件,超过三次不合法则退出程序;

import  hashlib
import re
class Login():

    def user(self):
        count = 0
        while count < 3:
            password = input('password>>>').strip()
            index = password.split()[0]
            if not password:continue
            if len(password)>5 :
                if re.match('[a-zA-Z]',index):
                    self.md(password)
                else:
                    print('error1')
                    count +=1
                    continue
            else:
                print('error2')
                count +=1
                continue
    def md(self,password):
        h1 = hashlib.md5()
        h1.update(str.encode('utf-8'))
        print(h1.hexdigest()[8:-8])
        with open('password.txt','w',encoding='utf-8') as  f:
            f.write(h1.hexdigest()[8:-8])


l = Login()
l.user()


#输出
password>>>4fdgdghfgd
error1
password>>>

猜你喜欢

转载自www.cnblogs.com/mjiu/p/9104798.html