Algoritmos de planificación de CPU, implementados en lenguaje C.
Created
March 31, 2017 13:35
-
-
Save fitorec/57344289af3d2da8a7a255f2448d26b1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include<stdio.h> | |
| int main(int n, char **args) { | |
| printf("FCFS: FIRST COME FIRST SERVERED"); | |
| int np=11, procesos[10]; | |
| double tf = 0, tp;// tiempo promedio. | |
| while (np > 10 || np <= 0) { | |
| printf("\nNumero de procesos: "); | |
| scanf("%d", &np); | |
| } | |
| //para i=0, mientras i<np, hacer:... | |
| // pedimos el tamaño de cada proceso. | |
| for(int i=0; i<np; i++) { | |
| printf("\nInserte el proceso %d :", i+1); | |
| scanf("%d", &procesos[i]); | |
| } | |
| // Algoritmo FCFS | |
| for(int i=0; i<np; i++) { | |
| tf += procesos[i]; | |
| tp = tp + tf; | |
| printf("\nProceso %d, concluye en %2.1f", i+1, tf); | |
| } | |
| printf("\n-------------------------------"); | |
| printf("\nLa suma de los procesos %2.1f", tp); | |
| tp = tp / np; | |
| printf("\n\nTiempo promedio en FCFS fue de: %2.2f:", tp); | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * Licencia MIT | |
| * | |
| * Copyright (c) 2017 @Fitorec <chanerec at gmail.com>. | |
| * | |
| * Se concede permiso, de forma gratuita, a cualquier persona que obtenga una | |
| * copia de este software y de los archivos de documentación asociados | |
| * (el "Software"), para utilizar el Software sin restricción, incluyendo sin | |
| * limitación los derechos a usar, copiar, modificar, fusionar, publicar, | |
| * distribuir, sublicenciar, y/o vender copias del Software, y a permitir a las | |
| * personas a las que se les proporcione el Software a hacer lo mismo, sujeto a | |
| * las siguientes condiciones: | |
| * | |
| * El aviso de copyright anterior y este aviso de permiso se incluirán en todas | |
| * las copias o partes sustanciales del Software. | |
| * | |
| * EL SOFTWARE SE PROPORCIONA "TAL CUAL", SIN GARANTÍA DE NINGÚN TIPO, EXPRESA O | |
| * IMPLÍCITA, INCLUYENDO PERO NO LIMITADO A GARANTÍAS DE COMERCIALIZACIÓN, | |
| * IDONEIDAD PARA UN PROPÓSITO PARTICULAR Y NO INFRACCIÓN. EN NINGÚN CASO LOS | |
| * AUTORES O TITULARES DEL COPYRIGHT SERÁN RESPONSABLES DE NINGUNA RECLAMACIÓN, | |
| * DAÑOS U OTRAS RESPONSABILIDADES, YA SEA EN UNA ACCIÓN DE CONTRATO, AGRAVIO O | |
| * CUALQUIER OTRO MOTIVO, QUE SURJA DE O EN CONEXIÓN CON EL SOFTWARE O EL USO U | |
| * OTRO TIPO DE ACCIONES EN EL SOFTWARE. | |
| * | |
| */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include<stdio.h> | |
| int main(int n, char **args) { | |
| printf("Round Robin"); | |
| int np=11, procesos[10], quantum = 0, nq = 0; | |
| double tp = 0;// tiempo promedio. | |
| int finalizado = 0; | |
| while (np > 10 || np <= 0) { | |
| printf("\nNumero de procesos(%d): ", np); | |
| scanf("%d", &np); | |
| } | |
| //para i=0, mientras i<np, hacer:... | |
| // pedimos el tamaño de cada proceso. | |
| for(int i=0; i<np; i++) { | |
| printf("\nInserte el proceso %d :", i+1); | |
| scanf("%d", &procesos[i]); | |
| } | |
| while (quantum <= 0) { | |
| printf("Tamaño de quantum:"); | |
| scanf("%d", &quantum); | |
| } | |
| // Algoritmo RR | |
| while(finalizado == 0) { | |
| finalizado = 1;// Sí finalizado | |
| for(int i=0; i<np; i++) { | |
| if(procesos[i] > 0) { | |
| procesos[i] -= quantum; | |
| nq++; | |
| printf("\nQuantum[%d] proceso %d",nq, i+1); | |
| if (procesos[i]>0) { | |
| finalizado = 0; //No finalizado | |
| } else { | |
| tp += nq*quantum; | |
| } | |
| } | |
| } | |
| } | |
| tp = tp / np; | |
| printf("\nTiempo promedio RR %f:", tp); | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include<stdio.h> | |
| int main(int n, char **args) { | |
| printf("SJF: SHORTEST JOB FIRST"); | |
| // cada proceso contiene dos elementos: | |
| // - Posición 0 el tiempo del trabajo | |
| // - Posición 1 la posición original del proceso. | |
| int np=11, procesos[10][2]; | |
| double tf = 0, tp;// tiempo promedio. | |
| while (np > 10 || np <= 0) { | |
| printf("\nNumero de procesos: "); | |
| scanf("%d", &np); | |
| } | |
| //para i=0, mientras i<np, hacer:... | |
| // pedimos el tamaño de cada proceso. | |
| for(int i=0; i<np; i++) { | |
| printf("\nInserte el proceso %d :", i+1); | |
| scanf("%d", &procesos[i][0]); | |
| procesos[i][1] = i+1; | |
| } | |
| // Algoritmo SJF | |
| // ordenamos de menor a mayor | |
| for (int i=0; i<np-1; i++) { | |
| for(int j=i+1; j<np; j++) { | |
| if (procesos[j][0]<procesos[i][0]) { | |
| int aux[2] = {procesos[j][0], procesos[j][1]}; | |
| procesos[j][0] = procesos[i][0]; | |
| procesos[j][1] = procesos[i][1]; | |
| procesos[i][0] = aux[0]; | |
| procesos[i][1] = aux[1]; | |
| } | |
| } | |
| } | |
| for (int i=0; i<np; i++) { | |
| tf += procesos[i][0]; | |
| tp = tp + tf; | |
| printf("\nProceso %d, concluye en %2.1f", procesos[i][1], tf); | |
| } | |
| printf("\n-------------------------------"); | |
| printf("\nLa suma de los procesos %2.1f", tp); | |
| tp = tp / np; | |
| printf("\n\nTiempo promedio en SJF fue de: %2.2f:", tp); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment