ECE650-midterm1 review
1 Concurrency 并发
1.1 Process
- an instance of a computer program being executed
- 一个process可以interact with其他process/recourses
- PC – program counter; SP – stack pointer
- process间不能通过直接读写common data通信,可以通过system-provided inter-process communication mechanisms
1.2 Thread
- 线程必须在某个进程中执行,process将一组资源分组在一起,包含program和data和其他resource
- 线程有自己的PC,有reg,有一个stack
- 进程用于group resources together
- 线程是entities实体 scheduled for execution on the CPU
- processs 是unit of allocation,thread是unit of execution/control within a process
- 操作系统创建并管理进程,每个进程包含PID,state,memory manegement info,file,I/O
- OS 给illusion of 隔绝的machine access,有的资源和活动可使用户transparently看到,称为virtualization
- OS给予多处理器错觉(virtual CPU),每个virtual CPU需要PC,SP,regs
- 进程由OS system call产生,fork()复制此process并run,child return 0,parent rerutn child‘s PID;exec() follow fork()运行不同程序
- 一个进程create & start 线程的运行,通过system call比如clone(),通过library比如pthread_create()
- concurrent programs:1CPU上多线程time-slice with 1 hardware thread;1CPU上多线程同时 with n hardware threads;m个CPU上多线程同时 with n hardware threads(多核)
- 多线程共享同信息,如上图,可通信
- serializable : A concurrent schedule of operations (in a general sense) is serializable if it produces a result (state) that can be produced with a fully sequential execution
- race condition: 并发process/thread的结果取决于precise timing of 一个线程相对于其他thread执行指令序列,有时对,有时错
- critical sections:作用于共享变量并且可以产生非序列化执行的计算部分称为临界区;临界区是每一组想要被atomically执行的操作,也就是说,它们好像是一个单独的操作,没有中断;一次只有一个进程/线程可以在critical section里
- point to point event: 解决shared memory的concurrent access:flag,monitor,semaphores信号量(二进制初始值1是lock/mutex)
- global event : BARRIER(name, num)等待直到num threads到达,性能差,尽量不用
- 解决并发问题需要硬件支持:most处理器提供atomic操作
- OR
- SUM
1.3 多线程programming
- 使用多线程可以让并行任务通信大量数据;独立进程实现并行只能通信很少,通过shared memory segment(system call)或特殊通信通道
- C语言:pthreads library; C++:std::thread或boost::thread
- 线程通常用于执行特定的函数
- pthread_create; pthread_join自己线程挂起,直到第一个参数指定的线程结束,立即return; pthread_exit线程函数结束前退出
- 互斥锁:pthread_mutex_init或pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_lock; pthread_mutex_trylock; pthread_mutex_unlock
- 读写锁:3-state lock – unlocked, read locked, write locked; pthread_rwlock_t x = PTHREAD_RWLOCK_INITIALIZER; pthread_rwlock_rdlock已经是read,read计数增加1,直接read; pthread_rwlock_wrlock如果已经是read/write,等变为unlock才write; pthread_rwlock_unlock; pthread_rwlock_destroy
- pthread_barrier_t barrier = PTHREAD_BARRIER_INITIALIZER(count) 或 pthread_barrier_init; pthread_barrier_wait线程被挂起,直到所有参与这个barrier的threads都call it
- 线程本地存储:好像每个线程都有一个变量,应用于global data,static data等,__thread
2 Inter-Process Communication(IPC)
2.1 Message Passing
- 同一进程的线程share memory,因为同地址空间
- shared memory:快,容易(不需要set up),但需要同步(解决race conditions)
- massage passing:安全(可以确认身份),共享数据显式可见(message),但需要明确的编程支持,performance overhead性能开销(例如通过操作系统空间复制交换的信息)
- message interchange:若无channel则建立,交换信息using data transfer primitives,关闭channel
- Direct naming/addressing:P1和P2通信必须显式name it:send(P2, msg); receive(P1, msg),即每对有一个link,但灵活性差
- indirect naming/addressing:messages向mailboxes(每个mailbox有一个unique ID)或ports发送或接收 – send(mailbox, msg); receive(mailbox, msg) – 更灵活,link可以被多个进程共享,每对进程可以有多个链接,每一个link一个mailbox
- synchronization:当send()和receive()都是blocking,进程实现同步 – rendez-vous
- Blocking send():发送方阻塞,直到消息被收到
- non-blocking send():发送方发送并立即恢复
- blocking receive():接收方阻塞,直到消息可用
- non-blocking receive(): receive接收到一个消息或null