Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef struct queue_data
- {
- unsigned short PID;
- unsigned short priority;
- unsigned long exec_time;
- unsigned int memory_start;
- unsigned int memory_finish;
- unsigned int quantum;
- struct generic_queue *stack_data;
- }Data;
- typedef struct SandF
- {
- struct generic_queue *first;
- struct generic_queue *last;
- }Delimitation;
- typedef struct generic_queue
- {
- void *info;
- struct generic_queue *next;
- }TCell, *TList, **aList;
- int extrQueue(Delimitation *Q, aList aQ)
- {
- if (!(*Q).first)
- {
- return 0;
- }
- (*aQ) = (*Q).first;
- (*Q).first = (*Q).first->next;
- return 1;
- }
- int insrtQueue(void* info, Delimitation *Q)
- {
- TList aux = malloc(sizeof(TCell));
- if (!aux)
- {
- printf("Eroare\n");
- return 0;
- }
- aux->info = info;
- aux->next = NULL;
- if (!(*Q).first)
- {
- (*Q).first = (*Q).last = aux;
- }
- else
- {
- (*Q).last->next = aux;
- (*Q).last = aux;
- }
- return 1;
- }
- int Push(void* info, aList aS)
- {
- TList aux = malloc(sizeof(TCell));
- if (!aux)
- {
- printf("Eroare\n");
- return 0;
- }
- aux->info = info;
- aux->next = (*aS);
- (*aS) = aux;
- return 1;
- }
- int Pop(aList aS, aList elem)
- {
- if (!aS)
- {
- printf("Eroare\n");
- return 0;
- }
- else
- {
- (*elem) = (*aS);
- (*aS) = (*aS)->next;
- (*elem)->next = NULL;
- }
- return 1;
- }
- int sortPID(Delimitation *DelQ, Delimitation *Running, int *nr_processes)
- {
- Data *auxData = malloc(sizeof(Data));
- TList ListAux = malloc(sizeof(TCell));
- Delimitation *aux = malloc(sizeof(Delimitation));
- Delimitation *aux2 = malloc(sizeof(Delimitation));
- Delimitation *sortPID = malloc(sizeof(Delimitation));
- aux->first = NULL;
- aux2->first = NULL;
- sortPID->first = NULL;
- int auxiliar = *nr_processes;
- int i;
- int currentPID = 1;
- if (Running->first != NULL)
- {
- auxData = Running->first->info;
- insrtQueue(auxData, sortPID);
- }
- while (DelQ->first != NULL)
- {
- auxData = DelQ->first->info;
- extrQueue(DelQ, &ListAux);
- insrtQueue(auxData, sortPID);
- insrtQueue(auxData, aux);
- }
- while (aux->first != NULL)
- {
- auxData = aux->first->info;
- extrQueue(aux, &ListAux);
- insrtQueue(auxData, DelQ);
- }
- while (*nr_processes != 0)
- {
- auxData = sortPID->first->info;
- unsigned int min_PID = auxData->PID;
- for (i = 0; i < *nr_processes; i++)
- {
- auxData = sortPID->first->info;
- if (auxData->PID < min_PID)
- {
- min_PID = auxData->PID;
- }
- extrQueue(sortPID, &ListAux);
- insrtQueue(auxData, aux);
- }
- (*nr_processes)--;
- while (aux->first != NULL)
- {
- auxData = aux->first->info;
- if (auxData->PID == min_PID)
- {
- insrtQueue(auxData, sortPID);
- extrQueue(aux, &ListAux);
- }
- else
- {
- insrtQueue(auxData, aux2);
- extrQueue(aux, &ListAux);
- }
- }
- while (sortPID->first != NULL)
- {
- auxData = sortPID->first->info;
- extrQueue(sortPID, &ListAux);
- insrtQueue(auxData, aux2);
- }
- while (aux2->first != NULL)
- {
- auxData = aux2->first->info;
- extrQueue(aux2, &ListAux);
- insrtQueue(auxData, sortPID);
- }
- }
- while (sortPID->first != NULL)
- {
- auxData = sortPID->first->info;
- if (currentPID != auxData->PID)
- {
- (*nr_processes) = auxiliar;
- return currentPID;
- }
- currentPID++;
- extrQueue(sortPID, &ListAux);
- }
- (*nr_processes) = auxiliar;
- return currentPID;
- }
- int sortMemory(Delimitation *Mem, Delimitation* DelQ, Delimitation* Running, int *nr_processes)
- {
- Data *auxData = malloc(sizeof(Data));
- TList ListAux = malloc(sizeof(TCell));
- Delimitation *aux = malloc(sizeof(Delimitation));
- Delimitation *aux2 = malloc(sizeof(Delimitation));
- aux->first = NULL;
- aux2->first = NULL;
- Mem->first = NULL;
- int auxiliary = *nr_processes;
- int i;
- if (Running->first != NULL)
- {
- auxData = Running->first->info;
- insrtQueue(auxData, Mem);
- }
- while (DelQ->first != NULL)
- {
- auxData = DelQ->first->info;
- extrQueue(DelQ, &ListAux);
- insrtQueue(auxData, Mem);
- insrtQueue(auxData, aux);
- }
- while (aux->first != NULL)
- {
- auxData = aux->first->info;
- extrQueue(aux, &ListAux);
- insrtQueue(auxData, DelQ);
- }
- while (*nr_processes > 1)
- {
- auxData = Mem->first->info;
- unsigned int min_memory = auxData->memory_start;
- int min_PID = auxData->PID;
- for (i = 0; i < *nr_processes; i++)
- {
- auxData = Mem->first->info;
- if (auxData->memory_start < min_memory)
- {
- min_memory = auxData->memory_start;
- min_PID = auxData->PID;
- }
- extrQueue(Mem, &ListAux);
- insrtQueue(auxData, aux);
- }
- (*nr_processes)--;
- while (aux->first != NULL)
- {
- auxData = aux->first->info;
- if (auxData->PID == min_PID)
- {
- insrtQueue(auxData, Mem);
- extrQueue(aux, &ListAux);
- }
- else
- {
- insrtQueue(auxData, aux2);
- extrQueue(aux, &ListAux);
- }
- }
- while (Mem->first != NULL)
- {
- auxData = Mem->first->info;
- extrQueue(Mem, &ListAux);
- insrtQueue(auxData, aux2);
- }
- while (aux2->first != NULL)
- {
- auxData = aux2->first->info;
- extrQueue(aux2, &ListAux);
- insrtQueue(auxData, Mem);
- }
- }
- *nr_processes = auxiliary;
- return 1;
- }
- void freeMemory(Delimitation* DelMem, Delimitation* Mem, Delimitation *DelQ, Delimitation *Running, int *nr_processes)
- {
- int m_finish1 = 0;
- Delimitation *DelAux = malloc(sizeof(Delimitation));
- TList ListAux = malloc(sizeof(TCell));
- TList aux = DelAux->first;
- DelAux->first = NULL;
- DelMem->first = NULL;
- sortMemory(Mem, DelQ, Running, nr_processes);
- while (Mem->first != NULL)
- {
- Data *aux2;
- aux2 = Mem->first->info;
- int m_start2, m_finish2, s;
- m_start2 = aux2->memory_start;
- m_finish2 = aux2->memory_finish;
- s = m_start2 - m_finish1;
- insrtQueue(s, DelMem);
- extrQueue(Mem, &ListAux);
- insrtQueue(aux2, DelAux);
- m_finish1 = m_finish2;
- }
- while (DelAux->first != NULL)
- {
- aux = DelAux->first;
- Data *aux2 = aux->info;
- extrQueue(DelAux, &aux);
- insrtQueue(aux2, Mem);
- }
- insrtQueue((int*)3145728 - m_finish1, DelMem);
- }
- int add(unsigned int memory, unsigned int time, unsigned short priority,
- Delimitation *DelQ, Delimitation *MemDelim,
- Delimitation *Running, int *nr_processes, Delimitation *Mem)
- {
- Data *queueData = malloc(sizeof(Data));
- queueData->exec_time = time;
- queueData->priority = priority;
- queueData->stack_data = NULL;
- TList ListAux = malloc(sizeof(TCell));
- int currentPID = sortPID(DelQ, Running, nr_processes);
- if (DelQ->first == NULL && Running->first == NULL)
- {
- TList aux = malloc(sizeof(TCell));
- if (!aux)
- {
- printf("Eroare\n");
- return 0;
- }
- queueData->PID = 1;
- queueData->memory_start = 0;
- queueData->memory_finish = memory - 1;
- insrtQueue(queueData, Running);
- (*nr_processes)++;
- printf("Process created successfully: PID: %d, Memory starts at %x.\n", queueData->PID, queueData->memory_start);
- }
- else
- {
- TList aux = malloc(sizeof(TCell));
- Delimitation *DelAux = malloc(sizeof(Delimitation));
- Data *auxData = malloc(sizeof(Data));
- Data *auxData2 = malloc(sizeof(Data));
- DelAux->first = NULL;
- if (!aux)
- {
- printf("Eroare\n");
- return 0;
- }
- int i = 0;
- while ((unsigned int)MemDelim->first->info < memory)
- {
- if (MemDelim->first == NULL)
- {
- printf("Cannot reserve memory for PID 0x%d.\n", currentPID);
- return 0;
- }
- extrQueue(MemDelim, &ListAux);
- if (i > 0)
- {
- auxData = Mem->first->info;
- extrQueue(Mem, &ListAux);
- insrtQueue(auxData, DelAux);
- }
- i++;
- }
- auxData = Mem->first->info;
- queueData->PID = currentPID;
- queueData->memory_start = auxData->memory_finish + 1;
- queueData->memory_finish = queueData->memory_start + memory - 1;
- insrtQueue(queueData, DelAux);
- printf("Process created successfully: PID: %d, Memory starts at 0x%x.\n", queueData->PID, queueData->memory_start);
- (*nr_processes)++;
- while (DelQ->first != NULL)
- {
- auxData = DelQ->first->info;
- extrQueue(DelQ, &ListAux);
- insrtQueue(auxData, DelAux);
- }
- while (DelAux->first != NULL)
- {
- aux = DelAux->first;
- auxData = aux->info;
- extrQueue(DelAux, &aux);
- insrtQueue(auxData, DelQ);
- }
- }
- freeMemory(MemDelim, Mem, DelQ, Running, nr_processes);
- return 1;
- }
- int sortQueue(Delimitation* DelQ, int *nr_processes, Delimitation *Finished, Delimitation *Running)
- {
- Data *auxData = malloc(sizeof(Data));
- Delimitation *aux = malloc(sizeof(Delimitation));
- Delimitation *aux2 = malloc(sizeof(Delimitation));
- TList ListAux = malloc(sizeof(TCell));
- aux->first = NULL;
- aux2->first = NULL;
- int i;
- int auxiliary = *nr_processes;
- int prevPID = 0;
- if (Running->first != NULL)
- {
- auxData = Running->first->info;
- extrQueue(Running, &ListAux);
- if (auxData->exec_time == 0)
- {
- insrtQueue(auxData, Finished);
- }
- else
- {
- insrtQueue(auxData, DelQ);
- prevPID = auxData->PID;
- }
- }
- while (DelQ->first != NULL)
- {
- auxData = DelQ->first->info;
- extrQueue(DelQ, &ListAux);
- if (auxData->exec_time == 0)
- {
- insrtQueue(auxData, Finished);
- }
- else
- {
- insrtQueue(auxData, aux);
- }
- }
- while (aux->first != NULL)
- {
- auxData = aux->first->info;
- extrQueue(aux, &ListAux);
- insrtQueue(auxData, DelQ);
- }
- while (*nr_processes > 0)
- {
- auxData = DelQ->first->info;
- int min_time = auxData->exec_time;
- int min_pirority = auxData->priority;
- int min_PID = auxData->PID;
- for (i = 0; i < *nr_processes; i++)
- {
- auxData = DelQ->first->info;
- if (auxData->priority > min_pirority)
- {
- min_pirority = auxData->priority;
- min_time = auxData->exec_time;
- min_PID = auxData->PID;
- }
- else if (auxData->priority == min_pirority)
- {
- if (auxData->exec_time < min_time)
- {
- min_pirority = auxData->priority;
- min_time = auxData->exec_time;
- min_PID = auxData->PID;
- }
- else if (auxData->exec_time == min_time)
- {
- if (auxData->PID < min_PID)
- {
- min_pirority = auxData->priority;
- min_time = auxData->exec_time;
- min_PID = auxData->PID;
- }
- }
- }
- extrQueue(DelQ, &ListAux);
- insrtQueue(auxData, aux);
- }
- (*nr_processes)--;
- while (aux->first != NULL)
- {
- auxData = aux->first->info;
- if (auxData->PID == min_PID)
- {
- insrtQueue(auxData, DelQ);
- extrQueue(aux, &ListAux);
- }
- else
- {
- insrtQueue(auxData, aux2);
- extrQueue(aux, &ListAux);
- }
- }
- //printf("\n\n");
- //print_waiting(DelQ);
- while (DelQ->first != NULL)
- {
- auxData = DelQ->first->info;
- extrQueue(DelQ, &ListAux);
- insrtQueue(auxData, aux2);
- }
- while (aux2->first != NULL)
- {
- auxData = aux2->first->info;
- extrQueue(aux2, &ListAux);
- insrtQueue(auxData, DelQ);
- }
- }
- if (DelQ->first != NULL)
- {
- auxData = DelQ->first->info;
- extrQueue(DelQ, &ListAux);
- insrtQueue(auxData, Running);
- }
- *nr_processes = auxiliary;
- return 1;
- }
- int get(Delimitation* DelQ, Delimitation *Finished, int searchedPID, Delimitation *Running)
- {
- Data *auxData = malloc(sizeof(Data));
- TList ListAux = malloc(sizeof(TCell));
- Delimitation *aux = malloc(sizeof(Delimitation));
- aux->first = NULL;
- int found = 0;
- auxData = Running->first->info;
- if (auxData->PID == searchedPID)
- {
- printf("Process %d is running (remaining_time: %d)\n", searchedPID, auxData->exec_time);
- found = 1;
- }
- if (found == 1)
- {
- return 1;
- }
- while (DelQ->first != NULL)
- {
- auxData = DelQ->first->info;
- extrQueue(DelQ, &ListAux);
- insrtQueue(auxData, aux);
- if (auxData->PID == searchedPID)
- {
- printf("Process %d is waiting (remaining_time: %d)\n", searchedPID, auxData->exec_time);
- found = 1;
- }
- }
- while (aux->first != NULL)
- {
- auxData = aux->first->info;
- extrQueue(aux, &ListAux);
- insrtQueue(auxData, DelQ);
- }
- if (found == 0)
- {
- while (Finished->first != NULL)
- {
- auxData = Finished->first->info;
- extrQueue(Finished, &ListAux);
- insrtQueue(auxData, aux);
- if (auxData->PID == searchedPID)
- {
- printf("Process %d is finished.\n", searchedPID);
- found = 1;
- }
- }
- while (aux->first != NULL)
- {
- auxData = aux->first->info;
- extrQueue(aux, &ListAux);
- insrtQueue(auxData, Finished);
- }
- }
- if (found == 0)
- {
- printf("Process %d is not found.\n", searchedPID);
- }
- return 1;
- }
- int push_stack(Delimitation* DelQ, int searchedPID, int dataBytes, Delimitation *Running)
- {
- Data *auxData = malloc(sizeof(Data));
- TList ListAux = malloc(sizeof(TCell));
- Delimitation *aux = malloc(sizeof(Delimitation));
- aux->first = NULL;
- int pushed = 0;
- auxData = Running->first->info;
- if (auxData->PID == searchedPID)
- {
- Push((int *)dataBytes, &(auxData->stack_data));
- pushed = 1;
- }
- if (pushed == 1)
- {
- return 1;
- }
- while (DelQ->first != NULL)
- {
- auxData = DelQ->first->info;
- extrQueue(DelQ, &ListAux);
- insrtQueue(auxData, aux);
- if (auxData->PID == searchedPID)
- {
- Push((int*)dataBytes, &auxData->stack_data);
- pushed = 1;
- }
- }
- while (aux->first != NULL)
- {
- auxData = aux->first->info;
- extrQueue(aux, &ListAux);
- insrtQueue(auxData, DelQ);
- }
- if (pushed == 0)
- {
- printf("PID %d not found.\n", searchedPID);
- }
- return 1;
- }
- int pop_stack(Delimitation* DelQ, int searchedPID, Delimitation *Running)
- {
- Data *auxData = malloc(sizeof(Data));
- TList ListAux = malloc(sizeof(TCell));
- Delimitation *aux = malloc(sizeof(Delimitation));
- aux->first = NULL;
- int popped = 0;
- auxData = Running->first->info;
- if (auxData->PID == searchedPID)
- {
- if (auxData->stack_data == NULL)
- {
- printf("Empty stack PID %d.\n", searchedPID);
- }
- else
- {
- Pop(&auxData->stack_data, &ListAux);
- popped = 1;
- }
- }
- if (popped == 1)
- {
- return 1;
- }
- while (DelQ->first != NULL)
- {
- auxData = DelQ->first->info;
- extrQueue(DelQ, &ListAux);
- insrtQueue(auxData, aux);
- if (auxData->PID == searchedPID)
- {
- if (auxData->stack_data == NULL)
- {
- popped = 1;
- printf("Empty stack PID %d.\n", searchedPID);
- }
- else
- {
- Pop(&auxData->stack_data, &ListAux);
- popped = 1;
- }
- }
- }
- while (aux->first != NULL)
- {
- auxData = aux->first->info;
- extrQueue(aux, &ListAux);
- insrtQueue(auxData, DelQ);
- }
- if (popped == 0)
- {
- printf("PID %d not found.\n", searchedPID);
- }
- return 1;
- }
- int print_stack(Delimitation* DelQ, int searchedPID, Delimitation *Running)
- {
- Data *auxData = malloc(sizeof(Data));
- TList ListAux = NULL;
- TList ListAux2 = NULL;
- Delimitation *aux = malloc(sizeof(Delimitation));
- aux->first = NULL;
- int popped = 0;
- auxData = Running->first->info;
- if (auxData->PID == searchedPID)
- {
- if (auxData->stack_data == NULL)
- {
- popped = 1;
- printf("Empty stack PID %d.\n", searchedPID);
- }
- else
- {
- popped = 1;
- printf("Stack of PID %d:", searchedPID);
- while (auxData->stack_data != NULL)
- {
- Pop(&auxData->stack_data, &ListAux);
- printf(" %d", (int)ListAux->info);
- Push((int*)ListAux->info, &ListAux2);
- }
- printf(".\n");
- while (ListAux2 != NULL)
- {
- Pop(&ListAux2, &ListAux);
- Push((int*)ListAux->info, &auxData->stack_data);
- }
- }
- }
- while (DelQ->first != NULL)
- {
- auxData = DelQ->first->info;
- extrQueue(DelQ, &ListAux);
- insrtQueue(auxData, aux);
- if (auxData->PID == searchedPID)
- {
- if (auxData->stack_data == NULL)
- {
- popped = 1;
- printf("Empty stack PID %d.\n", searchedPID);
- }
- else
- {
- popped = 1;
- printf("Stack of PID %d:", searchedPID);
- while (auxData->stack_data != NULL)
- {
- Pop(&auxData->stack_data, &ListAux);
- printf(" %d", (int)ListAux->info);
- Push((int*)ListAux->info, &ListAux2);
- }
- printf(".\n");
- while (ListAux2 != NULL)
- {
- Pop(&ListAux2, &ListAux);
- Push((int*)ListAux->info, &auxData->stack_data);
- }
- }
- }
- }
- while (aux->first != NULL)
- {
- auxData = aux->first->info;
- extrQueue(aux, &ListAux);
- insrtQueue(auxData, DelQ);
- }
- if (popped == 0)
- {
- printf("PID %d not found.\n", searchedPID);
- }
- return 1;
- }
- int print_waiting(Delimitation* DelQ)
- {
- Data *auxData = malloc(sizeof(Data));
- TList ListAux = malloc(sizeof(TCell));
- Delimitation *aux = malloc(sizeof(Delimitation));
- aux->first = NULL;
- printf("Waiting queue:\n[");
- while (DelQ->first != NULL)
- {
- auxData = DelQ->first->info;
- extrQueue(DelQ, &ListAux);
- printf("(%d: priority = %d, remaining_time = %d)", auxData->PID, auxData->priority, auxData->exec_time);
- if (DelQ->first != NULL)
- {
- printf(",\n");
- }
- insrtQueue(auxData, aux);
- }
- printf("]\n");
- while (aux->first != NULL)
- {
- auxData = aux->first->info;
- extrQueue(aux, &ListAux);
- insrtQueue(auxData, DelQ);
- }
- return 1;
- }
- int print_finished(Delimitation* Finished)
- {
- Data *auxData = malloc(sizeof(Data));
- TList ListAux = malloc(sizeof(TCell));
- Delimitation *aux = malloc(sizeof(Delimitation));
- aux->first = NULL;
- printf("Finished queue:\n[");
- while (Finished->first != NULL)
- {
- auxData = Finished->first->info;
- extrQueue(Finished, &ListAux);
- printf("(%d: priority = %d, remaining_time = %d)", auxData->PID, auxData->priority, auxData->exec_time);
- if (Finished->first != NULL)
- {
- printf(",\n");
- }
- insrtQueue(auxData, aux);
- }
- printf("]\n");
- while (aux->first != NULL)
- {
- auxData = aux->first->info;
- extrQueue(aux, &ListAux);
- insrtQueue(auxData, Finished);
- }
- return 1;
- }
- int finish(Delimitation *DelQ, Delimitation *Running)
- {
- Data *auxData = malloc(sizeof(Data));
- TList ListAux = malloc(sizeof(TCell));
- int s = 0;
- while (DelQ->first != NULL)
- {
- auxData = DelQ->first->info;
- s = s + auxData->exec_time;
- extrQueue(DelQ, &ListAux);
- }
- while (Running->first != NULL)
- {
- auxData = Running->first->info;
- s = s + auxData->exec_time;
- extrQueue(Running, &ListAux);
- }
- printf("Total time: %d\n", s);
- return s;
- }
- int run(int time_to_run, int quantum, Delimitation *DelQ, Delimitation *Running, Delimitation *Finished, int *nr_processes)
- {
- Data *auxData = malloc(sizeof(Data));
- TList ListAux = malloc(sizeof(TCell));
- Delimitation *aux = malloc(sizeof(aux));
- aux->first = NULL;
- int i, dif;
- sortQueue(DelQ, nr_processes, Finished, Running);
- if (Running->first == NULL)
- {
- sortQueue(DelQ, nr_processes, Finished, Running);
- auxData = Running->first->info;
- auxData->quantum = quantum;
- }
- int j = 1;
- for (i = time_to_run; i > 0; i = i - quantum)
- {
- //printf("%d\n", j);
- //j++;
- if (i > quantum)
- {
- dif = quantum;
- }
- else
- {
- dif = i;
- }
- auxData = Running->first->info;
- if (auxData->quantum == 0)
- {
- auxData->quantum = quantum;
- if (DelQ->first != NULL)
- {
- extrQueue(Running, &ListAux);
- insrtQueue(auxData, aux);
- if (DelQ->first != NULL)
- {
- auxData = DelQ->first->info;
- extrQueue(DelQ, &ListAux);
- insrtQueue(auxData, Running);
- while (DelQ->first != NULL)
- {
- auxData = DelQ->first->info;
- extrQueue(DelQ, &ListAux);
- insrtQueue(auxData, aux);
- }
- while (aux->first != NULL)
- {
- auxData = aux->first->info;
- extrQueue(aux, &ListAux);
- insrtQueue(auxData, DelQ);
- }
- }
- }
- }
- if (auxData->quantum < dif)
- {
- if (auxData->exec_time <= auxData->quantum)
- {
- dif = dif - auxData->exec_time;
- extrQueue(Running, &ListAux);
- auxData->exec_time = 0;
- insrtQueue(auxData, Finished);
- (*nr_processes)--;
- sortQueue(DelQ, nr_processes, Finished, Running);
- auxData = Running->first->info;
- auxData->quantum = quantum;
- }
- else
- {
- dif = dif - auxData->exec_time;
- auxData->exec_time -= auxData->quantum;
- sortQueue(DelQ, nr_processes, Finished, Running);
- auxData = Running->first->info;
- auxData->quantum = quantum;
- }
- }
- while(dif != 0)
- {
- //sortQueue(DelQ, nr_processes, Finished, Running);
- auxData = Running->first->info;
- if (auxData->quantum == 0)
- {
- auxData->quantum = quantum;
- if (DelQ->first != NULL)
- {
- extrQueue(Running, &ListAux);
- insrtQueue(auxData, aux);
- if (DelQ->first != NULL)
- {
- auxData = DelQ->first->info;
- extrQueue(DelQ, &ListAux);
- insrtQueue(auxData, Running);
- while (DelQ->first != NULL)
- {
- auxData = DelQ->first->info;
- extrQueue(DelQ, &ListAux);
- insrtQueue(auxData, aux);
- }
- while (aux->first != NULL)
- {
- auxData = aux->first->info;
- extrQueue(aux, &ListAux);
- insrtQueue(auxData, DelQ);
- }
- }
- }
- }
- else
- {
- while (auxData->exec_time < dif)
- {
- dif = dif - auxData->exec_time;
- extrQueue(Running, &ListAux);
- auxData->exec_time = 0;
- insrtQueue(auxData, Finished);
- (*nr_processes)--;
- if (*nr_processes == 0)
- {
- break;
- }
- sortQueue(DelQ, nr_processes, Finished, Running);
- auxData = Running->first->info;
- auxData->quantum = quantum;
- }
- if (auxData->exec_time == dif)
- {
- dif = 0;
- extrQueue(Running, &ListAux);
- auxData->exec_time = 0;
- insrtQueue(auxData, Finished);
- (*nr_processes)--;
- sortQueue(DelQ, nr_processes, Finished, Running);
- auxData = Running->first->info;
- auxData->quantum = quantum;
- }
- if (auxData->exec_time > dif)
- {
- auxData = Running->first->info;
- auxData->exec_time -= dif;
- dif = 0;
- auxData->quantum = 0;
- }
- }
- }
- }
- return 1;
- }
- int execute(int argc, char *argv[])
- {
- char buffer[1000], *p, *comanda, arg_4[1000];
- int arg_1, arg_2, arg_3;
- FILE *in = fopen(argv[1], "r");
- Delimitation *DelQ = malloc(sizeof(Delimitation));
- Delimitation *MemDelim = malloc(sizeof(Delimitation));
- Delimitation *Running = malloc(sizeof(Delimitation));
- Delimitation *Mem = malloc(sizeof(Delimitation));
- Delimitation *Finished = malloc(sizeof(Delimitation));
- DelQ->first = NULL;
- MemDelim->first = NULL;
- Running->first = NULL;
- Mem->first = NULL;
- Finished->first = NULL;
- int nr_processes = 0, quantum;
- fscanf(in, "%d", &quantum);
- while (fscanf(in, "%s", buffer) == 1)
- {
- p = strtok(buffer, " \n");
- comanda = malloc(sizeof(p) + 1);
- strcpy(comanda, p);
- if (strcmp(comanda, "add") == 0)
- {
- fscanf(in, "%d", &arg_1);
- fscanf(in, "%d", &arg_2);
- fscanf(in, "%d", &arg_3);
- add(arg_1, arg_2, arg_3, DelQ, MemDelim, Running, &nr_processes, Mem);
- }
- else if (strcmp(comanda, "get") == 0)
- {
- fscanf(in, "%d", &arg_1);
- get(DelQ, Finished, arg_1, Running);
- }
- else if (strcmp(comanda, "push") == 0)
- {
- fscanf(in, "%d", &arg_1);
- fscanf(in, "%d", &arg_2);
- push_stack(DelQ, arg_1, arg_2, Running);
- }
- else if (strcmp(comanda, "pop") == 0)
- {
- fscanf(in, "%d", &arg_1);
- pop_stack(DelQ, arg_1, Running);
- }
- else if (strcmp(comanda, "print") == 0)
- {
- fscanf(in, "%s", arg_4);
- if (strcmp(arg_4, "stack") == 0)
- {
- fscanf(in, "%d", &arg_1);
- print_stack(DelQ, arg_1, Running);
- }
- else if (strcmp(arg_4, "waiting") == 0)
- {
- sortQueue(DelQ, &nr_processes, Finished, Running);
- print_waiting(DelQ);
- }
- else if (strcmp(arg_4, "finished") == 0)
- {
- print_finished(Finished);
- }
- }
- else if (strcmp(comanda, "run") == 0)
- {
- fscanf(in, "%d", &arg_1);
- run(arg_1, quantum, DelQ, Running, Finished, &nr_processes);
- }
- else if (strcmp(comanda, "finish") == 0)
- {
- finish(DelQ, Running);
- }
- }
- fclose(in);
- }
- int main(int argc, char *argv[])
- {
- TList Q = NULL;
- TList mem = NULL;
- Delimitation *DelQ = malloc(sizeof(Delimitation));
- Delimitation *MemDelim = malloc(sizeof(Delimitation));
- Delimitation *Finished = malloc(sizeof(Delimitation));
- DelQ->first = NULL;
- MemDelim->first = NULL;
- Finished->first = NULL;
- execute(argc, argv);
- //add(100, 20, 2, Q, mem, DelQ, MemDelim, 1);
- //add(200, 30, 3, Q, mem, DelQ, MemDelim, 2);
- //add(100, 20, 2, Q, mem, DelQ, MemDelim, 1);
- //sortQueue(DelQ, 2, Finished);
- Data *x = malloc(sizeof(Data));
- //x = DelQ->first->info;
- //get(DelQ, Finished, 1);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement