标签:
#include<stdio.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#define BUF_SIZE 1024
int main()
{
int id = shmget(IPC_PRIVATE,BUF_SIZE,0666);
if(id==-1)
{
perror("create shared memory fail");
}
system("ipcs -m");
return 0;
}
#include<stdio.h>
#include<sys/types.h>
#include<sys/shm.h>
#include<stdlib.h>
int main(int args,char* argc[])
{
if(args<2)
{
puts("input the shmid!");
exit(-1);
}
int shmid = atoi(argc[1]);
char* addr = shmat(shmid,0,0);
if(addr!=(void*)-1)
{
perror("shmat error");
}
system("ipcs");
while(1);
return 0;
}
#include<stdio.h>
#include<sys/types.h>
#include<sys/shm.h>
#include<stdlib.h>
int main(int args,char* argc[])
{
if(args<3)
{
puts("error!");
exit(-1);
}
int shmid = atoi(argc[1]);
int oper = atoi(argc[2]);
char* addr = shmat(shmid,0,0);
if(addr==(void*)-1)
{
perror("shmat error");
exit(-1);
}
if(oper==1) // write shared memory
{
scanf("%s",addr);
}
else if(oper==2) // read shared memory
{
printf("%s\n",addr);
}
return 0;
}
标签:
原文地址:http://www.cnblogs.com/ZhangJinkun/p/4564692.html