-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconcurrent_write_fork.c
58 lines (55 loc) · 1.61 KB
/
concurrent_write_fork.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
#include <stdio.h>
#include "simplefs.h"
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#define LEN 500000
char msg[LEN];
int main() {
unlink("concurent_write_fs");
simplefs_init("concurrent_write_fs", 4096, 1024);
if(fork() == 0) {
int fsfd = simplefs_openfs("concurrent_write_fs");
simplefs_creat("/a.txt", fsfd);
int fd;
fd = simplefs_open("/a.txt", WRITE_MODE ,fsfd);
int i;
for(i = 0; i < LEN; ++i) {
msg[i] = 'a';
}
simplefs_write(fd, msg, LEN, fsfd);
simplefs_close(fd);
sleep(2); //niech oba zdaza zapisac
fd = simplefs_open("/b.txt", READ_MODE, fsfd);
simplefs_read(fd, msg, LEN, fsfd);
for(i = 0; i < LEN; ++i) {
if(msg[i] != 'b') {
printf("Blad odczytu");
}
}
simplefs_close(fd);
printf("dziecko koniec");
} else {
int fsfd = simplefs_openfs("concurrent_write_fs");
simplefs_creat("/b.txt", fsfd);
int fd;
fd = simplefs_open("/b.txt", WRITE_MODE ,fsfd);
int i;
for(i = 0; i < LEN; ++i) {
msg[i] = 'b';
}
simplefs_write(fd, msg, LEN, fsfd);
simplefs_close(fd);
sleep(2); //niech oba zdaza zapisac
fd = simplefs_open("/a.txt", READ_MODE, fsfd);
simplefs_read(fd, msg, LEN, fsfd);
for(i = 0; i < LEN; ++i) {
if(msg[i] != 'a') {
printf("Blad odczytu");
}
}
simplefs_lseek(fd, SEEK_SET, 0, fsfd);
printf("ojciec koniec");
}
}