[gelöst]Child Prozess will nicht von Pipe lesen
-
Hallo Leute
Hab ein Problem mit einem child-Prozess. Der Parentprozess schriebt eine int nummer in die dafür vorgesehene pipe. Nur der child prozess liest die Werte nicht aus. Ich komme nicht auf den Fehler. Kann mir von euch jemand helfen?
Hier mal der Code.
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> #include <time.h> /* define preprocessor macros */ #define READ 0 #define WRITE 1 /** * main program */ int main(int argc, char **argv) { /* some variables ... */ time_t t1; (void) time(&t1); srand48((long) t1); /* use time in seconds to set seed */ /* ---------------------------- */ /* create pipe */ int thePipe[2]; int status; pid_t pid; /*Creates Pipe and check whether is was successful */ if(pipe(thePipe)) { perror("pipe call"); exit(EXIT_FAILURE); } /* ----------------------------- */ /* fork stuff & co */ pid = fork(); if (pid < (pid_t)0) { /* fork failed */ fprintf(stderr, "Fork failed.\n"); return EXIT_FAILURE; } if(pid > (pid_t)0) { /* code for master */ int array[]={19,9,17,15,4,12,6,3,4.9,136}; int count=10; int threshold=10; close(thePipe[READ]); //Close read pipe printf("Parent\n"); /* write number of int values to slave */ write(thePipe[WRITE], count, (sizeof(int))); printf("[P]count %d\n", count); /* write the threshold to child */ //write(thePipe[WRITE], &th reshold, sizeof(int)); /* write actual int values to child */ //write(thePipe[WRITE], &array, sizeof(int)); /* close pipes */ close(thePipe[WRITE]); /* wait for child and exit*/ wait(&status); exit(EXIT_SUCCESS); } if (pid == (pid_t)0) { /* step 4.2: code for child */ printf("Child"); /*close unnessary pipes and create local variables */ close(thePipe[WRITE]); int count; int threshold; int array[count]; /* read number of int values first */ read(thePipe[READ], count, (sizeof(int))); printf("[C] reads count %d", count); /* read threshold */ //read(thePipe[READ], &threshold, sizeof(int)); //printf("[C] reads threshold %d", threshold); /* close pipes and exit */ close(thePipe[READ]); exit(EXIT_SUCCESS); } }
Ah ja hab noch eine Frage. Wenn ich mehrere Werte hintereinander vom Parent zum child schicken will muss ich dann für jeden Wert den ich schicke eine eigene Pipe aufmachen oder kann ich dazu nur eine verwenden.
Gruß
Christian
-
Für Einsteiger empfehle ich immer die Option "-Wall" beim Kompilieren:
> gcc -Wall -o pipe-test pipe-test.c pipe-test.c: In function `main': pipe-test.c:85: warning: passing arg 2 of `write' makes pointer from integer wit hout a cast pipe-test.c:73: warning: unused variable `array' pipe-test.c:75: warning: unused variable `threshold' pipe-test.c:130: warning: passing arg 2 of `read' makes pointer from integer wit hout a cast pipe-test.c:125: warning: unused variable `threshold' pipe-test.c:126: warning: unused variable `array' pipe-test.c:155: warning: control reaches end of non-void function
Daran siehst Du sofort, dass die write()-Aufrufe fehlerhaft sind. write() erwartet als 2. Parameter einen Zeiger auf eine Zeichenkette; was soll write() also mit "count" als 2. Parameter anfangen?
Zu Deiner 2. Frage: Nein. Du kannst beliebig viele Daten über die Pipe schicken.
Mehr zu Pipes und Prozessen findest Du übrigens auch in "C und Linux".
Martin
-
Hi Martin!
Danke für die Antwort. So ein dummer Fehler. Aber jetzt funktionierts.
Gruß
Christian