prácticas de sistemas operativos - arantxa.ii.uam.esarantxa.ii.uam.es/~darroyo/s2.pdf ·...
TRANSCRIPT
![Page 1: Prácticas de Sistemas operativos - arantxa.ii.uam.esarantxa.ii.uam.es/~darroyo/s2.pdf · Practicas´ de Sistemas operativos ... 3 Ejercicios 2, 3, 4, 5,6, 7 y 8 w Ejercicios 4, 5,](https://reader030.vdocuments.net/reader030/viewer/2022021520/5bb9ef1e09d3f27c3d8d0396/html5/thumbnails/1.jpg)
Practicas de Sistemas operativos
David Arroyo Guardeno
Escuela Politecnica Superior de la Universidad Autonoma de Madrid
Segunda Semana: Procesos, ProcesosPadre y Procesos Hijo, familia exec()
![Page 2: Prácticas de Sistemas operativos - arantxa.ii.uam.esarantxa.ii.uam.es/~darroyo/s2.pdf · Practicas´ de Sistemas operativos ... 3 Ejercicios 2, 3, 4, 5,6, 7 y 8 w Ejercicios 4, 5,](https://reader030.vdocuments.net/reader030/viewer/2022021520/5bb9ef1e09d3f27c3d8d0396/html5/thumbnails/2.jpg)
1 Entregas
2 ProcesosForkEstados de un procesoTerminacion de procesosWaitExec
![Page 3: Prácticas de Sistemas operativos - arantxa.ii.uam.esarantxa.ii.uam.es/~darroyo/s2.pdf · Practicas´ de Sistemas operativos ... 3 Ejercicios 2, 3, 4, 5,6, 7 y 8 w Ejercicios 4, 5,](https://reader030.vdocuments.net/reader030/viewer/2022021520/5bb9ef1e09d3f27c3d8d0396/html5/thumbnails/3.jpg)
Entregas
3 Ejercicios 2, 3, 4, 5,6, 7 y 8☠ Ejercicios 4, 5, 6 y 8
![Page 4: Prácticas de Sistemas operativos - arantxa.ii.uam.esarantxa.ii.uam.es/~darroyo/s2.pdf · Practicas´ de Sistemas operativos ... 3 Ejercicios 2, 3, 4, 5,6, 7 y 8 w Ejercicios 4, 5,](https://reader030.vdocuments.net/reader030/viewer/2022021520/5bb9ef1e09d3f27c3d8d0396/html5/thumbnails/4.jpg)
Definicion de proceso
7 Instancia de un programa en ejecucion3 Programa = 1 o mas procesos3 Proceso→ espacio en memoria + datos:
conjunto de estructuras de datos
7 Elemento central en un SO7 SO↔ gestion de procesos
![Page 5: Prácticas de Sistemas operativos - arantxa.ii.uam.esarantxa.ii.uam.es/~darroyo/s2.pdf · Practicas´ de Sistemas operativos ... 3 Ejercicios 2, 3, 4, 5,6, 7 y 8 w Ejercicios 4, 5,](https://reader030.vdocuments.net/reader030/viewer/2022021520/5bb9ef1e09d3f27c3d8d0396/html5/thumbnails/5.jpg)
Programa Coleccion de instrucciones y dedatos almacenados en un fichero(a.out)
3 Conjunto de cabeceras→atributos del fichero
3 Texto del programa→intrucciones lenguaje maquina
3 bss: block started by symbol→datos a inicializar al arrancar
3 Otras secciones
![Page 6: Prácticas de Sistemas operativos - arantxa.ii.uam.esarantxa.ii.uam.es/~darroyo/s2.pdf · Practicas´ de Sistemas operativos ... 3 Ejercicios 2, 3, 4, 5,6, 7 y 8 w Ejercicios 4, 5,](https://reader030.vdocuments.net/reader030/viewer/2022021520/5bb9ef1e09d3f27c3d8d0396/html5/thumbnails/6.jpg)
Proceso Programa leıdo por el nucleo ycargado en memoria para ejecutarse
3 Segmento de texto→instrucciones CPU
3 Segmento de datos→ variablesestaticas y globales
3 Segmento de pila→ marcos depila
![Page 7: Prácticas de Sistemas operativos - arantxa.ii.uam.esarantxa.ii.uam.es/~darroyo/s2.pdf · Practicas´ de Sistemas operativos ... 3 Ejercicios 2, 3, 4, 5,6, 7 y 8 w Ejercicios 4, 5,](https://reader030.vdocuments.net/reader030/viewer/2022021520/5bb9ef1e09d3f27c3d8d0396/html5/thumbnails/7.jpg)
Marcos de pila
3 Ejecucion funcion→ marco de pila3 Informacion necesaria para restaurar el
marco de pila anterior a la llamada3 Contador de programa y puntero de pila
anteriores a la llamada a la funcion
![Page 8: Prácticas de Sistemas operativos - arantxa.ii.uam.esarantxa.ii.uam.es/~darroyo/s2.pdf · Practicas´ de Sistemas operativos ... 3 Ejercicios 2, 3, 4, 5,6, 7 y 8 w Ejercicios 4, 5,](https://reader030.vdocuments.net/reader030/viewer/2022021520/5bb9ef1e09d3f27c3d8d0396/html5/thumbnails/8.jpg)
Modos de ejecucion
Modo usuario Pila: argumentos, variableslocales, datos funciones en modousuario
Modo supervisor Marcos de pila de lasllamada a sistema
![Page 9: Prácticas de Sistemas operativos - arantxa.ii.uam.esarantxa.ii.uam.es/~darroyo/s2.pdf · Practicas´ de Sistemas operativos ... 3 Ejercicios 2, 3, 4, 5,6, 7 y 8 w Ejercicios 4, 5,](https://reader030.vdocuments.net/reader030/viewer/2022021520/5bb9ef1e09d3f27c3d8d0396/html5/thumbnails/9.jpg)
Estructuras de control del SO
7 Tabla de memoria7 Tabla de dispositivos de E/S7 Tabla de ficheros7 Tabla de procesos
![Page 10: Prácticas de Sistemas operativos - arantxa.ii.uam.esarantxa.ii.uam.es/~darroyo/s2.pdf · Practicas´ de Sistemas operativos ... 3 Ejercicios 2, 3, 4, 5,6, 7 y 8 w Ejercicios 4, 5,](https://reader030.vdocuments.net/reader030/viewer/2022021520/5bb9ef1e09d3f27c3d8d0396/html5/thumbnails/10.jpg)
Estructuras de control del SO
7 Tabla de memoria3 Gestion de la memoria principal y secundaria3 Asignacion de memoria a procesos, atributos
de proteccion de memoria, informacion paragestionar memoria
7 Tabla de dispositivos de E/S7 Tabla de ficheros7 Tabla de procesos
![Page 11: Prácticas de Sistemas operativos - arantxa.ii.uam.esarantxa.ii.uam.es/~darroyo/s2.pdf · Practicas´ de Sistemas operativos ... 3 Ejercicios 2, 3, 4, 5,6, 7 y 8 w Ejercicios 4, 5,](https://reader030.vdocuments.net/reader030/viewer/2022021520/5bb9ef1e09d3f27c3d8d0396/html5/thumbnails/11.jpg)
Estructuras de control del SO
7 Tabla de memoria7 Tabla de dispositivos de E/S
3 Administracion de dispositivos y canales deE/S
3 Almacena si dispositivo asignado a proceso3 Estado del dispositivo: informacion en curso
7 Tabla de ficheros7 Tabla de procesos
![Page 12: Prácticas de Sistemas operativos - arantxa.ii.uam.esarantxa.ii.uam.es/~darroyo/s2.pdf · Practicas´ de Sistemas operativos ... 3 Ejercicios 2, 3, 4, 5,6, 7 y 8 w Ejercicios 4, 5,](https://reader030.vdocuments.net/reader030/viewer/2022021520/5bb9ef1e09d3f27c3d8d0396/html5/thumbnails/12.jpg)
fork→ man fork
7 Proceso en UNIX: entidad tras llamada afork. . . salvo el proceso numero 0
7 Proceso padre fork−−→ proceso hijo7 proceso→ PID
7 Proceso 0: tras arrancar el sistema fork−−→init (proceso 1 arrancar−−−−→ /etc/rc*): proceso0, intercambiador | gestion de la memoriavirtual
![Page 13: Prácticas de Sistemas operativos - arantxa.ii.uam.esarantxa.ii.uam.es/~darroyo/s2.pdf · Practicas´ de Sistemas operativos ... 3 Ejercicios 2, 3, 4, 5,6, 7 y 8 w Ejercicios 4, 5,](https://reader030.vdocuments.net/reader030/viewer/2022021520/5bb9ef1e09d3f27c3d8d0396/html5/thumbnails/13.jpg)
Estados de un proceso
1
2
7
34
9
1⇒ Ejecucion modousuario2⇒ Ejecucion modosupervisor3⇒ en MP: listo paraejecutarse4⇒ en MP: durmiendo5⇒ en MS: listo paraejecutarse6⇒ en MS: durmiendo
56
8
llamada al sistema o
interrupcionretorno
sacar de MP
orden en ejecucion
dormir
despertar
cambio de
contexto
introducir en MPsacar de MP
exit
sı memoria
no memoria
fork
![Page 14: Prácticas de Sistemas operativos - arantxa.ii.uam.esarantxa.ii.uam.es/~darroyo/s2.pdf · Practicas´ de Sistemas operativos ... 3 Ejercicios 2, 3, 4, 5,6, 7 y 8 w Ejercicios 4, 5,](https://reader030.vdocuments.net/reader030/viewer/2022021520/5bb9ef1e09d3f27c3d8d0396/html5/thumbnails/14.jpg)
#include <un is td . h>#include <s t d l i b . h>#include <s t d i o . h>
i n t main ( i n t argc , char ∗argv [ ] , char ∗env [ ] ){
i n t i , a=1;
for ( i =0; i <10; i ++){switch ( f o r k ( ) ) {
case 0:a = 3;p r i n t f ( ” a=%d\n ” ,a ) ; break ;
}p r i n t f ( ‘ ‘ a=%d\n ” ,a ) ;
}e x i t ( 0 ) ;
}
![Page 15: Prácticas de Sistemas operativos - arantxa.ii.uam.esarantxa.ii.uam.es/~darroyo/s2.pdf · Practicas´ de Sistemas operativos ... 3 Ejercicios 2, 3, 4, 5,6, 7 y 8 w Ejercicios 4, 5,](https://reader030.vdocuments.net/reader030/viewer/2022021520/5bb9ef1e09d3f27c3d8d0396/html5/thumbnails/15.jpg)
Llamada a fork I
1 Verificar si espacio en tabla de procesos2 Asignar memoria al proceso hijo3 Imagen proceso padre→ memoria
proceso hijo4 Hallar una ranura de proceso libro y
copiar la ranura del proceso padre en el5 Asociar mapa de memoria a la tabla del
proceso6 Elegir un pid para el proceso hijo
![Page 16: Prácticas de Sistemas operativos - arantxa.ii.uam.esarantxa.ii.uam.es/~darroyo/s2.pdf · Practicas´ de Sistemas operativos ... 3 Ejercicios 2, 3, 4, 5,6, 7 y 8 w Ejercicios 4, 5,](https://reader030.vdocuments.net/reader030/viewer/2022021520/5bb9ef1e09d3f27c3d8d0396/html5/thumbnails/16.jpg)
Llamada a fork II
7 Notificar la existencia del proceso hijo alkernel y al sistema de archivo
8 Informar del mapa de la memoria delproceso hijo al kernel
9 Enviar mensajes de notificacion alproceso padre y al proceso hijo
![Page 17: Prácticas de Sistemas operativos - arantxa.ii.uam.esarantxa.ii.uam.es/~darroyo/s2.pdf · Practicas´ de Sistemas operativos ... 3 Ejercicios 2, 3, 4, 5,6, 7 y 8 w Ejercicios 4, 5,](https://reader030.vdocuments.net/reader030/viewer/2022021520/5bb9ef1e09d3f27c3d8d0396/html5/thumbnails/17.jpg)
¿Por que puede fallar fork?
Ø Hay que controlar la respuesta de fork
![Page 18: Prácticas de Sistemas operativos - arantxa.ii.uam.esarantxa.ii.uam.es/~darroyo/s2.pdf · Practicas´ de Sistemas operativos ... 3 Ejercicios 2, 3, 4, 5,6, 7 y 8 w Ejercicios 4, 5,](https://reader030.vdocuments.net/reader030/viewer/2022021520/5bb9ef1e09d3f27c3d8d0396/html5/thumbnails/18.jpg)
¿Por que puede fallar fork?
Ø Hay que controlar la respuesta de forkØ No hay recuros suficientes
![Page 19: Prácticas de Sistemas operativos - arantxa.ii.uam.esarantxa.ii.uam.es/~darroyo/s2.pdf · Practicas´ de Sistemas operativos ... 3 Ejercicios 2, 3, 4, 5,6, 7 y 8 w Ejercicios 4, 5,](https://reader030.vdocuments.net/reader030/viewer/2022021520/5bb9ef1e09d3f27c3d8d0396/html5/thumbnails/19.jpg)
¿Por que puede fallar fork?
Ø Hay que controlar la respuesta de forkØ No hay recuros suficientesØ Numero maximo de procesos en
ejecucion
![Page 20: Prácticas de Sistemas operativos - arantxa.ii.uam.esarantxa.ii.uam.es/~darroyo/s2.pdf · Practicas´ de Sistemas operativos ... 3 Ejercicios 2, 3, 4, 5,6, 7 y 8 w Ejercicios 4, 5,](https://reader030.vdocuments.net/reader030/viewer/2022021520/5bb9ef1e09d3f27c3d8d0396/html5/thumbnails/20.jpg)
#include <s t d l i b . h>#include <s t d i o . h>
i n t main ( i n t argc , char ∗argv [ ] , char ∗env ){
i n t i = 0 ;
switch ( f o r k ( ) ) {case −1:
pe r ro r ( ” E r ro r a l c rear procesos ” ) ;e x i t (−1) ;break ;
case 0:while ( i <10) {
sleep ( 1 ) ;p r i n t f ( ” \ t \ t Soy e l proceso h i j o : %d\n ” , i ++) ;
}break ;
defaul t :while ( i <10){
p r i n t f ( ” Soy e l proceso padre : %d\n ” , i ++) ;s leep ( 2 ) ;
}} ;
}
![Page 21: Prácticas de Sistemas operativos - arantxa.ii.uam.esarantxa.ii.uam.es/~darroyo/s2.pdf · Practicas´ de Sistemas operativos ... 3 Ejercicios 2, 3, 4, 5,6, 7 y 8 w Ejercicios 4, 5,](https://reader030.vdocuments.net/reader030/viewer/2022021520/5bb9ef1e09d3f27c3d8d0396/html5/thumbnails/21.jpg)
Terminacion de procesos
#include <s t d l i b . h>void e x i t ( i n t s ta tus ) ;. . .#include <sys / wa i t . h>p i d t wa i t ( i n t ∗ s t a t l o c ) ;. . .
p i d t p id ;i n t estado ;. . .p id = wa i t (& estado ) ;
p id = wa i t (NULL) ;
![Page 22: Prácticas de Sistemas operativos - arantxa.ii.uam.esarantxa.ii.uam.es/~darroyo/s2.pdf · Practicas´ de Sistemas operativos ... 3 Ejercicios 2, 3, 4, 5,6, 7 y 8 w Ejercicios 4, 5,](https://reader030.vdocuments.net/reader030/viewer/2022021520/5bb9ef1e09d3f27c3d8d0396/html5/thumbnails/22.jpg)
#include <sys / wa i t . h>#include <s t d l i b . h>#include <s t d i o . h>
#define NUM PROCESOS 5
i n t I = 0 ;void cod igo de l proceso ( i n t i d ){
i n t i ;for ( i =0; i <50; i ++){
p r i n t f ( ” Proceso %d : i = %d , I = %d\n ” , id , i , I ++) ;}e x i t ( i d ) ;
}
![Page 23: Prácticas de Sistemas operativos - arantxa.ii.uam.esarantxa.ii.uam.es/~darroyo/s2.pdf · Practicas´ de Sistemas operativos ... 3 Ejercicios 2, 3, 4, 5,6, 7 y 8 w Ejercicios 4, 5,](https://reader030.vdocuments.net/reader030/viewer/2022021520/5bb9ef1e09d3f27c3d8d0396/html5/thumbnails/23.jpg)
main ( ){
i n t p , pid , s a l i d a ;i n t i d [NUM PROCESOS] = {1 ,2 ,3 ,4 ,5} ;for ( p=0;p<NUM PROCESOS; p++){
p id = f o r k ( ) ;i f ( p id == −1){
pe r ro r ( ” E r ro r a l c rear un proceso : ” ) ;e x i t (−1) ;
} else i f ( p id == 0)cod igo de l proceso ( i d [ p ] ) ;
}
for ( p=0; p < NUM PROCESOS; p++){p id = wa i t (& s a l i d a ) ;p r i n t f ( ” Proceso %d con i d = %x terminado\n ” , pid , sa l ida >>8) ;
}}
![Page 24: Prácticas de Sistemas operativos - arantxa.ii.uam.esarantxa.ii.uam.es/~darroyo/s2.pdf · Practicas´ de Sistemas operativos ... 3 Ejercicios 2, 3, 4, 5,6, 7 y 8 w Ejercicios 4, 5,](https://reader030.vdocuments.net/reader030/viewer/2022021520/5bb9ef1e09d3f27c3d8d0396/html5/thumbnails/24.jpg)
#include <un is td . h>#include <s t d i o . h>#include <s t d l i b . h>
i n t main ( ) {p i d t pid , ppid ;
/ / obtener e l p id de l procesop id = ge tp id ( ) ;
/ / obtener e l p id de l padre de l procesoppid = getpp id ( ) ;
p r i n t f ( ” Mi p id e : %d\n ” , p id ) ;p r i n t f ( ” El p id de mi padre es %d\n ” , ppid ) ;
return 0;}
![Page 25: Prácticas de Sistemas operativos - arantxa.ii.uam.esarantxa.ii.uam.es/~darroyo/s2.pdf · Practicas´ de Sistemas operativos ... 3 Ejercicios 2, 3, 4, 5,6, 7 y 8 w Ejercicios 4, 5,](https://reader030.vdocuments.net/reader030/viewer/2022021520/5bb9ef1e09d3f27c3d8d0396/html5/thumbnails/25.jpg)
Familia exec
Ø No se debe realizar ningun tratamientotras llamada a execØ Solo tratamiento de error: exec no vuelve
a la funcion que la llaman a menos quese produzca un error
![Page 26: Prácticas de Sistemas operativos - arantxa.ii.uam.esarantxa.ii.uam.es/~darroyo/s2.pdf · Practicas´ de Sistemas operativos ... 3 Ejercicios 2, 3, 4, 5,6, 7 y 8 w Ejercicios 4, 5,](https://reader030.vdocuments.net/reader030/viewer/2022021520/5bb9ef1e09d3f27c3d8d0396/html5/thumbnails/26.jpg)
#include <un is td . h>#include <s t d l i b . h>#include <s t d i o . h>
i n t main ( i n t argc , char ∗ argv [ ] ) {
/ / Para e jecu ta r ls , l os argmuentos de entrada son : l s − l / b inchar ∗ l s a r g s [ 4 ] = { ” / b in / l s ” , ”− l ” , ” / b in ” , NULL} ;
/ / e jecu ta l sexecv ( l s a r g s [ 0 ] , l s a r g s ) ;
/ / aqui so lo se l l e g a s i se ha producido un e r r o rpe r ro r ( ” execv ha f a l l a d o ” ) ;
return 2; / / se devuelve codigo de e r r o r}}
![Page 27: Prácticas de Sistemas operativos - arantxa.ii.uam.esarantxa.ii.uam.es/~darroyo/s2.pdf · Practicas´ de Sistemas operativos ... 3 Ejercicios 2, 3, 4, 5,6, 7 y 8 w Ejercicios 4, 5,](https://reader030.vdocuments.net/reader030/viewer/2022021520/5bb9ef1e09d3f27c3d8d0396/html5/thumbnails/27.jpg)
#include <un is td . h>#include <s t d l i b . h>#include <s t d i o . h>
i n t main ( i n t argc , char ∗ argv [ ] ) {
char ∗ l s a r g s [ 4 ] = { ” l s ” , ”− l ” , ” / b in ” , NULL} ;
execvp ( l s a r g s [ 0 ] , l s a r g s ) ;
pe r ro r ( ” execvp ha f a l l a d o ” ) ;
return 2;}
![Page 28: Prácticas de Sistemas operativos - arantxa.ii.uam.esarantxa.ii.uam.es/~darroyo/s2.pdf · Practicas´ de Sistemas operativos ... 3 Ejercicios 2, 3, 4, 5,6, 7 y 8 w Ejercicios 4, 5,](https://reader030.vdocuments.net/reader030/viewer/2022021520/5bb9ef1e09d3f27c3d8d0396/html5/thumbnails/28.jpg)
#include <un is td . h>#include <s t d l i b . h>#include <s t d i o . h>#include <sys / types . h>#include <sys / wa i t . h>
i n t main ( i n t argc , char ∗ argv [ ] ) {char ∗ l s a r g s [ 4 ] = { ” l s ” , ”− l ” , ” / b in ” , NULL} ;p i d t p id 0 , p id ;i n t s ta tus ;p id 0 = f o r k ( ) ;i f ( p id 0 == 0){
/∗ HIIJO ∗ /p r i n t f ( ” H i j o : ejecutando l s \n ” ) ;execvp ( l s a r g s [ 0 ] , l s a r g s ) ;pe r ro r ( ” execvp ha f a l l a d o ” ) ;
}else i f ( p id 0 > 0){/∗ PADRE ∗ /i f ( ( p id = wa i t (& s ta tus ) ) < 0){
pe r ro r ( ” wa i t ha f a l l a d o ” ) ;e x i t ( 1 ) ;
}p r i n t f ( ” Padre : he terminado\n ” ) ;
}else{pe r ro r ( ”Ha f a l l a d o e l f o r k ” ) ;
e x i t ( 1 ) ;}return 0; / / re to rno con e x i t o
}
![Page 29: Prácticas de Sistemas operativos - arantxa.ii.uam.esarantxa.ii.uam.es/~darroyo/s2.pdf · Practicas´ de Sistemas operativos ... 3 Ejercicios 2, 3, 4, 5,6, 7 y 8 w Ejercicios 4, 5,](https://reader030.vdocuments.net/reader030/viewer/2022021520/5bb9ef1e09d3f27c3d8d0396/html5/thumbnails/29.jpg)
Referencias
⇒ Francisco M. Marquez. Unix,Programacion Avanzada. Editorial:Ra-Ma. 3a Edicion. ISBN: 84-7897-603-5