Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6568401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:28:23+00:00 2026-05-25T14:28:23+00:00

I have a program that takes two files as an argument. The first file

  • 0

I have a program that takes two files as an argument. The first file is to be copied into the second. The program forks into 2 children, the first child reads file and throws it thru the pipe to the other, then the other child writes it out to a file. The two files are supposed to be identical in the end.

When I run diff to compare the two files I get the following error:

virtual@ubuntu:~/Documents/OSprojects$ ./parent test.txt test2.txt
virtual@ubuntu:~/Documents/OSprojects$ cat test.txt
123456789112233445566778899
virtual@ubuntu:~/Documents/OSprojects$ cat test2.txt
123456789112233445566778899
virtual@ubuntu:~/Documents/OSprojects$ diff test.txt test2.txt
Binary files test.txt and test2.txt differ
virtual@ubuntu:~/Documents/OSprojects$ 

As you can see they are both the same, but the diff prints out that they are different. Obviously its just something I don’t understand about the diff cmd. Any help would be appreciated.

I believe for some reason the file I create is a binary file whereas the first file is not, but I am unaware as to why it is a binary file. I believe it might have to do with this line of code:

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

In one of the children this is writing out to the buffer then I am clearing the buffer. Am I clearing out that buffer wrong?

Here is the result of cat -e:

virtual@ubuntu:~/Documents/OSprojects$ cat -e test2.txt
123456789112233445566778899$
^@^@^@^@virtual@ubuntu:~/Documents/OSprojects$

Here is the result of cmp:

virtual@ubuntu:~/Documents/OSprojects$ cmp test.txt test2.txt
cmp: EOF on test.txt
virtual@ubuntu:~/Documents/OSprojects$ 

I believe that is my problem, how can I clear out that buffer so it doesn’t throw those at the end?

ALL OF MY CODE::

Parent:

#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");
}

Child 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);
}

Child 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);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T14:28:24+00:00Added an answer on May 25, 2026 at 2:28 pm

    You don’t check (and use) the read data length. Therefore, your data gets padded with garbage.

    There should be actual data bytes read (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
    -----------------------^^^^^^^^
    

    The same applies to the other child. You should also check the error conditions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a perl program that takes input and output file arguments, and I'd
Purpose: Create a program that takes two separate files, opens and reads them, assigns
I have a java program that generates an HTML file. The Java program takes
I have a program that sorts big files by splitting them into chunks, sort
I have been assigned wit the task to write a program that takes a
I have a program, that takes a long time to load. Because of this,
I have a file that has two columns of floating point values. I also
I'm working on a program that takes a redirected file as input. For example,
I have written a program which takes a file as input and whenever it
Looking for help on writing a Perl program that takes an input file and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.