-
Notifications
You must be signed in to change notification settings - Fork 0
/
Helper.c
96 lines (88 loc) · 2.13 KB
/
Helper.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
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/msg.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include"split.h"
#include<sys/sem.h>
#include<string.h>
#include<errno.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<signal.h>
#define NAMEFILE "helper.txt"
#define MAX_SIZE 100
#define TABLE_LIMIT 2
void File_Read(FILE *mf, char *string){
mf = fopen(NAMEFILE, "r");
char *estr;
char str[MAX_SIZE] = {};
do{
estr = fgets(str, MAX_SIZE, mf);
if(estr == NULL){
break;
}
strcat(string, str);
}while(estr);
fclose(mf);
}
int Choose(char* dish, char** tokens, int tokensCount){
int i;
for(i = 0; i < tokensCount; i += 2){
if(!strcmp(tokens[i], dish)){
return atoi(tokens[i + 1]);
}
}
printf("error in choose");
exit(-1);
}
int main(){
int msgid;
int len;
struct mymsgbuf{
long mtype;
char mtext[MAX_SIZE];
} mybuf;
int semid1;
char pathname[] = "a.c";
key_t key;
struct sembuf buf;
if((key = ftok(pathname, 0)) < 0){
exit(-1);
}
if((semid1 = semget(key, 1, 0666 | IPC_CREAT)) < 0){
exit(-1);
}
if((msgid = msgget(key, 0666 | IPC_CREAT) < 0)) printf("error");
msgid = msgget(key, 0666 |IPC_CREAT);
char string[MAX_SIZE] = {};
FILE *f;
char text[MAX_SIZE];
File_Read(f, text);
printf("text :\n%s", text);
char **tokens;
tokens = (char**)calloc(MAX_SIZE, sizeof(char*));
int i;
for(i = 0; i < MAX_SIZE; i++){
tokens[i] = (char*)calloc(MAX_SIZE, sizeof(char));
}
char delimeters[] = {'\n', ':'};
int tokensCount = 0;
Split(text, delimeters, &tokens, &tokensCount);
FILE *mf;
int slp = 0;
while(mybuf.mtype != 228){
msgrcv(msgid, (struct msgbuf *) &mybuf, sizeof(struct mymsgbuf) - sizeof(long), 1, 0);
printf("I recive %s\n", mybuf.mtext);
slp = Choose(mybuf.mtext, tokens, tokensCount);
sleep(slp);
printf("I help with %s in %d sec\n", mybuf.mtext, slp);
buf.sem_op = 1;
buf.sem_flg = 0;
buf.sem_num = 0;
semop(semid1, &buf, 1);
}
return 0;
}