linux 用共享内存实现剪贴板原理
来源:互联网 发布:玻璃深加工erp软件 编辑:程序博客网 时间:2024/06/08 03:00
创建共享内存,但是在读共享内存之前,该共享内存不会被删除
#include <stdio.h>
#include <sys/types.h>#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <signal.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
int shmid;
void sig_handler(int sig)
{
int res = shmctl(shmid,IPC_RMID,0);
if(res == -1) perror("shmctl"),exit(-1);
printf("共享内存关闭 FROM ysd|\n");
exit(0);
}
int main(int argc,char* argv[])
{
if(signal(SIGINT,sig_handler) == SIG_ERR)
{
perror("signal"),exit(-1);
}
key_t key = ftok(".",200);
if(shmid == -1)
perror("ftok"),exit(-1);
shmid = shmget(key,4096,IPC_CREAT|IPC_EXCL|0600);
if(-1 == shmid)
perror("shmget"),exit(-1);
void* p = shmat(shmid,0,0);
if(p == (void*)-1)
perror("shmat"),exit(-1);
memset(p,0,4096);
strcpy(p,"hello,world");
int res = shmdt(p);
if(res == -1)
perror("shmdt"),exit(-1);
while(1);
return 0;
}
读共享内存.
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <signal.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
int main(int argc,char* argv[])
{
key_t key = ftok(".",200);
if(key == -1) perror("key"),exit(-1);
int shmid = shmget(key,0,0);
void* p = shmat(shmid,0,0);
printf("%s\n",(char*)p);
shmdt(p);
return 0;
}
0 0
- linux 用共享内存实现剪贴板原理
- 共享内存实现原理
- 共享内存实现原理
- linux rdp 共享剪贴板
- linux共享内存原理剖析
- 共享内存的使用实现原理
- 转载——Linux共享内存实例及文件映射编程及实现原理
- 共享内存的实现---Linux内核笔记
- 分析linux共享内存的实现
- linux下共享内存的实现
- linux SysV IPC shm共享内存实现
- Linux对共享内存的实现
- 分析linux共享内存的实现
- Linux进程通信---共享内存 代码实现
- Linux共享内存的内核实现
- linux中使用共享内存实现通信
- 【Linux】共享内存--实现进程间通信
- Linux共享内存实现进程间通信
- Opent错误总结
- php curl连接未释放,strace结果:poll([{fd=5, events=POLLIN|POLLPRI|POLLRDNORM|POLLRDBAND}], 1, 1000) = 0 (Tim
- 简单理解:ML、DB、NLP
- vector erase
- 总结
- linux 用共享内存实现剪贴板原理
- 关于compareTo方法小问题
- Redis运行流程源码解析
- XCode 中ARC和MRC设置
- hdu 3483 A Very Simple Problem 矩阵快速幂+二项式展开
- c++11 成员变量初始化
- 20161031
- Android中的DrawRect()理解
- 无线网络自动获取IP批处理脚本