Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

write data to a pipe and read from pipes 1

Status
Not open for further replies.

adev111

Programmer
Jul 8, 2004
44
0
0
CA
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
 
Yes it is. Nobody is being asked to "do" it but only steer me in the right direction.
 
Please use the [tt][ignore]
Code:
[/ignore][/tt]
tags when posting code.

You're missing the vital call to pipe() to create the pipe in the first place.

--
 
Thanks for the pointers Salem.

For the pipe() call, i do

Code:
          ...
          pipe(fd);
          .....
.

Isn't that enough? where fd has the two ends of the pipe. i.e. 0 and 1

 
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>

#define PIPE_RD 0
#define PIPE_WR 1

int main(void)
{
  int     fd[2];
  pid_t   childpid;
  char    string[] = "Hello, world!\n";
  char    readbuffer[80];

  pipe(fd);

  if((childpid = fork()) == -1)
  {
    perror("fork");
    exit(1);
  }

  if(childpid == 0)
  {
    /* Child process closes up input side of pipe */
    close(fd[PIPE_RD]);

    /* make the other end of the pipe stdout */
    close(STDOUT_FILENO);
    dup(fd[PIPE_WR]);

    /* Send "string" through the output side of pipe */
    write(STDOUT_FILENO, string, strlen(string));
    exit(0);
  }
  else
  {
    ssize_t nbytes;
    /* Parent process closes up output side of pipe */
    close(fd[PIPE_WR]);

    /* make the other end of the pipe stdin */
    close(STDIN_FILENO);
    dup(fd[PIPE_RD]);

    nbytes = read(STDIN_FILENO, readbuffer, sizeof readbuffer );
    printf( "Received %ld\n", (long)nbytes );
    if ( nbytes > 0 ) {
      readbuffer[nbytes] = '\0';
      printf( "Msg=%s", readbuffer );
    }
  }

  return 0;
}

> Then I can go ahead with the "actual" program.
Replace the read() and write() calls with execl() calls to run the programs you want connected by the pipe.
Such programs will see the ends of the pipe as stdin and stdout.

--
 
Thanks for the heads up salem. I will be building a program that simulates behaviour similar to a router. I won't be dealing with real addresses but reading addressing info from text files and have different tables for all the links in the network. I think this post will serve as the base for my work. Will keep the post updated!
 
So in continuation with the current thread, here is what I am doing.

Sending messages from routers B, A and C to Networks 54 and 02. And update routing tables.

Consider this scenario
B <--> NET54 <--> A <--> NET02 <-->C

Initial Routing table for A

Network HopCount NextHop
NET02 1 --
NET54 1 --

Initial Routing table for B

Network HopCount NextHop
NET54 1 --

Initial Routing table for C

Network HopCount NextHop
NET02 1 --

All messages are communicating one at a time.

So B sends a message to A -- Update their routing tables
A sends a message to C -- Update their routing tables

All these routing tables are in 3 separate files
When B sends a message to A the routing table for B becomes

Network HopCount NextHop
NET54 2 --
i.e. add 1 to the count

after that I update the routing table for A which after applying my logic becomes
Network HopCount NextHop
NET02 1 --
NET54 1 B

A sends message to C
A
Network HopCount NextHop
NET02 2 B
NET54 2 --

C
Network HopCount NextHop
NET54 2 B
NET02 2 A

A sends message to B
Table for A
Network HopCount NextHop
NET54 3 B
NET02 3 --

Table for B is
Network HopCount NextHop
NET54 2 A
NET02 3 A

C sends message to A

Table for C becomes
Network HopCount NextHop
NET54 3 B
NET02 3 A

Table for A becomes
Network HopCount NextHop
NET54 3 B
NET02 1 C

So the challenges that I face here are:
1) When B sends message to A I have to increment the hop count in table for B
and leave third column as --
2)When A receives a message from B it checks if the line one it it's own table is the same as

line 1 from the original table for B. If it is no change and make the third column name as "B"
Then when its done reading the rows from table B it leaves the rest of the rows in table A as is.

I need to figure out how could I get the two tables to be updated and once I update it I have to

continue sending messages i.e. A--> C, A-->B, C-->A

The way I was thinking to do this was using salems idea as the base:
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>

#define PIPE_RD 0
#define PIPE_WR 1

int main(void)
{
  int fds[2];
pipe(fds);
if(fork()){
/update the table for router B i.e. increment hop counts */
/* send message to A */
dup2(fds[1],STDOUT_FILENO);
/* got message from B. Facilitate a way to send in "B" */

execlp("test2","test2",NULL);
return 0;
}

/*test2.c*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>

#define PIPE_RD 0
#define PIPE_WR 1

main(void)
{
in here I need to check table for B and compare it's entries to the ones in table for A.
Increment them or modify them as needed

once that is done I need to send a message from A to C
so again execl or something
Basically behaviour similar to the previous one
}
I understand the concept but my weakness is the syntax. Can Somebody tell me how I could do this?

Thank You
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top