Note that there are some explanatory texts on larger screens.

plurals
  1. POShared memory program not synchronized - Giving segmentation faults
    primarykey
    data
    text
    <p>I am trying to use shared memory with 2 programs which I have shown here. a and b are integer arrays and array1 and array2 are arrays of strings. This is the snippet first program which loads the data in the memory</p> <pre><code>int size = (NUMBER_OF_DATA*(sizeof(int)+sizeof(float))); key_t key1, key2, key3; key1 = ftok("/home/an/Desktop/newww.c", 4); key2 = ftok("/home/an/Desktop/part1.c", 5); key3 = ftok("/home/an/Desktop/part2.c", 6); int shmid = shmget(key1, size, 0777|IPC_CREAT); int shmid1 = shmget(key2, 20*NUMBER_OF_DATA, 0777|IPC_CREAT); int shmid2 = shmget(key3, 20*NUMBER_OF_DATA, 0777|IPC_CREAT); int *a = (int *)shmat(shmid, 0, 0); float *b = (float *)(a + NUMBER_OF_DATA); char **array1 = (char **)shmat(shmid1, 0, 0); printf("array1 %p\n", &amp;array1); char *mempos = (char *)(array1 + NUMBER_OF_DATA); printf("MEMPOS %p\n", &amp;mempos); char **array2 = (char **)shmat(shmid2, 0, 0); char *mempos1 = (char *)(array2 + (NUMBER_OF_DATA)); for(i=0; i&lt;NUMBER_OF_DATA; i++) { a[i] = a[i]; b[i] = b[i]; array1[i] = mempos; strcpy(array1[i], array1[i]); mempos += 20; array2[i] = mempos1; strcpy(array2[i], array2[i]); mempos1 += 20; printf("%p %p %p %p\n", &amp;a[i], &amp;b[i], array1[i], array2[i]); } j = 0; while (j&lt;5) { j++; for (i=0; i&lt;NUMBER_OF_DATA; i++) { printf("%s %s %d %f\n", array1[i], array2[i], a[i], b[i]); } printf("\n"); sleep(2); } </code></pre> <p>I am trying to load some data in the shared memory by the above file and alter the elements in the shared memory by the file below. But as soon as I update a string in the file below, I get garbage values from the program above instead of the altered values. I am totally confused. Have tried debugging with gdb but everything looks okay there. </p> <pre><code>int size = (NUMBER_OF_DATA*(sizeof(int)+sizeof(float))) + (2*(20*NUMBER_OF_DATA)); key_t key1, key2, key3; key1 = ftok("/home/an/Desktop/newww.c", 4); key2 = ftok("/home/an/Desktop/part1.c", 5); key3 = ftok("/home/an/Desktop/part2.c", 6); int shmid = shmget(key1, size, 0777|IPC_CREAT); int shmid1 = shmget(key2, 20*NUMBER_OF_DATA, 0777|IPC_CREAT); int shmid2 = shmget(key3, 20*NUMBER_OF_DATA, 0777|IPC_CREAT); int *a = (int *)shmat(shmid, 0, 0); float *b = (float *)(a + NUMBER_OF_DATA); char **array1 = (char **)shmat(shmid1, 0, 0); printf("array1 %p\n", &amp;array1); char *mempos = (char *)(array1 + NUMBER_OF_DATA); printf("MEMPOS %p\n", &amp;mempos); char **array2 = (char **)shmat(shmid2, 0, 0); char *mempos1 = (char *)(array2 + (NUMBER_OF_DATA)); int i; for(i=0; i&lt;5; i++) { printf("enter\n"); array1[i] = mempos; scanf("%s", array1[i]); mempos += 20; } </code></pre> <p>What is the solution?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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