-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDisaptch.c
104 lines (96 loc) · 2.54 KB
/
Disaptch.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
97
98
99
100
101
102
103
104
/*
* Disaptch.c
*
* Created on: May 20, 2017
* Author: user
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
int COUNT = 0;
void my_signal_handler( int signum, siginfo_t* info, void* ptr)
{
int sonPid = info->si_pid;
char* pipeName = malloc(14 + sizeof(int));//TODO ok?
sprintf(pipeName, "//tmp//counter_%d" , (int) sonPid); //TODO double //?
size_t fdPipe = open(pipeName, O_RDONLY);
char* cRead = malloc(sizeof(int));
int countRead = read(fdPipe,cRead,1);
if(countRead < 0){
printf("Read has failed on the file: %s\n , I read : %d", strerror( errno ),countRead); //TODO trouble?
return;
}
int amount = atoi(cRead);
COUNT+= amount;
return;
}
int main(int argc, char** argv){
if(argc != 3){
printf("Wrong arguments");
return -1;
}
char ccount = argv[1][0];
char* filename = argv[2];
struct stat sb;
if(stat(filename,&sb)<0){
{
printf("Stat has failed on the file: %s\n", strerror( errno ));
return errno;
}
}
off_t N = sb.st_size;
off_t Q = 16; //text has to be at least 16 bytes?
off_t K = N/Q; // TODO TEMP
if(N < 2 * getpagesize()){
Q = 1;
K = N/Q;
}
// Structure to pass to the registration syscall
struct sigaction new_action;
memset(&new_action, 0, sizeof(new_action));
// Assign pointer to our handler function
new_action.sa_handler = my_signal_handler;
// Setup the flags
new_action.sa_flags = SA_SIGINFO;
// Register the handler
if( 0 != sigaction(SIGUSR1, &new_action, NULL) )
{
printf("Signal handle registration failed. %s\n", strerror(errno));
return -1;
}
for(int i=0; i<Q; i++){
pid_t cpid = fork();
char* length = malloc(sizeof(length));
char* offset = malloc(sizeof(off_t));
if(i == 15){
sprintf(length,"%ld",(long) N - (Q-1)* K);
}
else{
sprintf(length,"%ld",(long)K);
}
sprintf(offset,"%ld",(long) i * K);
if(cpid == 0) // child
{
printf("Proccess %d. My parametrs are: %s %s %s %s \n",(int)getpid(), argv[1], argv[2] , offset, length );
char *argvv[] = {"Counter.c", argv[1], argv[2] , offset, length, NULL};
execv("./coun",argvv);
printf("execv failed: %s\n", strerror(errno));
return -1;
}
else{
int status;
wait(&status);
continue;
}
}
printf("The amount of appearances of the character : %d\n", COUNT);
}