-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path21a.c
More file actions
62 lines (51 loc) · 1.65 KB
/
21a.c
File metadata and controls
62 lines (51 loc) · 1.65 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
/*
============================================================================
Name : 21a
Author : Piyush Singh
Description : Write two programs so that both can communicate by FIFO -Use two way communications.
Date: 21th Sep, 2025.
============================================================================
*/
// Before we are about to run the program we will be first creating a 2 fifo
// mkfifo fifo_A_to_B
// mkfifo fifo_B_to_A
// Program A: Client — sends message to server and waits for reply
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int main()
{
const char *fifo_out = "fifo_A_to_B"; // Client → Server
const char *fifo_in = "fifo_B_to_A"; // Server → Client
char send_msg[] = "👋 Hello Server, this is Client!";
char recv_buffer[100];
// Open FIFOs
int fd_out = open(fifo_out, O_WRONLY);
int fd_in = open(fifo_in, O_RDONLY);
if (fd_out == -1 || fd_in == -1)
{
perror("❌ Client failed to open FIFOs");
exit(1);
}
// Send message to server
write(fd_out, send_msg, strlen(send_msg));
printf("📤 Client sent: %s\n", send_msg);
// Read reply from server
ssize_t bytes_read = read(fd_in, recv_buffer, sizeof(recv_buffer));
if (bytes_read > 0)
{
recv_buffer[bytes_read] = '\0';
printf("📥 Client received: %s\n", recv_buffer);
}
close(fd_out);
close(fd_in);
return 0;
}
/*
Output:
╰─ ./21a ─╯
📤 Client sent: 👋 Hello Server, this is Client!
📥 Client received: 🤝 Hello Client, message received!
*/