-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadder.c
More file actions
52 lines (41 loc) · 1.33 KB
/
adder.c
File metadata and controls
52 lines (41 loc) · 1.33 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
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
int main()
{
const char *pipe_name = "adder_pipe";
while (1)
{
//open the named pipe for writing
int pipe_fd = open(pipe_name, O_WRONLY);
if (pipe_fd == -1)
{
perror("Error opening the named pipe");
exit(EXIT_FAILURE);
}
//write the name of the operation performed for ease of understanding in worker_monitor terminal
char *mess = "Adder";
write(pipe_fd, mess, strlen(mess) + 1);
//read two numbers from the user
int num1, num2;
printf("Enter two numbers: ");
while (scanf("%d %d", &num1, &num2) != 2) {
printf("Invalid input. Please enter two numbers.\n");
//clear the input buffer
while (getchar() != '\n');
printf("Enter two numbers: ");
}
double result = num1 + num2;
//write the operation details to the named pipe
write(pipe_fd, &num1, sizeof(num1));
write(pipe_fd, &num2, sizeof(num2));
write(pipe_fd, &result, sizeof(result));
write(pipe_fd, "+", sizeof(char));
close(pipe_fd);
sleep(1); //sleep for a short time to allow the monitor to read
}
return 0;
}