OS-Labs/lab2/src/part2_partitioner.cpp

138 lines
4.5 KiB
C++
Raw Normal View History

2024-09-03 00:24:21 +05:30
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <unistd.h>
#include <sys/wait.h>
#include <signal.h>
#include <string>
#include <cstring>
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 my_pid = getpid();
pid_t searcher_pid;
pid_t children[2];
cout << "[" << my_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;
children[0] = fork();
if (children[0] == 0)
{
//Left Child
char start_str[10], mid_str[10], chunk_size_str[10];
string temp = to_string(search_start_position);
strcpy(start_str, temp.c_str());
temp = to_string(mid);
strcpy(mid_str, temp.c_str());
temp = to_string(max_chunk_size);
strcpy(chunk_size_str, temp.c_str());
char *args[] = { "./part2_partitioner.out",file_to_search_in , pattern_to_search_for, start_str, mid_str, chunk_size_str, NULL };
execv("./part2_partitioner.out", args);
}
else
{
// Parent process
cout << "[" << my_pid << "] forked left child " << children[0] << "\n";
}
children[1] = fork();
if (children[1] == 0)
{
//Right Child
char mid_next[10], end_str[10], chunk_size_str[10];
string temp = to_string(mid+1);
strcpy(mid_next, temp.c_str());
temp = to_string(search_end_position);
strcpy(end_str, temp.c_str());
temp = to_string(max_chunk_size);
strcpy(chunk_size_str, temp.c_str());
char *args[] = { "./part2_partitioner.out", file_to_search_in, pattern_to_search_for, mid_next, end_str, chunk_size_str, NULL };
execv("./part2_partitioner.out", args);
}
else
{
// Parent process
cout << "[" << my_pid << "] forked right child " << children[1] << "\n";
}
2024-09-03 15:32:41 +05:30
for(int i = 0; i < 2; ++i) {
pid_t returned_pid = wait(NULL);
if(returned_pid == children[0]) {
cout << "[" << my_pid << "] left child returned\n";
}
else {
cout << "[" << my_pid << "] right child returned\n";
}
}
2024-09-03 00:24:21 +05:30
}
else
{
searcher_pid = fork();
if (searcher_pid == 0)
{
// Child process
char start_str[10], end_str[10];
2024-09-03 15:32:41 +05:30
string temp = to_string(search_start_position);
strcpy(start_str, temp.c_str());
temp = to_string(search_end_position);
strcpy(end_str, temp.c_str());
2024-09-03 00:24:21 +05:30
char *args[] = { "./part2_searcher.out", file_to_search_in, pattern_to_search_for, start_str, end_str ,NULL};
execv("./part2_searcher.out", args);
}
else
{
// Parent process
cout << "[" << my_pid << "] forked searcher child " << searcher_pid << "\n";
}
waitpid(searcher_pid, NULL, 0);
cout << "[" << my_pid << "] searcher child returned \n";
}
//TODO
//cout << "[" << my_pid << "] start position = " << search_start_position << " ; end position = " << search_end_position << "\n";
//cout << "[" << my_pid << "] forked left child " << my_children[0] << "\n";
//cout << "[" << my_pid << "] forked right child " << my_children[1] << "\n";
//cout << "[" << my_pid << "] left child returned\n";
//cout << "[" << my_pid << "] right child returned\n";
//cout << "[" << my_pid << "] left child returned\n";
//cout << "[" << my_pid << "] right child returned\n";*/
//cout << "[" << my_pid << "] forked searcher child " << searcher_pid << "\n";
//cout << "[" << my_pid << "] searcher child returned \n";
//cout << "[" << my_pid << "] received SIGTERM\n"; //applicable for Part III of the assignment
return 0;
}