C/C++ on Linux/Unix

Moderators: Lundin
Number of threads: 336
Number of posts: 663

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
fork() && pipe() problem Posted by bushwacker on 15 Feb 2008 at 1:06 PM
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<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<wait.h>
#include<sstream>
#include<signal.h>

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\n",buf);
}
}

Any help would be deeply appreciated. Thanks.














Report
Re: fork() && pipe() problem Posted by itwasntme2004 on 21 Feb 2008 at 2:07 PM
: 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<stdio.h>
: #include<sys/types.h>
: #include<unistd.h>
: #include<wait.h>
: #include<sstream>
: #include<signal.h>
:
: 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\n",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!



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.