Skip to main content
  1. cpp/

6. 并行&并发编程

2 mins
Table of Contents

CPU & 编程模型
#

并发
#
并行
#

资源管理/共享
#


进程
#


线程
#


协程
#


阻塞非阻塞
#


同步异步
#


SYSTEM V vs POSIX
#

SYSTEM VPOSIX
背景AT&T 于 1983提出三种形式IPC,消息队列message queues、共享内存shared memory和信号量semaphoresIEEE提出可移植操作系统接口Portable Operating System Interface standards,同样提供三种API
IPC方法pipes, named pipes, message queues, signals, semaphores, and shared memory;socket and Unix Domain socketspipes, named pipes, message queues, signals, semaphores, and shared memory;socket and Unix Domain sockets
共享内存shmget(), shmat(), shmdt(), shmctl()
shmget 时固定共享内存大小
使用 shmctl(), ipcs, ipcrm 控制操作或查看状态
shm_open(), mmap(), shm_unlink()
使用 ftruncate() 调整shm大小
使用 munmap() 和 mmap() (或 mremap()) 重建shm
可使用 fstat(), fchmod() 操作共享内存对象
消息队列msgget(), msgsnd(), msgrcv(), msgctl()mq_open(), mq_send(), mq_receive(), mq_unlink()
select(), poll() and epoll 能监听消息队列
信号量semget(), semop(), semctl()Named Semaphores:sem_open(), sem_close(), sem_unlink(), sem_post(), sem_wait(), sem_trywait(), sem_timedwait(), sem_getvalue()
Unnamed semaphores(Memory based):sem_init(), sem_post(), sem_wait(), sem_getvalue(),sem_destroy()
信号量差异信号量测试基于内核的,使用System V信号量会进入内核态信号量基于内存,即信号量值是放在共享内存中的,它使与文件系统中的路径名对应的名字来标识
识别IPC对象使用 keys 和 identifiers使用 名称 names 和 文件描述符 file descriptors
其他-mutex locks, conditional variables, read-write locks等线程安全