0% found this document useful (0 votes)
2 views

IPC_Mechanisms_Windows

The document outlines various Inter-Process Communication (IPC) mechanisms in Windows using C programs, including unnamed pipes, named pipes, shared memory, and message queues. Each section provides an aim, algorithm, and code examples for implementing these IPC methods. The document demonstrates how data can be exchanged between parent-child processes and separate processes using different IPC techniques.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

IPC_Mechanisms_Windows

The document outlines various Inter-Process Communication (IPC) mechanisms in Windows using C programs, including unnamed pipes, named pipes, shared memory, and message queues. Each section provides an aim, algorithm, and code examples for implementing these IPC methods. The document demonstrates how data can be exchanged between parent-child processes and separate processes using different IPC techniques.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

A.

ANAND
MRITS_CSE

Lab _OS

Week 7 &8:IPC Mechanisms in Windows - C Programs

a) Unnamed Pipes
• Aim: To demonstrate IPC using unnamed pipes between parent and child processes in
Windows.
• Algorithm:

1. Use CreatePipe() to create a read and write pipe.


2. Set security attributes so the handles can be inherited.
3. Use CreateProcess() to start a child process that inherits the pipe.
4. Parent writes data to the pipe, and child reads it.

parent_pipe.c

#include <windows.h>
#include <stdio.h>

int main() {
HANDLE hRead, hWrite;
SECURITY_ATTRIBUTES sa = {sizeof(SECURITY_ATTRIBUTES), NULL, TRUE};
PROCESS_INFORMATION pi;
STARTUPINFO si = {sizeof(STARTUPINFO)};
char buffer[] = "Hello from Parent Process";
char commandLine[] = "child_pipe.exe";

if (!CreatePipe(&hRead, &hWrite, &sa, 0)) {


printf("Failed to create pipe.\n");
return 1;
}

si.dwFlags = STARTF_USESTDHANDLES;
si.hStdInput = hRead;
si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
si.hStdError = GetStdHandle(STD_ERROR_HANDLE);

if (!CreateProcess(NULL, commandLine, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi)) {


A.ANAND
MRITS_CSE

printf("Failed to create child process.\n");


return 1;
}

DWORD written;
WriteFile(hWrite, buffer, sizeof(buffer), &written, NULL);

CloseHandle(hWrite);
CloseHandle(hRead);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);

return 0;
}

child_pipe.c

#include <windows.h>
#include <stdio.h>

int main() {
char buffer[100];
DWORD bytesRead;

ReadFile(GetStdHandle(STD_INPUT_HANDLE), buffer, sizeof(buffer), &bytesRead, NULL);


buffer[bytesRead] = '\0';

printf("Child received: %s\n", buffer);


return 0;
}

Output:

Child received: Hello from Parent Process


A.ANAND
MRITS_CSE

b) Named Pipes (FIFO Equivalent)


• Aim: To illustrate IPC using named pipes (FIFOs) between two separate processes in
Windows.
• Algorithm:

5. Server creates a named pipe using CreateNamedPipe().


6. Server waits for a client connection using ConnectNamedPipe().
7. Client connects using CreateFile().
8. Data is exchanged using WriteFile() and ReadFile().

fifo_server.c

#include <windows.h>
#include <stdio.h>

int main() {
HANDLE hPipe;
char buffer[1024];
DWORD bytesRead;

hPipe = CreateNamedPipe(TEXT("\\.\pipe\MyNamedPipe"),
PIPE_ACCESS_INBOUND, PIPE_TYPE_BYTE | PIPE_WAIT,
1, 1024, 1024, 0, NULL);

printf("Waiting for client...\n");


ConnectNamedPipe(hPipe, NULL);

ReadFile(hPipe, buffer, sizeof(buffer), &bytesRead, NULL);


buffer[bytesRead] = '\0';

printf("Received: %s\n", buffer);


CloseHandle(hPipe);
return 0;
}
A.ANAND
MRITS_CSE

fifo_client.c

#include <windows.h>
#include <stdio.h>

int main() {
HANDLE hPipe;
DWORD bytesWritten;

hPipe = CreateFile(TEXT("\\.\pipe\MyNamedPipe"), GENERIC_WRITE, 0, NULL,


OPEN_EXISTING, 0, NULL);
if (hPipe == INVALID_HANDLE_VALUE) {
printf("Failed to connect to pipe.\n");
return 1;
}

WriteFile(hPipe, "Hello from Client", 18, &bytesWritten, NULL);


CloseHandle(hPipe);
return 0;
}

Output:

Server: Received: Hello from Client


A.ANAND
MRITS_CSE

c) Shared Memory (Memory-Mapped Files)


• Aim: To demonstrate IPC using shared memory (memory-mapped files) between two
processes.
• Algorithm:

9. Create a shared memory object using CreateFileMapping().


10. Write process maps the object and writes data.
11. Read process opens the object and reads data.
12. Both processes unmap the memory and close the handle.

shm_writer.c

#include <windows.h>
#include <stdio.h>
#include <string.h>

int main() {
HANDLE hMapFile;
LPCTSTR pBuf;

hMapFile = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 256,


TEXT("MySharedMemory"));
if (hMapFile == NULL) {
printf("Unable to create shared memory\n");
return 1;
}

pBuf = (LPTSTR) MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, 256);


CopyMemory((PVOID)pBuf, "Hello Shared Memory", strlen("Hello Shared Memory") + 1);

printf("Writer: Data written\n");


getchar(); // Pause to keep memory mapped

UnmapViewOfFile(pBuf);
CloseHandle(hMapFile);
return 0;
}
A.ANAND
MRITS_CSE

shm_reader.c

#include <windows.h>
#include <stdio.h>

int main() {
HANDLE hMapFile;
LPCTSTR pBuf;

hMapFile = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, TEXT("MySharedMemory"));


if (hMapFile == NULL) {
printf("Unable to open shared memory\n");
return 1;
}

pBuf = (LPTSTR) MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, 256);


printf("Reader: Data read = %s\n", pBuf);

UnmapViewOfFile(pBuf);
CloseHandle(hMapFile);
return 0;
}

Output:

Reader: Data read = Hello Shared Memory


A.ANAND
MRITS_CSE

d.Message Queues (Windows)


• Aim: To simulate IPC using message queues in Windows. As Windows does not support
System V-style message queues directly in C, we can simulate message queues using files or
named pipes, or use Windows Messages between GUI applications.
• Algorithm:

1Sender creates or opens a file and writes messages to it (simulate message send).
2Receiver opens the same file and reads messages (simulate message receive).
3Synchronization or queue logic can be handled via file locks or using higher-level APIs like
PostMessage between GUI apps.

sender.c

#include <stdio.h>
#include <stdlib.h>

int main() {
FILE *fptr = fopen("msgqueue.txt", "w");
if (fptr == NULL) {
printf("Unable to open file.\n");
return 1;
}
fprintf(fptr, "Hello from sender process!\n");
fclose(fptr);
printf("Message sent.\n");
return 0;
}

receiver.c

#include <stdio.h>
#include <stdlib.h>

int main() {
char buffer[256];
FILE *fptr = fopen("msgqueue.txt", "r");
if (fptr == NULL) {
printf("Unable to open file.\n");
A.ANAND
MRITS_CSE

return 1;
}
fgets(buffer, sizeof(buffer), fptr);
printf("Message received: %s", buffer);
fclose(fptr);
return 0;
}

You might also like