#include <iostream>
#include <sys/wait.h>
#include "prolificScheduler.hpp"

using namespace std;

ProlificScheduler::ProlificScheduler(){}
ProlificScheduler::~ProlificScheduler(){}

int ProlificScheduler::partition(double*s, int low, int high){
	double pivot=s[high];
	int i=low-1;
	for (int j=low; j<high; j++){
		if (s[j]<pivot){
			i++;
			double temp= s[i];
			s[i]=s[j];
			s[j]=temp;
			}
		}
		double temp = s[i +1];
		s[i+1] = s[high];
		s[high]=temp;
	return i+1;
	}

void ProlificScheduler::quick_sort(double*s, int low, int high){
	if(low<high){
	int pi=partition(s, low, high);
	quick_sort(s , low, pi-1);
	quick_sort(s, pi+1, high);
	}
}

void ProlificScheduler::execute(int k, int rows, int n, double* data){
	pid_t root=getpid();
	for (int i=0; i<k; i++){
		if (fork()==0){
			quick_sort(data + (r* n), 0, n-1);
			}
		_exit(0);
		}
	while (wait(NULL) >0);
}


