Note that there are some explanatory texts on larger screens.

plurals
  1. POUnix Programming Shared Memory strange results
    text
    copied!<p>I have been working on semaphores and shared memory for a week now, and have some difficulties yet,so i tried to make this program which the children are supposed to write to a memory shared multidimensional integer array and the father is suppose to read that array from the memory that is shared.</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;unistd.h&gt; #include &lt;sys/ipc.h&gt; #include &lt;sys/shm.h&gt; #include &lt;stdio.h&gt; #include &lt;sys/fcntl.h&gt; #include &lt;semaphore.h&gt; #include &lt;sys/wait.h&gt; #define MAXCHILDS 1 #define MAX_SIZE 10 #define MAX_WRITES 100 typedef struct{ int m[MAX_SIZE][MAX_SIZE]; }matrix; /*fork variables*/ pid_t child[MAXCHILDS]; /*semphores variables */ sem_t *empty, *full, * mutex; /*share memory id*/ int shmid; /*shared memory array pointer */ matrix * sh_mem; void init(){ /*semaphores unlink and creation */ sem_unlink("EMPTY"); empty=sem_open("EMPTY",O_CREAT|O_EXCL,0700,50); sem_unlink("FULL"); full=sem_open("FULL",O_CREAT|O_EXCL,0700,0); sem_unlink("MUTEX"); mutex=sem_open("MUTEX",O_CREAT|O_EXCL,0700,1); /*initialize shared memory */ shmid = shmget(IPC_PRIVATE,sizeof(matrix),IPC_CREAT|0777); /*map shared memory*/ sh_mem = (matrix*)shmat(shmid,NULL,0); if(sh_mem == (matrix*)(-1)){ perror("shmat"); } } void writer(int m[MAX_SIZE][MAX_SIZE],int n_child){ int i,k; for(i = 0;i&lt;MAX_SIZE;i++){ for(k= 0;k&lt;MAX_SIZE;k++){ m[i][k] = 0; if(i==(k+1)){ m[i][k] = 1; } } } } void reader(int m[MAX_SIZE][MAX_SIZE]){ int i = 0; int k = 0; int sum = 0; for(i = 0;i&lt;MAX_SIZE;i++){ for(k= 0;k&lt;MAX_SIZE;k++){ printf("%d",m[k][i]); } sum++; printf("[i=]%d[k=]%d\n",i,k); } printf("%d",sum); } void terminate() { sem_close(empty); sem_close(full); sem_close(mutex); sem_unlink("EMPTY"); sem_unlink("FULL"); sem_unlink("MUTEX"); shmctl(shmid, IPC_RMID, NULL); } int main(int argc, char **argv) { int i,sum; init(); for(i = 0;i&lt;MAXCHILDS;i++){ if((child[i]= fork()) &lt; 0) // error occured { perror("Fork Failed"); exit(1); } if((child[i] =fork())==0){ writer(sh_mem-&gt;m,i); exit(0); } } /*father*/ sleep(10); sum++; printf("%d\n",sum); reader(sh_mem-&gt;m); wait(NULL); terminate(); return 0; } </code></pre> <p>I have two problems right now ... I need to save the array to a memory mapped file which i dont seem to understand and the output of the father is throwing is very strange ...His output is </p> <pre><code>0000100000[i=]3[k=]10 0000010000[i=]4[k=]10 0000001000[i=]5[k=]10 0100000000[i=]0[k=]10 0000000100[i=]6[k=]10 0010000000[i=]1[k=]10 0000000010[i=]7[k=]10 0001000000[i=]2[k=]10 0000000001[i=]8[k=]10 0000100000[i=]3[k=]10 0000000000[i=]9[k=]10 0000010000[i=]4[k=]10 0000001000[i=]5[k=]10 0000000100[i=]6[k=]10 0000000010[i=]7[k=]10 0000000001[i=]8[k=]10 0000000000[i=]9[k=]10 </code></pre> <p>And it should be something like this:</p> <pre><code>0000100000[i=]0[k=]10 0000010000[i=]1[k=]10 0000001000[i=]2[k=]10 0100000000[i=]3[k=]10 0000000100[i=]4[k=]10 0010000000[i=]5[k=]10 0000000010[i=]6[k=]10 0001000000[i=]7[k=]10 0000000001[i=]8[k=]10 0000100000[i=]9[k=]10 </code></pre>
 

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