2013-06-21 37 views
0

我正在做一個項目,我必須使用fork創建一個子進程,然後父進程告訴子進程執行另一個C程序「read.c」(它從.txt文件讀取所有整數並計算平均值)通過使用execve,然後我必須通過管道將該平均值發送給父進程。我不知道如何在「process.c」程序的子進程中獲得「read.c」程序的結果的「平均值」。一些可能的朋友說,我必須將用於管道(pfd [2])的文件描述符傳遞給execve,並從其他程序(read.c)中傳遞,我必須使用管道將數據寫入到process.c程序中。但我不知道該怎麼做,所以不能正常工作。我發佈了process.c和read.c的代碼,並告訴我應該做出哪些更改以使其性能良好。如何使用execve和pipe從讀取程序到主程序的數據?

process.c

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

main() 
{ 

    int pid; 
    int pfd[2]; 
    int i; 
    char string1[12]; 
    snprintf(string1,12,"%i",pfd[1]); 

    char *args[] = {"read",&string1[0],NULL}; 

    pipe(pfd); 
    pid = fork(); 

    if(pid == 0) 
    { 

     execve("read",args,NULL); 

     int size = 100; 
     char buf[size]; 

     close(pfd[1]); 
     read(pfd[0],buf,size); 
     printf("%s\n",buf); 

    } 

} 

read.c

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


int main (int argc,char *argv[],char *envp[]) 
{ 

    int pfd[2]; 
    pfd[1]= atoi(argv[1]); 
    close(pfd[0]); 

    FILE *ptr_file; 
    char buf[1000]; 
    int sum = 0; 
    int avg =0; 
    int no = 0; 
    int count = 0; 

    ptr_file =fopen("data.txt","r"); 
    if (!ptr_file) 
     return 1; 

    while (fgets(buf,1000, ptr_file)!=NULL) 
    { 
     printf(" %s \n",buf); 
     no = atoi(buf); 
     sum = sum + no; 
     printf("Sum = %d \n", sum); 
     count++; 
    } 

    fclose(ptr_file); 
    avg = sum/count; 
    printf("Average : %d \n",avg); 
    write(pfd[1],"hello",6);/*i just write here hello to see that it is working or not*/ 
    return 0; 
} 

如果您有任何解決方案,使從read.c文件process.c的子進程輸出,那麼請告訴我。

+0

這有什麼錯將它們作爲兩個字符串? – 2013-06-21 17:55:18

+0

作業作業... – devnull

+0

@ H2CO3我不知道如何傳遞整數數組作爲一個string.char * args [] = {「讀取」,(char *)pfd,NULL};我試過這個,所以它不給我任何警告,但在read.c我沒有得到我所需要的。 – TusharU

回答

0

您的「read.c」應該將結果打印到標準輸出。所以,打開文件並按照你的計算結果。然後使用printf()打印結果。這可以從命令行運行,輸出應該發送到終端。

printf("%f\n", result); 
close (1); // Just in case, Making sure to flush for pipe 

這是建立父母與子女之間的管道的代碼。我認爲這應該有助於解決您的項目。

process.c的剪斷

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

int main (int argc, char *argv[], char *envp[]) 
{ 
    int fd[2], pid; 

    pipe (fd); 
    pid = fork() 

    if (pid == 0) 
    { 
     /* Child process */ 
     close (1);  // Close STDOUT. 
     dup(fd[1]);  // STDOUT will be fd[1] 
     close (0);  // Close STDIN, Don't need this. 
     close (2);  // Close STDERR, Don't need this. 
     close (fd[0]); // Don't need this. 
     close (fd[1]); // Don't need this. 

     execvpe ("./read", argv, envp); // Choose your exec() version 

     /* exec() family of calls never returns. Process reed executes here. */ 
    } 
    else 
    { 
     /* Parent process executes here */ 

     char chr[256]; 
     float average = 0; 
     int ret = 0; 

     /* Parent process. Making it very simple without any dups etc. */ 
     /* No error checks are performed... */ 
     /* You may experiment with enhancements here!!! */ 

     ret = read (fd[0], chr, 255); // Blocking Read syscall 
     if (ret <= 0) 
     { 
      printf ("Error or Nothing is read. Debug the problem!!!\n"); 
      exit (1); 
     } 

     chr[ret] = '\0'; 
     printf ("Average from read Child process[PID is %d] = %s\n", pid, chr); 
    } 
} 
相關問題