<commit_before>#include "include/process.h"
#include "include/lib.h"
#include "include/scheduler.h"
#include "include/defs.h"
#include "include/videoDriver.h"
#include "include/mutex.h"
#include "include/scheduler.h"
#include "include/strings.h"
#include "include/keyboardDriver.h"

extern void * _get_rsp();
extern void _set_rsp(void * rsp);
extern void * get_rip();
extern void _pushAll();
extern void _popAll();

static processSlot * current = NULL;
static processSlot * foreground = NULL;
static int cantProcesses = 0;

static int amountFreeableProcess=0;
static processSlot * processToFree[100];

int debug=0;

process* getMyProcessData(){
	return current->process;
}


int insertProcess(void * entryPoint, int cargs, void ** pargs) {
	process * p = createProcess(entryPoint, (int)cargs, (void **)pargs);
    return addProcessSlot(p);

}

int addProcessSlot(process * process) {

	processSlot * slot = (processSlot *)malloc(sizeof(processSlot));
	int notPreviouslyLocked=lockScheduler();

	slot->process = process;

	if (current == NULL) {
		current = slot;
		foreground = slot;
		current->next = current;
	} else {
		slot->next = current->next;
		current->next = slot;
	}
	cantProcesses++;
	if(notPreviouslyLocked) unlockScheduler();

	return process->pid;
}

int getCurrentPid() {
	return current==NULL?-1:current->process->pid;
}

int getForegroundPid() {
	return foreground->process->pid;
}

void setForeground(int pid) {
	int i = 0;
	int notPreviouslyLocked=lockScheduler();

	processSlot * slot = foreground;
	for (; i < cantProcesses; i++) {
		if (slot->process->pid == pid) {
			// new foreground process found
			foreground = slot;
//			print("\nnew foreground proceed: ");
//			printNum(getForegroundPid());
			signalCondVar(getSTDINCondVar());
			if(notPreviouslyLocked) unlockScheduler();

			return;
		}
		slot = slot->next;
	}
	// pid does not exists
	if(notPreviouslyLocked) unlockScheduler();

	return;
}

void changeProcessState(int pid, processState state) {

	int i = 0;
    int notPreviouslyLocked=lockScheduler();
    processSlot * slot = current;
	for (; i < cantProcesses; i++) {
		if (slot->process->pid == pid) {
			// process found
			slot->process->state = state;

			if (slot->process->pid == getForegroundPid() && state == DEAD) {
				setForeground(1);
//				printAllProcesses();
			}
			if(notPreviouslyLocked) unlockScheduler();
			return;
		}
		slot = slot->next;
	}
	if(notPreviouslyLocked) unlockScheduler();
	// pid does not exists
	//print("pid not found: "); printNum(pid); print("\n");
	//print("state: "); printNum(state); print("\n");
	return;<commit_msg>BugFix cannot kill init or console<commit_after>#include "include/process.h"
#include "include/lib.h"
#include "include/scheduler.h"
#include "include/defs.h"
#include "include/videoDriver.h"
#include "include/mutex.h"
#include "include/scheduler.h"
#include "include/strings.h"
#include "include/keyboardDriver.h"

extern void * _get_rsp();
extern void _set_rsp(void * rsp);
extern void * get_rip();
extern void _pushAll();
extern void _popAll();

static processSlot * current = NULL;
static processSlot * foreground = NULL;
static int cantProcesses = 0;

static int amountFreeableProcess=0;
static processSlot * processToFree[100];

int debug=0;

process* getMyProcessData(){
	return current->process;
}


int insertProcess(void * entryPoint, int cargs, void ** pargs) {
	process * p = createProcess(entryPoint, (int)cargs, (void **)pargs);
    return addProcessSlot(p);

}

int addProcessSlot(process * process) {

	processSlot * slot = (processSlot *)malloc(sizeof(processSlot));
	int notPreviouslyLocked=lockScheduler();

	slot->process = process;

	if (current == NULL) {
		current = slot;
		foreground = slot;
		current->next = current;
	} else {
		slot->next = current->next;
		current->next = slot;
	}
	cantProcesses++;
	if(notPreviouslyLocked) unlockScheduler();

	return process->pid;
}

int getCurrentPid() {
	return current==NULL?-1:current->process->pid;
}

int getForegroundPid() {
	return foreground->process->pid;
}

void setForeground(int pid) {
	int i = 0;
	int notPreviouslyLocked=lockScheduler();

	processSlot * slot = foreground;
	for (; i < cantProcesses; i++) {
		if (slot->process->pid == pid) {
			// new foreground process found
			foreground = slot;
//			print("\nnew foreground proceed: ");
//			printNum(getForegroundPid());
			signalCondVar(getSTDINCondVar());
			if(notPreviouslyLocked) unlockScheduler();

			return;
		}
		slot = slot->next;
	}
	// pid does not exists
	if(notPreviouslyLocked) unlockScheduler();

	return;
}

void changeProcessState(int pid, processState state) {

	int i = 0;
	if(pid==0 || pid==1 && state==DEAD) return;
	int notPreviouslyLocked=lockScheduler();
    processSlot * slot = current;
	for (; i < cantProcesses; i++) {
		if (slot->process->pid == pid) {
			// process found
			slot->process->state = state;

			if (slot->process->pid == getForegroundPid() && state == DEAD) {
				setForeground(1);
//				printAllProcesses();
			}
			if(notPreviouslyLocked) unlockScheduler();
			return;
		}
		slot = slot->next;
	}
	if(notPreviouslyLocked) unlockScheduler();
	// pid does not exists
	//print("pid not found: "); printNum(pid); print("\n");
	//print("state: "); printNum(state); print("\n");
	return;
{'fraction_non_alphanumeric': 0.15029450883526504, 'fraction_numerical': 0.004180125403762113, 'mean_word_length': 4.541052631578947, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3902749', 'n_tokens_mistral': 1873, 'n_tokens_neox': 1738, 'n_words': 415}