-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path16.c
More file actions
118 lines (99 loc) · 3.49 KB
/
16.c
File metadata and controls
118 lines (99 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
============================================================================
Name : 16
Author : Piyush Singh
Description : Write a program to send and receive data from parent to child vice versa. Use two way
communication.
Date: 19th Sep, 2025.
============================================================================
*/
#include <stdio.h> // For printf(), perror()
#include <stdlib.h> // For exit()
#include <unistd.h> // For pipe(), fork(), read(), write()
#include <string.h> // For strlen()
#include <sys/wait.h> // For wait()
int main()
{
int pipe_parent_to_child[2]; // Pipe 1: parent writes, child reads
int pipe_child_to_parent[2]; // Pipe 2: child writes, parent reads
pid_t pid;
char parent_msg[] = "👨👦 Hi child, how are you?";
char child_msg[] = "👶 I'm good, thanks for asking!";
char buffer[100];
// Step 1: Create both pipes
if (pipe(pipe_parent_to_child) == -1 || pipe(pipe_child_to_parent) == -1)
{
perror("❌ Failed to create pipes");
exit(1);
}
// Step 2: Fork the process
pid = fork();
if (pid < 0)
{
perror("❌ Fork failed");
exit(1);
}
if (pid == 0)
{
// 👶 Child process
// Close unused ends
close(pipe_parent_to_child[1]); // Child doesn't write to parent pipe
close(pipe_child_to_parent[0]); // Child doesn't read from its own pipe
// Step 3a: Read message from parent
ssize_t bytes_read = read(pipe_parent_to_child[0], buffer, sizeof(buffer));
if (bytes_read < 0)
{
perror("❌ Child failed to read");
exit(1);
}
buffer[bytes_read] = '\0'; // Null-terminate
printf("👶 Child received: %s\n", buffer);
// Step 3b: Send reply to parent
ssize_t bytes_written = write(pipe_child_to_parent[1], child_msg, strlen(child_msg));
if (bytes_written < 0)
{
perror("❌ Child failed to write");
exit(1);
}
// Close used ends
close(pipe_parent_to_child[0]);
close(pipe_child_to_parent[1]);
}
else
{
// 👨👦 Parent process
// Close unused ends
close(pipe_parent_to_child[0]); // Parent doesn't read from its own pipe
close(pipe_child_to_parent[1]); // Parent doesn't write to child pipe
// Step 4a: Send message to child
ssize_t bytes_written = write(pipe_parent_to_child[1], parent_msg, strlen(parent_msg));
if (bytes_written < 0)
{
perror("❌ Parent failed to write");
exit(1);
}
// Step 4b: Read reply from child
ssize_t bytes_read = read(pipe_child_to_parent[0], buffer, sizeof(buffer));
if (bytes_read < 0)
{
perror("❌ Parent failed to read");
exit(1);
}
buffer[bytes_read] = '\0'; // Null-terminate
printf("👨👦 Parent received: %s\n", buffer);
// Close used ends
close(pipe_parent_to_child[1]);
close(pipe_child_to_parent[0]);
// Wait for child to finish
wait(NULL);
printf("✅ Parent: Child has exited. Communication complete.\n");
}
return 0;
}
/*
Output:
╰─ ./16 ─╯
👶 Child received: 👨👦 Hi child, how are you?
👨👦 Parent received: 👶 I'm good, thanks for asking!
✅ Parent: Child has exited. Communication complete.
*/