2017-11-17 8 views
1

私が理解できないいくつかの問題を見つけたとき、私は練習を終えようとしていました。 最初の文字: システム(文字列)を使用してファイルをソートしようとしています。文字列には/usr/bin/sort -n -o%s%sが含まれています。 %sは、親からパイプを通して読み込んだパスです。正しい文字列を表示しています。 -o%sを削除するとすべて動作しているようです。私がそれを追加すると、プログラムは並べ替えに固執します。ここで コード:Unix - 子ブロックからソート

str=strcat(dir, str); 
printf(" %s  \n", str); 
sprintf(ssys, "/usr/bin/sort -n -o %s %s", str, str); 
printf("  1111 \n"); 
system(ssys); 
printf("  2222 \n"); 

、出力は次のとおりです。

/home/dio/Desktop/Operating-System-PoliTo/Lab6/ex2/texts/1.txt

ご覧のとおり、3番目のprintfは表示されませんが、ファイルがソートされていない場合でもCtrl + cを押してプロセスを強制終了すると表示されます。私はそれを実行し、psと私はソートのプロセスが実行されているので、私はそれを実行し、チェックする理由は理解できません。それは親が終了するのを待っていますが、とにかくソートしないようです。

これは全体のコードです:

#include <stdlib.h> 
#include <stdio.h> 
#include <unistd.h> 
#include <string.h> 
#include <sys/wait.h> 
#include <sys/types.h> 

static void sig_handler (int); 

static void sig_handler (int signo) 
{ 
     if (signo == SIGPIPE) 
       exit(0); 
} 

int main (int argc, char **argv) 
{ 

     signal(SIGPIPE, sig_handler); 

     if (argc<3) 
     { 
       printf("Too little arguments"); 
       return 1; 
     } 

     int c=atoi(argv[1]); 
     int *pids=malloc(sizeof(int)*c); 
     int i=0, num_char_str, id, line_sort, request_pipe[c][2], data_pipe[c][2], answer_pipe[c][2]; 
     pid_t pid; 
     char cR, cW; 
     char *dir=malloc(strlen(argv[2])+1); 
     char *str=malloc(strlen(argv[2])*10); 
     char *ssys=malloc(strlen(argv[2])*10); 
     FILE* fpp; 
     FILE* fp; 

     strcpy(dir, argv[2]); 
     sprintf(str, "ls %s > list.txt", dir); 
     system(str); 
     for(i=0; i<c; i++) 
     { 
       if (pipe(request_pipe[i]) == -1) 
         return 2; 
       if (pipe(data_pipe[i]) == -1) 
         return 2; 
       if (pipe(answer_pipe[i]) == -1) 
         return 2; 
     } 
     for(i=0; i<c; i++) 
     { 
       pid=fork(); 
       if(pid>0) 
         pids[i]=pid; 
       if (pid == -1) 
       { 
         printf("error forking"); 
         return 3; 
       } 
       if (pid==0) 
         break; 
     } 
    if (pid>0) /// parent 
     { 
       for(i=0; i<c; i++) 
       { 
         close(answer_pipe[i][1]); /// close answer pipe on writing 
         close(request_pipe[i][1]); /// close request pipe on writing 
         close(data_pipe[i][0]); /// close data pipe on reading 
       } 
       fp=fopen("list.txt", "r"); 
       if (fp==NULL) 
         return 3; 
       i=0; 
       while (fgets(str, sizeof(str), fp)!=NULL) 
       { 
         /// wait for ask 
         read(request_pipe[i][0], &cR, 1); 
         /// sending first the number of characters to read 
         /// and then the file name 
         num_char_str=(strlen(str) +1); 
         write(data_pipe[i][1], &num_char_str, sizeof(int)); 
         write(data_pipe[i][1], str, num_char_str); 
         /// read the answer 
         read(answer_pipe[i][0], &id, sizeof(int)); 
         //pids[i]=id; 
         read(answer_pipe[i][0], &line_sort, sizeof(int)); 
         if (i==c-1) 
           i=-1; 
         i++; 
       } 
       /// sending termination signal to children 
       for (i=0; i<c; i++) 
         kill(pids[i], SIGPIPE); 
       /// producing all_sorted.txt 
       strcpy(str, "touch all_sorted.txt"); 
       system(str); 
       sprintf(str, "find %s -name \"*.txt\" -exec cat \"{}\"\";\" > all_sorted.txt", dir); 
       system(str); 
       /// sorting all_sorted 

     } 
     if (pid == 0) /// child 
     { 
       close(answer_pipe[i][0]); /// close answer pipe on reading 
       close(request_pipe[i][0]); /// close request pipe on reading 
       close(data_pipe[i][1]); /// close data pipe on writing 
       while (1) 
       { 
         /// send request to parent 
         write(request_pipe[i][1], &cW, 1); 
         /// read length of string 
         read(data_pipe[i][0], &num_char_str, sizeof(int)); 
         /// read the file name 
         read(data_pipe[i][0], str, num_char_str); 
         /// sorting 
         str=strcat(dir, str); 
         sprintf(ssys, "/usr/bin/sort -n -o %s %s", str, str); 
printf("  1111 \n"); 
         system(ssys); 
printf("  2222 \n"); 
         /// sending back data 
         id=getpid(); 
         write(answer_pipe[i][1], &id, sizeof(int)); 
         //sprintf(str, "wc -l %s/%s", dir, str); 
         //fpp=popen(str, "r"); 
         //fscanf(fpp, "%d", &line_sort); 
         //pclose(fpp); 
         line_sort=1; 
         write(answer_pipe[i][1], &line_sort, sizeof(int)); 
       } 
     } 

     return 0; 

} 
+1

'sort'がEOFを待っているか、ストリームを閉じてストリームにパイプしています... – wally

+0

あなたの 'sort'がハングアップしていると思われるファイルを調べるべきです。それは普通のファイルですか?それは非常に大きいですか? hangingプロセスで 'strace -fp 'を呼び出して、どのコールが滞っているかを調べてみてください。 – Ctx

答えて

0

は、私はおよそstraceを知りませんでした、ありがとうございます。 Btwは、fgetsが読み込んで並べ替えの呼び出しにいくつかの問題を与えた "\ n"という問題でした