Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 137b451

Browse files
KazakosVas=
authored andcommitted
Signal Handling and process synchronization
1 parent 816b608 commit 137b451

20 files changed

+930
-76
lines changed

Syspro/HelpFunctions.c

Lines changed: 89 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
#include <stdio.h>
66
#include <stdlib.h>
77
#include <string.h>
8+
#include <sys/types.h>
9+
#include <unistd.h>
10+
#include <dirent.h>
11+
#include <errno.h>
812

913
#include "patient.h"
1014
#include "list.h"
@@ -13,6 +17,7 @@
1317

1418
#include "Hashtable.h"
1519
#include "HelpFunctions.h"
20+
#include "process_synchronization.h"
1621

1722

1823
void split_line (char ** split_line, char * line)
@@ -35,11 +40,12 @@ void split_line (char ** split_line, char * line)
3540
}
3641
void read_file(char * path, List * listptr, Hashtable * diseases, Hashtable * countries)
3742
{
43+
3844
FILE * FD = fopen(path, "r");
3945
if(FD ==NULL)
40-
{
41-
perror("Fopen");
42-
}
46+
perror("Error fopen");
47+
48+
4349

4450
char * line = malloc(512);
4551
char ** line_ptr = & line;
@@ -76,4 +82,84 @@ void read_file(char * path, List * listptr, Hashtable * diseases, Hashtable * co
7682
fclose(FD);
7783

7884

85+
7986
}
87+
88+
89+
/*This Function reads a directory and returns a 2d array with all files inside a directory */
90+
char ** open_directories(char * path, int * number_files)
91+
{
92+
/*Directory structures */
93+
DIR * folder;
94+
struct dirent * entry;
95+
96+
/*Open Directory */
97+
folder = opendir(path);
98+
if(folder ==NULL)
99+
{
100+
perror("Unable to read directory");
101+
exit(2);
102+
}
103+
/*Count Files of Directory */
104+
* number_files =0;
105+
while( (entry=readdir(folder)) )
106+
* number_files = * number_files +1;
107+
* number_files = * number_files-2;
108+
109+
closedir(folder);
110+
111+
/*Create a 2d array to store all files between a directory*/
112+
char ** directory_files = malloc(sizeof(char * )* (*number_files) );
113+
for (int i=0 ; i< *number_files; i++)
114+
directory_files[i]= malloc(512);
115+
116+
folder = opendir(path);
117+
if(folder ==NULL)
118+
{
119+
perror("Unable to read directory");
120+
exit(2);
121+
}
122+
int index =0;
123+
while( (entry=readdir(folder)) )
124+
{
125+
if(strcmp(entry->d_name, ".")!=0 && strcmp(entry->d_name, "..")!=0 )
126+
{
127+
strcpy(directory_files[index], path);
128+
strcpy(directory_files[index]+ strlen(directory_files[index])+1 , entry->d_name);
129+
directory_files[index][strlen(directory_files[index])] = '/';
130+
131+
//printf("%s\n",directory_files[index]);
132+
index++;
133+
134+
}
135+
}
136+
closedir(folder);
137+
138+
return directory_files;
139+
140+
}
141+
142+
143+
144+
145+
146+
147+
void * command_prompt()
148+
{
149+
char * error;
150+
printf("Give command:");
151+
152+
char *command = malloc(512);
153+
error = fgets(command, 512, stdin);
154+
if(error ==NULL)
155+
printf("FOUND the bufg\n");
156+
157+
command[strlen(command) - 1] = '\0';
158+
159+
return command;
160+
161+
162+
163+
164+
165+
}

Syspro/HelpFunctions.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,20 @@
55
#ifndef SYSPRO_HELPFUNCTIONS_H
66
#define SYSPRO_HELPFUNCTIONS_H
77

8+
#define READ 0
9+
#define WRITE 1
10+
11+
typedef struct process_data{
12+
pid_t id;
13+
char ** folder_files;
14+
int number_files;
15+
}process_data;
16+
817
void read_file(char * path, List * listptr, Hashtable * diseases, Hashtable * countries);
918
void split_line (char ** split_line, char * line);
1019

20+
char ** open_directories(char * path, int * number_files);
21+
void * command_prompt();
22+
1123

1224
#endif //SYSPRO_HELPFUNCTIONS_H

Syspro/Makefile

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
CC = gcc -g -Wall
1+
CC = gcc -g
22

3-
diseaseMonitor: main.o patient.o HelpFunctions.o list.o RBTree.o Hashtable.o StatFunctions.o
4-
$(CC) -o diseaseMonitor main.o patient.o HelpFunctions.o list.o RBTree.o Hashtable.o StatFunctions.o
3+
all: diseaseMonitor child
4+
5+
diseaseMonitor: main.o patient.o HelpFunctions.o list.o RBTree.o Hashtable.o StatFunctions.o process_synchronization.o
6+
$(CC) -o diseaseMonitor main.o patient.o HelpFunctions.o list.o RBTree.o Hashtable.o StatFunctions.o process_synchronization.o
57

68
main.o: main.c
79
$(CC) -c main.c
@@ -24,8 +26,17 @@ Hashtable.o: Hashtable.c
2426
StatFunctions.o: StatFunctions.c
2527
$(CC) -c StatFunctions.c
2628

29+
process_synchronization.o: process_synchronization.c
30+
$(CC) -c process_synchronization.c
31+
32+
child: child.o patient.o HelpFunctions.o list.o RBTree.o Hashtable.o StatFunctions.o process_synchronization.o
33+
$(CC) -o child child.o patient.o HelpFunctions.o list.o RBTree.o Hashtable.o StatFunctions.o process_synchronization.o
34+
35+
child.o: child.c
36+
$(CC) -c child.c
37+
2738
.PHONY: clean
2839

2940
clean:
30-
rm -f diseaseMonitor main.o patient.o HelpFunctions.o list.o RBTree.o Hashtable.o StatFunctions.o
41+
rm -f diseaseMonitor main.o patient.o HelpFunctions.o list.o RBTree.o Hashtable.o StatFunctions.o child child.o process_synchronization.o
3142
clear

0 commit comments

Comments
 (0)