Note that there are some explanatory texts on larger screens.

plurals
  1. POTesting shared memory ,strange thing happen
    text
    copied!<p>I have 2 program compiled in 4.1.2 running in RedHat 5.5 , It is a simple job to test shared memory , shmem1.c like following :</p> <pre><code>#define STATE_FILE "/program.shared" #define NAMESIZE 1024 #define MAXNAMES 100 typedef struct { char name[MAXNAMES][NAMESIZE]; int heartbeat ; int iFlag ; } SHARED_VAR; int main (void) { int first = 0; int shm_fd; static SHARED_VAR *conf; if((shm_fd = shm_open(STATE_FILE, (O_CREAT | O_EXCL | O_RDWR), (S_IREAD | S_IWRITE))) &gt; 0 ) { first = 1; /* We are the first instance */ } else if((shm_fd = shm_open(STATE_FILE, (O_CREAT | O_RDWR), (S_IREAD | S_IWRITE))) &lt; 0) { printf("Could not create shm object. %s\n", strerror(errno)); return errno; } if((conf = mmap(0, sizeof(SHARED_VAR), (PROT_READ | PROT_WRITE), MAP_SHARED, shm_fd, 0)) == MAP_FAILED) { return errno; } if(first) { for(idx=0;idx&lt; 1000000000;idx++) { conf-&gt;heartbeat = conf-&gt;heartbeat + 1 ; } } printf("conf-&gt;heartbeat=(%d)\n",conf-&gt;heartbeat) ; close(shm_fd); shm_unlink(STATE_FILE); exit(0); }//main </code></pre> <p>And shmem2.c like following :</p> <pre><code>#define STATE_FILE "/program.shared" #define NAMESIZE 1024 #define MAXNAMES 100 typedef struct { char name[MAXNAMES][NAMESIZE]; int heartbeat ; int iFlag ; } SHARED_VAR; int main (void) { int first = 0; int shm_fd; static SHARED_VAR *conf; if((shm_fd = shm_open(STATE_FILE, (O_RDWR), (S_IREAD | S_IWRITE))) &lt; 0) { printf("Could not create shm object. %s\n", strerror(errno)); return errno; } ftruncate(shm_fd, sizeof(SHARED_VAR)); if((conf = mmap(0, sizeof(SHARED_VAR), (PROT_READ | PROT_WRITE), MAP_SHARED, shm_fd, 0)) == MAP_FAILED) { return errno; } int idx ; for(idx=0;idx&lt; 1000000000;idx++) { conf-&gt;heartbeat = conf-&gt;heartbeat + 1 ; } printf("conf-&gt;heartbeat=(%d)\n",conf-&gt;heartbeat) ; close(shm_fd); exit(0); } </code></pre> <p>After compiled :</p> <pre><code> gcc shmem1.c -lpthread -lrt -o shmem1.exe gcc shmem2.c -lpthread -lrt -o shmem2.exe </code></pre> <p>And Run both program almost at the same time with 2 terminal :</p> <pre><code> [test]$ ./shmem1.exe First creation of the shm. Setting up default values conf-&gt;heartbeat=(840825951) [test]$ ./shmem2.exe conf-&gt;heartbeat=(1215083817) </code></pre> <p>I feel confused !! since shmem1.c is a loop 1,000,000,000 times , how can it be possible to have a answer like 840,825,951 ? </p> <p>I run shmem1.exe and shmem2.exe this way,most of the results are conf->heartbeat will larger than 1,000,000,000 , but seldom and randomly , I will see result conf->heartbeat will lesser than 1,000,000,000 ,<br> either in shmem1.exe or shmem2.exe !!</p> <p>if run shmem1.exe only , it is always print 1,000,000,000 , my question is , what is the reason cause conf->heartbeat=(840825951) in shmem1.exe ?</p> <p>Update: Although not sure , but I think I figure it out what is going on , If shmem1.exe run 10 times for example , then conf->heartbeat = 10 , in this time shmem1.exe take a rest and then back , shmem1.exe read from shared memory and conf->heartbeat = 8 , so shmem1.exe will continue from 8 , why conf->heartbeat = 8 ? I think it is because shmem2.exe update the shared memory data to 8 , shmem1.exe did not write 10 back to shared memory before it took a rest ....that is just my theory... i don't know how to prove it !!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload