Hi all,
This is officially the first blog that i went forward to write. I am working as software programmer.So most of the time you will find blogs while me writing something about it or asking for help.
Today i will write about shared memory. It comes under the section of IPC(ie Inter process communication).There are several ways that to process can communicate. Some ways are via TCP,UDP,semaphores.
We have tried communication via TCP and unix domain sockets and other IPC mechanisms as part of performance improvement but though it was fast, the problem was it produces multiple copies of the same message. Hence we are going forward with using shared memory as it keeps a single copy of the message in the memory which is accessible to both or more(doubt here) communicating parties.To avoid redundancy was the main goal for us to look for the other means of IPC.
Shared memody is memory that may be simultaneously accessed by multiple processes with goal provide communication among them or avoid redundant copies. Shared memory is an efficient means of passing data between programs.
We can have same process accessing the same memory or buffer you can say, with the help of threads.
One thread reading (get) from the memory and other thread writing (put) in memory.
The trick here is you have to use some sort of mechanism to avoid multiple access to same data as it would result in incompleted data. For Eg. if the thread writing data has to put 1 packet (say 500 bytes) of data in
memory it is in middle of writing data ie written only say 250 bytes and during this the other thread tries to read data as both have them have access to that buffer at that time it will result in incomplete data read from the reader thread.
So we need a way of making sure that if one process is using a shared memory, the other processes will be excluded from accessing the same memory and when that process has finished its work with the memory the other process previously denied access may be granted access to same.In this way the cycle will go on.
This is known as Mutual exclusion.
In the above scenario if the process who is granted permission over the shared segment does not release its lock over the memory this would result in chaos known as Deadlock, as the other process will not ever be able to access the shared segment resulting in no work done and waste of resources and time.
So We have to be very careful while implementing locks and mutual exclusion.
Here is one server and client code..
This is officially the first blog that i went forward to write. I am working as software programmer.So most of the time you will find blogs while me writing something about it or asking for help.
Today i will write about shared memory. It comes under the section of IPC(ie Inter process communication).There are several ways that to process can communicate. Some ways are via TCP,UDP,semaphores.
We have tried communication via TCP and unix domain sockets and other IPC mechanisms as part of performance improvement but though it was fast, the problem was it produces multiple copies of the same message. Hence we are going forward with using shared memory as it keeps a single copy of the message in the memory which is accessible to both or more(doubt here) communicating parties.To avoid redundancy was the main goal for us to look for the other means of IPC.
Shared memody is memory that may be simultaneously accessed by multiple processes with goal provide communication among them or avoid redundant copies. Shared memory is an efficient means of passing data between programs.
We can have same process accessing the same memory or buffer you can say, with the help of threads.
One thread reading (get) from the memory and other thread writing (put) in memory.
The trick here is you have to use some sort of mechanism to avoid multiple access to same data as it would result in incompleted data. For Eg. if the thread writing data has to put 1 packet (say 500 bytes) of data in
memory it is in middle of writing data ie written only say 250 bytes and during this the other thread tries to read data as both have them have access to that buffer at that time it will result in incomplete data read from the reader thread.
So we need a way of making sure that if one process is using a shared memory, the other processes will be excluded from accessing the same memory and when that process has finished its work with the memory the other process previously denied access may be granted access to same.In this way the cycle will go on.
This is known as Mutual exclusion.
In the above scenario if the process who is granted permission over the shared segment does not release its lock over the memory this would result in chaos known as Deadlock, as the other process will not ever be able to access the shared segment resulting in no work done and waste of resources and time.
So We have to be very careful while implementing locks and mutual exclusion.
Here is one server and client code..
server.c
#include#include #include #include #define SHMSZ 27 main() { char c; int shmid; key_t key; char *shm, *s; /* * We'll name our shared memory segment * "5678". */ key = 5678; /* * Create the segment. */ if ((shmid = shmget(key, SHMSZ, IPC_CREAT | 0666)) < 0) { perror("shmget"); exit(1); } /* * Now we attach the segment to our data space. */ if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) { perror("shmat"); exit(1); } /* * Now put some things into the memory for the * other process to read. */ s = shm; for (c = 'a'; c <= 'z'; c++) *s++ = c; *s = NULL; /* * Finally, we wait until the other process * changes the first character of our memory * to '*', indicating that it has read what * we put there. */ while (*shm != '*') sleep(1); exit(0); }
client.c
/* * shm-client - client program to demonstrate shared memory. */ #include#include #include #include #define SHMSZ 27 main() { int shmid; key_t key; char *shm, *s; /* * We need to get the segment named * "5678", created by the server. */ key = 5678; /* * Locate the segment. */ if ((shmid = shmget(key, SHMSZ, 0666)) < 0) { perror("shmget"); exit(1); } /* * Now we attach the segment to our data space. */ if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) { perror("shmat"); exit(1); } /* * Now read what the server put in the memory. */ for (s = shm; *s != NULL; s++) putchar(*s); putchar('\n'); /* * Finally, change the first character of the * segment to '*', indicating we have read * the segment. */ *shm = '*'; exit(0); }
No comments:
Post a Comment