diff --git a/lab2/CS22BT001_MC22BT009_CS22BT023_report.pdf b/lab2/CS22BT001_MC22BT009_CS22BT023_report.pdf new file mode 100644 index 0000000..0be6b4f Binary files /dev/null and b/lab2/CS22BT001_MC22BT009_CS22BT023_report.pdf differ diff --git a/lab2/src/part3_searcher.cpp b/lab2/src/part3_searcher.cpp index 0575f5b..090b960 100644 --- a/lab2/src/part3_searcher.cpp +++ b/lab2/src/part3_searcher.cpp @@ -1,3 +1,5 @@ +// The logic of this part of the assignment was inspired from CS038 Om Nakrani as we were stuck and were facing issues to get it to work properly. + #include #include #include diff --git a/lab2/src/sol/nak_part2_partitioner.cpp b/lab2/src/sol/nak_part2_partitioner.cpp new file mode 100644 index 0000000..732a5dc --- /dev/null +++ b/lab2/src/sol/nak_part2_partitioner.cpp @@ -0,0 +1,122 @@ +#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 new file mode 100644 index 0000000..6221fde --- /dev/null +++ b/lab2/src/sol/part3_partitioner.cpp @@ -0,0 +1,136 @@ +#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 new file mode 100644 index 0000000..7e36514 --- /dev/null +++ b/lab2/src/sol/part3_searcher.cpp @@ -0,0 +1,81 @@ +#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 <