Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copying contents of a file using pipe() and fork()

Tags:

c

linux

Similar questions has already been asked, but their solutions aren't helping me much

Program that read file and send it to parent process with pipe

Read/writing on a pipe, accomplishing file copying in C


I'm trying to read from a file test.txt ( which contains a single line of text), write it to a pipe, from where a child process will read from the pipe and write the contents to another file.

 /* Read the contents of a file and display it using pipe */

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

void main()
{
  char buffer[100];
  char childbuff[100];
  int fd[2], des, bytes, target;

  pipe(fd);

  if(fork()) {
    /* parent process closes the downstream */
    close(fd[0]);

    /* reads the file */
    des = open("test.txt", O_RDONLY);
    bytes = read(des, buffer, sizeof(buffer));

    /* puts data in pipe */
    write(fd[1], buffer, bytes);
  } else {
    /* Child process closes the upstream */
    close(fd[1]);

    /* reads from the pipe */
    read(fd[0], childbuff, sizeof(childbuff));
    close(fd[0]);

    /* output the received string */
    printf("\nReceived string is -- %s", childbuff);
    target = open("copy.txt", O_CREAT, 00777);
    write(target, childbuff, (strlen(childbuff)-1));
  }
}

Problem is printf() prints the string on terminal, a file named copy.txt also gets created, but nothing is getting copied to it (it seems there is a problem with the write() function )

however, if I change

write(target, childbuff, (strlen(childbuff)-1));

to

write(1, childbuff, (strlen(childbuff)-1));

string is simply getting written on my terminal.

So what possibly am I doing wrong while writing to file?

like image 838
byteseeker Avatar asked Dec 17 '15 07:12

byteseeker


People also ask

What is pipe in file?

A named pipe can last until as long as the system is up and running or until it is deleted. It is a special file that follows the FIFO (first in, first out) mechanism. It can be used just like a normal file; i.e., you can write to it, read from it, and open or close it.

Why use pipe in C?

pipe() is used for passing information from one process to another. pipe() is unidirectional therefore, for two-way communication between processes, two pipes can be set up, one for each direction.

What is a pipe in processes?

A pipe between two processes is a pair of files that is created in a parent process. The pipe connects the resulting processes when the parent process forks. A pipe has no existence in any file name space, so it is said to be anonymous.


1 Answers

You also need O_WRONLY to write to the file:

target = open("copy.txt", O_CREAT |O_WRONLY, 00777);

Note that you can't use strlen() or %s to print it as a C-string. read(2) doesn't return a NUL terminated string.

Instead get the number of bytes read from read() and use it in write():

    ssize_t num_bytes = read(fd[0], childbuff, sizeof(childbuff));

    write(target, childbuff, num_bytes);

You should check the return of all system calls for failure.

like image 140
P.P Avatar answered Oct 03 '22 08:10

P.P