From 86df05a44f6ed13f4b7e2f5123c2fc61bbc236a0 Mon Sep 17 00:00:00 2001 From: ItsMAX0112 Date: Tue, 10 Sep 2024 15:54:55 +0530 Subject: [PATCH] added seekg in part1 --- lab2/src/part1_searcher.cpp | 4 +- lab2/src/sol/nak_part2_partitioner.cpp | 122 ---------------------- lab2/src/sol/part3_partitioner.cpp | 136 ------------------------- lab2/src/sol/part3_searcher.cpp | 81 --------------- 4 files changed, 2 insertions(+), 341 deletions(-) delete mode 100644 lab2/src/sol/nak_part2_partitioner.cpp delete mode 100644 lab2/src/sol/part3_partitioner.cpp delete mode 100644 lab2/src/sol/part3_searcher.cpp diff --git a/lab2/src/part1_searcher.cpp b/lab2/src/part1_searcher.cpp index dfa5f94..193d4ec 100644 --- a/lab2/src/part1_searcher.cpp +++ b/lab2/src/part1_searcher.cpp @@ -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); diff --git a/lab2/src/sol/nak_part2_partitioner.cpp b/lab2/src/sol/nak_part2_partitioner.cpp deleted file mode 100644 index 732a5dc..0000000 --- a/lab2/src/sol/nak_part2_partitioner.cpp +++ /dev/null @@ -1,122 +0,0 @@ -#include -#include -#include -#include -#include - -using namespace std; - -int main(int argc, char **argv) -{ - if (argc != 6) - { - cout << "usage: ./partitioner.out \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; -} \ No newline at end of file diff --git a/lab2/src/sol/part3_partitioner.cpp b/lab2/src/sol/part3_partitioner.cpp deleted file mode 100644 index 6221fde..0000000 --- a/lab2/src/sol/part3_partitioner.cpp +++ /dev/null @@ -1,136 +0,0 @@ -#include -#include -#include -#include -#include -#include -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 \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; -} \ No newline at end of file diff --git a/lab2/src/sol/part3_searcher.cpp b/lab2/src/sol/part3_searcher.cpp deleted file mode 100644 index 7e36514..0000000 --- a/lab2/src/sol/part3_searcher.cpp +++ /dev/null @@ -1,81 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -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 \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 <