1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
std::string result;
pid_t pid;
int pipeA[2];
int pipeB[2];
if(pipe(pipeA) < 0) perror("pipe a failed"); // To Child
if(pipe(pipeB) < 0) perror("pipe b failed"); // From Child
pid = fork();
if(pid == -1){
perror("fork failed");
exit(1);
}
else if(pid == 0){
if(close(pipeA[1])<0) perror("c close a1 failed"); // No Writing in A
if(close(pipeB[0])<0) perror("c close b0 failed"); // No Reading B
if(dup2(pipeA[0], STDIN_FILENO)==-1) perror("c dub a failed");
if(dup2(pipeB[1], STDOUT_FILENO)==-1) perror("c dub b failed");
std::cerr << "child process starting ..." << std::endl;
std::stringstream sstr;
sstr << frequency;
execl("../helloworld/app", "app", (char*)0);
if(close(pipeA[0])<0) perror("c close a0 failed");
if(close(pipeB[1])<0) perror("c close b1 failed");
std::cerr << "child stopping ..." << std::endl;
exit(1);
}
else{
if(close(pipeA[0])<0) perror("close a0 failed"); // No Reading from A
if(close(pipeB[1])<0) perror("close b1 failed"); // No Writing to B
//if(dup2(pipeA[1], STDOUT_FILENO)==-1) perror("dub a failed"); // Write A
if(dup2(pipeB[0], STDIN_FILENO)==-1) perror("dub b failed"); // Read B
// write pipe
FILE* stream;
stream = fdopen (pipeA[1], "w");
if(stream == 0) perror("stream failed");
fwrite(&midi[0], midi.size(), sizeof(BYTE), stream);
fflush(stream);
fclose(stream);
std::cout << "Start reading from position: "
<< lseek(pipeB[0], 0, SEEK_CUR) << std::endl;
// read pipe
char buf[1024];
if(toFile == "") {
// works second time, but not complete?
//while (read(pipeB[0], &buf, 1024) > 0 || result == "")
// result += buf;
// Does not work second time, no chars received
std::string lineInput;
while (std::cin >> lineInput || result == "")
result.append(lineInput);
}
// close pipes
//if(close (pipeA[1])<0) perror("close a1 failed");
if(close (pipeB[0])<0) perror("close b0 failed");
if(kill(pid,SIGKILL)<0) perror("sigkill failed");
}
|