-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |