2013-05-10 50 views
0

所以我們必須創建一個程序來創建3個子進程,它們將信息彼此傳遞。有人可以幫我完成管道上的任務嗎?

  • 第一個進程從文件讀入數據。
  • 第二個過程通過轉換每個字母上的上限(即小寫字母轉換爲大寫字母,反之亦然)來更改該數據。
  • 第三個進程將信息寫入文件。

我的問題是,我似乎無法使用fork()waitpid()來運行進程。對於waitpid(),我真的不知道第二和第三個參數屬於什麼。這是我的主:

#include <windows.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <ctype.h> 
#include <sys/types.h> 
#include <sys/wait.h> 
#include "pipe.c" 


int main(void) { 

int p, p2, rc, rc2; 
rc = _pipe(f_des); 
rc2 = _pipe(C1C2); 

pid_t pid[3]; 

pid[0] = fork(); 
if (pid[0] == 0) { 
    puts("create child 1"); 
    do_child0(); 
    exit(0); 
} 
pid[1] = fork(); 
if (pid[1] == 0) { 
    puts("create child 2"); 
    do_child1(); 
    exit(0); 
} 
pid[2] = fork(); 
if (pid[2] == 0) { 
    puts("create child 3"); 
    do_child2(); 
    exit(0); 
} 

waitpid(pid[0], NULL, 0); 
waitpid(pid[1], NULL, 0); 
waitpid(pid[2], NULL, -1); 
printf("children done, parent goes"); 

return 0; 
} 

pipe.c:

#include "pipe.h" 

void do_child1() { 

int c, p2; 
int rc, rc2; 
char *line2, *line3; 
int i = 0; 
int j = 0; 
int k = 0; 
char buffer[80]; 
//char *line; 

close(f_des[1]); 
close(C1C2[0]); 
printf("Received by child 2 from child 1: "); 

while(k<26) { 
for (i = 0; i <= 40; i++) { 
    if ((read(f_des[0], buffer, 1)) != '\0') { 
//while((rc = read(f_des[0], buffer, 40))) { 
    printf("%s", buffer); 
    j = 0; 
    line2 = (char *)malloc(sizeof(char)*40); 
    read(f_des[0], buffer, 40); 
    line2 = buffer; 
    line3 = (char *)malloc(sizeof(char)*40); 
    line2 = line3; 

    rc = read(f_des[0], line2, 40); 
    printf("%s\n",line2); 
    while(j < 40) { 
     if(islower(*(line3+j))) 
      *(line3+j) = toupper(*(line3+j)); 
     if(isupper(*(line2+j))) 
      *(line3+j) = tolower(*(line3+j)); 
     } 
    rc2 = write(C1C2[1], line3, 40); 
    printf("\nLine 3: %s", line3); 

    k++; 
} 
} 
} 

printf("\n"); 
close(C1C2[1]); 
close(f_des[0]); 
exit(0); 
} 

void do_child2() { 
int c; 
int rc; 
char *line4; 

close(C1C2[1]); 

while((rc= read(C1C2[0], line4, 40)>0)) { 
    line4 = (char*)malloc(sizeof(char)*40); 
    printf("%s\n", line4); 
} 

FILE *file; 
file = fopen("C:\\Users\\apkim_000\\Desktop\\newReluctance.c", "a+"); 
fprintf(file, "%s", line4); 
fclose(file); 




} 



void do_child0() { 
FILE *inf = fopen("C:\\MinGW\\bin\\Reluctance.c", "r"); 
int c; 
int rc; 
int i =0; 
char buffer[80]; 
char *line; 
cntr = 0; 
close(f_des[0]); 

printf("From child 1: \n"); 
while (1) { 
    fgets(buffer, 100, inf); //reads in at most 80 char from a line 
    if (feof(inf)) //this checks to see if the special EOF was read 
     break;  //if so, break out of while and continue with your main 
    line = (char*)malloc(sizeof(char)*40); 
    line = strtok(buffer, "\n"); 
    printf("%s\n", line); 
    i++; 
    cntr++; 
    write(f_des[1], buffer, 40); 
} 
close(f_des[1]); 

exit(0); 

} 

pipe.h:

#include <stdio.h> 
#include <stdlib.h> 
#include <windows.h> 


void do_child1(); 
void do_child2(); 
void do_child0(); 

int f_des[2]; 
int C1C2[2]; 
int cntr, p3; 

編譯器輸出:

create child 1 
From child 1: 
Reluctance 
OUT through the fields and the woods 
And over the walls I have wended; 
I have climbed the hills of view 
And looked at the world, and descended; 
I have come by the highway home, 
And lo, it is ended. 
The leaves are all dead on the ground, 
Save those that the oak is keeping 
To ravel them one by one 
And let them go scraping and creeping 
Out over the crusted snow, 
When others are sleeping. 
And the dead leaves lie huddled and still, 
No longer blown hither and thither; 
The last lone aster is gone; 
The flowers of the witch-hazel wither; 
The heart is still aching to seek, 
But the feet question 'Whither?' 
Ah, when to the heart of man 
Was it ever less than a treason 
To go with the drift of things, 
To yield with a grace to reason, 
And bow and accept the end 
create child 2 
Received by child 2 from child 1: RUT through the fields and the woods 
create child 3 

回答

0

我看到的第一件事情,但我並不太自信,因爲它密集的代碼就是y你應該只等待執行時間最長的孩子。如果你不知道哪一個孩子的過程是,你最好的辦法就是等待一個先決條件,或讓每個孩子創建下一個孩子,這樣父母只需要等待一個孩子。

是的,讓每個孩子都做好自己的工作,然後等待那個孩子,等到孩子退出時,主程序開始創建下一個孩子。沒有理由讓他們同時做這件事。

0

在擔心waitpid()之前,您應該照顧do_child1()do_child0()do_child1()之間的管道工作正常,但do_child1()中的管線緩衝區管理是一團糟。 do_child0()總是每行寫入40個字符,而do_child1()首先只讀取1個字符(「R」),然後是40個字符(它丟棄),然後是另外40個字符(從第二行的第二個字符開始),產生輸出「RUT通過田野和樹林」。然後輸入一個無限循環while(j < 40);因此,該程序不會繼續。

相關問題