(Warnung - nicht perfekt Englisch!)Linux. Pipes in C. Senden von Daten an einen anderen Prozess. Schreiben und lesen
Auf Collage gaben sie mir eine Aufgabe zu tun und ich kämpfte mit ihm für ein paar Stunden und kann es nicht lösen. Ich habe ein Problem beim Senden von Daten von einem Prozess zu einem anderen. In dem folgenden Code sende ich Eingabedaten an untergeordneten Prozess, aber ich erhalte es nicht vom untergeordneten Prozess. Können Sie mir helfen?
Programmbeschreibung: Meine Aufgabe ist es, eine Eingabe vom Benutzer zu nehmen, schickte es unter Verwendung von Rohr zu untergeordneten Prozess, einige Berechnungen dann diese Berechnungen weiter zum nächsten Prozess gesendet. Der letzte Prozess summiert eingehende Daten und weist sie dem Endergebnis zu. Um genauer zu sein, berechnet der erste Kindprozess einzelne Terme der Maclaurin-Reihe und sendet sie an den Prozess, der sie summiert. Hier ist der Code:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <math.h>
#include <sys/stat.h>
int mkfifo(const char *s, mode_t m) {
return 0;
}
int i = 1;
double FINAL_RESULT = 0 ;
double factorial(int N){
if(N==1)return 1.0;
return N*factorial(N - 1);
}
double Maclaurine(int i , double x , double eps){
double result;
result = (pow(x,(double)i)/(double)factorial(i));
if(result < eps) return 0;
return result;
}
void print(char * tab, int size){
printf("\n");
for(int i = 0; i < size ; i++){
printf("%c" , tab[i]);
}
printf("\n");
}
int main(){
int pd;
if(mkfifo("tmp/myfifo1", 0666) == -1){
printf("Failed to create myfifo2\n");
}
if(mkfifo("tmp/myfifo2", 0666) == -1){
printf("Failed to create myfifo2\n");
}
pd = open("tmp/myfifo1", O_WRONLY);
double x1,eps1;
printf("X Value : \n");
scanf("%lf", &x1);
printf("EPS Value : \n");
scanf("%lf", &eps1);
char tab1[128], tab2[128];
//converting to char*
sprintf(tab1,"%lf",x1);
sprintf(tab2,"%lf",eps1);
//printing to standard output
print(tab1, strlen(tab1));
print(tab2, strlen(tab2));
//creating new process
if(fork() == 0){
//creating another process
if(fork() == 0){
int pd3;
pd3 = open("tmp/myfifo2", O_RDONLY);
char tab5[128];
double x3;
//reading data from pipe , myfifo2
read(pd3,tab5,sizeof(tab5));
//converting char* to float
x3 = atof(tab5);
FINAL_RESULT += x3;
printf("\nCurrent result = %lf" , FINAL_RESULT);
}
int pd2;
//opening myfifo1 for reading
pd2 = open("tmp/myfifo1", O_RDONLY);
if(pd2 == - 1){
printf("\nFailed to open myfifo1 for reading!\n");
}
double x2, eps2;
char tab3[128], tab4[128];
//reading inputed parameters
read(pd2,tab3, sizeof(tab3));
read(pd2,tab4, sizeof(tab4));
//converting to floats
x2 = atof(tab3);
eps2 = atof(tab4);
printf("\nReceived initial data x=%f, eps = %f \n" , x2, eps2);
double singleTerm = 0;
int pd4;
char wyraz[128];
//opening myfifo2 for writing
pd4 = open("tmp/myfifo2", O_WRONLY);
if(pd4 == -1){
printf("\nfailed to open myfifo2\n");
}
do{
//calculate single term of Maclaurin series
singleTerm = Maclaurine(i,x2,eps2);
printf("\n%d. Series term = %lf\n", i,singleTerm);
sprintf(wyraz,"%lf",singleTerm);
if(write(pd4, wyraz, sizeof(wyraz)) == -1){
printf("\nfailed to send singleTerm to myfifo2\n");
}
i++;
//if its less than eps, loop ends
}while(singleTerm);
printf("\nFinal Result : %lf", FINAL_RESULT);
close(pd4);
close(pd2);
unlink("tmp/myfifo2");
}
//write data to myfifo1
write(pd, tab1, sizeof(tab1));
write(pd, tab2, sizeof(tab2));
close(pd);
unlink("tmp/myfifo1");
return 0;
}
Und hier das Ergebnis des Codes:
[email protected]:~$ gcc pipes.c -lm -o x
[email protected]:~$ ./x
X Value :
10
EPS Value :
0.001
10.000000
0.001000
[email protected]:~$
Failed to open myfifo1 for reading!
Received initial data x=0.000000, eps = 0.000000
failed to open myfifo2
1. Series term = 0.000000
failed to send singleTerm to myfifo2
Final Result : 0.000000
Current result = 0.000000
Failed to open myfifo1 for reading!
Received initial data x=0.000000, eps = 0.000000
failed to open myfifo2
1. Series term = 0.000000
failed to send singleTerm to myfifo2
Final Result : 0.000000
Ich möchte nur hinzufügen, dass ich diese Aufgabe gescheitert, weil es in seiner Klasse war Arbeit. Ich poste es, weil ich verstehen will, was hier vor sich geht. – Pawel
Ok danke, ich werde es korrigieren. – Pawel
Ok, jetzt ist es korrigiert. Ich hoffe, es sieht jetzt gut aus. – Pawel