目录
linux 进程间通讯
- shm
- 基于传统SYS V的共享内存
- 基于POSIX mmap文件映射实现共享内存
- 通过memfd_create()及fd跨进程实现共享内存
- 基于dma-buf实现共享内存
- pipe
- fifo
- msgq
- semaphore
- signal
- socket
- notify
- env
读写
1.函数api
ssize_t write(int fd, const void *buf, size_t count);
ssize_t read(int fd, void *buf, size_t count);
ssize_t send(int sockfd, const void *buf, size_t len, int flags);
ssize_t sendto(int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen);
ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags);
ssize_t recv(int sockfd, void *buf, size_t len, int flags);
ssize_t recvfrom(int sockfd, void *restrict buf, size_t len, int flags, struct sockaddr *restrict src_addr,
socklen_t *restrict addrlen);
ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags);
shm 共享内存
1.SYS V函数api
int shmget(key_t key, size_t size, int shmflg); //创建/获取shm_id
void *shmat(int shm_id, const void *shm_addr, int shmflg); //通过唯一的shm_id映射shm的首地址
int shmdt(const void *shmaddr); //取消shm的映射
int shmctl(int shm_id, int command, struct shmid_ds *buf); //对shm进行控制(删除...)
2.POSIX mmap函数api
int shm_open(const char *name, int oflag, mode_t mode); //以文件形式新建共享内存,返回fd
void *mmap(void *addr, size_t length, int prot, int flags,int fd, off_t offset); //将共享内存文件映射到进程中来,获取映射首地址
int msync ( void * addr, size_t len, int flags); //将进程内存中的数据同步到磁盘文件中
int munmap(void *addr, size_t length); //取消共享内存文件的进程映射
int shm_unlink(const char *name); //删除共享内存文件
int ftruncate(int fd, off_t length); //改变文件大小
int fstat(int fd, struct stat *buf); //获取文件属性
3.memfd_create()函数api
int memfd_create(const char *name, unsigned int flags);
void *mmap(void *addr, size_t length, int prot, int flags,int fd, off_t offset);
int msync ( void * addr, size_t len, int flags);
int munmap(void *addr, size_t length);
int shm_unlink(const char *name);
int ftruncate(int fd, off_t length);
4.dma-buf函数api
kernel层封装对应的dma_buf_fd
int ioctl(int fd, int cmd, ...);
void *mmap(void *addr, size_t length, int prot, int flags,int fd, off_t offset);
int munmap(void *addr, size_t length);
int shm_unlink(const char *name);
int ftruncate(int fd, off_t length);
pipe 匿名管道
1.函数api
FILE* popen(const char *command, const char *open_mode);
int pclose(FILE *stream_to_close);
int pipe(int file_descriptor[2]);
int dup(int file_descriptor);
int dup2(int file_descriptor_one, int file_descriptor_two);
int close(int fd);
fifo 命名管道
1.函数api
int mkfifo(const char *filename, mode_t mode);
int mknod(const char *filename, mode_t mode | S_IFIFO, (dev_t)0);
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
msgq 消息队列
1.函数api
int msgget(key_t, key, int msgflg);
int msgsend(int msgid, const void *msg_ptr, size_t msg_sz, int msgflg);
int msgrcv(int msgid, void *msg_ptr, size_t msg_st, long int msgtype, int msgflg);
int msgctl(int msgid, int command, struct msgid_ds *buf);
semaphore 信号量
1.SYS V函数api
int semget(key_t key, int num_sems, int sem_flags); //进程间通讯使用,返回sem_id
int semop(int sem_id, struct sembuf *sem_opa, size_t num_sem_ops); //进程间信号量控制
int semctl(int sem_id, int sem_num, int command, ...); //控制进程间的信号量
2.POSIX有名信号量函数api
sem_t *sem_open(const char *name,int oflag,mode_t mode,unsigned int value); //以文件的形式创建信号量并设置初值,方便进行进程间通讯
int sem_getvalue(sem_t *restrict sem, int *restrict sval); //获取信号量储存在sval,成功返回0或失败返回-1
int sem_wait(sem_t *sem); //信号量-1,信号量为0时,挂起阻塞等待
int sem_trywait(sem_t *sem); //信号量-1,信号量为0时,返回错误值,并继续执行
int sem_timedwait(sem_t *sem, const struct timespec *abs_timeout); //信号量-1,信号量为0时作超时阻塞等待
int sem_post(sem_t *sem); //信号量+1
int sem_unlink(const char *name); //信号量文件删除
int sem_close(sem_t *sem); //信号量文件关闭
3.POSIX无名信号量函数api
int sem_init (sem_t *__sem, int __pshared, unsigned int __value); //初始化信号量值,返回成功0或失败-1
int sem_wait(sem_t *sem); //信号量-1,信号量为0时,挂起阻塞等待
int sem_trywait(sem_t *sem); //信号量-1,信号量为0时,返回错误值,并继续执行
int sem_timedwait(sem_t *sem, const struct timespec *abs_timeout); //信号量-1,信号量为0时作超时阻塞等待
int sem_post(sem_t *sem); //信号量+1
int sem_destroy(sem_t *sem); //信号量销毁
signal 信号
1.函数api
void (*signal(int sig, void (*func)(int)))(int);
int sigaction(int sig, const struct sigaction *act, struct sigaction *oact);
int sigemptyset(sigset_t *set);
int sigfillset(sigset_t *set);
int sigaddset(sigset_t *set, int signo);
int sigdelset(sigset_t *set, int signo);
int sigismember(sigset_t *set, int signo);
int sigpromask(int how, const sigset_t *set, sigset_t *oset);
int sigpending(sigset_t *set);
int sigsuspend(const sigset_t *sigmask);
socket 嵌套字
1.函数api
/*数据流TCP*/
int socket(int domain, int type, int protocol);
int bind( int socket, const struct sockaddr *address, size_t address_len);
int listen(int socket, int backlog);
int accept(int socket, struct sockaddr *address, size_t *address_len);
int connect(int socket, const struct sockaddr *address, size_t address_len);
ssize_t write(int fd, const void *buf, size_t count);
ssize_t read(int fd, void *buf, size_t count);
int close(int fd);
/*数据报文UDP*/
int socket(int domain, int type, int protocol);
int bind( int socket, const struct sockaddr *address, size_t address_len);
int sendto(int sockfd, void *buffer, size_t len, int flags, struct sockaddr *to, socklen_t tolen);
int recvfrom(int sockfd, void *buffer, size_t len,int flags, struct sockaddr *src_from, socklen_t *src_len);
int close(int fd);
notify/inotify 文件扫描
1.函数api
int access(const char* pathname, int mode);
size_t strlen(const char *str);
char *strcpy(char *dest, const char *src);
char *strrchr(const char *str, int c);
int strncmp(const char *str1, const char *str2, size_t n);
int stat(const char *path, struct stat *buf);
int fstat(int fd, struct stat *buf);
int lstat(const char *path, struct stat *buf);
DIR *opendir(const char *name);
DIR *fdopendir(int fd);
struct dirent *readdir(DIR *dirp);
int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result);
int closedir(DIR *dirp);
char *strcat(char *dest, const char *src);
env 环境变量
1.函数api
char *getenv(const char *name); //获取环境变量
int putenv(char *string); //设置环境变量
int setenv(const char *name, const char *value, int overwrite); //设置环境变量
int unsetenv(const char *name); //删除环境变量
标签:ipc,const,int,void,char,sem,size
From: https://www.cnblogs.com/kuanghl/p/17306758.html