-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread.c
More file actions
67 lines (61 loc) · 1.25 KB
/
thread.c
File metadata and controls
67 lines (61 loc) · 1.25 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
57
58
59
60
61
62
63
64
65
66
67
#include<wait.h>
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/shm.h>
#include<sys/ipc.h>
#define MAX_PROCESS 8
int main(){
int pid = 0;
int smid = 0;
int root = 0;
int status;
int* offset;
int* lock;
key_t key = 9999;
key_t mukey = 7777;
FILE* text = fopen("temp.txt", "a+");
void* sms;
if((smid = shmget(key, sizeof(int), 0666|IPC_CREAT)) == -1) printf("chk\n");
if((sms = shmat(smid, (void*) 0, 0)) == (void*)-1) printf("chk\n");
offset = (int*) sms;
if((smid = shmget(mukey, sizeof(int), 0666|IPC_CREAT)) == -1) printf("chk\n");
if((sms = shmat(smid, (void*) 0, 0)) == (void*)-1) printf("chk\n");
lock = (int*) sms;
*lock = 0;
*offset = 0;
if(sms == (void*) -1) exit(0);
for (int i = 0; i < 3; i++){
if(pid == 0){
pid = fork();
if (pid != 0){
pid = fork();
if(pid != 0 && i == 0) root++;
}
}
}
int tmp = 0;
char data[100000];
while(1){
if (!*lock){
*lock = 1;
break;
}
}
fseek(text, *offset, 0);
for(int i = 0; i < 2; i ++){
fgets(data, 100000, text);
tmp += atoi(data);
}
*offset = ftell(text);
printf("%d\n", tmp);
fprintf(text ,"%d\n", tmp);
*lock = 0;
if(root){
fclose(text);
if(shmctl(smid, IPC_RMID, NULL)==-1){
exit(0);
}
}
return 0;
}