#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <errno.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <sstream>
#include <sys/times.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <fstream>
#include <ctime>
#include <cstdlib>
#include <chrono>
#define Dneedle_max 6
#define Dnew_line 10
using namespace std;
const int maxPar = 3;
const int maxProcess = 32;
static int end_main = 0;
int fd;
int maxproc = 0;
unsigned long len_max;
const key_t shm_id = 8274;
char *fileinmemory = 0;
char needle[Dneedle_max];
struct stat fd_st;
struct process_struct
{
pid_t pid;
int returnCode;
time_t timeProcSystem_us;
time_t timeProcUser_us;
time_t countTimeProcess_start;
time_t countTimeProcess_stop;
char *shm_start;
unsigned long shm_diff;
unsigned long shm_len;
};
struct tms bufor;
struct process_struct *process = new process_struct[maxProcess];
void sygnal (int signo, siginfo_t * sigf, void *b);
int child_extern (int processnr, int shmid, unsigned long shmdiff,
unsigned long shmlen, char *needleargv);
int
main (int argc, char *argv[])
{
pid_t pid_tmp;
sigset_t sig_mask;
int returnCode, opt_check;
int i;
struct rusage timeData;
struct sigaction sigA;
long adressum = 0;
unsigned long sizetmp;
unsigned long shmComputeDiv;
char *copyfim = 0;
const auto timeTestStart = chrono::high_resolution_clock::now ();
cout << "-------------------------- Start:" << (double) chrono::
duration_cast < chrono::microseconds >
(chrono::high_resolution_clock::now () -
timeTestStart).count () / 1000000 << "s" << endl;
if (argc == 1)
{
cout << "Nie podano parametrow w wywolaniu programu." << endl;
return 128;
}
if (argc > (maxPar + 1))
{
cout << "Za duzo parametrow, maksymalnie podaj " << maxPar <<
" parametrow." << endl;
return 129;
}
if (argc < (maxPar + 1))
{
cout << "Za malo parametrow, podaj " << maxPar << " parametrow." <<
endl;
return 129;
}
while ((opt_check = getopt (argc, argv, "vh")) != (-1))
{
switch (opt_check)
{
case 'v':
cout << "Versja 1.0\n" << endl;
return 130;
case 'h':
cout <<
"Poprawne wywolanie programu: szukany ciąg 6 znaków, plik, ile procesów."
<< endl;
cout << "Maksymalnie podaj " << maxPar << " parametrów." << endl;
return 130;
}
}
fd = open (argv[2], O_RDONLY);
if (fd == -1)
{
perror ("Multifind: open.");
return 1;
}
if (fstat (fd, &fd_st) == -1)
{
perror ("Multifind: fstat.");
return 1;
}
if (!S_ISREG (fd_st.st_mode))
{
fprintf (stderr, "%sMultifind: to nie plik.\n", argv[2]);
return 1;
}
len_max = fd_st.st_size;
cout << "-------------------------- Po open:" << (double) chrono::
duration_cast < chrono::microseconds >
(chrono::high_resolution_clock::now () -
timeTestStart).count () / 1000000 << "s" << endl;
int smid = shmget (shm_id, len_max, IPC_CREAT | SHM_NORESERVE | 0666);
if (smid == -1)
{
perror ("Multifind: shmget.");
return 1;
}
cout << "-------------------------- Po shmget:" << (double) chrono::
duration_cast < chrono::microseconds >
(chrono::high_resolution_clock::now () -
timeTestStart).count () / 1000000 << "s" << endl;
fileinmemory = (char *) shmat (smid, NULL, 0);
if (*fileinmemory == -1)
{
perror ("Multifind: shmat.");
return 1;
}
cout << "-------------------------- Przed read:" << (double) chrono::
duration_cast < chrono::microseconds >
(chrono::high_resolution_clock::now () -
timeTestStart).count () / 1000000 << "s" << endl;
int readerr = read (fd, fileinmemory, len_max);
if (readerr == -1)
{
fprintf (stderr, "Multifind: read.");
return 1;
}
cout << "-------------------------- Po read:" << (double) chrono::
duration_cast < chrono::microseconds >
(chrono::high_resolution_clock::now () -
timeTestStart).count () / 1000000 << "s" << endl;
if (close (fd) == -1)
{
perror ("Multifind: close.");
return 1;
}
if (strlen (argv[1]) != Dneedle_max)
{
fprintf (stderr, "%sZla dlugośc needle.\n", argv[1]);
return 1;
}
else
{
strcpy (needle, argv[1]);
}
maxproc = atoi (argv[3]);
if (maxproc > maxProcess)
{
fprintf (stderr, "Multifind: za duzo prosesow.");
return 1;
}
else
{
if (maxproc == 0)
{
fprintf (stderr, "Multifind: podales zero procesow.");
return 1;
}
}
shmComputeDiv = len_max / maxproc;
for (i = 0; i < maxproc; ++i)
{
process[i].shm_start = fileinmemory + (shmComputeDiv * (i));
process[i].shm_len = shmComputeDiv;
}
process[maxproc - 1].shm_len =
(fileinmemory + len_max) - process[maxproc - 1].shm_start;
printf ("@ fileinmemory: %p \n", fileinmemory);
printf ("@ len_max: 0x%lX \n", len_max);
printf ("@ sum: 0x%lX \n", (long unsigned) fileinmemory + len_max);
for (i = 1; i < maxproc; ++i)
{
sizetmp = process[i - 1].shm_len;
copyfim = process[i - 1].shm_start;
while ((copyfim[sizetmp] != (char) Dnew_line))
{
--sizetmp;
}
process[i - 1].shm_len = sizetmp;
process[i].shm_start =
process[i - 1].shm_start + process[i - 1].shm_len;
}
Brak komentarzy:
Prześlij komentarz