Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Windows Anonymous Pipe
Windows anonymous pipes are unidirectional communication channels that enable data transfer between parent and child processes. They behave similarly to UNIX pipes but use Windows-specific APIs. Reading and writing operations are performed using standard ReadFile() and WriteFile() functions.
Syntax
BOOL CreatePipe(
PHANDLE hReadPipe, // Read handle
PHANDLE hWritePipe, // Write handle
LPSECURITY_ATTRIBUTES sa, // Security attributes
DWORD nSize // Buffer size
);
Parameters
The CreatePipe() function requires four parameters −
- hReadPipe − Pointer to receive the read handle
- hWritePipe − Pointer to receive the write handle
- sa − Security attributes structure for handle inheritance
- nSize − Buffer size in bytes (0 for default size)
Key Concepts
Unlike UNIX systems, Windows requires explicit specification of which attributes the child process inherits. This involves −
- Initializing
SECURITY_ATTRIBUTESstructure for handle inheritance - Redirecting child's standard input/output to pipe handles
- Preventing inheritance of unused pipe ends (half-duplex requirement)
Example: Parent Process
The following example demonstrates a parent process creating an anonymous pipe to communicate with its child −
Note: This example requires Windows environment and cannot be executed in online compilers. It demonstrates the concept for educational purposes.
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#define BUFFER_SIZE 25
int main() {
HANDLE ReadHandle, WriteHandle;
STARTUPINFO si;
PROCESS_INFORMATION pi;
char message[BUFFER_SIZE] = "Greetings";
DWORD written;
/* Set up security attributes to allow pipes to be inherited */
SECURITY_ATTRIBUTES sa = {sizeof(SECURITY_ATTRIBUTES), NULL, TRUE};
/* Initialize memory */
ZeroMemory(&pi, sizeof(pi));
/* Create the pipe */
if (!CreatePipe(&ReadHandle, &WriteHandle, &sa, 0)) {
fprintf(stderr, "Create Pipe Failed");
return 1;
}
/* Establish STARTUPINFO structure for child process */
GetStartupInfo(&si);
si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
/* Redirect standard input to read end of pipe */
si.hStdInput = ReadHandle;
si.dwFlags = STARTF_USESTDHANDLES;
/* Prevent child from inheriting write end of pipe */
SetHandleInformation(WriteHandle, HANDLE_FLAG_INHERIT, 0);
/* Create child process */
CreateProcess(NULL, "child.exe", NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi);
/* Close unused read end */
CloseHandle(ReadHandle);
/* Parent writes to pipe */
if (!WriteFile(WriteHandle, message, BUFFER_SIZE, &written, NULL))
fprintf(stderr, "Error writing to pipe.");
/* Clean up */
CloseHandle(WriteHandle);
WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return 0;
}
Example: Child Process
The child process reads from the pipe using the inherited read handle −
#include <stdio.h>
#include <windows.h>
#define BUFFER_SIZE 25
int main() {
HANDLE ReadHandle;
CHAR buffer[BUFFER_SIZE];
DWORD read;
/* Get read handle of the pipe */
ReadHandle = GetStdHandle(STD_INPUT_HANDLE);
/* Child reads from pipe */
if (ReadFile(ReadHandle, buffer, BUFFER_SIZE, &read, NULL))
printf("Child read: %s", buffer);
else
fprintf(stderr, "Error reading from pipe");
return 0;
}
How It Works
Key Points
- Windows anonymous pipes are half-duplex − data flows in one direction only
- Parent and child must close unused pipe ends to prevent blocking
- Handle inheritance must be explicitly configured using
SECURITY_ATTRIBUTES - Use
WaitForSingleObject()to synchronize parent with child completion
Conclusion
Windows anonymous pipes provide efficient inter-process communication between parent and child processes. They require careful handle management and explicit inheritance configuration but offer reliable unidirectional data transfer using standard Windows file APIs.
