Está en la página 1de 38

Hilos

Captulo 5
Silberschatz Galvin

Hilos
Concepto
Beneficios
Hilos a nivel krnel y a nivel usuario
Modelos multihilos
Hilos de Solaris 2
Hilos de Java

Hilos

Sistemas Operativos

Copyright 2006, Intel Corporation. All rights reserved.


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 bsica de utilizacin
del CPU; consiste de:
Contador de programa
Conjunto de registros
Espacio de stack

Hilos

Sistemas Operativos

Copyright 2006, Intel Corporation. All rights reserved.


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:
Seccin de cdigo
Seccin de datos
Recursos del sistema operativo
En conjunto se les conoce como tarea
Un proceso tradicional o pesado, es igual a una tarea con un
hilo.

Hilos

Sistemas Operativos

Copyright 2006, Intel Corporation. All rights reserved.


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 y procesos

un proceso
un hilo

varios procesos
un hilo por proceso

Hilos

un proceso
varios hilos

varios procesos
varios hilos por proceso

Sistemas Operativos

Copyright 2006, Intel Corporation. All rights reserved.


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.

Procesos con un solo hilo y con mltiples


hilos

Cdigo

Datos

Archivos

Cdigo

Datos

Archivos

Hilos

Hilo

Mono-hilo

Hilos

Multi-hilo

Sistemas Operativos

Copyright 2006, Intel Corporation. All rights reserved.


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 de procesos de un solo hilo y de


muchos hilos

Modelo de proceso
multihilos

Modelo de proceso
de un solo hilo
Bloque
de control
del proceso

Espacio de
direcciones
del usuario

Hilo

Stack
del
usuario

Stack
del
Kernel

Bloque de
control del
proceso

Espacio de
direcciones
del usuario

Hilos

Hilo

Hilo

Bloque de
control
del hilo

Bloque de
control
del hilo

Bloque de
control
del hilo

Stack
del
usuario

Stack
del
usuario

Stack
del
usuario

Stack
del
kernel

Stack
del
kernel

Stack
del
kernel

Sistemas Operativos

Copyright 2006, Intel Corporation. All rights reserved.


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 ejecucin
dentro de un mismo proceso.
MS-DOS soporta un solo hilo.
UNIX soporta mltiples procesos de usuarios, pero slo un hilo
por proceso.
Windows 2000, Solaris, Linux, Mac OS, OS/2 soportan mltiples
hilos.

Hilos

Sistemas Operativos

Copyright 2006, Intel Corporation. All rights reserved.


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 mltiples hilos, mientras un hilo
servidor est bloqueado y esperando, otro hilo en la misma
tarea puede ejecutarse.
Cooperacin de mltiples hilos en la misma tarea aumenta la
tasa de trabajos por unidad tiempo y mejora el rendimiento.
Aplicaciones que requieren compartir un buffer comn
(productor-consumidor) sacan provecho de la utilizacin de
hilos.

Hilos

Sistemas Operativos

Copyright 2006, Intel Corporation. All rights reserved.


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.

10 Hilos

Sistemas Operativos

Copyright 2006, Intel Corporation. All rights reserved.


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
Comparticin de recursos
Economa
Utilizacin de arquitecturas de mltiples procesadores

11 Hilos

Sistemas Operativos

Copyright 2006, Intel Corporation. All rights reserved.


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 librera de hilos a nivel
usuario

Ejemplos
- Hilos POSIX
- Mach C-threads
- Hilos de Solaris

12 Hilos

Sistemas Operativos

Copyright 2006, Intel Corporation. All rights reserved.


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


ncleos)

Proceso de usuario gestiona y


planifica los mltiples hilos

Nivel usuario
Nivel krnel
(sistema
operativo)

Planificador del SO
Hardware
CPU

13 Hilos

Core

Core

Sistemas Operativos

Copyright 2006, Intel Corporation. All rights reserved.


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 podan implementarse en las aplicaciones que se
ejecutaban en sistemas operativos que no son capaces de
planificar hilos
Ejemplo: Primeras implementaciones de UNIX

14 Hilos

Sistemas Operativos

Copyright 2006, Intel Corporation. All rights reserved.


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 ncleo del procesador en un procesador que tiene
ms de un ncleo
No puede distribuir los hilos a nivel el usuario en los dos
ncleos

15 Hilos

Sistemas Operativos

Copyright 2006, Intel Corporation. All rights reserved.


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 krnel


Soportados por el krnel
Ejemplos
Windows 95/98/NT
Solaris
Linux
Mac OS

16 Hilos

Sistemas Operativos

Copyright 2006, Intel Corporation. All rights reserved.


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


ncleo)

Nivel usuario
Nivel krnel
(sistema
operativo)

Planificador del SO
Hardware
CPU

17 Hilos

Core

Sistemas Operativos

Copyright 2006, Intel Corporation. All rights reserved.


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


ncleo)

Nivel usuario
Nivel krnel
(sistema
operativo)

Planificador del SO
Hardware
CPU

18 Hilos

Core

Core

Sistemas Operativos

Copyright 2006, Intel Corporation. All rights reserved.


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 krnel


El proceso de usuario no se tiene que encargar de la
planificacin de los hilos

Si tenemos un procesador con ms de un ncelo, el Sistema


operativo puede planificar los hilos en diferentes ncleos

19 Hilos

Sistemas Operativos

Copyright 2006, Intel Corporation. All rights reserved.


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

20 Hilos

Sistemas Operativos

Copyright 2006, Intel Corporation. All rights reserved.


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 krnel.

Usado en sistemas que no soportan hilos a nivel krnel.

21 Hilos

Sistemas Operativos

Copyright 2006, Intel Corporation. All rights reserved.


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

22 Hilos

Sistemas Operativos

Copyright 2006, Intel Corporation. All rights reserved.


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.

Relaciones entre estados de ULTs y estados


de procesos

Hilo 1
Listo

Hilo 2

Ejecucin

Listo

Ejecucin

Bloqueado
Bloqueado

Usuario

Listo

Kernel
Ejecucin

Bloqueado

23 Hilos

Sistemas
Operativos
Proceso
B

Copyright 2006, Intel Corporation. All rights reserved.


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 krnel.

Ejemplos:
- Windows 95/98/NT
- OS/2

24 Hilos

Sistemas Operativos

Copyright 2006, Intel Corporation. All rights reserved.


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

Hilo a nivel usuario

kk

25 Hilos

kk

kk

kk

Hilo a nivel krnel

Sistemas Operativos

Copyright 2006, Intel Corporation. All rights reserved.


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

Hilo a nivel usuario

kk

26 Hilos

kk

kk

Hilo a nivel krnel

Sistemas Operativos

Copyright 2006, Intel Corporation. All rights reserved.


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.

Soporte de hilos en Solaris 2


Solaris 2 es una versin de UNIX con soporte para hilos a nivel
kernel y a nivel usuario, procesamiento simtrico y planificacin
en tiempo real.

27 Hilos

Sistemas Operativos

Copyright 2006, Intel Corporation. All rights reserved.


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.

Soporte de hilos en Solaris 2


LWP Nivel intermedio entre los hilos a nivel kernel y los hilos
a nivel usuario.

28 Hilos

Sistemas Operativos

Copyright 2006, Intel Corporation. All rights reserved.


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 Solaris 2. Recursos necesarios para


los tipos de hilos
Hilos del kernel
Estructura de datos pequea y un stack
El intercambio de hilos no requiere cambiar la informacin del
acceso a memoria
Cambios de hilos relativamente rpidos

29 Hilos

Sistemas Operativos

Copyright 2006, Intel Corporation. All rights reserved.


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 Solaris 2. Recursos necesarios para


los tipos de hilos
LWP
PCB con datos de los registros
Informacin sobre la memoria
El intercambio entre LWP es relativamente lento

30 Hilos

Sistemas Operativos

Copyright 2006, Intel Corporation. All rights reserved.


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 Solaris 2. Recursos necesarios para


los tipos de hilos
Hilos a nivel usuario
Solo requiere un stack y un contador de programa
No involucrar al kernel significa un intercambio rpido
El kernel solo ve los LWP que soporta los hilos a nivel usuario

31 Hilos

Sistemas Operativos

Copyright 2006, Intel Corporation. All rights reserved.


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.

Ejemplo de la arquitectura multihilo de


Solaris

Proceso 1

Proceso 2

Proceso 3

Proceso 4

Proceso 5

Usuario

Biblioteca
de hilos

Ncleo

Hardware

Hilo a nivel de
usuario

Hilo del ncleo

32 Hilos

P
L

Proceso ligero

Sistemas Operativos

P
P

P
Procesador

Copyright 2006, Intel Corporation. All rights reserved.


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.

Gestin de hilos bsica


Un hilo tiene :
Un identificador (ID)
Una pila
Una prioridad de ejecucin
Una direccin de inicio de ejecucin.

33 Hilos

Sistemas Operativos

Copyright 2006, Intel Corporation. All rights reserved.


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.

Gestin de hilos bsica


(POSIX)
Se dice que un hilo es dinmico si se puede crear en cualquier
instante durante la ejecucin.
En POSIX :
los hilos se crean dinmicamente con la funcin
pthread_create.
pthread_exit termina el hilo que la invoca.

34 Hilos

Sistemas Operativos

Copyright 2006, Intel Corporation. All rights reserved.


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.

Gestin de hilos bsica (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

35 Hilos

Sistemas Operativos

Copyright 2006, Intel Corporation. All rights reserved.


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);

Crea un hilo
nuevo

pthread_join(a_thread);

Espera a que
termine el hilo
creado

Hilo principal

funcion_hilo()
{

36 Hilos

Hilo creado con la llamada


pthread_create

Sistemas Operativos

Copyright 2006, Intel Corporation. All rights reserved.


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.

Gestin de hilos bsica


(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.

37 Hilos

Sistemas Operativos

Copyright 2006, Intel Corporation. All rights reserved.


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 memoria

Cdigo
(compartido)
Datos
(compartidos)
Int cont=0 (var. Global)

Stack (de cada hilo)


Int i
(var local)
Stack (de cada hilo)
Int i
(var local)

38 Hilos

Sistemas Operativos

Copyright 2006, Intel Corporation. All rights reserved.


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.

También podría gustarte