Shared Memory
allows 2 or more processes to share a memory region or segment of memory for reading and writing purposes
the problem with pipes, fifo and message queue is that mode switches are involved as the data has to apss from one process buffer to the kernel buffer and then to another process buffer
since access to user-space memory does not require a mode switch , thereofre , shared memory is considered as one of the quickest means of IPC
shmget system call
creates a new shared memory segment or obtains the identifier of an existing segment. the contents of a enwly created shared memory segment are initialized to 0. return value is the id of the shared memory segment.
shmat system call
attaches the shared memory segment identified by shmid to the address space of the calling process
shmdt system call
when a process no longer need to access a shared memory segment , it can call shmdt() to detach the segment from its address space
a child creted by fork() inherits its parent's attached shared memory segments, however , after an exec() , all attached shared memory segments are detached
writer
#include <stdio.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#include <sys/stat.h>
int main(){
// ftok to generate unique key
key_t key = ftok("f1.txt", 65);
// shmget returns an identifier in shmid
int shmid = shmget(key, 1024, 0666|IPC_CREAT);
// shmat to attach to shared memory
char *buffer = (char*)shmat(shmid, NULL, 0);
printf("Please enter a string to be written in shared memory:\n");
fgets(buffer, 512, stdin);
printf("\nData has been written in shared memory. Bye\n");
//detach from shared memory
shmdt(buffer);
return 0;
}
reader
#include <stdio.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#include <sys/stat.h>
int main(){
// ftok to generate unique key
key_t key = ftok("f1.txt", 65);
// shmget returns an identifier of existing shared memory
int shmid = shmget(key, 1024, 0666|IPC_CREAT);
// shmat to attach to shared memory
char *buffer = (char*)shmat(shmid, NULL, 0);
printf("Data read from memory: %s\n", buffer);
//detach from shared memory
shmdt(buffer);
// destroy the shared memory
// shmctl(shmid, IPC_RMID, NULL);
return 0;
}
Memory Mapped Files
is mostly a segment of virtual memory that has been assigned a direct byte-for-byte correlation with some portion of a file on disk.
Memory mapped I/O let us map a file on disk into a buffer in process address space so that when we fetch bytes from the buffer, the corresponding bytes of the file are read. similarly when we store data in the buffer, the corresponding bytes are automatically written to the file. this lets us perform I/O without using read or write system calls.
Persisted / File Mapping
Non-Persisted ? Anonymous Mapping
Shared File Mapping
Mutiple process mapping the same region of file share the same physical pages of memory. whenever a process tries to write, the modifictions to the contents of the mapping are carried through to the file.
uses of shared file mapping
- Memory mapped I/O
- IPC in a manner similar to System V shared memory segments between related or unrelated processes.
Private File Mapping
Modification are not visible to other processes. Mutiple porcesses mapping the same file initially share the same physical pages of memory. whenever a process tries to write , copy on write technique is employed, so that changes to the mapping by one process are invisible to other processes
mmap system call
used to request the creation of memory mappings in the address space of the calling process. On success it returns the starting address of the mapping
msync system call
causes the changes in part of all of the memory segment to be written back to (or read from) the mapped file.
munmap system call
simply unmaps the memory mapped region pointed to by addr with length len (addr, len are arguements of this system call)
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <string.h>
int main(int argc, char* argv[]){
//open a file
int fd = open("f1.txt", O_RDONLY);
//get attributes of the file
struct stat sbuff;
fstat(fd, &sbuff);
int fsize = sbuff.st_size;
//memory map the file
char* filedata = mmap(NULL, fsize, PROT_READ, MAP_SHARED, fd, 0);
//create a child process
pid_t cpid = fork();
if(cpid==0){
printf("Child access memory mapped file:\n%s", filedata);
munmap(filedata, fsize);
close(fd);
exit(0);
}
else{
waitpid(cpid, NULL,0);
printf("Parent access memory mapped file:\n%s", filedata);
munmap(filedata, fsize);
close(fd);
exit(0);
}
return 0;
}
'Operating System > System Programming(Arif Butt)' 카테고리의 다른 글
Lec33) Overview of TCP/IP Architecture and Service (0) | 2021.10.23 |
---|---|
Lec31) Synchronization among Threads (0) | 2021.07.25 |
Lec28) Message Queues (0) | 2021.07.21 |
Lec26,27) Programming UNIX Pipes and Named Pipes (0) | 2021.07.20 |
Lec25) Design and Code Of Signal Handlers (0) | 2021.07.17 |