2011-09-10 19 views
1

2つのファイルを引数として取るプログラムがあります。最初のファイルは2番目のファイルにコピーされます。プログラムは2人の子供にフォークし、最初の子はファイルを読み取り、それをパイプを通して他の子にスローし、もう1人の子はそれをファイルに書き出します。 2つのファイルは最終的には同じものとされています。彼らは異なっていることをあなたは、彼らは両方とも同じに見ることができるようにdiff cmd、バイナリファイルxとyが異なる

[email protected]:~/Documents/OSprojects$ ./parent test.txt test2.txt 
[email protected]:~/Documents/OSprojects$ cat test.txt 
123456789112233445566778899 
[email protected]:~/Documents/OSprojects$ cat test2.txt 
123456789112233445566778899 
[email protected]:~/Documents/OSprojects$ diff test.txt test2.txt 
Binary files test.txt and test2.txt differ 
[email protected]:~/Documents/OSprojects$ 

が、差分のプリントアウト:

私は、私は次のエラーを取得する2つのファイルを比較するdiffを実行します。明らかに、私はdiff cmdについて理解していないだけのものです。どんな助けもありがとう。

私が作成するファイルはバイナリファイルですが、最初のファイルはありませんが、なぜバイナリファイルなのか分かりません。これは、私はバッファをクリアしていますバッファに書き出すている子の1つで

write(1, buf, BUF_SIZE);   //write to buffer 
    memset(buf, '\0', BUF_SIZE); 

:私はそれがこのコード行としなければならないかもしれないと考えています。そのバッファを間違ってクリアしていますか?ここで

は猫の-eの結果である:ここで

[email protected]:~/Documents/OSprojects$ cat -e test2.txt 
123456789112233445566778899$ 
^@^@^@^@[email protected]:~/Documents/OSprojects$ 

は、CMPの結果である:

[email protected]:~/Documents/OSprojects$ cmp test.txt test2.txt 
cmp: EOF on test.txt 
[email protected]:~/Documents/OSprojects$ 

私はそれが私の問題であると考えて、どのように私はそれように、そのバッファをクリアすることができます最後にそれらを投げない?

MY CODE :: OF ALL

親:

#include <unistd.h> 
#include <stdio.h> 
#include <errno.h> 
#include <stdlib.h> 
#include <sys/stat.h> 
#include <fcntl.h> 

#define BUF_SIZE 16 

void exitWithError(char* errorMsg, int exitWith);  //generic error out function 
void launch_writer(const char* pathname, char* const argv[], int pfd[]);  
void launch_reader(const char* pathname, char* const argv[], int pfd[]); 

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

//making the pipe 
int pfd[2]; 
if(pipe(pfd) == -1) //test pipe creation 
    exitWithError("PIPE FAILED", 1); 

//forking 
pid_t reader_child_pid; 
pid_t writer_child_pid; 

//args for each fork 
char *args_1[] = {"reader", argv[1], (char *) 0}; 
char *args_2[] = {"writer", argv[2], (char *) 0}; 

if((writer_child_pid = fork()) == -1) { 
    exitWithError("WRITER FORK FAILED", 1); 
} 
else if (writer_child_pid == 0) {  //first child comes here 
    launch_writer("./writer", args_2, pfd); 
} 
else if ((reader_child_pid = fork()) == -1) { 
    exitWithError("READER FORK FAILED", 1); 
} 
else if (reader_child_pid == 0) {  //second child comes here 
    launch_reader("./reader", args_1, pfd); 
} 

//parent picks up here 

//close off pipe from parents end 
close(pfd[0]); 
close(pfd[1]); 


//wait for all processes to exit before ending 
for(;;) { 
    if(wait(NULL) == -1){ 
     if(errno == ECHILD) 
      exit(0); 
     else { 
      exitWithError("WAIT ERROR", 1); 
     } 
    } 
} 
} 

void exitWithError(char* errorMsg, int exitWith) { 
perror(errorMsg); 
exit(exitWith); 
} 

void launch_writer(const char* pathname, char* const argv[], int pfd[]) { 
dup2(pfd[0], 0); 
close(pfd[1]); 
close(pfd[0]); 
execve(pathname, argv, NULL); 
perror("execve failed"); 
} 

void launch_reader(const char* pathname, char* const argv[], int pfd[]) { 
dup2(pfd[1], 1); 
close(pfd[1]); 
close(pfd[0]); 
execve(pathname, argv, NULL); 
perror("execve failed"); 
} 

子供1:

#include <unistd.h> 
#include <stdio.h> 
#include <sys/stat.h> 
#include <fcntl.h> 
#include <stdlib.h> 
#include <string.h> 

#define BUF_SIZE 16 

int main(int argc, char* argv[]){ 
//Opens file to be read from 
int inFile = open(argv[1], O_RDONLY); 

//declaring variables 
char buf[BUF_SIZE]; //temp hold whats read/written 
int read_test;  //check if EOF 
for(;;) { 
    read_test = read(inFile, buf, BUF_SIZE); //read from file 
    if(read_test == 0) //eof 
     break; 
    write(1, buf, BUF_SIZE);   //write to buffer 
    memset(buf, '\0', BUF_SIZE); 
} 
close(inFile); 
exit(0); 
} 

子供2:あなたはチェックしません

#include <unistd.h> 
#include <stdio.h> 
#include <sys/stat.h> 
#include <fcntl.h> 
#include <stdlib.h> 

#define BUF_SIZE 16 

int main(int argc, char* argv[]){ 
//Opens a file for reading/writing, if exists then truncates, otherwise makes new one 
//with correct permissions 
int wri_inFile = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC , S_IRUSR | S_IWUSR); 
if(wri_inFile == -1) 
    perror("ERROR OPENING FILE"); 

//declaring variables 
char buf[BUF_SIZE]; //to store what is read in/written out 
int read_test;  //test if EOF 
for(;;) { 
    read_test = read(0, buf, BUF_SIZE); //read from buffer 
    if(read_test == 0) //eof 
     break; 
    write(wri_inFile, buf, BUF_SIZE); //write to file 

} 
close(wri_inFile); 
exit(0); 
} 
+4

ファイルの16進ダンプを作成します。ファイルの先頭または末尾に文字や制御文字が見えないため、おそらく違います。 – GolezTrol

+2

'cat -e'を試して、印刷できない文字があるかどうか調べてください。私の賭けは、第2のものの最後に '\ 0'があるということです。 –

+0

'diff'ではなく' cmp'を使うとどうなりますか? –

答えて

1

(と使用してください)。したがって、データにガベージが埋め込まれます。

実際のデータがあるはずです(read_test)読み取ったバイト:

read_test = read(0, buf, BUF_SIZE); //read from buffer 
if(read_test == 0) //eof 
    break; 
write(wri_inFile, buf, BUF_SIZE); //write to file 
-----------------------^^^^^^^^ 

同じことが他の子にも適用されます。また、エラー状態を確認する必要があります。

+0

ありがとうございました。私はなぜ私がしたことをしたのか分からないが、あなたの提案はそれを直した。 Diffは両方のファイルが同じであることを表示します。再度、感謝します。 – forTruce

関連する問題