473,326 Members | 2,173 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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 9681
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

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

Similar topics

11
by: Michael Schuler | last post by:
The use of STL in shared memory poses a real problem since (non-smart) pointers are not allowed there. Is there any solution for containers in shared memory using smart pointers? Where can I...
5
by: Jim | last post by:
Hello, I have a broken server that we are going to be moving off to a new server with a new version of DB2 but here is what I have right now: RedHat 7.0 (2.2.24smp) DB2 v6.1.0.40 I am...
1
by: Alexander Cohen | last post by:
Hi, sometimes ill get this error: FATAL: could not create shared memory segment: Cannot allocate memory DETAIL: Failed system call was shmget(key=2, size=4153344, 03600). HINT: This error...
2
by: zeny | last post by:
Hi everybody! In the file "server.c" i created a shared memory segment of the size of a 1024 byte string and then i attached a string to the segment, just like this: int key = 12345; char...
1
by: zeny | last post by:
Hey everyone! I´ve been tying to create a shared memory segment with the size of a structure, as follows: typedef struct{ int id; char message; }data; In the line where i create the shared...
1
by: cavemandave | last post by:
hi people i have been teaching myself C for the last few months and have recently moved onto ICP shared memory. I have with aid developed some simple code that prints out some chars i then wanted to...
1
by: Ron Eggler | last post by:
Hi, I'm running an application on an embedded PC 104 platform on a Linux OS. I need some shared memory and I'm trying to create it with: int prg_shm = ::shmget(0x1999,sizeof(struct...
4
by: hugo.arregui | last post by:
Hi! I have two struts like that: struct { int num; int num2; struct b arrayOfB; } a;
0
by: Tosh2pointO | last post by:
Hello, I'm trying to make a program that will multiply 2 matrices using shmget() and fork(). For example, I would need to multiply a 64 x 64 matrix using 4 processes or 16 processes, and the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.