Skip to content

Commit

Permalink
共享内存写端
Browse files Browse the repository at this point in the history
  • Loading branch information
Hedenghui committed Aug 9, 2020
1 parent 7863127 commit 3cf3dd3
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions ipc/shm_write.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<sys/shm.h>
#define IPC_KEY 0x12345678
int main()
{
//1.创建共享内存
int shm_id=shmget(IPC_KEY,32,IPC_CREAT|0664);
if(shm_id<0)
{
perror("shmget error");
return -1;
}
//2.建立映射关系
void*shm_start=shmat(shm_id,NULL,0);
if(shm_start==(void*)-1)
{
perror("shmat error");
return -1;
}
//3.进行内存操作
int i=0;
while(1)
{
sprintf(shm_start,"%s+%d","又下雨了~",i++);
sleep(1);
}
//4.解除映射关系
shmdt(shm_start);
//5.删除共享内存
shmctl(shm_id,IPC_RMID,NULL);
return 0;
}

0 comments on commit 3cf3dd3

Please sign in to comment.