0

here is a bit of background on what I am trying to do for this class assignment. I am going to have 10 total programs that are going using semaphores to be synchronized to accessing a shared memory construct that is going to hold an Integer. This integer will hold the amount of text that has currently been read. How exactly am I am going to take this setup from my textbook for strings and convert it for use with Integers?

The 9 of the programs will only increment the value of the shared memory and then one of the programs will just read the shared memory value for later use. "Producer" is going to be the 9 programs that will increment the value of the shared memory. "Consumer" is going to be the single program that will read the value later on to do separate operations.

The "Consumer" program will be initializing the shared memory and setting the int value to 0.

Below is the "Producer" program that will build and write to the shared memory construct.

#include <stdio.h>
#include <stlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <sys/stat.h>

int main()
{

     /* the size (in bytes) of shared memory object */
     const int SIZE 4096;

     /* name of the shared memory object */
     const char *name = "OS";

     /* strings written to shared memory */
     const char *message 0 = "Hello";
     const char *message 1 = "World!";

     /* shared memory file descriptor */
     int shm fd;

     /* pointer to shared memory obect */
     void *ptr;

     /* create the shared memory object */
     shm fd = shm open(name, O CREAT | O RDRW, 0666);

     /* configure the size of the shared memory object */
     ftruncate(shm fd, SIZE);

     /* memory map the shared memory object */
     ptr = mmap(0, SIZE, PROT WRITE, MAP SHARED, shm fd, 0);

     /* write to the shared memory object */
     sprintf(ptr,"%s",message 0);
     ptr += strlen(message 0);
     sprintf(ptr,"%s",message 1);
     ptr += strlen(message 1);
     return 0;

}

Now below is the "Consumer" program that will read from the shared memory

#include <stdio.h>
#include <stlib.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <sys/stat.h>

int main()
{

     /* the size (in bytes) of shared memory object */
     const int SIZE 4096;

     /* name of the shared memory object */
     const char *name = "OS"; 

     /* shared memory file descriptor */
     int shm fd; 

     /* pointer to shared memory obect */
     void *ptr;

     /* open the shared memory object */
     shm fd = shm open(name, O RDONLY, 0666);

     /* memory map the shared memory object */
     ptr = mmap(0, SIZE, PROT READ, MAP SHARED, shm fd, 0);

     /* read from the shared memory object */
     printf("%s",(char *)ptr);

     /* remove the shared memory object */
     shm unlink(name);
     return 0;

}

My idea of how to change this is to end up having the following changes for the Producer

  • change SIZE to 4 bytes for the size of Int
  • read the value of the int from shared memory assign that value to shmValue
  • shmValue then gets incremented
  • shmValue then gets written back to shared memory

My idea of how to change the Consumer program

  • change SIZE to 4 bytes for the size of Int
  • initialize the shared memory to int with a value of 0
  • reads shared memory to shmValue
  • then uses shmValue for later operations.

Again, guys, I would very much appreciate the help in dealing with this issue that I am having in understanding how to use this technology. Thank you for the help in advance!!

Fel
  • 15
  • 5
  • The question is within the first paragraph. Are these changes really all I have to do to make this work? Because i am a bit fuzzy on this. I dont really understand most of the topic of shared memory and how the code works. I have looked through the man pages and i guess i dont really understand how i would change this to work with my setup from what i given above. – Fel Dec 09 '19 at 08:12
  • However, in order to have a global shared variable (a global shared memory in your case), you can declare in one of the file this variable (let's say `int shared;`). In the other files, you can re-use the same variable using `extern int shared;`. You MUST pay a lot of attention on synch the access to that variable (mutex, semaphores et cetera). However, consider to use `pthreads` – Leos313 Dec 09 '19 at 08:12
  • The program seems not to be designed for concurrent access to the shared memory. You should add a mutex to prevent race conditions. Otherwise, go on and write that code. Then ask about the problems you face. (Related: https://stackoverflow.com/q/27220482/1025391) – moooeeeep Dec 09 '19 at 08:14
  • Are the permissions correct for each for what I am wanting to setup for example within sem_open() and mmap()? Do i have to change the consumer program to also be read and write so i can initialize the value of integer? – Fel Dec 09 '19 at 08:15
  • advice: start by reading this https://www.cs.cmu.edu/afs/cs/academic/class/15492-f07/www/pthreads.html and, after, move to study how to use/manage shared variables by using mutex and sem – Leos313 Dec 09 '19 at 08:17
  • @moooeeeep, yes, currently this setup is not going to work in any concurrent access as I dont have semaphores set. I just want to understand how to work with the shared memory. Once i understand how to actually write this, implementing it into the synchronized setting wont be too much of a problem as I have had experience using semaphores. – Fel Dec 09 '19 at 08:18
  • @Leos313, I already know how to use pthreads and semaphores, i just did not want to end up creating any unnecessary code for people to have to decipher so i could get an answer on how to use shared memory. – Fel Dec 09 '19 at 08:20
  • ok, so it is enough to declare your shared variable in one file and use the keyword `extern` to re-use the same in other concurrent threads. – Leos313 Dec 09 '19 at 08:23
  • Never heard of the keyword "extern". Also these are going to be completely seperate processes generated from forks which the children then execute another program – Fel Dec 09 '19 at 08:28
  • It is a requirement to use the posix shared memory – Fel Dec 09 '19 at 08:28
  • Also related: https://stackoverflow.com/q/10684499/1025391 and https://stackoverflow.com/q/6477525/1025391 – moooeeeep Dec 09 '19 at 09:03

0 Answers0