added seekg in part1

This commit is contained in:
ItsMAX0112 2024-09-10 15:54:55 +05:30
parent 342c7225e3
commit 86df05a44f
4 changed files with 2 additions and 341 deletions

View File

@ -23,13 +23,13 @@ int main(int argc, char **argv)
int len = sizeof(pattern_to_search_for);
int search_start_position = atoi(argv[3]);
int search_end_position = atoi(argv[4]);
int find = 0;
// int find = 0;
pid_t pid = getpid();
ifstream file(file_to_search_in, ios::binary);
int len2 = search_end_position - search_start_position;
string buffer;
buffer.resize(len2);
// file.seekg(i);
file.seekg(search_start_position);
file.read(&buffer[0], len2);
//TODO
auto a = buffer.find(pattern_to_search_for);

View File

@ -1,122 +0,0 @@
#include <iostream>
#include <fstream>
#include <unistd.h>
#include <sys/wait.h>
#include <signal.h>
using namespace std;
int main(int argc, char **argv)
{
if (argc != 6)
{
cout << "usage: ./partitioner.out <path-to-file> <pattern> <search-start-position> <search-end-position> <max-chunk-size>\nprovided arguments:\n";
for (int i = 0; i < argc; i++)
cout << argv[i] << "\n";
return -1;
}
char *file_to_search_in = argv[1];
char *pattern_to_search_for = argv[2];
int search_start_position = atoi(argv[3]);
int search_end_position = atoi(argv[4]);
int max_chunk_size = atoi(argv[5]);
pid_t parent_pid = getpid();
pid_t left_child_pid, right_child_pid, searcher_pid;
cout << "[" << parent_pid << "] start position = " << search_start_position << " ; end position = " << search_end_position << "\n";
if (search_end_position - search_start_position >= max_chunk_size)
{
int mid = (search_start_position + search_end_position) / 2;
left_child_pid = fork();
if (left_child_pid == 0)
{
//Left Child
char start_str[10], mid_str[10], max_str[10];
sprintf(start_str, "%d", search_start_position);
sprintf(mid_str, "%d", mid);
sprintf(max_str, "%d", max_chunk_size);
char *args[] = { "./part2_partitioner.out",file_to_search_in , pattern_to_search_for, start_str, mid_str, max_str, NULL };
execv("./part2_partitioner.out", args);
perror("execv left partitioner failed");
return -1;
}
else if (left_child_pid > 0)
{
// Parent process
cout << "[" << parent_pid << "] forked left child " << left_child_pid << "\n";
}
else
{
perror("fork failed");
return -1;
}
right_child_pid = fork();
if (right_child_pid == 0)
{
//Right Child
char mid_plus_one_str[10], end_str[10], max_str[10];
sprintf(mid_plus_one_str, "%d", mid + 1);
sprintf(end_str, "%d", search_end_position);
sprintf(max_str, "%d", max_chunk_size);
char *args[] = { "./part2_partitioner.out", file_to_search_in, pattern_to_search_for, mid_plus_one_str, end_str, max_str, NULL };
if (execv("./part2_partitioner.out", args)==-1){
perror("execv right partitioner failed");
}
return -1;
}
else if (right_child_pid > 0)
{
// Parent process
cout << "[" << parent_pid << "] forked right child " << right_child_pid << "\n";
}
else
{
perror("fork failed");
return -1;
}
waitpid(left_child_pid, NULL, 0);
cout << "[" << parent_pid << "] left child returned\n";
waitpid(right_child_pid, NULL, 0);
cout << "[" << parent_pid << "] right child returned\n";
}
else
{
searcher_pid = fork();
if (searcher_pid == 0)
{
// Child process
//cout << "[" << getpid() << "] forked searcher child " << getpid() << "\n";
char start_str[10], end_str[10];
sprintf(start_str, "%d", search_start_position);
sprintf(end_str, "%d", search_end_position);
char *args[] = { "./part2_searcher.out", file_to_search_in, pattern_to_search_for, start_str, end_str ,NULL};
execv("./part2_searcher.out", args);
perror("execv searcher failed");
cout << *args<< endl;
return -1;
}
else if (searcher_pid > 0)
{
// Parent process
cout << "[" << parent_pid << "] forked searcher child " << searcher_pid << "\n";
}
else
{
perror("fork failed");
return -1;
}
waitpid(searcher_pid, NULL, 0);
cout << "[" << parent_pid << "] searcher child returned\n";
}
return 0;
}

View File

@ -1,136 +0,0 @@
#include <iostream>
#include <fstream>
#include <unistd.h>
#include <sys/wait.h>
#include <signal.h>
#include <cstring>
using namespace std;
void handler(int signo, siginfo_t *info, void *context)
{
if(signo == SIGTERM)
{
cout <<"["<< getpid()<<"] "<<"received SIGTERM\n";
exit(0);
}
}
int main(int argc, char **argv)
{
struct sigaction sa;
memset(&sa, 0, sizeof(struct sigaction));
sa.sa_sigaction = &handler;
if (sigaction(SIGTERM, &sa, NULL) == -1)
{
perror("sigaction");
return EXIT_FAILURE;
}
if (argc != 6)
{
cout << "usage: ./partitioner.out <path-to-file> <pattern> <search-start-position> <search-end-position> <max-chunk-size>\nprovided arguments:\n";
for (int i = 0; i < argc; i++)
cout << argv[i] << "\n";
return -1;
}
char *file_to_search_in = argv[1];
char *pattern_to_search_for = argv[2];
int search_start_position = atoi(argv[3]);
int search_end_position = atoi(argv[4]);
int max_chunk_size = atoi(argv[5]);
pid_t my_pid = getpid();
pid_t left_child_pid, right_child_pid, searcher_pid;
cout << "[" << my_pid << "] start position = " << search_start_position << " ; end position = " << search_end_position << "\n";
if (search_end_position - search_start_position + 1 > max_chunk_size)
{
int mid_position = (search_start_position + search_end_position) / 2;
left_child_pid = fork();
if (left_child_pid == 0)
{
// Child process
//cout << "[" << getpid() << "] forked left child " << getpid() << "\n";
char start_str[10], mid_str[10], max_str[10];
sprintf(start_str, "%d", search_start_position);
sprintf(mid_str, "%d", mid_position);
sprintf(max_str, "%d", max_chunk_size);
char *args[] = { "./part3_partitioner.out",file_to_search_in , pattern_to_search_for, start_str, mid_str, max_str, NULL };
execv("./part2_partitioner.out", args);
perror("execv left partitioner failed");
return -1;
}
else if (left_child_pid > 0)
{
// Parent process
cout << "[" << my_pid << "] forked left child " << left_child_pid << "\n";
}
else
{
perror("fork failed");
return -1;
}
right_child_pid = fork();
if (right_child_pid == 0)
{
// Child process
//cout << "[" << getpid() << "] forked right child " << getpid() << "\n";
char mid_plus_one_str[10], end_str[10], max_str[10];
sprintf(mid_plus_one_str, "%d", mid_position + 1);
sprintf(end_str, "%d", search_end_position);
sprintf(max_str, "%d", max_chunk_size);
char *args[] = { "./part3_partitioner.out", file_to_search_in, pattern_to_search_for, mid_plus_one_str, end_str, max_str, NULL };
if (execv("./part2_partitioner.out", args)==-1){
perror("execv right partitioner failed");
}
return -1;
}
else if (right_child_pid > 0)
{
// Parent process
cout << "[" << my_pid << "] forked right child " << right_child_pid << "\n";
}
else
{
perror("fork failed");
return -1;
}
waitpid(left_child_pid, NULL, 0);
cout << "[" << my_pid << "] left child returned\n";
waitpid(right_child_pid, NULL, 0);
cout << "[" << my_pid << "] right child returned\n";
}
else
{
searcher_pid = fork();
if (searcher_pid == 0)
{
// Child process
//cout << "[" << getpid() << "] forked searcher child " << getpid() << "\n";
char start_str[10], end_str[10];
sprintf(start_str, "%d", search_start_position);
sprintf(end_str, "%d", search_end_position);
char *args[] = { "./part3_searcher.out", file_to_search_in, pattern_to_search_for, start_str, end_str ,NULL};
execv("./part2_searcher.out", args);
perror("execv searcher failed");
cout << *args<< endl;
return -1;
}
else if (searcher_pid > 0)
{
// Parent process
cout << "[" << my_pid << "] forked searcher child " << searcher_pid << "\n";
}
else
{
perror("fork failed");
return -1;
}
waitpid(searcher_pid, NULL, 0);
cout << "[" << my_pid << "] searcher child returned\n";
}
return 0;
}

View File

@ -1,81 +0,0 @@
#include <iostream>
#include <fstream>
#include <cstring>
#include <unistd.h>
#include <signal.h>
#include <string>
#include <cstring>
using namespace std;
void handler(int signo, siginfo_t *info, void *context)
{
if(signo == SIGTERM)
{
cout <<"["<< getpid()<<"] "<<"received SIGTERM\n";
exit(0);
}
}
int main(int argc, char **argv)
{
struct sigaction sa;
memset(&sa, 0, sizeof(struct sigaction));
sa.sa_sigaction = &handler;
if (sigaction(SIGTERM, &sa, NULL) == -1)
{
perror("sigaction");
return EXIT_FAILURE;
}
pid_t pgid = getpgid(getpid());
//cout << pgid<< endl;
if(argc != 5)
{
cout <<"usage: ./partitioner.out <path-to-file> <pattern> <search-start-position> <search-end-position>\nprovided arguments:\n";
for(int i = 0; i < argc; i++)
cout << argv[i] << "\n";
return -1;
}
char *file_to_search_in = argv[1];
char *pattern_to_search_for = argv[2];
int search_start_position = atoi(argv[3]);
int search_end_position = atoi(argv[4]);
//TODO
ifstream inputFile(file_to_search_in);
//cout << file_to_search_in <<"\n";
//cout << pattern_to_search_for <<"\n";
string pattern = pattern_to_search_for;
//cout << pattern;
int len = pattern.length();
//cout << fork() << endl;
if ( !inputFile.is_open()){
cerr << "Error opening the file" << endl;
return 1;
}
//string line;
//cout << "File Content: " << endl;
int i=search_start_position;
char ch;
string pat="";
inputFile.seekg(search_start_position);
while (inputFile.get(ch) and i<=search_end_position){
i++;
if(pat.length() < len){
pat = pat + ch;
continue;
}
pat.erase(0,1);
pat = pat+ ch;
//pid_t pid = fork();
//cout <<pat<<endl;
if (pat == pattern){
cout<< "[" << getpid() <<"] found at " << i - len << endl;
killpg(pgid,SIGTERM);
return 0;
//break;
}
}
cout <<"[" <<getpid() <<"] didn't find\n";
return 1;
}