Hi!
I want to fork 2 processes,get their process ids n write into a pipe n read from it.
This is the first thing i want to do.
After this is done,i want to modify the program so that a child process reads from a text file,writes it in to the pipe.Then another child process reads it.For
example:
aaaa 100 200
My code in C :
#include<stdio.h>
#include <stdlib.h>
#include<unistd.h>
void do_write(int fds[])
{
int c;
int rc;
while ((c = getchar()) > 0)
{ /* write the character to the pipe. */
rc = write(fds[1], &c, 1);
if (rc == -1) { /* write failed - notify the user and exit */
perror("Parent: write");
close(fds[1]);
exit(1);
}
}
}
void do_read(int fds[])
{
int c;
int rc;
while ((rc = read(fds[0], &c, 1)) > 0)
/* probably pipe was broken, or got EOF via the pipe. */
exit(0);
}
main()
{
int fds[2];
pipe(fds);
if (fork()==0){
dup2(fds[1],1);
close(fds[0]);
printf("%d",getpid());
do_write(fds);
}
if (fork()==0){
dup2(fds[0],0);
close(fds[1]);
printf("%d",getpid());
do_read(fds);
}
else{
close(fds[0]);
close(fds[1]);
wait(0);
wait(0);
}
}
it is just not givin me the desired output.I am a begginner!