ANP - 6. IPC-shared Memory
ANP - 6. IPC-shared Memory
Shared Memory
• Shared memory is the fastest form of IPC
Inter-process Communication available.
– For example, in usage of message queue. A
(IPC):POSIX Shared Memory message gets copied from sender’s user-space to
the message queue, and from message queue to
Harshad B. Prajapati user’s space for receiver. (Copying from input and
Associate Professor sending output to output device adds two more
Information Technology Department, copying operations)
Dharmsinh Desai University, Nadiad
• After memory is mapped into the user’s
address space, there is no kernel involvement.
1
7/21/2017
2
7/21/2017
ShmCounterNSemCreator.c ShmCounterNSemCreator.c
#include <stdio.h> int main(int argc,char** argv){
#include <unistd.h> int fd,i,nloop;
#include <sys/types.h> struct shared *ptr;
#include <sys/stat.h> sem_t* lock;
#include <fcntl.h> if(argc!=4){
#include <sys/mman.h> fprintf(stderr,"Usage: %s pathname #Count semaphore-
#include <semaphore.h> name\n",argv[0]);
#define FILE_MODE S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH exit(-1);
struct shared{ }
int counter; nloop=atoi(argv[2]);
}shared;
3
7/21/2017
ShmCounterNSemCreator.c ShmCounterNSemCreator.c
/*open file, initialize to 0, map into memory*/ for(i=0;i<nloop;i++){
fd=open(argv[1],O_RDWR|O_CREAT,FILE_MODE); sem_wait(lock);
write(fd,&shared,sizeof(struct shared)); printf("Process(%d): counter = %d\n",getpid(),ptr-
ptr=mmap(NULL,sizeof(struct shared), >counter++);
PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); sem_post(lock);
close(fd); sleep(1);
/*Create a named semaphore and initialize it*/ }
lock=sem_open(argv[3], O_CREAT,FILE_MODE,1); exit(0);
setbuf(stdout,NULL); }
ShmCounterNSemUser.c ShmCounterNSemUser.c
#include <stdio.h> int main(int argc,char** argv){
#include <unistd.h> int fd,i,nloop;
#include <sys/types.h> struct shared *ptr;
#include <sys/stat.h> sem_t* lock;
#include <fcntl.h> if(argc!=4){
#include <sys/mman.h> fprintf(stderr,"Usage: %s pathname #Count semaphore-
#include <semaphore.h> name\n",argv[0]);
#define FILE_MODE S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH exit(-1);
struct shared{ }
int counter; nloop=atoi(argv[2]);
}shared;
ShmCounterNSemUser.c ShmCounterNSemUser.c
/*open file, map into memory*/ for(i=0;i<nloop;i++){
fd=open(argv[1],O_RDWR,FILE_MODE); sem_wait(lock);
ptr=mmap(NULL,sizeof(struct printf("Process(%d): counter = %d\n",getpid(),ptr-
shared),PROT_READ|PROT_WRITE,MAP_SHARED,fd,0); >counter++);
close(fd); sem_post(lock);
/*Create a named semaphore and initialize it, in this case it sleep(1);
already exists*/ }
lock=sem_open(argv[3],O_CREAT); exit(0);
setbuf(stdout,NULL); }
4
7/21/2017
Functions Functions
• Create a new or open an existing shared memory object: • Removes a name of shared object.
#include <sys/mman.h> #include <sys/mman.h>
int shm_open(const char* name, int oflag, mode_t mode); int shm_unlink(const char* name);
– oflag: either O_RDONLY or O_RDWR. – It removes the name of shared memory object.
Other flags (optional) can also be specified – Unlinking has no effect on existing references to the underlying object,
O_CREAT or O_EXCL until all references to that object are closed. (It maintains reference
If O_TRUNC is specified along with O_RDWR flag, then if the shared count, on each close call, reference count is decremented by one.)
memory object already exists, it is truncated to 0-length.
– mode: It specifies permission bits. It is used when O_CREAT flag is
specified. It is compulsory, unlike mq_open and sem_open functions.
– The return value from shm_open is passed as the fifth argument to
the mmap.
Functions Functions
• Change size of regular file or a shared memory • Obtain information about existing shared memory object
#include <sys/types.h>
object.
#include <sys/stat.h>
#include <unistd.h> int fstat(int fd, struct stat* buf);
int ftruncate(int fd, off_t length); Following members provide information related to shared memory object.
Other members are not listed.
– For a regular file: struct stat{
• Size of file was larger than length, the extra data is discarded.
…
• Size of file was smaller than length, “what happens to file” is unspecified.
mode_t st_mode; /*mode:S_I{R,W}{USR,GRP,OTH}*/
Portable way to extend file size to length bytes is to lseek to offset
uid_t st_uid; /* user ID of owner*/
length-1 and write 1 byte of data.
gid_t st_gid; /* group ID of owner*/
– For a shared memory object: ftruncate sets the size of the off_t st_size; /* size in bytes */
object to length. …
};
5
7/21/2017
ShmCounterNSemCreator.c ShmCounterNSemCreator.c
int main(int argc,char** argv){ /* open shared memory object, change it size, map into
int fd,i,nloop; memory*/
struct shared *ptr; fd=shm_open(argv[1],O_RDWR|O_CREAT,FILE_MODE);
sem_t* lock; ftruncate(fd,sizeof(struct shared));
if(argc!=4){ ptr=mmap(NULL,sizeof(struct
fprintf(stderr,"Usage: %s shm-object-name #Count shared),PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
semaphore-name\n",argv[0]);
fprintf(stderr,"Ex: %s /counter 10 /lock\n",argv[0]);
exit(-1);
}
nloop=atoi(argv[2]);
ShmCounterNSemCreator.c ShmCounterNSemUser.c
/*Create a named semaphre and initialize it*/ #include <stdio.h>
lock=sem_open(argv[3], O_CREAT,FILE_MODE,1); #include <unistd.h>
setbuf(stdout,NULL); #include <sys/types.h>
for(i=0;i<nloop;i++){ #include <sys/stat.h>
sem_wait(lock); #include <fcntl.h>
printf("Process(%d): counter = %d\n",getpid(),ptr- #include <sys/mman.h>
>counter++); #include <semaphore.h>
sem_post(lock); #define FILE_MODE S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH
sleep(1); struct shared{
} int counter;
exit(0); }shared;
}
6
7/21/2017
ShmCounterNSemUser.c ShmCounterNSemUser.c
int main(int argc,char** argv){ nloop=atoi(argv[2]);
int fd,i,nloop; /*open file, map into memory*/
struct shared *ptr; fd=shm_open(argv[1],O_RDWR,FILE_MODE);
sem_t* lock; ptr=mmap(NULL,sizeof(struct
if(argc!=4){ shared),PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
fprintf(stderr,"Usage: %s s shm-object-name #Count
semaphore-name\n",argv[0]);
fprintf(stderr,"Ex: %s /counter 5 /lock\n",argv[0]);
exit(-1);
}