-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path021.c
More file actions
103 lines (85 loc) · 2.52 KB
/
021.c
File metadata and controls
103 lines (85 loc) · 2.52 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
/*
..........................................................................................................................................
Name : 030.c
Author : SHRUTI VERMA
Description : Write two programs so that both can communicate by FIFO -Use two way communications.
Date : 17 Sep 2025
..........................................................................................................................................
*/
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<sys/types.h>
#include<fcntl.h>
#include<sys/stat.h>
#define FIFO1 "fifo1"
#define FIFO2 "fifo2"
void reader(const char* fifo1, const char* fifo2) {
int fd1, fd2;
fd1 = open(fifo1, O_RDONLY);
fd2 = open(fifo2, O_WRONLY);
char msg[100];
char buf[100];
while(1) {
int n = read(fd1, buf, sizeof(buf));
buf[n] = '\0';
printf("Data from writer : %s\n", buf);
printf("Reader process : ");
scanf(" %[^\n]", msg);
write(fd2, msg, sizeof(msg));
}
close(fd1);
close(fd2);
}
void writer(const char* fifo1, const char* fifo2) {
int fd1, fd2;
fd2 = open(fifo1, O_WRONLY);
fd1 = open(fifo2, O_RDONLY);
char msg[100];
char buf[100];
while(1) {
printf("Writer process : ");
fflush(stdout);
scanf(" %[^\n]", msg);
write(fd2, msg, sizeof(msg));
read(fd1, buf, sizeof(buf));
buf[99] = '\0';
printf("Data from reader : %s\n", buf);
}
close(fd1);
close(fd2);
}
int main() {
int choice = 0;
int fd1, fd2;
mkfifo(FIFO1, 0666);
mkfifo(FIFO2, 0666);
printf("1. Writer process\n2. Reader process\nEnter your choice : ");
scanf("%d", &choice);
if(choice == 1) {
writer(FIFO1, FIFO2);
} else {
reader(FIFO1, FIFO2);
}
}
/*-----------------------------------------------OUTPUT-----------------------------------------------
terminal 1
vumma@vumma-VivoBook-15-ASUS-Laptop-X507UF:~/Desktop/SS/HOL2$ ./021.out
1. Writer process
2. Reader process
Enter your choice : 2
Data from writer : hello reader
Reader process : hi writer
Data from writer : what's the status?
Reader process : just doing some FIFO problem
terminal2
vumma@vumma-VivoBook-15-ASUS-Laptop-X507UF:~/Desktop/SS/HOL2$ ./021.out
1. Writer process
2. Reader process
Enter your choice : 1
Writer process : hello reader
Data from reader : hi writer
Writer process : what's the status?
Data from reader : just doing some FIFO problem
Writer process :
*/