468,496 Members | 1,776 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,496 developers. It's quick & easy.

plz get me the code for shmget() in shared memory

14
HI
Can anyone please get me the code for shmget() function implementation in shared memory as soon as possible.........thank you
Oct 18 '06 #1
3 9294
tyreld
144 100+
Please be more specific. Are you wanting to see the actual library implementation of the "shmget" function, or are you wanting an example of how to write code that utilizes shared memory? Shared memory is not exactly a cake walk. In particular you usually need to utilze semaphores to synchronize access to the shared memory segment.
Oct 18 '06 #2
jmbn2k
14
Please be more specific. Are you wanting to see the actual library implementation of the "shmget" function, or are you wanting an example of how to write code that utilizes shared memory? Shared memory is not exactly a cake walk. In particular you usually need to utilze semaphores to synchronize access to the shared memory segment.

Can you please get me the library implementation and also an example of how to write code to utilize shared memory.......
Oct 18 '06 #3
tyreld
144 100+
If you want to see how shared memory is implemented then go look at the Linux kernel source code. That is an excercise I leave up to you. However, I will warn you that it is not pretty, and probably not going to make sense unless you have a high level understanding of C programming and the implementation of the Linux kernel.

As far as using shared memory goes here is an incredibly simplistic client/server example using shared memory. Mind you in production code you want to use semaphores or a record locking mechanism for syncronization and concurrency which raise the code complexity by several magnitudes.

Server.c
Expand|Select|Wrap|Line Numbers
  1. #include <sys/types.h>
  2. #include <sys/ipc.h>
  3. #include <sys/shm.h>
  4. #include <stdio.h>
  5.  
  6. #define SHMSZ     27
  7.  
  8. main()
  9. {
  10.     char c;
  11.     int shmid;
  12.     key_t key;
  13.     char *shm, *s;
  14.  
  15.     /*
  16.      * We'll name our shared memory segment
  17.      * "5678".
  18.      */
  19.     key = 5678;
  20.  
  21.     /*
  22.      * Create the segment.
  23.      */
  24.     if ((shmid = shmget(key, SHMSZ, IPC_CREAT | 0666)) < 0) {
  25.         perror("shmget");
  26.         exit(1);
  27.     }
  28.  
  29.     /*
  30.      * Now we attach the segment to our data space.
  31.      */
  32.     if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) {
  33.         perror("shmat");
  34.         exit(1);
  35.     }
  36.  
  37.     /*
  38.      * Now put some things into the memory for the
  39.      * other process to read.
  40.      */
  41.     s = shm;
  42.  
  43.     for (c = 'a'; c <= 'z'; c++)
  44.         *s++ = c;
  45.     *s = NULL;
  46.  
  47.     /*
  48.      * Finally, we wait until the other process 
  49.      * changes the first character of our memory
  50.      * to '*', indicating that it has read what 
  51.      * we put there.
  52.      */
  53.     while (*shm != '*')
  54.         sleep(1);
  55.  
  56.     exit(0);
  57. }
  58.  
Client.c
Expand|Select|Wrap|Line Numbers
  1. /*
  2.  * shm-client - client program to demonstrate shared memory.
  3.  */
  4. #include <sys/types.h>
  5. #include <sys/ipc.h>
  6. #include <sys/shm.h>
  7. #include <stdio.h>
  8.  
  9. #define SHMSZ     27
  10.  
  11. main()
  12. {
  13.     int shmid;
  14.     key_t key;
  15.     char *shm, *s;
  16.  
  17.     /*
  18.      * We need to get the segment named
  19.      * "5678", created by the server.
  20.      */
  21.     key = 5678;
  22.  
  23.     /*
  24.      * Locate the segment.
  25.      */
  26.     if ((shmid = shmget(key, SHMSZ, 0666)) < 0) {
  27.         perror("shmget");
  28.         exit(1);
  29.     }
  30.  
  31.     /*
  32.      * Now we attach the segment to our data space.
  33.      */
  34.     if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) {
  35.         perror("shmat");
  36.         exit(1);
  37.     }
  38.  
  39.     /*
  40.      * Now read what the server put in the memory.
  41.      */
  42.     for (s = shm; *s != NULL; s++)
  43.         putchar(*s);
  44.     putchar('\n');
  45.  
  46.     /*
  47.      * Finally, change the first character of the 
  48.      * segment to '*', indicating we have read 
  49.      * the segment.
  50.      */
  51.     *shm = '*';
  52.  
  53.     exit(0);
  54. }
  55.  
Oct 18 '06 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

11 posts views Thread by Michael Schuler | last post: by
5 posts views Thread by Jim | last post: by
1 post views Thread by Alexander Cohen | last post: by
2 posts views Thread by zeny | last post: by
1 post views Thread by zeny | last post: by
1 post views Thread by cavemandave | last post: by
1 post views Thread by Ron Eggler | last post: by
4 posts views Thread by hugo.arregui | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.