Forks and Shared Memory in C (Linux OS)

Reply

Join Date: Feb 2008
Posts: 4
Reputation: cwarren13 is an unknown quantity at this point 
Solved Threads: 0
cwarren13 cwarren13 is offline Offline
Newbie Poster

Forks and Shared Memory in C (Linux OS)

 
0
  #1
20 Days Ago
So, I have a server process that forks for every client that connects. I keep a list of connected clients that I want each fork process to have. Of course, when the process forks the child only has what it had when the fork occurred. For the solution to this, I decided to use shared memory. For simplicities sake (and because this is my main issue I'm dealing with right now) I'll illustrate how I've attempted to pass an integer from the parent to child processes.

Parent process:

  1. key_t key_numentries;
  2. int shmid_numentries;
  3. char *shm_numentries, *shm1;
  4.  
  5. char buf[];
  6.  
  7. //Key for numentries is 2345
  8.  
  9. key_numentries = 2345;
  10.  
  11. //Create the shared memory segment
  12.  
  13. if((shmid_numentries = shmget(key_numentries, 8, IPC_CREAT | 0666)) < 0) {
  14. perror("shmget");
  15. exit(1);
  16. }
  17.  
  18. //Attach shared memory to dataspace
  19.  
  20. if((shm1 = shmat(shmid_numentries, NULL, 0)) == (char *) -1) {
  21. perror("shmat");
  22. exit(1);
  23. }
  24.  
  25. //did this because I wasn't sure how to send an int over the shm
  26. //numentries does contain the right value, I've tested it.
  27.  
  28. sprintf (buf,"%d",numentries);
  29.  
  30. //not sure if this would work, but a printf(%s) with &shm1[0] printed
  31. //the correct value
  32. shm1 = buf;
  33.  
  34. //pretty sure the code below fork is irrelevant
  35.  
  36. if(!fork()){
  37. close(socket_in);
  38. recievecmds(new_fd);
  39. close(new_fd);
  40. exit(0);}

Child process:

  1. int shmid_numentries;
  2. key_t key_numentries;
  3. char *shm_numentries, *shm1;
  4.  
  5. //set key to 2345
  6.  
  7. key_numentries = 2345;
  8.  
  9. //locate the segment
  10.  
  11. if ((shmid_numentries = shmget(key_numentries, 8, 0666)) < 0) {
  12. perror("shmget");
  13. exit(1);
  14. }
  15.  
  16. //attach the segment to our data space
  17.  
  18. if ((shm1 = shmat(shmid_numentries, NULL, 0)) == (char *) -1) {
  19. perror("shmat");
  20. exit(1);
  21. }
  22.  
  23. // I have no idea what to do here... here are some of the things I've tried
  24.  
  25.  
  26. //send # of entries to client
  27. //char c = (char)&shm1;
  28. //printf("got this from the parent too %s\n",c);
  29. //printf("in SHM buf: %s\n",&shm1[0]);
  30. numentries = (int)&shm1[0];
  31. //numentries = strtol(&shm1[0],NULL,0);
  32.  
  33. for (; shm1 != NULL; shm1++)
  34. putchar(*shm1);
  35. putchar('\n');

heres where I'm not sure what to do... how do I get the data from the pointer? Everything I've tried is either nothing or it's a bunch of random gibberish that obviously I didn't store in the pointer... so I'm afraid the memory space isn't actually being shared. Can anyone point me in the right direction here? I am BRAND NEW to shared memory to be honest. Haven't dealt with forks that much either.

Thanks in advance! Please let me know if I need to clarify anything.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 16
Reputation: venomxxl is an unknown quantity at this point 
Solved Threads: 2
venomxxl's Avatar
venomxxl venomxxl is offline Offline
Newbie Poster
 
0
  #2
19 Days Ago
I had once held a small project for Linux but that was a few months ago, and I hardly remember anything since it was my last project which I wrote for Linux. I mostly program for Windows now, but I'm not going to write my autobiography here!

Here's a little snippet I can provide, not sure if it works though:
  1. #include <sys/types.h>
  2. #include <sys/mman.h>
  3. #include <fcntl.h>
  4.  
  5. typedef struct{
  6. // Variables
  7. }SharedMemory;
  8.  
  9. static int FileDescriptor;
  10.  
  11. static SharedMemory * Memory;
  12.  
  13. void SharedMemoryMap(){
  14. FileDescriptor=open("/dev/zero", O_RDWR);
  15.  
  16. if(FileDescriptor==-1){
  17. // Error
  18. }
  19.  
  20. Memory=(SharedMemory *)mmap(0, sizeof(SharedMemory), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
  21.  
  22. if(Memory==(SharedMemory *)-1){
  23. // Error
  24. }
  25. }

Change it to suit your needs, hope it helps.
Last edited by venomxxl; 19 Days Ago at 2:03 pm.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC