C++ (2)

💻 Programming

[C++] pipe() and fork() (파이프, 포크)

Pipes


  • Conceptually, a pipe is a connection between two processes, such that the standard output from one process becomes the standard input of the other process
  • It is possible to have a series of processes arranged in a a pipeline, with a pipe between each pair of processes in the series.
  • Implementation: A pipe can be implemented as a 10k buffer in main memory with 2 pointers, one for the FROM process and one for TO process
  • One process cannot read from the buffer until another has written to it
  • The UNIX command-line interpreter (e.g., csh) provide a pipe facility.
    • % prog | more
    • This command runs the prog1 program and send its output to the more program.

Pipe System Call

  • pipe() is a system call that facilitates inter-process communication. It opens a pipe, which is an area of main memory that is treated as a "virtual file". The pipe can be used by the creating process, as well as all its child processes, for reading and writing.
  • One process can write to this "virtual file" or pipe and another related process can read from it.
  • If a process tries to read before something is written to the pipe, the process is suspended until something is written.
  • The pipe system call finds the first two available positions in the process's open file table and allocates them for the read and write ends of the pipe. Recall that the open system call allocates only one position in the open file table.

Syntax in a C/C++ program:

 #include <unistd.h>
 int pip[2];
 (void) pipe(pip); 

With error checking:

 #include <unistd.h>
 int pip[2];
 int result;
 result = pipe(pip); 
 if (result == -1)
 {
  perror("pipe");
  exit(1);
 }

Programming notes:

  • The pipe() system call is passed a pointer to the beginning of an array of two integers.
  • It appears in C/C++ as if pipe() is passed the name of the array without any brackets.
  • The system call places two integers into this array. These integers are the file descriptors of the first two available locations in the open file table.
  • pip[0] - the read end of the pipe - is a file descriptor used to read from the pipe
  • pip[1] - the write end of the pipe - is a file descriptor used to write to the pipe

The buffer size for the pipe is fixed. Once it is full all further writes are blocked. Pipes work on a first in first out basis.



source from: http://www2.cs.uregina.ca/~hamilton/courses/330/notes/unix/pipes/pipes.html


( Tested on Linux/Cent OS 6.2 64 bit )

 

Let's say we have main function below....

 

int main(){
 First *fp = new First[10];          // creating array of First object
 delete [] fp;
return 0;
}

 

{If we have one of First classes below }// ??bytes of heap memory allocated


case 1:
class First{ double db; }// 8bytes * 10
class First{ int size;  }// 4bytes * 10
class First{ int *pt;  }// 8bytes * 10

 

case 2:
class First{ int size;
  int *pt;  }// 16 bytes * 10


class First{ int *pt;
  int size;  }// 16 bytes * 10

 

class First{ int size;
  double db; }// 16 bytes * 10


class First{ double db;
  int size;  }// 16 bytes * 10

 

case 3: 
class First{ int size;
  int size2;
  double db; }// 16 bytes * 10



 



 

class First{ double db;
  int size;
  int size2; }// 16 bytes * 10

 

 

 

class First{ int size;  
  double db;
  int size2; }// 24 bytes * 10

 

 

 

case 4:
class First{ int size;
  double db;
  int size2;
  char ch1; }// 24 bytes * 10

 





class First{ int size;
  double db;
  int size2;
  char ch1;
  char ch2;
  char ch3;
  char ch4; }// 24 bytes * 10

 





class First{ int size;
  double db;
  int size2;
  char ch1;
  char ch2;
  char ch3;
  char ch4; 
  char ch5; }// 32 bytes * 10