please dont rip this site

Language Ccpp Cref Examples PIPE1.C

/ ****************************************************************
 *
 * Purpose: Basic example of pipe. 
 *          Read and write fixed length records across a pipe.
 *          This is about a simple as they come...
 *
 * Author:  M J Leslie
 *
 * Date:    17 Apr 96
 *
 ****************************************************************/

#include <sys/types.h>
#include <unistd.h>			/* pipe.		*/
#include <signal.h>

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

main()
{

  pid_t		Pid;
  int 		fd[2];

  pipe(fd);				/* Create two file descriptors 	*/

  Pid = fork();

  if ( Pid == 0)			/* Child			*/
  {
    close(fd[0]);
    Child(fd[1]);
    puts("Child end");
  }
  else					/* Parent.			*/
  {
    close(fd[1]);
    Parent(fd[0]);
    puts("Parent end");
  }
}

/****************************************************************
 *
 *      The Child sends data to the parent.
 *
 ****************************************************************/

void Child(pid_t Handle)
{
  char Buff[]="Martin 1 abcdefghijklmnop ";

  write(Handle, Buff, strlen(Buff)+1);

  Buff[7] = '2';
  write(Handle, Buff, strlen(Buff)+1);

  Buff[7] = '3';
  write(Handle, Buff, strlen(Buff)+1);

  Buff[7] = '4';
  write(Handle, Buff, strlen(Buff)+1);  
  
  close(Handle);
}

/****************************************************************
 *
 *      Read the data sent by the child.
 *
 ****************************************************************/


void Parent(pid_t Handle)
{

  char Buff[50];

  /* ...	Read EXACTLY the number of bytes sent. 
     ...	0 is returned when the pipe is closed by the child. */

  while (read(Handle,Buff, 27) > 0)
  {
    printf("%s\n", Buff);
  }
      
}







file: /Techref/language/ccpp/cref/EXAMPLES/pipe1.c, 1KB, , updated: 1997/4/21 09:52, local time: 2024/5/3 00:42,
TOP NEW HELP FIND: 
18.117.216.229:LOG IN

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF="http://www.piclist.com/techref/language/ccpp/cref/EXAMPLES/pipe1.c"> language ccpp cref EXAMPLES pipe1</A>

Did you find what you needed?

  PICList 2024 contributors:
o List host: MIT, Site host massmind.org, Top posters @none found
- Page Editors: James Newton, David Cary, and YOU!
* Roman Black of Black Robotics donates from sales of Linistep stepper controller kits.
* Ashley Roll of Digital Nemesis donates from sales of RCL-1 RS232 to TTL converters.
* Monthly Subscribers: Gregg Rew. on-going support is MOST appreciated!
* Contributors: Richard Seriani, Sr.
 

Welcome to www.piclist.com!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .