[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Hjärn-släpp ??



Hej !

Sitter och leker lite med pipes osv. Hittade ett litet exempel i c som är rätt så simpelt.
Försökte konvertera det till C++ (skall ha det i ett program senare), men tyvärr så verkar
det som att min hjärna har gått hem och lagt sig eller så glömde jag ta den med mig i morse.

Skulle någon vilja förklara för mig varför mitt C++ exempel inte fungerar ??

Bifoar båda källkoderna.
-------------- pipe.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>
#include <stdio.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);
  }
}

Min C++ variant.
---------- pipetest.cpp -------

/****************************************************************
 * Purpose: Basic example of pipe. 
 *          Read and write fixed length records across a pipe.
 *          This is about a simple as they come...
 * Author:  Joachim Holst
 ****************************************************************/

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

class Class1 {
public:
  Class1(int fd[2]) { 
    if(close(fd[0]))
      printf("FD[0] is closed.\n");
  }

  void Write(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);
    printf("Class1 done\n");
  }  
};

class Class2 {
  char Buff[50];
public:
  Class2(int fd[2]){ close(fd[1]); }
  void Read(pid_t Handle){
    /* ...	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);
    }
    printf("Class2 done\n");
  }
};

main()
{
  int fd[2];
  if(-1 == pipe(fd)){ /* Create two file descriptors 	*/
    printf("Couldn't create pipe.\n");
    exit(0);
  }
  Class1 c1(fd);
  Class2 c2(fd);
  c1.Write(fd[1]);
  c2.Read(fd[0]);
}


/Jocke!



 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 20:08 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *