Make final changes (many improvements)
Co-authored-by: Juan Barmasch <jbarmasch@itba.edu.ar> Co-authored-by: Ezequiel Bellver <ebellver@itba.edu.ar>
This commit is contained in:
parent
076fa4a66d
commit
e9162b4f52
|
@ -4,3 +4,5 @@
|
|||
|
||||
# Folders
|
||||
Test/*
|
||||
Backup/*
|
||||
NotUsed/*
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
#include <sys/types.h>
|
||||
#include <sys/select.h>
|
||||
#include <fcntl.h>
|
||||
#include "error.h"
|
||||
#include "../error.h"
|
||||
|
||||
#define SLAVE_NUM 3
|
||||
#define MAX_SIZE 200
|
||||
|
|
19
Makefile
19
Makefile
|
@ -1,16 +1,17 @@
|
|||
CC = gcc
|
||||
CCFLAGS = -Wall -std=c99 -pedantic -g
|
||||
|
||||
SOURCES = $(wildcard *.c)
|
||||
OBJECTS = $(SOURCES:.c=.o)
|
||||
#SOURCES = $(wildcard *.c)
|
||||
#OBJECTS = $(SOURCES:.c=.o)
|
||||
OBJECTS = master.o error.o slave.o shr_mem.o view.o solve view
|
||||
|
||||
#all: $(OBJECTS)
|
||||
all: $(OBJECTS)
|
||||
|
||||
#%.o : %.c
|
||||
# $(CC) $(CFLAGS) $^ -o $@
|
||||
all: master.o error.o slave.o shr_mem.o view.o
|
||||
$(CC) $(CCFLAGS) -o solve master.o error.o shr_mem.o -lrt -lpthread
|
||||
$(CC) $(CCFLAGS) -o view view.o error.o shr_mem.o -lrt -lpthread
|
||||
#all: master.o error.o slave.o shr_mem.o view.o
|
||||
#$(CC) $(CCFLAGS) -o solve master.o error.o shr_mem.o -lrt -lpthread
|
||||
#$(CC) $(CCFLAGS) -o view view.o error.o shr_mem.o -lrt -lpthread
|
||||
master.o: master.c error.h
|
||||
$(CC) $(CCFLAGS) -c master.c
|
||||
error.o: error.c error.h
|
||||
|
@ -21,10 +22,14 @@ shr_mem.o: shr_mem.c error.h
|
|||
$(CC) $(CCFLAGS) -c shr_mem.c
|
||||
view.o: view.c
|
||||
$(CC) $(CCFLAGS) -c view.c
|
||||
solve: master.o error.o shr_mem.o
|
||||
$(CC) $(CCFLAGS) -o solve master.o error.o shr_mem.o -lrt -lpthread
|
||||
view: view.o error.o shr_mem.o
|
||||
$(CC) $(CCFLAGS) -o view view.o error.o shr_mem.o -lrt -lpthread
|
||||
|
||||
clean:
|
||||
rm -rf $(OBJECTS)
|
||||
rm -rf solve view
|
||||
#rm -rf solve view
|
||||
|
||||
test:
|
||||
pvs-studio-analyzer trace -- make
|
||||
|
|
69
master.c
69
master.c
|
@ -1,6 +1,9 @@
|
|||
#define _SVID_SOURCE 1
|
||||
//#define _DEFAULT_SOURCE 1
|
||||
|
||||
#define _XOPEN_SOURCE 500
|
||||
#define _POSIX_C_SOURCE 2
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
@ -15,16 +18,21 @@
|
|||
#include "error.h"
|
||||
#include "shr_mem.h"
|
||||
#include <semaphore.h>
|
||||
#include <signal.h>
|
||||
#include <sys/mman.h>
|
||||
|
||||
#define SLAVE_NUM 5
|
||||
#define MAX_SIZE 200
|
||||
#define MAX_SHM_SIZE 0x4B000
|
||||
#define MAX_SIZE 300
|
||||
#define MAX_PATH_SIZE 100
|
||||
#define MAX_OUTPUT_SIZE 200 + MAX_PATH_SIZE
|
||||
|
||||
void createPipes(int pipesOutput[SLAVE_NUM][2], int pipesInput[SLAVE_NUM][2]);
|
||||
int writeOutput(char * output, FILE * outputFile, void * shmP);
|
||||
int writeOutput(char * output, FILE * outputFile, void * shmP, char viewLinked);
|
||||
void closePipes(int pipesInput[SLAVE_NUM][2], int pipesOutput[SLAVE_NUM][2], int slave);
|
||||
char sendTask(struct stat statBuf, char * fileName, int pipesInput[SLAVE_NUM][2], int slave);
|
||||
void changeViewFlag();
|
||||
|
||||
static char viewLinked = 0;
|
||||
int main(int argc, char *argv[]) {
|
||||
if (setvbuf(stdout, NULL, _IONBF, 0) != 0)
|
||||
printSystemError("setvbuf() -- ERROR");
|
||||
|
@ -32,20 +40,37 @@ int main(int argc, char *argv[]) {
|
|||
if (argc == 1)
|
||||
printError("Error: no arguments provided");
|
||||
|
||||
sem_unlink("/FULL");
|
||||
sem_unlink("/EMPTY");
|
||||
//sem_unlink("/EMPTY");
|
||||
|
||||
FILE * outputFile;
|
||||
if ((outputFile = fopen("outputFile.txt", "w")) == NULL)
|
||||
printSystemError("fopen() -- ERROR");
|
||||
|
||||
char * shmPointer;
|
||||
int shmFd = createShm(MAX_SHM_SIZE, &shmPointer);
|
||||
void * shmStart = shmPointer;
|
||||
//char * shmPointer;
|
||||
//int shmFd;
|
||||
//shmFd = createShm((argc - 1) * MAX_OUTPUT_SIZE, &shmPointer);
|
||||
|
||||
printf("%d\n", argc - 1);
|
||||
printf("%d\n", getpid());
|
||||
|
||||
struct sigaction sigAct;
|
||||
sigAct.sa_handler = changeViewFlag;
|
||||
sigAct.sa_flags = 0;
|
||||
if (sigaction(SIGUSR1, &sigAct, NULL) == -1)
|
||||
printSystemError("sigaction() -- ERROR");
|
||||
|
||||
sleep(2);
|
||||
|
||||
char * shmPointer;
|
||||
int shmFd;
|
||||
void * shmStart;
|
||||
|
||||
if (viewLinked) {
|
||||
//shmPointer = mmap(0, (argc - 1) * MAX_OUTPUT_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, shmFd, 0);
|
||||
shmFd = createShm((argc - 1) * MAX_OUTPUT_SIZE, &shmPointer);
|
||||
shmStart = shmPointer;
|
||||
}
|
||||
|
||||
int pipesOutput[SLAVE_NUM][2], pipesInput[SLAVE_NUM][2];
|
||||
createPipes(pipesOutput, pipesInput);
|
||||
|
||||
|
@ -90,7 +115,7 @@ int main(int argc, char *argv[]) {
|
|||
buf[readBytes] = 0;
|
||||
|
||||
// printf("\n%s\n", buf);
|
||||
shmPointer += writeOutput(buf, outputFile, shmPointer);
|
||||
shmPointer += writeOutput(buf, outputFile, shmPointer, viewLinked);
|
||||
count--;
|
||||
|
||||
if (tasks < argc) {
|
||||
|
@ -104,11 +129,26 @@ int main(int argc, char *argv[]) {
|
|||
}
|
||||
} while (flag);
|
||||
|
||||
close(shmFd);
|
||||
sigset_t set;
|
||||
sigaddset(&set, SIGUSR2);
|
||||
|
||||
if (viewLinked) {
|
||||
int temp = 0;
|
||||
sigwait(&set, &temp);
|
||||
terminateShm("/BottlerSHM", shmFd, shmStart, (argc - 1) * MAX_OUTPUT_SIZE);
|
||||
closeSem();
|
||||
}
|
||||
|
||||
fclose(outputFile);
|
||||
|
||||
//close(shmFd);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
void changeViewFlag() {
|
||||
viewLinked = 1;
|
||||
}
|
||||
|
||||
char sendTask(struct stat statBuf, char * fileName, int pipesInput[SLAVE_NUM][2], int slave) {
|
||||
if (stat(fileName, &statBuf) == -1)
|
||||
printSystemError("Error, one of the specified paths is incorrect");
|
||||
|
@ -132,15 +172,16 @@ void closePipes(int pipesInput[SLAVE_NUM][2], int pipesOutput[SLAVE_NUM][2], int
|
|||
pipesInput[slave][0] = -1;
|
||||
}
|
||||
|
||||
int writeOutput(char * output, FILE * outputFile, void * shmPointer) {
|
||||
int writeOutput(char * output, FILE * outputFile, void * shmPointer, char viewLinked) {
|
||||
int length = strlen(output);
|
||||
|
||||
if (fwrite(output, sizeof(char), length, outputFile) != length)
|
||||
printSystemError("fwrite() -- ERROR");
|
||||
|
||||
if (viewLinked)
|
||||
writeShm(shmPointer, output, length);
|
||||
|
||||
return length + 1;
|
||||
return length;
|
||||
}
|
||||
|
||||
void createPipes(int pipesOutput[SLAVE_NUM][2], int pipesInput[SLAVE_NUM][2]) {
|
||||
|
@ -166,8 +207,8 @@ void createPipes(int pipesOutput[SLAVE_NUM][2], int pipesInput[SLAVE_NUM][2]) {
|
|||
if (close(pipesInput[i][0]) < 0)
|
||||
printSystemError("close() -- ERROR");
|
||||
|
||||
char * param[] = {"slave.o", NULL};
|
||||
if (execv("slave.o", param) < 0)
|
||||
char * params[] = {"slave.o", NULL};
|
||||
if (execv("slave.o", params) < 0)
|
||||
printSystemError("execv() -- ERROR");
|
||||
}
|
||||
else if (pid[i] < 0)
|
||||
|
|
72
shr_mem.c
72
shr_mem.c
|
@ -14,78 +14,48 @@
|
|||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
sem_t * fullSem = NULL;
|
||||
sem_t * emptySem = NULL;
|
||||
sem_t * sem = NULL;
|
||||
|
||||
int createShm(int totalSize, void ** shmPointer) {
|
||||
int fd;
|
||||
if ((fd = shm_open("/BottlerSHMem", O_CREAT | O_RDWR, S_IRWXU)) < 0)
|
||||
if ((fd = shm_open("/BottlerSHM", O_CREAT | O_RDWR, S_IRWXU)) < 0)
|
||||
printSystemError("shm_open() -- ERROR");
|
||||
ftruncate(fd, totalSize);
|
||||
* shmPointer = mmap(0, totalSize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
||||
return fd;
|
||||
}
|
||||
|
||||
/*
|
||||
char * attachShm(int shmId) {
|
||||
char *retAddr = shmat(shmid, addr, flags);
|
||||
if (retAddr == (void *) -1)
|
||||
printSystemError("shmat() -- ERROR");
|
||||
return retAddr;
|
||||
}
|
||||
*/
|
||||
|
||||
int writeShm(void * shmPointer, char * str, int len) {
|
||||
if (fullSem == NULL) {
|
||||
if ((fullSem = sem_open("/FULL", O_CREAT | O_RDWR, S_IRWXU, 1023)) == SEM_FAILED)
|
||||
if (sem == NULL) {
|
||||
if ((sem = sem_open("/EMPTY", O_CREAT | O_RDWR, S_IRWXU, 0)) == SEM_FAILED)
|
||||
printSystemError("sem_open() -- ERROR");
|
||||
}
|
||||
if (emptySem == NULL) {
|
||||
if ((emptySem = sem_open("/EMPTY", O_CREAT | O_RDWR, S_IRWXU, 0)) == SEM_FAILED)
|
||||
printSystemError("sem_open() -- ERROR");
|
||||
}
|
||||
if (sem_wait(fullSem) < 0)
|
||||
printSystemError("sem_wait() -- ERROR");
|
||||
|
||||
if (memcpy(shmPointer, str, len) == NULL)
|
||||
return EXIT_FAILURE;
|
||||
|
||||
if (sem_post(emptySem) < 0)
|
||||
printSystemError("sem_post() -- ERROR");
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
int readShmDeprecated(char * shmPointer, char * str, int len) {
|
||||
if (fullSem == NULL) {
|
||||
if ((fullSem = sem_open("/FULL", O_CREAT | O_RDWR, S_IRWXU, 1023)) == SEM_FAILED)
|
||||
printSystemError("sem_open() -- ERROR");
|
||||
}
|
||||
if (emptySem == NULL) {
|
||||
if ((emptySem = sem_open("/EMPTY", O_CREAT | O_RDWR, S_IRWXU, 0)) == SEM_FAILED)
|
||||
printSystemError("sem_open() -- ERROR");
|
||||
}
|
||||
if (sem_wait(emptySem) < 0)
|
||||
printSystemError("sem_wait() -- ERROR");
|
||||
|
||||
if (memcpy(str, shmPointer, len) == NULL)
|
||||
return EXIT_FAILURE;
|
||||
|
||||
if (sem_post(fullSem) < 0)
|
||||
if (sem_post(sem) < 0)
|
||||
printSystemError("sem_post() -- ERROR");
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
void terminateShm(const char * name, int fd, void * shmPointer, int totalSize) {
|
||||
if (munmap(shmPointer, totalSize) == -1)
|
||||
printSystemError("munmap() -- ERROR");
|
||||
if (shm_unlink(name) == -1)
|
||||
printSystemError("shm_unlink() -- ERROR");
|
||||
munmap(shmPointer, totalSize);
|
||||
if (close(fd) == -1)
|
||||
printSystemError("close() -- ERROR");
|
||||
return;
|
||||
}
|
||||
|
||||
void closeSem() {
|
||||
if (sem_close(sem) < 0)
|
||||
printSystemError("sem_close() -- ERROR");
|
||||
if (sem_unlink("/EMPTY") < 0)
|
||||
printSystemError("sem_unlink() -- ERROR");
|
||||
}
|
||||
|
||||
int copyShm(char * destPointer, char * srcPointer, int len) {
|
||||
int i;
|
||||
for (i = 0; srcPointer[i] != '\n' && srcPointer[i] != '\0' && i < len; i++) {
|
||||
|
@ -97,26 +67,20 @@ int copyShm(char * destPointer, char * srcPointer, int len) {
|
|||
}
|
||||
|
||||
int readShm(char * shmPointer, char * str, int len) {
|
||||
if (fullSem == NULL) {
|
||||
if ((fullSem = sem_open("/FULL", O_CREAT | O_RDWR, S_IRWXU, 1023)) == SEM_FAILED)
|
||||
if (sem == NULL) {
|
||||
if ((sem = sem_open("/EMPTY", O_CREAT | O_RDWR, S_IRWXU, 0)) == SEM_FAILED)
|
||||
printSystemError("sem_open() -- ERROR");
|
||||
}
|
||||
if (emptySem == NULL) {
|
||||
if ((emptySem = sem_open("/EMPTY", O_CREAT | O_RDWR, S_IRWXU, 0)) == SEM_FAILED)
|
||||
printSystemError("sem_open() -- ERROR");
|
||||
}
|
||||
if (sem_wait(emptySem) < 0)
|
||||
if (sem_wait(sem) < 0)
|
||||
printSystemError("sem_wait() -- ERROR");
|
||||
|
||||
int copied = copyShm(str, shmPointer, len);
|
||||
|
||||
// Creo que es lo mismo si hacemos:
|
||||
/*
|
||||
memcpy(str, shmPointer, len);
|
||||
*shmPointer += len;
|
||||
*/
|
||||
|
||||
if (sem_post(fullSem) < 0)
|
||||
printSystemError("sem_post() -- ERROR");
|
||||
|
||||
return copied;
|
||||
}
|
||||
|
|
|
@ -5,5 +5,7 @@ int createShm();
|
|||
char * attachShm(int shmId);
|
||||
int writeShm(void * address, char * str, int len);
|
||||
int readShm(char * address, char * str, int len);
|
||||
void terminateShm(const char * name, int fd, void * shmPointer, int totalSize);
|
||||
void closeSem();
|
||||
|
||||
#endif
|
||||
|
|
35
view.c
35
view.c
|
@ -1,3 +1,5 @@
|
|||
#define _POSIX_C_SOURCE 200112L
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
@ -8,39 +10,49 @@
|
|||
#include <sys/mman.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <signal.h>
|
||||
|
||||
#define MAX_SIZE 300
|
||||
#define MAX_AMOUNT_SIZE 10
|
||||
#define MAX_PID_SIZE 10
|
||||
#define MAX_PATH_SIZE 100
|
||||
#define MAX_OUTPUT_SIZE 200 + MAX_PATH_SIZE
|
||||
|
||||
int main(int argc, char * argv[]) {
|
||||
// sleep(2);
|
||||
char amount[MAX_AMOUNT_SIZE];
|
||||
char masterPid[MAX_PID_SIZE];
|
||||
switch (argc) {
|
||||
case 1:
|
||||
fgets(amount, MAX_AMOUNT_SIZE, stdin);
|
||||
amount[strcspn(amount, "\n")] = '\0';
|
||||
fgets(masterPid, MAX_PID_SIZE, stdin);
|
||||
masterPid[strcspn(masterPid, "\n")] = '\0';
|
||||
break;
|
||||
//read(STDIN_FILENO, readAddr, MAX_SIZE); break;
|
||||
case 2:
|
||||
case 3:
|
||||
strcpy(amount, argv[1]);
|
||||
strcpy(masterPid, argv[2]);
|
||||
break;
|
||||
default:
|
||||
printError("View must receive amount of files by argument or stdin");
|
||||
printError("View must receive amount of files and the pid of master process by argument or stdin");
|
||||
}
|
||||
|
||||
int files = atoi(amount);
|
||||
int mPid = atoi(masterPid);
|
||||
printf("%d\n", files);
|
||||
printf("%d\n", mPid);
|
||||
|
||||
union sigval value;
|
||||
sigqueue(mPid, SIGUSR1, value);
|
||||
|
||||
sleep(2);
|
||||
|
||||
int fd;
|
||||
char * shmPointer;
|
||||
|
||||
if ((fd = shm_open("BottlerSHM", O_RDONLY, S_IRUSR)) < 0)
|
||||
if ((fd = shm_open("/BottlerSHM", O_RDONLY, S_IRUSR)) < 0)
|
||||
printSystemError("shm_open() -- ERROR");
|
||||
|
||||
struct stat statBuf;
|
||||
fstat(fd, &statBuf);
|
||||
|
||||
if ((shmPointer = mmap(0, statBuf.st_size, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED){
|
||||
if ((shmPointer = mmap(0, (argc - 1) * MAX_OUTPUT_SIZE, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED){
|
||||
printSystemError("mmap() -- ERROR");
|
||||
}
|
||||
void * shmStart;
|
||||
|
@ -52,10 +64,13 @@ int main(int argc, char * argv[]) {
|
|||
printf("%s\n", buf);
|
||||
}
|
||||
|
||||
if (munmap(shmStart, statBuf.st_size) == -1)
|
||||
if (munmap(shmStart, (argc - 1) * MAX_OUTPUT_SIZE) == -1)
|
||||
printSystemError("munmap() -- ERROR");
|
||||
|
||||
close(fd);
|
||||
// terminateShm("/BottlerSHM", fd, shmPointer, (argc - 1) * MAX_OUTPUT_SIZE);
|
||||
|
||||
sigqueue(mPid, SIGUSR2, value);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue