Note that there are some explanatory texts on larger screens.

plurals
  1. POProgram goes into a deadlock when invoking fork()
    primarykey
    data
    text
    <p>I've implemented a pipe that's based on a shared memory , and I have a problem when I try to invoke <code>fork</code> with a <code>main</code> program . </p> <p>The following main : </p> <pre><code># include "my_shm_piper.h" #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; #include &lt;sys/wait.h&gt; #include &lt;unistd.h&gt; #include &lt;fcntl.h&gt; #include &lt;sys/stat.h&gt; #include &lt;sys/types.h&gt; #include &lt;sys/shm.h&gt; #include &lt;semaphore.h&gt; #include &lt;sys/mman.h&gt; int main() { int spd[2], pid, rb; char buff[4096]; fork(); // that fork is okay , but if we put it after initPipe() , there's a deadlock initPipe(); if (my_pipe(spd) &lt; 0) { perror("my_pipe"); exit(1); } if (fork()) { rb = my_read(spd[0], buff, sizeof(buff)); if (rb &gt; 0) write(1, buff, rb); } else { my_write(spd[1], "hello world!\n", sizeof("hello world!\n")); } my_close(spd[0]); my_close(spd[1]); removePipe(); return 0; } </code></pre> <p>Is using on an <strong>Anonymous-pipe</strong> that's implemented using shared memory library. </p> <p>When I put the <code>1st</code> command of <code>fork()</code> , as above , then my program is working as expected , all the <code>hello-world</code>-s are presented . </p> <p>But when I put the <code>fork</code> after <code>initPipe()</code> , there's deadlock , and the program hangs : </p> <pre><code>int main() { int spd[2], pid, rb; char buff[4096]; initPipe(); fork(); // now the fork() is after the initialization ,and we have a deadlock if (my_pipe(spd) &lt; 0) { perror("my_pipe"); exit(1); } // from here the same as above } </code></pre> <p>I think that the initialization stage for the <code>fork()</code> is happening only once , and not twice , as in the first <code>main()</code> . </p> <p>I guess that there's something wrong with the writing/reading stage , but I can't seem to find the exact source . </p> <p>I'd appreciate for your help with the matter</p> <p>Thanks </p> <p><strong>EDIT:</strong></p> <p>the struct in the H. file :</p> <pre><code>struct PipeShm { int init; int flag; sem_t *mutex; char * ptr1; char * ptr2; int status1; int status2; int semaphoreFlag; }; </code></pre> <p>this is initPipe:</p> <pre><code>int initPipe() { if (!myPipe.init) { myPipe.mutex = mmap (NULL, sizeof *myPipe.mutex, PROT_READ | PROT_WRITE,MAP_SHARED | MAP_ANONYMOUS, -1, 0); if (!sem_init (myPipe.mutex, 1, 1)) { myPipe.init = TRUE; } else perror ("initPipe"); } return 1; // always successful } </code></pre> <p>this is my_pipe():</p> <pre><code>int my_pipe(int spd[2]) { spd[0] = shmget(2009, SHMSIZE, 0); // for reading spd[1] = shmget(2009, SHMSIZE, 0666 | IPC_CREAT); // for writing if (spd[0] == -1 || spd[1] == -1) { perror("shmget"); exit(EXIT_FAILURE); return -1; } return 1; } </code></pre> <p>This is the reading :</p> <pre><code>ssize_t my_read(int spd, void *buf, size_t count) { char array[4096]; memset (array, '\0', 4096); ssize_t returnVal = 0; sem_wait (myPipe.mutex); int sval; sem_getvalue (myPipe.mutex, &amp;sval); printf ("my_read - wait %d\n", sval); if (sem_wait (myPipe.mutex)) perror ("sem_wait"); printf ("my_read - proceed\n"); if (myPipe.flag == FALSE) { myPipe.ptr1 = shmat (spd, NULL, 0); // attaching the segment if (myPipe.ptr1 == (void *) -1) error_out ("shmat"); strncpy (array, myPipe.ptr1, count); array[count] = '\0'; returnVal = strlen (array); buf = (void *) array; printf ("Output:%s", array); } else if (myPipe.flag == TRUE) { const size_t region_size = sysconf (_SC_PAGE_SIZE); myPipe.ptr1 = mmap (0, region_size, PROT_READ | PROT_WRITE, MAP_SHARED, spd, 0); if (myPipe.ptr1 == (void *) -1) error_out ("mmap"); strncpy (array, myPipe.ptr1, count); array[count] = '\0'; returnVal = strlen (array); buf = (void *) array; printf ("Output:%s", array); } return returnVal; } </code></pre> <p>and this is the writing :</p> <pre><code>ssize_t my_write(int spd, const void *buf, size_t count) { ssize_t returnVal = 0; sleep(1); // debug to ensure that read goes first for testing. if (myPipe.flag == FALSE) { myPipe.ptr2 = shmat (spd, NULL, 0); // attaching the segment if (myPipe.ptr2 == (void *) -1) error_out ("shmat"); char *d = (char *) buf; returnVal = snprintf (myPipe.ptr2, count, "%s", d); } else { const size_t region_size = sysconf (_SC_PAGE_SIZE); // Map the region into memory. myPipe.ptr2 = mmap (0, region_size, PROT_READ | PROT_WRITE, MAP_SHARED, spd, 0); if (myPipe.ptr2 == MAP_FAILED) error_out ("mmap"); char *d = (char *) buf; returnVal = snprintf (myPipe.ptr2, count, "%s", d); } sem_post (myPipe.mutex); return returnVal; } </code></pre> <p>The processes are hanging like that - in the second main (this is the output on the console): </p> <pre><code>my_read - wait 0 my_read - proceed Output:hello world! // here it just gets stuck </code></pre> <p>And in the first main , the output in console is: </p> <pre><code>my_read - wait 0 my_read - wait 0 my_read - proceed my_read - proceed Output:hello world! Output:hello world! // here the program is done , the end </code></pre>
    singulars
    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