hilos capítulo 5 silberschatz galvin. copyright © 2006, intel corporation. all rights reserved....

33
Hilo s Capítulo 5 Silberschatz Galvin

Upload: jose-francisco-herrera-serrano

Post on 24-Jan-2016

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Hilos Capítulo 5 Silberschatz Galvin. Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered

Hilos

Capítulo 5

Silberschatz Galvin

Page 2: Hilos Capítulo 5 Silberschatz Galvin. Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered

Hilos2

Copyright © 2006, Intel Corporation. All rights reserved.

Sistemas Operativos

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Hilos

Concepto

Beneficios

Hilos a nivel kérnel y a nivel usuario

Modelos multihilos

Hilos de Solaris 2

Hilos de Java

Page 3: Hilos Capítulo 5 Silberschatz Galvin. Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered

Hilos3

Copyright © 2006, Intel Corporation. All rights reserved.

Sistemas Operativos

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Hilos

Un hilo (o proceso ligero) es una unidad básica de utilización del CPU; consiste de:

• Contador de programa

• Conjunto de registros

• Espacio de stack

Page 4: Hilos Capítulo 5 Silberschatz Galvin. Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered

Hilos4

Copyright © 2006, Intel Corporation. All rights reserved.

Sistemas Operativos

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Hilos

Un hilo comparte con sus hilos pares:

• Sección de código

• Sección de datos

• Recursos del sistema operativo

• En conjunto se les conoce como proceso

Un proceso tradicional o pesado, es igual a una tarea con un hilo.

Page 5: Hilos Capítulo 5 Silberschatz Galvin. Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered

Hilos5

Copyright © 2006, Intel Corporation. All rights reserved.

Sistemas Operativos

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

un procesoun hilo

varios procesosun hilo por proceso

varios procesosvarios hilos por proceso

un procesovarios hilos

Hilos y procesos

Page 6: Hilos Capítulo 5 Silberschatz Galvin. Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered

Hilos6

Copyright © 2006, Intel Corporation. All rights reserved.

Sistemas Operativos

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Código Datos Archivos Código Datos Archivos

Hilo Hilos

Mono-hilo Multi-hilo

Procesos con un solo hilo y con múltiples hilos

Page 7: Hilos Capítulo 5 Silberschatz Galvin. Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered

Hilos7

Copyright © 2006, Intel Corporation. All rights reserved.

Sistemas Operativos

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Bloque decontroldel hilo

Stackdel

usuario

Stackdel

usuario

Stackdel

kernel

Stackdel

Kernel

Espacio dedireccionesdel usuario

Espacio dedireccionesdel usuario

Bloque decontrol delproceso

Bloquede control

del proceso

Hilo

Modelo de procesode un solo hilo

Modelo de procesomultihilos

Bloque decontroldel hilo

Stackdel

usuario

Stackdel

kernel

Hilo

Bloque decontroldel hilo

Stackdel

usuario

Stackdel

kernel

Hilo

Modelos de procesos de un solo hilo y de muchos hilos

Page 8: Hilos Capítulo 5 Silberschatz Galvin. Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered

Hilos8

Copyright © 2006, Intel Corporation. All rights reserved.

Sistemas Operativos

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Multihilo

Sistema operativo que mantiene varios hilos de ejecución dentro de un mismo proceso.

MS-DOS soporta un solo hilo.

UNIX soporta múltiples procesos de usuarios, pero sólo un hilo por proceso.

Windows 2000, Solaris, Linux, Mac OS, OS/2 soportan múltiples hilos.

Page 9: Hilos Capítulo 5 Silberschatz Galvin. Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered

Hilos9

Copyright © 2006, Intel Corporation. All rights reserved.

Sistemas Operativos

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Hilos

En una tarea donde hay múltiples hilos, mientras un hilo servidor está bloqueado y esperando, otro hilo en la misma tarea puede ejecutarse.

• Cooperación de múltiples hilos en la misma tarea aumenta la tasa de trabajos por unidad tiempo y mejora el rendimiento.

• Aplicaciones que requieren compartir un buffer común (productor-consumidor) sacan provecho de la utilización de hilos.

Page 10: Hilos Capítulo 5 Silberschatz Galvin. Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered

Hilos10

Copyright © 2006, Intel Corporation. All rights reserved.

Sistemas Operativos

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Hilos

Los hilos proveen un mecanismo que permiten a procesos secuenciales hacer llamadas bloqueantes mientras otros en paralelo ejecutan otras operaciones.

Page 11: Hilos Capítulo 5 Silberschatz Galvin. Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered

Hilos11

Copyright © 2006, Intel Corporation. All rights reserved.

Sistemas Operativos

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Beneficios

Compartición de recursos

Economía

Utilización de arquitecturas de múltiples procesadores

Page 12: Hilos Capítulo 5 Silberschatz Galvin. Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered

Hilos12

Copyright © 2006, Intel Corporation. All rights reserved.

Sistemas Operativos

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Hilos a nivel usuario

El manejo de los hilos lo hace una librería de hilos a nivel usuario

Ejemplos

- Hilos POSIX

- Mach C-threads

- Hilos de Solaris

Page 13: Hilos Capítulo 5 Silberschatz Galvin. Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered

Hilos13

Copyright © 2006, Intel Corporation. All rights reserved.

Sistemas Operativos

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Hilos a nivel usuario (un CPU con dos núcleos)

Nivel kérnel (sistema operativo)

Nivel usuarioProceso de usuario gestiona y

planifica los múltiples hilos

Core CoreCPU

Hardware

Planificador del SO

Page 14: Hilos Capítulo 5 Silberschatz Galvin. Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered

Hilos14

Copyright © 2006, Intel Corporation. All rights reserved.

Sistemas Operativos

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Ventajas de los hilos a nivel usuario

Los hilos podían implementarse en las aplicaciones que se ejecutaban en sistemas operativos que no son capaces de planificar hilos

• Ejemplo: Primeras implementaciones de UNIX

Page 15: Hilos Capítulo 5 Silberschatz Galvin. Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered

Hilos15

Copyright © 2006, Intel Corporation. All rights reserved.

Sistemas Operativos

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Desventajas de los hilos a nivel usuario

El planificador del sistema operativo solo ve un hilo por proceso

No ve los hilos a nivel usuario

Usa un solo núcleo del procesador en un procesador que tiene más de un núcleo

No puede distribuir los hilos a nivel el usuario en los dos núcleos

Page 16: Hilos Capítulo 5 Silberschatz Galvin. Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered

Hilos16

Copyright © 2006, Intel Corporation. All rights reserved.

Sistemas Operativos

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Hilos a nivel kérnel

Soportados por el kérnel

Ejemplos

• Windows 95/98/NT

• Solaris

• Linux

• Mac OS

Page 17: Hilos Capítulo 5 Silberschatz Galvin. Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered

Hilos17

Copyright © 2006, Intel Corporation. All rights reserved.

Sistemas Operativos

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Hilos a nivel kernel (con un CPU con un solo núcleo)

Nivel kérnel (sistema operativo)

Nivel usuario

CoreCPU

Hardware

Planificador del SO

Page 18: Hilos Capítulo 5 Silberschatz Galvin. Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered

Hilos18

Copyright © 2006, Intel Corporation. All rights reserved.

Sistemas Operativos

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Hilos a nivel kernel (con un CPU con doble núcleo)

Nivel kérnel (sistema operativo)

Nivel usuario

Core CoreCPU

Hardware

Planificador del SO

Page 19: Hilos Capítulo 5 Silberschatz Galvin. Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered

Hilos19

Copyright © 2006, Intel Corporation. All rights reserved.

Sistemas Operativos

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Ventajas de los hilos a nivel kérnel

El proceso de usuario no se tiene que encargar de la planificación de los hilos

Si tenemos un procesador con más de un núcelo, el Sistema operativo puede planificar los hilos en diferentes núcleos

Page 20: Hilos Capítulo 5 Silberschatz Galvin. Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered

Hilos20

Copyright © 2006, Intel Corporation. All rights reserved.

Sistemas Operativos

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Modelos multihilos

Muchos a uno

Uno a uno

Muchos a muchos

Page 21: Hilos Capítulo 5 Silberschatz Galvin. Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered

Hilos21

Copyright © 2006, Intel Corporation. All rights reserved.

Sistemas Operativos

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Muchos a uno

Muchos hilos a nivel usuario mapeados a un hilo a nivel kérnel.

Usado en sistemas que no soportan hilos a nivel kérnel.

Page 22: Hilos Capítulo 5 Silberschatz Galvin. Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered

Hilos22

Copyright © 2006, Intel Corporation. All rights reserved.

Sistemas Operativos

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Modelo muchos a uno

kk

Page 23: Hilos Capítulo 5 Silberschatz Galvin. Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered

Hilos23

Copyright © 2006, Intel Corporation. All rights reserved.

Sistemas Operativos

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

EjecuciónListoListo

Relaciones entre estados de ULTs y estados de procesos

Ejecución

BloqueadoBloqueado

Listo Ejecución

Bloqueado

Hilo 1 Hilo 2

Proceso B

Kernel

Usuario

Page 24: Hilos Capítulo 5 Silberschatz Galvin. Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered

Hilos24

Copyright © 2006, Intel Corporation. All rights reserved.

Sistemas Operativos

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Uno a uno

Cada hilo a nivel usuario corresponde a un hilo del kérnel.

Ejemplos:

- Windows 95/98/NT

- OS/2

Page 25: Hilos Capítulo 5 Silberschatz Galvin. Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered

Hilos25

Copyright © 2006, Intel Corporation. All rights reserved.

Sistemas Operativos

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Modelo uno a uno

kk kk kk kk

Hilo a nivel usuario

Hilo a nivel kérnel

Page 26: Hilos Capítulo 5 Silberschatz Galvin. Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered

Hilos26

Copyright © 2006, Intel Corporation. All rights reserved.

Sistemas Operativos

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Modelo muchos a muchos

kk kkkk

Hilo a nivel usuario

Hilo a nivel kérnel

Page 27: Hilos Capítulo 5 Silberschatz Galvin. Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered

Hilos27

Copyright © 2006, Intel Corporation. All rights reserved.

Sistemas Operativos

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Usuario

Núcleo

Hardware

Biblioteca

de hilos

Hilo a nivel de

usuario Hilo del núcleo Proceso ligero Procesador

LL L LL L L L L

L P

P P P P P

Proceso 1 Proceso 2 Proceso 3 Proceso 4 Proceso 5

Ejemplo de la arquitectura multihilo de Solaris

Establecer AfinidadEstablecer Afinidad

Page 28: Hilos Capítulo 5 Silberschatz Galvin. Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered

Hilos28

Copyright © 2006, Intel Corporation. All rights reserved.

Sistemas Operativos

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Gestión de hilos básica

Un hilo tiene :

• Un identificador (ID)

• Una pila

• Una prioridad de ejecución

• Una dirección de inicio de ejecución.

Page 29: Hilos Capítulo 5 Silberschatz Galvin. Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered

Hilos29

Copyright © 2006, Intel Corporation. All rights reserved.

Sistemas Operativos

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Gestión de hilos básica(POSIX)

Se dice que un hilo es dinámico si se puede crear en cualquier instante durante la ejecución.

En POSIX :

• los hilos se crean dinámicamente con la función pthread_create.

• pthread_exit termina el hilo que la invoca.

Page 30: Hilos Capítulo 5 Silberschatz Galvin. Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered

Hilos30

Copyright © 2006, Intel Corporation. All rights reserved.

Sistemas Operativos

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Gestión de hilos básica (POSIX), ejemplo:

Programa que crea un hilo donde muestra el mensaje “Hola Mundo”

Para compilar el programa :

• $ cc -D_REENTRANT thread2.c -o thread2 -lpthread

Page 31: Hilos Capítulo 5 Silberschatz Galvin. Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered

Hilos31

Copyright © 2006, Intel Corporation. All rights reserved.

Sistemas Operativos

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

int main(){

pthread_create(a_thread,funcion_hilo);

pthread_join(a_thread);

}

funcion_hilo(){ …}

Crea un hilo nuevo

Espera a que termine el hilo creado

Hilo principal

Hilo creado con la llamada pthread_create

Page 32: Hilos Capítulo 5 Silberschatz Galvin. Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered

Hilos32

Copyright © 2006, Intel Corporation. All rights reserved.

Sistemas Operativos

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

Gestión de hilos básica(POSIX)

Pthread_join()

• Hilo que ejecuta pthread_join, espera a que termine otro hilo

El hilo principal debe esperar a que terminen todos los hilos creados

• Si el hilo principal no espera a que terminen los hilos creados, al terminar termina el proceso con todos los hilos.

Page 33: Hilos Capítulo 5 Silberschatz Galvin. Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered

Hilos33

Copyright © 2006, Intel Corporation. All rights reserved.

Sistemas Operativos

Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.