Hi there,
I need to:
1) Create a pipe, read data from a variable. Write this data to the pipe. Send a message two file2 that pipe is ready to be read. All this in one file
2)Another file which will display message "reading from pipe". Reads until it's done.
to achieve this what I am trying to do is:
create global vars open/close ends of the pipe for writing. Now I know I am wrong here cuz it gives me an error on compilation
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
extern int fd[2], nbytes;
extern pid_t childpid;
extern char string[] = "Hello, world!\n";
extern char readbuffer[80];
main(void)
{
pipe(fd);
if((childpid = fork()) == -1)
{
perror("fork");
exit(1);
}
if(childpid == 0)
{
/* Child process closes up input side of pipe */
close(fd[0]);
/* Send "string" through the output side of pipe */
write(fd[1], string, strlen(string));
exit(0);
}
else
{
/* Parent process closes up output side of pipe */
close(fd[1]);
}
}
From another file I would like to read from the pipe. Now I don't know how to do this:
It'll be something like this but don't know if it'll work.
nbytes = read(fd[0], readbuffer, sizeof(readbuffer));
printf("Received string: %s", readbuffer);
The idea behind doing this is that if I can read from a var, write to a pipe and read from pipe. Then I can go ahead with the "actual" program. Note: I could have done this by forking all in one file but here I need to show the output i.e. "sending...." "receiving...." in different shells thats why I have to have to separate files
Any help is appreciated.
Thanks
I need to:
1) Create a pipe, read data from a variable. Write this data to the pipe. Send a message two file2 that pipe is ready to be read. All this in one file
2)Another file which will display message "reading from pipe". Reads until it's done.
to achieve this what I am trying to do is:
create global vars open/close ends of the pipe for writing. Now I know I am wrong here cuz it gives me an error on compilation
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
extern int fd[2], nbytes;
extern pid_t childpid;
extern char string[] = "Hello, world!\n";
extern char readbuffer[80];
main(void)
{
pipe(fd);
if((childpid = fork()) == -1)
{
perror("fork");
exit(1);
}
if(childpid == 0)
{
/* Child process closes up input side of pipe */
close(fd[0]);
/* Send "string" through the output side of pipe */
write(fd[1], string, strlen(string));
exit(0);
}
else
{
/* Parent process closes up output side of pipe */
close(fd[1]);
}
}
From another file I would like to read from the pipe. Now I don't know how to do this:
It'll be something like this but don't know if it'll work.
nbytes = read(fd[0], readbuffer, sizeof(readbuffer));
printf("Received string: %s", readbuffer);
The idea behind doing this is that if I can read from a var, write to a pipe and read from pipe. Then I can go ahead with the "actual" program. Note: I could have done this by forking all in one file but here I need to show the output i.e. "sending...." "receiving...." in different shells thats why I have to have to separate files
Any help is appreciated.
Thanks