fork() && pipe() problem

Hi All. I am devloping a program that will spawn a child process that will perform the task of reading characters from a text file one char at a time, and piping the data back to the parent process to be printed to the screen. This is what I have so far.

#include
#include
#include
#include
#include
#include

void Child(pid_t Handle)
void Parent(pid_t Handle)

main()
{
pid_t pid;
int fd[2];
pipe(fd);

pid = fork();

if(pid == 0)
{
close(fd[0]);
Child(fd[1]);
puts("Child process complete");
}
else
{
close(fd[1]);
Parent(fd[0]);
puts("Parent process complete");
}
}

void Child(pid_t Handle)
{
char buf[25];
FILE *ptr;

ptr = fopen("test.txt","r");


/*********** problem starts here *****************/
/* when I comment out the write statement and replace it with a */
/* printf("%c",c) the program outputs the text file. But the */
/* but the write statement does not work properly. */

write(Handle,buf,strlen(buf));
close(Handle);
}

void Parent(pid_t Handle)
{
char buf[25];
while(read(Handle,buf,25)>0)
{
printf("%s
",buf);
}
}

Any help would be deeply appreciated. Thanks.














Comments

  • : Hi All. I am devloping a program that will spawn a child process
    : that will perform the task of reading characters from a text file
    : one char at a time, and piping the data back to the parent process
    : to be printed to the screen. This is what I have so far.
    :
    : #include
    : #include
    : #include
    : #include
    : #include
    : #include
    :
    : void Child(pid_t Handle)
    : void Parent(pid_t Handle)
    :
    : main()
    : {
    : pid_t pid;
    : int fd[2];
    : pipe(fd);
    :
    : pid = fork();
    :
    : if(pid == 0)
    : {
    : close(fd[0]);
    : Child(fd[1]);
    : puts("Child process complete");
    : }
    : else
    : {
    : close(fd[1]);
    : Parent(fd[0]);
    : puts("Parent process complete");
    : }
    : }
    :
    : void Child(pid_t Handle)
    : {
    : char buf[25];
    : FILE *ptr;
    :
    : ptr = fopen("test.txt","r");
    :
    :
    : /*********** problem starts here *****************/
    : /* when I comment out the write statement and replace it with a */
    : /* printf("%c",c) the program outputs the text file. But the */
    : /* but the write statement does not work properly. */
    :
    : write(Handle,buf,strlen(buf));
    : close(Handle);
    : }
    :
    : void Parent(pid_t Handle)
    : {
    : char buf[25];
    : while(read(Handle,buf,25)>0)
    : {
    : printf("%s
    ",buf);
    : }
    : }
    :
    : Any help would be deeply appreciated. Thanks.
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    :
    She expressly said to read a "character-at-a-time"
    Your write statement is trying to write 25 chars at a time

    also, you do not need to be passing the pipe "pencil" in a function header, just declare it and use it (so write could be write(fd[1],.....)

    If you need further help, you need to post the error you are having. Thanks.

    See you tomorrow!
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion