-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxcapp.c
70 lines (64 loc) · 1.36 KB
/
xcapp.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <time.h>
void mode2(){
printf("Mode 2 (create, clean, date, exit): ");
}
void mode1(){
printf("Mode 1 (start, exit): ");
}
int main(int argc, char** argv){
pid_t pid1, pid2;
int pipefd[] = {12, 16};
char in[128];
if (pipe(pipefd) < 0){
perror("Pipe error: ");
return -1;
}
while (1){
mode1();
scanf("%s", in);
if (strcmp(in, "start") == 0){
if ((pid1 = fork()) == 0){
close(pipefd[1]);
while(1){
read(pipefd[0], in, 128);
if (strcmp(in, "create") == 0){
if ((pid2 = fork()) == 0){
printf("%d\n", getpid());
exit(0);
}
} else if (strcmp(in, "clean") == 0){
while (waitpid(-1, NULL, 0) > 0);
} else if (strcmp(in, "date") == 0){
char buff[100];
time_t d = time(NULL);
strftime(buff, 100, "%Y-%m-%d", localtime(&d));
printf("%s\n", buff);
} else if (strcmp(in, "exit") == 0){
close(pipefd[0]);
exit(0);
}
in[0] = '[';
}
} else {
while (1){
mode2();
scanf("%s", in);
write(pipefd[1], in, 256);
if (waitpid(pid1, NULL, WNOHANG) > 0 || strcmp(in, "exit") == 0){
wait(NULL);
break;
}
}
}
} else if (strcmp(in, "exit") == 0){
close(pipefd[1]);
return 0;
}
}
}