/
lab1-v12.c
152 lines (143 loc) · 2.81 KB
/
lab1-v12.c
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <fcntl.h>
#include <string.h>
#include <signal.h>
#include <sys/mman.h>
#define SWITCHED 1
#define BUF_SIZE 64
#define FILE_NAME_SIZE 256
int fd1, fd2;
int pipe1[2], pipe2[2];
pid_t pid1;
pid_t pid2;
static pid_t *pid2_t;
char file1[FILE_NAME_SIZE];
void switch_files(int sig_type)
{
if (sig_type == SIGUSR1)
{
write(pipe2[1], file1, strlen(file1)+1);
if (switch_file(pipe1[0], &fd1, 1) == SWITCHED)
{
kill(*pid2_t, SIGUSR2);
}
}
if (sig_type == SIGUSR2)
{
switch_file(pipe2[0], &fd2, 2);
}
}
int switch_file(int checking_desc, int *switching_desc, int p_idx)
{
char buf[FILE_NAME_SIZE];
if (read(checking_desc, buf, FILE_NAME_SIZE) > 0)
{
int openning = open(buf, O_RDONLY);
if (openning > 0)
{
if (p_idx == 1)
{
strncpy(file1, buf, FILE_NAME_SIZE);
//printf("file1 %s\n", file1);
}
close(*switching_desc);
*switching_desc = openning;
}
else
{
perror(buf);
return -1;
}
return SWITCHED;
}
return 0;
}
void terminate(int sig_type)
{
_exit(0);
}
int main(int argc, char **argv)
{
if (argc != 3)
{
fprintf(stderr, "give 2 args\n");
exit(-1);
}
pid2_t = mmap(NULL, sizeof(*pid2_t), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
pid_t lpid1, lpid2;
char file2[FILE_NAME_SIZE];
strncpy(file2, argv[2], FILE_NAME_SIZE);
char input[256];
strncpy(file1, argv[1], FILE_NAME_SIZE);
pipe(pipe1);
pipe(pipe2);
signal(SIGINT, terminate);
if ((lpid1 = fork()) == 0)
{
int read_bytes = 0;
char c;
printf("1 process pid is %d\n", getpid());
signal(SIGUSR1, switch_files);
fd1 = open(file1, O_RDONLY);
if (fd1 < 0)
{
fprintf(stderr, "error opening file %s\n", file1);
return -1;
}
while(1)
{
read_bytes = read(fd1, &c, 1);
if (read_bytes > 0)
{
fprintf(stdout, "%c\n", toupper(c));
}
usleep(500000);
}
close(fd1);
printf("fd1 closed\n");
return 0;
} else {
pid1 = lpid1;
}
if ((lpid2 = fork()) == 0)
{
int read_bytes = 0;
char c;
printf("2 process pid is %d\n", getpid());
signal(SIGUSR2, switch_files);
fd2 = open(file2, O_RDONLY);
if (fd2 < 0)
{
fprintf(stderr, "error opening file %s\n", file2);
return -1;
}
while(1)
{
read_bytes = read(fd2, &c, 1);
if (read_bytes > 0)
{
fprintf(stdout, "\t%c\n", tolower(c));
}
usleep(500000);
}
close(fd2);
printf("fd2 closed\n");
return 0;
} else {
*pid2_t = lpid2;
pid2 = lpid2;
}
printf("%d %d\n", pid1, pid2);
while(scanf("%s", input) > 0)
{
write(pipe1[1], input, strlen(input) + 1);
kill(pid1, SIGUSR1);
}
waitpid(pid1, 0, 0);
waitpid(pid2, 0, 0);
printf("exit\n");
}