#include <iostream>
#include <cstdlib>
#include <ctime>
#include <sys/mman.h>

#include "schedulers.h"

using namespace std;

int main() {

	int n;
	cout << "Give int: ";
	cin >> n;

	srand(time(0));

	size_t totalBytes = n * n * sizeof(int);

	int *a = (int*) mmap(
		nullptr,
		totalBytes,
		PROT_READ | PROT_WRITE,
		MAP_SHARED | MAP_ANONYMOUS,
		-1,
		0
	);
//PROT_READ & WRITE
//memory protection flags.  ~~~ memory can read and be written

//MAP_SHARED
//updates are shared across processes

//MAP_ANONYMOUS
//raw shared mmry not linked to disked file

//-1
//file descriptor (we have no file so -1)

//0
//offset inside file mapping (sme here)

	if (a== MAP_FAILED)
	{
		perror("mmap failed");
		return -1;
	}

	//fill matrix

	for (int i=0; i<n; i++){
		for(int j=0; j<n; j++){
			a[i*n +j] = rand() % 2001 -1000;
		}
	}

	//print original

	cout<<"Original matrix: "<<endl;

	for(int i=0;i<n;i++){
		for(int j=0; j<n; j++){
			cout << a[i*n +j] << " ";
		}
		cout<<endl;
	}

	ProlificScheduler sched;
	sched.run(a, n);

	cout << "\nAfter scheduler:\n";
	for(int i=0; i<n; i++){
		for(int j=0; j<n; j++){
			cout <<a[i*n +j] << " ";
		}
		cout<<endl;
	}

	munmap(a, totalBytes);
	return 0;
}
// regarding the [i*n +j]
//a is a flat shared memory!!!!
