82 lines
2.0 KiB
C
82 lines
2.0 KiB
C
#define _POSIX_C_SOURCE 200112L
|
|
|
|
#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>
|
|
#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[]) {
|
|
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;
|
|
case 3:
|
|
strcpy(amount, argv[1]);
|
|
strcpy(masterPid, argv[2]);
|
|
break;
|
|
default:
|
|
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)
|
|
printSystemError("shm_open() -- ERROR");
|
|
|
|
int totalSize = files * MAX_OUTPUT_SIZE;
|
|
|
|
if ((shmPointer = mmap(0, totalSize, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED){
|
|
printSystemError("mmap() -- ERROR");
|
|
}
|
|
void * shmStart;
|
|
shmStart = shmPointer;
|
|
|
|
int i = 0;
|
|
char buf[MAX_SIZE];
|
|
while (files-- > 0) {
|
|
//buf[0] = '\0';
|
|
shmPointer += readShm(shmPointer, buf, MAX_SIZE);
|
|
printf("%d\t", i++);
|
|
printf("%s\n", buf);
|
|
}
|
|
|
|
if (munmap(shmStart, totalSize) == -1)
|
|
printSystemError("munmap() -- ERROR");
|
|
|
|
close(fd);
|
|
// terminateShm("/BottlerSHM", fd, shmStart, totalSize);
|
|
|
|
sigqueue(mPid, SIGUSR2, value);
|
|
|
|
return EXIT_SUCCESS;
|
|
}
|