Add view, shr_mem library and finish master!

Co-authored-by: Juan Barmasch <jbarmasch@itba.edu.ar>
Co-authored-by: Ezequiel Bellver <ebellver@itba.edu.ar>
This commit is contained in:
Santiago Lo Coco 2021-09-06 10:12:45 -03:00
parent 7002c6b232
commit 076fa4a66d
9 changed files with 472 additions and 377 deletions

139
Backup/master.c Normal file
View File

@ -0,0 +1,139 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <math.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/select.h>
#include <fcntl.h>
#include "error.h"
#define SLAVE_NUM 3
#define MAX_SIZE 200
void create(int ans[2], int pipes[SLAVE_NUM][2]);
int main(int argc, char *argv[]) {
//setvbuf(stdout, NULL, _IONBF, 0);
if (argc == 1)
printError("Error: no arguments provided");
int ans[2];
int pipes[SLAVE_NUM][2];
create(ans, pipes);
//sleep(1);
fd_set ansSet;
FD_ZERO(&ansSet);
FD_SET(ans[0], &ansSet);
fd_set fdSet;
FD_ZERO(&fdSet);
int n = 0;
for (; n < SLAVE_NUM; n++)
FD_SET(pipes[n][1], &fdSet);
int flag = 0, j = 0;
struct stat statBuf;
int i = 1;
for (; i < argc; i++) {
if (stat(argv[i], &statBuf) == -1)
printSystemError("Error, one of the specified paths is incorrect");
if (!S_ISREG(statBuf.st_mode))
continue;
if (j == SLAVE_NUM) {
j = 0;
flag = 1;
}
if (!flag) {
if (write(pipes[j++][1], argv[i], strlen(argv[i]) + 1) < 0) {
//if (write(pipes[j++][1], "prueba.cnf\n", strlen(argv[i]) + 1) < 0) {
printSystemError("write() -- ERROR");
}
//close(pipes[j-1][1]);
} else {
int fd;
if ((fd = select(5, &fdSet, NULL, NULL, NULL) < 0))
printSystemError("select() -- ERROR");
char buf[MAX_SIZE];
if (read(fd, buf, MAX_SIZE) < 0)
printSystemError("read() -- ERROR");
printf("%s", buf);
int k = 0;
for (; i < SLAVE_NUM; k++) {
if (pipes[k][1] == fd) {
if (write(pipes[k][1], argv[i], strlen(argv[i])) < 0)
printSystemError("write() -- ERROR");
continue;
}
}
}
}
int fd;
char buf[MAX_SIZE];
if ((fd = select(ans[0] + 1, &ansSet, NULL, NULL, NULL)) < 0)
printSystemError("select() -- ERROR");
while (fd > 0) {
int readBytes = 0;
do {
if ((readBytes += read(ans[0], buf + readBytes, MAX_SIZE)) < 0)
printSystemError("read() -- ERROR");
} while (buf[readBytes - 1] != '\n');
buf[readBytes] = 0;
printf("%s\n", buf);
}
/*
readBytes = 0;
do {
if ((readBytes += read(ans[0], buf + readBytes, MAX_SIZE)) < 0)
printSystemError("read() -- ERROR");
} while (buf[readBytes - 1] != '\n');
buf[readBytes] = 0;
printf("%s\n", buf);
*/
}
void create(int ans[2], int pipes[5][2]) {
pid_t pid[SLAVE_NUM];
if (pipe(ans) < 0)
printSystemError("pipe() -- ERROR");
int i = 0;
for (; i < SLAVE_NUM; i++) {
if (pipe(pipes[i]) < 0)
printSystemError("pipe() -- ERROR");
if ((pid[i] = fork()) == 0) {
if (close(ans[0]) < 0)
printSystemError("close() -- ERROR");
if (dup2(ans[1], STDOUT_FILENO) < 0)
printSystemError("dup2() -- ERROR 1");
if (close(ans[1]) < 0)
printSystemError("close() -- ERROR");
if (close(pipes[i][1]) < 0)
printSystemError("close() -- ERROR");
if (dup2(pipes[i][0], STDIN_FILENO) < 0)
printSystemError("dup2() -- ERROR 2");
if (close(pipes[i][0]) < 0)
printSystemError("close() -- ERROR");
if (execv("slave.o", NULL) < 0)
printSystemError("execv() -- ERROR");
}
else if (pid[i] < 0)
printSystemError("fork() -- ERROR");
if (close(pipes[i][0]) < 0)
printSystemError("close() -- ERROR");
}
if (close(ans[1]) < 0)
printSystemError("close() -- ERROR");
}

View File

@ -1,5 +1,5 @@
CC = gcc
CCFLAGS = -Wall -std=c99 -pedantic -g # (si queremos debuggear)
CCFLAGS = -Wall -std=c99 -pedantic -g
SOURCES = $(wildcard *.c)
OBJECTS = $(SOURCES:.c=.o)
@ -8,20 +8,25 @@ OBJECTS = $(SOURCES:.c=.o)
#%.o : %.c
# $(CC) $(CFLAGS) $^ -o $@
all: masterV2.o error.o slave.o
$(CC) $(CCFLAGS) -o master masterV2.o error.o
masterV2.o: masterV2.c error.h
$(CC) $(CCFLAGS) -c masterV2.c
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
$(CC) $(CCFLAGS) -c error.c
slave.o: slave.c error.o
$(CC) $(CCFLAGS) slave.c -o slave.o error.o
shr_mem.o: shr_mem.c error.h
$(CC) $(CCFLAGS) -c shr_mem.c
view.o: view.c
$(CC) $(CCFLAGS) -c view.c
clean:
rm -rf $(OBJECTS)
rm -rf solve view
test:
# Agregar encabezados primero!!!!
pvs-studio-analyzer trace -- make
pvs-studio-analyzer analyze
plog-converter -a '64:1,2,3;GA:1,2,3;OP:1,2,3' -t tasklist -o report.tasks PVS-Studio.log

212
master.c
View File

@ -1,3 +1,6 @@
#define _SVID_SOURCE 1
//#define _DEFAULT_SOURCE 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -8,132 +11,171 @@
#include <sys/types.h>
#include <sys/select.h>
#include <fcntl.h>
#include <sys/shm.h>
#include "error.h"
#include "shr_mem.h"
#include <semaphore.h>
#define SLAVE_NUM 3
#define SLAVE_NUM 5
#define MAX_SIZE 200
#define MAX_SHM_SIZE 0x4B000
void create(int ans[2], int pipes[SLAVE_NUM][2]);
void createPipes(int pipesOutput[SLAVE_NUM][2], int pipesInput[SLAVE_NUM][2]);
int writeOutput(char * output, FILE * outputFile, void * shmP);
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);
int main(int argc, char *argv[]) {
//setvbuf(stdout, NULL, _IONBF, 0);
if (setvbuf(stdout, NULL, _IONBF, 0) != 0)
printSystemError("setvbuf() -- ERROR");
if (argc == 1)
printError("Error: no arguments provided");
int ans[2];
int pipes[SLAVE_NUM][2];
create(ans, pipes);
//sleep(1);
sem_unlink("/FULL");
sem_unlink("/EMPTY");
fd_set ansSet;
FD_ZERO(&ansSet);
FD_SET(ans[0], &ansSet);
FILE * outputFile;
if ((outputFile = fopen("outputFile.txt", "w")) == NULL)
printSystemError("fopen() -- ERROR");
fd_set fdSet;
FD_ZERO(&fdSet);
int n = 0;
for (; n < SLAVE_NUM; n++)
FD_SET(pipes[n][1], &fdSet);
char * shmPointer;
int shmFd = createShm(MAX_SHM_SIZE, &shmPointer);
void * shmStart = shmPointer;
printf("%d\n", argc - 1);
sleep(2);
int pipesOutput[SLAVE_NUM][2], pipesInput[SLAVE_NUM][2];
createPipes(pipesOutput, pipesInput);
int flag = 0, j = 0;
struct stat statBuf;
int i = 1;
for (; i < argc; i++) {
if (stat(argv[i], &statBuf) == -1)
printSystemError("Error, one of the specified paths is incorrect");
if (!S_ISREG(statBuf.st_mode))
int tasks = 1;
for (int j = 0; tasks <= SLAVE_NUM && tasks < argc; tasks++) {
if (sendTask(statBuf, argv[tasks], pipesInput, j++) == 0)
continue;
}
if (j == SLAVE_NUM) {
j = 0;
flag = 1;
char buf[MAX_SIZE];
fd_set pipesOutputSet;
FD_ZERO(&pipesOutputSet);
int r, readBytes, flag, count;
if (tasks != SLAVE_NUM) {
for (int k = tasks - 1; k < SLAVE_NUM; k++) {
closePipes(pipesInput, pipesOutput, k);
}
if (!flag) {
if (write(pipes[j++][1], argv[i], strlen(argv[i]) + 1) < 0) {
//if (write(pipes[j++][1], "prueba.cnf\n", strlen(argv[i]) + 1) < 0) {
printSystemError("write() -- ERROR");
}
do {
flag = 0;
FD_ZERO(&pipesOutputSet);
for (int n = 0; n < SLAVE_NUM; n++) {
if (pipesOutput[n][0] != -1) {
FD_SET(pipesOutput[n][0], &pipesOutputSet);
flag = 1;
}
//close(pipes[j-1][1]);
} else {
int fd;
if ((fd = select(5, &fdSet, NULL, NULL, NULL) < 0))
}
if (flag) {
if ((count = select(pipesOutput[SLAVE_NUM - 1][1] + 1, &pipesOutputSet, NULL, NULL, NULL)) <= 0)
printSystemError("select() -- ERROR");
char buf[MAX_SIZE];
if (read(fd, buf, MAX_SIZE) < 0)
printSystemError("read() -- ERROR");
printf("%s", buf);
int k = 0;
for (; i < SLAVE_NUM; k++) {
if (pipes[k][1] == fd) {
if (write(pipes[k][1], argv[i], strlen(argv[i])) < 0)
printSystemError("write() -- ERROR");
continue;
for (r = 0; r < SLAVE_NUM && count > 0; r++) {
if (FD_ISSET(pipesOutput[r][0], &pipesOutputSet)) {
readBytes = 0;
do {
if ((readBytes += read(pipesOutput[r][0], buf + readBytes, MAX_SIZE)) < 0)
printSystemError("read() -- ERROR");
} while (buf[readBytes - 1] != '\n');
buf[readBytes] = 0;
// printf("\n%s\n", buf);
shmPointer += writeOutput(buf, outputFile, shmPointer);
count--;
if (tasks < argc) {
if (sendTask(statBuf, argv[tasks++], pipesInput, r) == 0)
continue;
} else {
closePipes(pipesInput, pipesOutput, r);
}
}
}
}
}
} while (flag);
int fd;
char buf[MAX_SIZE];
if ((fd = select(ans[0] + 1, &ansSet, NULL, NULL, NULL)) < 0)
printSystemError("select() -- ERROR");
while (fd > 0) {
int readBytes = 0;
do {
if ((readBytes += read(ans[0], buf + readBytes, MAX_SIZE)) < 0)
printSystemError("read() -- ERROR");
} while (buf[readBytes - 1] != '\n');
buf[readBytes] = 0;
printf("%s\n", buf);
}
/*
readBytes = 0;
do {
if ((readBytes += read(ans[0], buf + readBytes, MAX_SIZE)) < 0)
printSystemError("read() -- ERROR");
} while (buf[readBytes - 1] != '\n');
buf[readBytes] = 0;
printf("%s\n", buf);
*/
close(shmFd);
fclose(outputFile);
return EXIT_SUCCESS;
}
void create(int ans[2], int pipes[5][2]) {
pid_t pid[SLAVE_NUM];
if (pipe(ans) < 0)
printSystemError("pipe() -- ERROR");
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");
int i = 0;
for (; i < SLAVE_NUM; i++) {
if (pipe(pipes[i]) < 0)
if (!S_ISREG(statBuf.st_mode))
return EXIT_FAILURE;
if ((write(pipesInput[slave][1], fileName, strlen(fileName) + 1)) < 0)
printSystemError("write() -- ERROR");
return EXIT_SUCCESS;
}
void closePipes(int pipesInput[SLAVE_NUM][2], int pipesOutput[SLAVE_NUM][2], int slave) {
if (close(pipesOutput[slave][0]) < 0)
printSystemError("close() -- ERROR");
if (close(pipesInput[slave][1]) < 0)
printSystemError("close() -- ERROR");
pipesOutput[slave][0] = -1;
pipesInput[slave][0] = -1;
}
int writeOutput(char * output, FILE * outputFile, void * shmPointer) {
int length = strlen(output);
if (fwrite(output, sizeof(char), length, outputFile) != length)
printSystemError("fwrite() -- ERROR");
writeShm(shmPointer, output, length);
return length + 1;
}
void createPipes(int pipesOutput[SLAVE_NUM][2], int pipesInput[SLAVE_NUM][2]) {
pid_t pid[SLAVE_NUM];
for (int i = 0; i < SLAVE_NUM; i++) {
if (pipe(pipesOutput[i]) < 0)
printSystemError("pipe() -- ERROR");
if (pipe(pipesInput[i]) < 0)
printSystemError("pipe() -- ERROR");
if ((pid[i] = fork()) == 0) {
if (close(ans[0]) < 0)
if (close(pipesOutput[i][0]) < 0)
printSystemError("close() -- ERROR");
if (dup2(ans[1], STDOUT_FILENO) < 0)
if (dup2(pipesOutput[i][1], STDOUT_FILENO) < 0)
printSystemError("dup2() -- ERROR 1");
if (close(ans[1]) < 0)
if (close(pipesOutput[i][1]) < 0)
printSystemError("close() -- ERROR");
if (close(pipes[i][1]) < 0)
if (close(pipesInput[i][1]) < 0)
printSystemError("close() -- ERROR");
if (dup2(pipes[i][0], STDIN_FILENO) < 0)
if (dup2(pipesInput[i][0], STDIN_FILENO) < 0)
printSystemError("dup2() -- ERROR 2");
if (close(pipes[i][0]) < 0)
if (close(pipesInput[i][0]) < 0)
printSystemError("close() -- ERROR");
if (execv("slave.o", NULL) < 0)
char * param[] = {"slave.o", NULL};
if (execv("slave.o", param) < 0)
printSystemError("execv() -- ERROR");
}
else if (pid[i] < 0)
printSystemError("fork() -- ERROR");
if (close(pipes[i][0]) < 0)
if (close(pipesInput[i][0]) < 0)
printSystemError("close() -- ERROR");
if (close(pipesOutput[i][1]) < 0)
printSystemError("close() -- ERROR");
}
if (close(ans[1]) < 0)
printSystemError("close() -- ERROR");
}

View File

@ -1,267 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <math.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/select.h>
#include <fcntl.h>
#include "error.h"
#define SLAVE_NUM 5
#define MAX_SIZE 200
void create(int pipesOutput[SLAVE_NUM][2], int pipesInput[SLAVE_NUM][2]);
int main(int argc, char *argv[]) {
setvbuf(stdout, NULL, _IONBF, 0);
if (argc == 1)
printError("Error: no arguments provided");
int pipesOutput[SLAVE_NUM][2];
int pipesInput[SLAVE_NUM][2];
create(pipesOutput, pipesInput);
//sleep(1);
struct stat statBuf;
int tasks = 1, j = 0;
//for (j = 0; j < argc; j++)
//printf("MBOEANAS PUAN. %s\n", argv[j]);
for (; tasks < SLAVE_NUM && tasks < argc; tasks++) {
//printf("%s\n", argv[tasks]);
if (stat(argv[tasks], &statBuf) == -1) {
printf("%s\n", argv[tasks]);
printSystemError("Error, one of the specified paths is incorrect");
}
if (!S_ISREG(statBuf.st_mode))
continue;
if (write(pipesInput[j++][1], argv[tasks], strlen(argv[tasks]) + 1) < 0) {
printSystemError("write() -- ERROR oli");
}
}
// for (; arg < argc; arg++) {
// if (stat(argv[arg], &statBuf) == -1)
// printSystemError("Error, one of the specified paths is incorrect");
//
// if (!S_ISREG(statBuf.st_mode))
// continue;
//
// if (write(pipesInput[j++][1], argv[arg], strlen(argv[arg]) + 1) < 0) {
// printSystemError("write() -- ERROR");
// }
// }
char buf[MAX_SIZE];
fd_set pipesOutputSet;
FD_ZERO(&pipesOutputSet);
// int n = 0;
// if (tasks >= SLAVE_NUM)
// for (; n < SLAVE_NUM; n++)
// FD_SET(pipesOutput[n][0], &pipesOutputSet);
// else
// for (; n < tasks; n++)
// FD_SET(pipesOutput[n][0], &pipesOutputSet);
// int count;
// if ((count = select(pipesOutput[SLAVE_NUM - 1][0] + 1, &pipesOutputSet, NULL, NULL, NULL)) <= 0)
// printSystemError("select() -- ERROR");
int r, readBytes, flag, count;
if (tasks == SLAVE_NUM) {
do {
int n;
flag = 0;
FD_ZERO(&pipesOutputSet);
for (n = 0; n < SLAVE_NUM; n++) {
if (pipesOutput[n][0] != -1) {
FD_SET(pipesOutput[n][0], &pipesOutputSet);
flag = 1;
}
}
if ((count = select(pipesOutput[SLAVE_NUM - 1][1] + 1, &pipesOutputSet, NULL, NULL, NULL)) <= 0)
printSystemError("select() -- ERROR");
for (r = 0; r < SLAVE_NUM && count > 0; r++) {
if (FD_ISSET(pipesOutput[r][0], &pipesOutputSet)) {
readBytes = 0;
do {
if ((readBytes += read(pipesOutput[r][0], buf + readBytes, MAX_SIZE)) < 0)
printSystemError("read() -- ERROR");
} while (buf[readBytes - 1] != '\n');
buf[readBytes] = 0;
printf("\n%s\n", buf);
count--;
if (tasks < argc) {
if (stat(argv[tasks], &statBuf) == -1)
printSystemError("Error, one of the specified paths is incorrect");
if (!S_ISREG(statBuf.st_mode))
continue;
if ((write(pipesInput[r][1], argv[tasks], strlen(argv[tasks]) + 1)) < 0)
printSystemError("write() -- ERROR");
tasks++;
} else {
close(pipesOutput[r][0]);
close(pipesInput[r][1]);
pipesOutput[r][0] = -1;
pipesInput[r][0] = -1;
}
}
}
} while(flag);
}
else {
int k;
for (k = tasks - 1; k < SLAVE_NUM; k++) {
close(pipesOutput[k][0]);
close(pipesInput[k][1]);
pipesOutput[k][0] = -1;
pipesInput[k][0] = -1;
}
do {
int n;
flag = 0;
FD_ZERO(&pipesOutputSet);
for (n = 0; n < tasks; n++) {
if (pipesOutput[n][0] != -1) {
FD_SET(pipesOutput[n][0], &pipesOutputSet);
flag = 1;
}
}
if (flag) {
if ((count = select(pipesOutput[tasks - 1][1] + 1, &pipesOutputSet, NULL, NULL, NULL)) <= 0)
printSystemError("select() -- ERROR");
for (r = 0; r < SLAVE_NUM && count > 0; r++) {
if (FD_ISSET(pipesOutput[r][0], &pipesOutputSet)) {
readBytes = 0;
do {
if ((readBytes += read(pipesOutput[r][0], buf + readBytes, MAX_SIZE)) < 0)
printSystemError("read() -- ERROR");
} while (buf[readBytes - 1] != '\n');
buf[readBytes] = 0;
close(pipesOutput[r][0]);
close(pipesInput[r][1]);
pipesOutput[r][0] = -1;
pipesInput[r][0] = -1;
printf("\n%s\n", buf);
count--;
}
}
}
} while (flag);
}
// if (tasks >= SLAVE_NUM) {
// for (r = 0; r < SLAVE_NUM && count > 0; r++) {
// if (FD_ISSET(pipesOutput[r][0], &pipesOutputSet)) {
// readBytes = 0;
// do {
// if ((readBytes += read(pipesOutput[r][0], buf + readBytes, MAX_SIZE)) < 0)
// printSystemError("read() -- ERROR");
// } while (buf[readBytes - 1] != '\n');
// buf[readBytes] = 0;
// if (tasks < argc)
// write(pipesInput[r][1], argv[tasks++], MAX_SIZE);
// else {
//
// }
// printf("%s", buf);
// count--;
// }
// }
// }
// else {
// for (r = 0; r < tasks && count > 0; r++) {
// if (FD_ISSET(pipesOutput[r][0], &pipesOutputSet)) {
// readBytes = 0;
// do {
// if ((readBytes += read(pipesOutput[r][0], buf + readBytes, MAX_SIZE)) < 0)
// printSystemError("read() -- ERROR");
// } while (buf[readBytes - 1] != '\n');
// buf[readBytes] = 0;
// printf("%s", buf);
// count--;
// }
// }
// }
// int fd;
// char buf[MAX_SIZE];
// if ((fd = select(pipesOutput[SLAVE_NUM - 1][0] + 1, &pipesOutputSet, NULL, NULL, NULL)) < 0)
// printSystemError("select() -- ERROR");
//
// int r = 0, readBytes;
// for (; r < SLAVE_NUM && fd > 0; r++) {
// if (FD_ISSET(pipesOutput[r][0], &pipesOutputSet)) {
// readBytes = 0;
// do {
// if ((readBytes += read(pipesOutput[r][0], buf + readBytes, MAX_SIZE)) < 0)
// printSystemError("read() -- ERROR");
// } while (buf[readBytes - 1] != '\n');
// buf[readBytes] = 0;
// printf("%s", buf);
// fd--;
// }
// }
return 0;
}
//void (int pipesOutput[SLAVE_NUM][2], int pipesInput[SLAVE_NUM][2]) {
void create(int pipesOutput[SLAVE_NUM][2], int pipesInput[SLAVE_NUM][2]) {
pid_t pid[SLAVE_NUM];
int i = 0;
for (; i < SLAVE_NUM; i++) {
if (pipe(pipesOutput[i]) < 0)
printSystemError("pipe() -- ERROR");
if (pipe(pipesInput[i]) < 0)
printSystemError("pipe() -- ERROR");
if ((pid[i] = fork()) == 0) {
if (close(pipesOutput[i][0]) < 0)
printSystemError("close() -- ERROR");
if (dup2(pipesOutput[i][1], STDOUT_FILENO) < 0)
printSystemError("dup2() -- ERROR 1");
if (close(pipesOutput[i][1]) < 0)
printSystemError("close() -- ERROR");
if (close(pipesInput[i][1]) < 0)
printSystemError("close() -- ERROR");
if (dup2(pipesInput[i][0], STDIN_FILENO) < 0)
printSystemError("dup2() -- ERROR 2");
if (close(pipesInput[i][0]) < 0)
printSystemError("close() -- ERROR");
if (execv("slave.o", NULL) < 0)
printSystemError("execv() -- ERROR");
}
else if (pid[i] < 0)
printSystemError("fork() -- ERROR");
if (close(pipesInput[i][0]) < 0)
printSystemError("close() -- ERROR");
if (close(pipesOutput[i][1]) < 0)
printSystemError("close() -- ERROR");
}
}

123
shr_mem.c
View File

@ -1,5 +1,122 @@
#include <stdio.h>
#define _SVID_SOURCE 1
//#define _DEFAULT_SOURCE 1
#define _POSIX_C_SOURCE 200112L
int main() {
return 0;
#include <stdio.h>
#include <sys/shm.h>
#include <semaphore.h>
#include <fcntl.h>
#include <string.h>
#include "shr_mem.h"
#include "error.h"
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
sem_t * fullSem = NULL;
sem_t * emptySem = NULL;
int createShm(int totalSize, void ** shmPointer) {
int fd;
if ((fd = shm_open("/BottlerSHMem", 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)
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)
printSystemError("sem_post() -- ERROR");
return EXIT_SUCCESS;
}
void terminateShm(const char * name, int fd, void * shmPointer, int totalSize) {
if (shm_unlink(name) == -1)
printSystemError("shm_unlink() -- ERROR");
munmap(shmPointer, totalSize);
if (close(fd) == -1)
printSystemError("close() -- ERROR");
return;
}
int copyShm(char * destPointer, char * srcPointer, int len) {
int i;
for (i = 0; srcPointer[i] != '\n' && srcPointer[i] != '\0' && i < len; i++) {
destPointer[i] = srcPointer[i];
}
destPointer[i++] = '\0';
return i;
}
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)
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");
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;
}

9
shr_mem.h Normal file
View File

@ -0,0 +1,9 @@
#ifndef SHR_MEM
#define SHR_MEM
int createShm();
char * attachShm(int shmId);
int writeShm(void * address, char * str, int len);
int readShm(char * address, char * str, int len);
#endif

14
slave.c
View File

@ -2,6 +2,7 @@
This is a personal academic project. Dear PVS-Studio, please check it.
PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
*/
#define _POSIX_C_SOURCE 2
#include <stdio.h>
#include <stdlib.h>
@ -12,15 +13,6 @@ PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
#define MAX_SIZE 300
#define COMMAND_MAX_SIZE 400
/*
Recibirá por STDIN los minisat files correspondientes
(los tomo separados con un \n aunque esto podría modificarse luego)
y, a su vez, entregará por STDOUT el resultado de minisat.
Ejemplo de uso: echo "prueba.txt" | ./slave.o
*/
//void printSystemError(char *);
void runCommand(char *, char *);
void printOutput(char *, char *);
@ -62,7 +54,7 @@ void runCommand(char * inputLine, char * output) {
//sprintf(command, "minisat %s | grep -o -e \"Number of .*[0-9]\\+\" -e \"CPU time.*\" -e \".*SATISFIABLE\" | awk '{if (NR == 1) {print $4} ; if (NR == 2) {print $4} ; if (NR == 3) {print $4 $5} ; if (NR == 4) {print $1}}'", inputLine);
sprintf(command, "minisat %s | grep -o -e \"Number of .*[0-9]\\+\" -e \"CPU time.*\" -e \".*SATISFIABLE\" | awk '{if (NR == 1) {printf \"Cantidad de variables: \" $4 \" - \"} ; if (NR == 2) {printf \"Cantidad de cláusulas: \" $4 \" - \"} ; if (NR == 3) {printf \"Tiempo de procesamiento: \" $4 $5 \" - \"} ; if (NR == 4) {printf \"Resultado: \" $1}}'", inputLine);
// Pequeña ayuda para popen: https://stackoverflow.com/questions/646241/c-run-a-system-command-and-get-output
FILE *fp;
FILE * fp;
if (!(fp = popen(command, "r")))
printSystemError("slave.c -- popen()");
@ -70,6 +62,8 @@ void runCommand(char * inputLine, char * output) {
size_t outputLength = fread(output, sizeof(char), MAX_SIZE, fp);
output[outputLength] = '\0';
if (ferror(fp))
if (pclose(fp) < 0)
printSystemError("slave.c -- pclose()");

6
smf.c
View File

@ -1,5 +1,5 @@
#include <stdio.h>
#include <semaphore.h>
int main() {
return 0;
}
#define FULL 0x4AC00
#define EMPTY 0

62
view.c
View File

@ -1,5 +1,61 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "error.h"
#include "shr_mem.h"
#include <semaphore.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
int main() {
return 0;
}
#define MAX_SIZE 300
#define MAX_AMOUNT_SIZE 10
int main(int argc, char * argv[]) {
// sleep(2);
char amount[MAX_AMOUNT_SIZE];
switch (argc) {
case 1:
fgets(amount, MAX_AMOUNT_SIZE, stdin);
amount[strcspn(amount, "\n")] = '\0';
break;
//read(STDIN_FILENO, readAddr, MAX_SIZE); break;
case 2:
strcpy(amount, argv[1]);
break;
default:
printError("View must receive amount of files by argument or stdin");
}
int files = atoi(amount);
printf("%d\n", files);
int fd;
char * shmPointer;
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){
printSystemError("mmap() -- ERROR");
}
void * shmStart;
shmStart = shmPointer;
char buf[MAX_SIZE];
while (files-- > 0) {
shmPointer += readShm(shmPointer, buf, MAX_SIZE);
printf("%s\n", buf);
}
if (munmap(shmStart, statBuf.st_size) == -1)
printSystemError("munmap() -- ERROR");
close(fd);
return EXIT_SUCCESS;
}