c - bad output from pipe with multiple processes -
in following code, have 2 pipes, one, fd[] handles ferrying range variable child processes. other pipe rw[] responsible printing results of method. fd works correctly, rw prints garbage. both range , narc_num long , have sucessfully printed string , char through rw pipe. ideas? thanks.
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <math.h> #include <string.h> /** * process propigating version of narcisstic base-10 number generator **/ /** * narcissistic number number of digits, when raised power n n number of digits in number, equla number itseld. * examples: * 2^1 = 2 **/ void isnarcissisticnumber(int rw[], long min, long max){ // printf("min %ld max %ld\n", min, max); long n; for(n = min; n <= max; n++){ long num = n; int digits = floor(log10(num)) + 1; int digit_arr[digits]; long narc_num = 0; int index = 0; do{ digit_arr[index++] = num%10; num /= 10; }while(num > 0); index = 0; for(index; index < digits; index++){ narc_num += pow(digit_arr[index], digits); } if(narc_num == n){ printf("%ld\n", n); // parent: writing only, close read-descriptor. close(rw[0]); write(rw[1], &n, sizeof(long)); // close write descriptor close(rw[1]); } } } int main(int argc, // number of strings in array argv char *argv[]){ // array of command-line argument strings) //check there 1 passed in parameter in addition program name @ argv[0] if(argc != 2){ printf("args found: %d. 1 arg required\n", argc); return; } //check argv passed in int if(!atoi(argv[1])){ printf("argument shoud # of processes proc.\n"); return; } int num_processes = atoi(argv[1]); //counter narcissistic numbers long offset = 10000; long range= -offset; //file pipe int fd[2]; //printing pipe int rw[2]; int n = 0; long output; while(n < num_processes){ // -1 offset line array index num_processes arg pipe(rw); pipe(fd); pid_t process = fork(); pid_t child_process; int status; if(process == 0){ //chid process --> execute program child_process = process; /* duplicate input side of pipe stdin */ // chid: reading only, close write-descriptor close(fd[1]); read(fd[0], &range, sizeof(range)); close(fd[0]); isnarcissisticnumber(rw, range, range+offset-1); }else if(process != 0){ // parent: writing only, close read-descriptor. close(fd[0]); range += offset; write(fd[1], &range, sizeof(range)); // close write descriptor close(fd[1]); // current child process complete routine befire checking output wait(&child_process); //read printing pipe close(rw[1]); while(read(rw[0], &output, sizeof(long))){ printf("printer %ld\n", output); } close(rw[0]); }else{ //failed fork printf("process failed fork!\n"); return -1; } n++; } }
edit #1: while made parent check after child completed, doesn't fix output of pipe, 0 printf shows otherwise.
i'm not sure fix problems have logic issue. in computation routine can write 0 or more longs output. in print (parent) routine, expect 1 value come through. if nothing sent, read fail. if multiple values sent, read first one. first fix check return value read , loop on it.
note - idea wait() on child process.
Comments
Post a Comment