Note that there are some explanatory texts on larger screens.

plurals
  1. POC - not reaching proper end with fork(), pipe(), select(), execl(), and write()
    primarykey
    data
    text
    <p>I am using forks and pipes to find the amount of 1s and 0s in a string inside of a file. However, I am never reaching the proper end to my program that tallys up the ones and zeroes. It's a pretty small amount of code so here is the whole program:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; #include &lt;unistd.h&gt; #include &lt;sys/time.h&gt; int main (int argc, char** argv) { int leftR, rightR; char *string; long size; int recursion = 0; if (argc == 3) { string = argv[1]; size = strlen(string); printf("the string is %s\n", string); if (size &lt;= 2) { int bitCounter[2]; bitCounter[0] = 0; bitCounter[1] = 0; int i; for (i=0; i &lt; size; i++) { if (string[i]=='0') { bitCounter[0]++; } else { bitCounter[1]++; } } write(STDOUT_FILENO, &amp;bitCounter, sizeof(int)*2); printf("read bits, sending back %d ones and %d zeroes\n", bitCounter[1], bitCounter[0]); return 0; } else { recursion = 1; } } if (argc == 2 || recursion) { char *data; if (!recursion) { FILE* filePointer; if ((filePointer = fopen(argv[1], "r")) == NULL) { perror("file didn't work"); } fseek(filePointer, 0, SEEK_END); size = ftell(filePointer); fseek(filePointer, 0, SEEK_SET); data = malloc(size+1); fread(data, size, 1, filePointer); fclose(filePointer); } else { data = malloc(size+1); data = string; } char *right; char *left = malloc((size/2)+1); if (size%2 == 0) { right = malloc(size/2 + 1); } else { right = malloc(size/2 + 2); } memcpy(left, data, size/2); if (size%2 == 0) { memcpy(right, (size/2) + data, size/2); } else { memcpy(right, (size/2) + data, (size/2) + 1); } int pidLeft, pidRight; int leftPipe[2]; int rightPipe[2]; pipe(leftPipe); pipe(rightPipe); fd_set readF; FD_ZERO(&amp;readF); FD_SET(leftPipe[0], &amp;readF); FD_SET(rightPipe[0], &amp;readF); pidLeft = fork(); if (pidLeft &gt; 0) { pidRight = fork(); if (pidRight &gt; 0) { struct timeval timer; timer.tv_sec = 3; timer.tv_usec = 0; close(rightPipe[1]); close(leftPipe[1]); dup2(leftPipe[0], STDOUT_FILENO); dup2(rightPipe[0], STDOUT_FILENO); select(2, &amp;readF, NULL, NULL, &amp;timer); read(leftPipe[0], &amp;leftR, sizeof(int)*2); read(rightPipe[0], &amp;rightR, sizeof(int)*2); printf("going back to parent.\n"); } else if (pidRight == 0) { close(rightPipe[0]); execl("my_program", "my_program", right, "y", NULL); printf("recursion start\n"); exit(1); } } else if (pidLeft == 0) { close(leftPipe[0]); execl("my_program", "my_program", left, "y", NULL); printf("start recursion LEFT\n"); exit(1); } else { fprintf(stderr, "something went wrong! No fork!\n"); } } else { fprintf(stderr, "Please input file name properly\n"); exit(1); } int zeroes = leftR + rightR; int* numOnes[2]; numOnes[0] = &amp;leftR + sizeof(int); numOnes[1] = &amp;rightR + sizeof(int); int ones = (int) *numOnes[0] + (int) *numOnes[1]; printf("0's: %d\n1's: %d\n", zeroes, ones); return 0; } </code></pre> <p>However, the output never reaches my desired end that adds everything up:</p> <pre><code>the string is 01010▒z the string is 010 the string is 0 read bits, sending back 0 ones and 1 zeroes the string is 10▒z the string is 10 read bits, sending back 1 ones and 1 zeroes the string is ▒z read bits, sending back 2 ones and 0 zeroes the string is 10 read bits, sending back 1 ones and 1 zeroes the string is 10100 z the string is 101 the string is 1 read bits, sending back 1 ones and 0 zeroes the string is 01 read bits, sending back 1 ones and 1 zeroes the string is 00 z the string is 00 read bits, sending back 0 ones and 2 zeroes the string is z read bits, sending back 2 ones and 0 zeroes (140) Admin $ </code></pre> <p>A couple quick points to understand the code a little easier:</p> <ul> <li>argc is only 3 when it is being executed within a child</li> <li>children will only read the string if it is less than or equal to 2, otherwise recursively cut the string in half again (and make more children)</li> <li>parents make both a left and right child, both taking half of the string to work on</li> </ul> <p>I guess a couple easy questions to start off would be: </p> <ul> <li>Am I using select() and execl() properly?</li> <li>Am I using read() and write() properly?</li> <li>Is there an exit in the main processes I am missing?</li> <li>Are both children working and using the pipe correctly?</li> <li>(less important) is the strange character in the strings messing up my count? Is that a null terminator somehow inside my string?</li> </ul>
    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