mmapread.c
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
#include <semaphore.h>
#include <string.h>
#include <errno.h>
int main(int argc,char **argv)
{
int shm_id;
char *ptr;
sem_t *sem;
if (argc!=2)
{
printf("usage:shm_open<pathname>\n");
return -1;
}
shm_id=shm_open(argv[1],O_RDWR|O_CREAT,0644);/*第一步:创建共享内存区*/
if (shm_id==-1)
{
printf( "open shared memory error.errno=%d,desc=%s.\n", errno, strerror(errno));
return -1;
}
ftruncate(shm_id,100);/*第二步:调整共享内存区大小,shmid问shm_open的返回值*/
sem=sem_open(argv[1],O_CREAT,0644,0);/*创建信号量*/
if (sem==SEM_FAILED)
{
printf( "open semaphore error.errno=%d,desc=%s.\n", errno, strerror(errno));
return -1;
}
ptr=mmap(NULL,