Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev-ds' into dev-in
Browse files Browse the repository at this point in the history
  • Loading branch information
ibnunes committed Jun 19, 2020
2 parents b96e70b + f389be7 commit d079312
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 8 deletions.
43 changes: 40 additions & 3 deletions include/scheduling.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,41 @@ int ltsched(PCB *pcb);
*/
int fcfs(PCB *pcb, MEMORY *mem, int pcbindex);

/* Function: sorted
/* Function: sortedbt
* ---------------
* Verify if the array is actually sorted.
* Verify if the array is actually sorted by burst time.
*
* p: array of process struct
* size: size of the array p
*
* return: 1 if sorted else 0
*/
int sorted(process *p, size_t size);
int sortedbt(process *p, size_t size);

/* Function: sortedpri
* ---------------
* Verify if the array is actually sorted by priority.
*
* p: array of process struct
* size: size of the array p
*
* return: 1 if sorted else 0
*/
int sortedpri(process *p, size_t size);

/* Function: comparebt
* ---------------
* Compares the priority of 2 processes.
*
* v1: process number 1
* v2: process number 2
*
* return:
* -1 when the bursttime of v1 < bursttime of v2,
* 0 when the burstime of v1 and v2 is equal,
* 1 when the bursttime of v1 > bursttime of v2.
*/
int comparepri(const void *v1, const void *v2);

/* Function: comparebt
* ---------------
Expand All @@ -69,6 +94,18 @@ int sorted(process *p, size_t size);
*/
int comparebt(const void *v1, const void *v2);

/* Function: psa
* ------------
* Executes the process in the PCB table using the Priority Scheduling Algorithm.
*
* pcb: the PCB table with the processes;
* mem: the memory array;
* pcbindex: current PCB table index at which the manager is working.
*
* return: next PCB index, or SCHEDULER_END if ended.
*/
int psa(PCB *pcb, MEMORY *mem, int pcbindex);

/* Function: sjf
* ------------
* Executes the process in the PCB table using the SJF algorithm.
Expand Down
1 change: 1 addition & 0 deletions include/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ typedef struct heap {
#define SCHEDULING_FCFS 1
#define SCHEDULING_SJF 2
#define SCHEDULING_RROBIN 4
#define SCHEDULING_PSA 8

#define SCHEDULING_COUNTER 5 //Number of instructions to be executed in a row before moving to next process

Expand Down
4 changes: 4 additions & 0 deletions src/chronos.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ int main(int argc, char const *argv[]) {
w.pcb.index = rrobin(pcb, memory, w.pcb.index);
w.pcb.rr_time++;
break;

case SCHEDULING_PSA:
w.pcb.index = psa(pcb, memory, w.pcb.index);
break;

default:
fprintf(stderr, "ERROR: Unknown scheduling algorithm. ABORTING!\n");
Expand Down
84 changes: 79 additions & 5 deletions src/scheduling.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,30 +81,104 @@ int fcfs(PCB *pcb, MEMORY *mem, int pcbindex) {
return pcbindex;
}

int sorted(process *p, size_t size) {
int sortedbt(process *p, size_t size) {
for (size_t i = 1; i < size; i++) {
if (p[i-1].timelimit > p[i].timelimit)
return 0;
}
return 1;
}

int sortedpri(process *p, size_t size) {
for (size_t i = 1; i < size; i++) {
if (p[i-1].priority > p[i].priority)
return 0;
}
return 1;
}

int comparepri(const void *v1, const void *v2) { // compara o priority dos processos
const process *p1 = (process *) v1;
const process *p2 = (process *) v2;

if (p1->priority == p2->priority)
return 0;
else if (p1->priority < p2->priority)
return -1;
else
return 1;
}

int comparebt(const void *v1, const void *v2) { // compara o Burst time dos processos
const process *p1 = (process *)v1;
const process *p2 = (process *)v2;

if (p1->timelimit == p2->timelimit) return 0;
if (p1->timelimit == p2->timelimit)
return 0;
else if (p1->timelimit < p2->timelimit)
return -1;
else return 1;
else
return 1;
}

int psa(PCB *pcb, MEMORY *mem, int pcbindex) {
debug("Working on PCB index %d.\n", pcbindex);

// Reorganizar o PCB por prioridade
if (!sortedpri(pcb->proc, pcb->top)) {
qsort(pcb->proc, pcb->top, sizeof(process), comparepri);
pcbindex = 0;
}

// Chegou ao fim da tabela PCB, não há mais processos em fila
if ((size_t)pcbindex >= pcb->top)
return SCHEDULER_END;

process *p = &(pcb->proc[pcbindex]);
switch (p->state)
{
case STATUS_NEW:
debug("Switching PID %d state to READY.\n", p->pid);
p->state = switchState(p->state, STATUS_READY);
break;

case STATUS_READY:
debug("Switching PID %d state to RUNNING.\n", p->pid);
p->state = switchState(p->state, STATUS_RUNNING);
p->timeinit = cputime;
break;

case STATUS_RUNNING:
debug("Running PID %d, instruction at PC=%d...\n", p->pid, p->counter);
p->timeused++;
run(mem, p);
break;

case STATUS_TERMINATED:
debug("Process with PID %d is TERMINATED.\n", p->pid);
pcbindex = sjf(pcb, mem, ++pcbindex);
break;

case STATUS_BLOCKED:
debug("Process with PID %d is BLOCKED.\n", p->pid);
pcbindex++;
break;

default:
fprintf(stderr, "ERROR: Unknown process state. ABORTING!\n");
exit(-1);
break;
}

return pcbindex;
}

int sjf(PCB *pcb, MEMORY *mem, int pcbindex) {
debug("Working on PCB index %d.\n", pcbindex);

// Reorganizar o PCB por Burst Time
if (!sorted(pcb->proc, pcb->size)) {
qsort(pcb->proc, pcb->size, sizeof(process), comparebt);
if (!sortedbt(pcb->proc, pcb->top)) {
qsort(pcb->proc, pcb->top, sizeof(process), comparebt);
pcbindex = 0;
}

Expand Down

0 comments on commit d079312

Please sign in to comment.