-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmulti_proc_basis.c
More file actions
56 lines (52 loc) · 2.19 KB
/
Copy pathmulti_proc_basis.c
File metadata and controls
56 lines (52 loc) · 2.19 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
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
#include <fcntl.h>
#define buffersize 5
int main (int argc, char* argv[]){
printf("[pid](%d) [ppid](%d): start\n", getpid(), getppid());
int n = atoi(argv[2]);
pid_t children[n-1];
int fd1_td1 = open("td1.txt", O_RDONLY);
//parent
printf("[pid](%d) [ppid](%d): [fd1_td1](%d) is got\n", getpid(), getppid(), fd1_td1);
for (int i = 0; i < n-1; i ++){
if ((children[i] = fork()) == 0){
//child
printf("[pid](%d) [ppid](%d): start\n", getpid(), getppid());
//use fd1_td1 to read file1
char buffer1[buffersize];
int flag1 = read(fd1_td1, buffer1, buffersize*sizeof(char));
printf("[pid](%d) [ppid](%d): [read](%s) by [fd1_td1](%d)\n", getpid(), getppid(), buffer1, fd1_td1);
//create fd2_td1 to read file1
int fd2_td1 = open("td1.txt", O_RDONLY);
printf("[pid](%d) [ppid](%d): [fd2_td1](%d) is got\n", getpid(), getppid(), fd2_td1);
char buffer2[buffersize];
int flag2 = read(fd2_td1, buffer2, buffersize*sizeof(char));
printf("[pid](%d) [ppid](%d): [read](%s) by [fd2_td1](%d)\n", getpid(), getppid(), buffer2, fd2_td1);
close(fd2_td1);
close(fd1_td1);
printf("[pid](%d) [ppid](%d): exit\n", getpid(), getppid());
exit(0);
}
}
//use fd1_td1 to read file1
char buffer1[buffersize];
int flag1 = read(fd1_td1, buffer1, buffersize*sizeof(char));
printf("[pid](%d) [ppid](%d): [read](%s) by [fd1_td1](%d)\n", getpid(), getppid(), buffer1, fd1_td1);
close(fd1_td1);
//use fd1_td2 to read file2
char buffer2[buffersize];
int fd1_td2 = open("td2.txt", O_RDONLY);
printf("[pid](%d) [ppid](%d): [fd1_td2](%d) is got\n", getpid(), getppid(), fd1_td2);
int flag2 = read(fd1_td2, buffer2, buffersize*sizeof(char));
printf("[pid](%d) [ppid](%d): [read](%s) by [fd1_td2](%d)\n", getpid(), getppid(), buffer2, fd1_td2);
close(fd1_td2);
sleep(2);
printf("[pid](%d) [ppid](%d): exit\n", getpid(), getppid());
exit(0);
return 0;
}