Sistemas de Computa˘c~ao Jantar dos Fil osofos · so poder a mudar para o estado comendo se nenhum...

Post on 28-Jan-2019

214 views 0 download

Transcript of Sistemas de Computa˘c~ao Jantar dos Fil osofos · so poder a mudar para o estado comendo se nenhum...

Universidade Federal do CearaCentro de Tecnologia

Departamento de Engenharia de Teleinformatica

Sistemas de ComputacaoJantar dos Filosofos

Professor: Alexandre CoelhoAluna: Maria Luciene Lira Monteiro - 0316855

Fortaleza-CE, 28 de janeiro de 2013

Sumario

1 Objetivos 3

2 Metodologia 42.1 Materiais Utilizados . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42.2 Estrategias utilizadas para a resolucao das atividades . . . . . . . . . . . . 4

2.2.1 Questao 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42.2.2 Questao 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

3 Resultados 53.1 Questao 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53.2 Questao 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

4 Analise dos Resultados 94.1 Questao 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94.2 Questao 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

5 Conclusao 11

2

Capıtulo 1

Objetivos

Esta pratica consiste em implementar o problema do jantar dos filosofos em C, usandouma thread Posix para modelar cada filosofo e um semaforo Posix para cada palito. Opadrao Posix IEEE 1003.1c (1995), mais conhecido como PThreads, busca definir umainterface padronizada para criacao e manipulacao de threads.

3

Capıtulo 2

Metodologia

2.1 Materiais Utilizados

Para realizacao da pratica utilizamos um computador do laboratorio de informatica comdistribuicao Ubuntu.Foi disponibilizada a pratica onde continha um site que nos auxiliounos conceitos de Thread e Mutex.A compilacao do arquivo em c e foi feita via terminal.

2.2 Estrategias utilizadas para a resolucao das ativi-

dades

2.2.1 Questao 1

Jantar dos Filosofos com impasse - uso de semaforos,mutex.Repetir dentro da funcaophilosopher uma funcao qualquer,por exemplo: take-forks,para que os filosofos tentassempegar os garfos aos mesmo tempo.Portanto,ocorreria deadlock.

2.2.2 Questao 2

Jantar dos Filosofos sem impasse - uso de semaforos,mutex.Sem deadlock,alterada a funcaophilosopher para que os filosofos alternassem suas tarefas e cada um pudesse executa-laem seu devido tempo.Uso de dois arranjos ,um state e um semaforo para cada filosofo.

4

Capıtulo 3

Resultados

3.1 Questao 1

Jantar dos Filosofos com impasse

#inc lude <s t d l i b . h>#inc lude <s t d i o . h>#inc lude <pthread . h>#inc lude <semaphore . h>

# de f i n e N 5# de f i n e LEFT ( i+N−1)%N# de f i n e RIGHT ( i +1)%N# de f i n e THINKING 0# de f i n e HUNGRY 1# de f i n e EATING 2# de f i n e TRUE 1

in t s t a t e [N ] ;sem t mutex ;sem t s [N ] ;p thread t thread1 , thread2 , thread3 , thread4 , thread5 ;

void t a k e f o r k s ( i n t i ) ;void pu t f o r k s ( i n t i ) ;

void t e s t ( i n t i ) ;void th ink ( i n t i ) ;

void eat ( i n t i ) ;

void ph i l o sophe r ( i n t i ) {

whi le (TRUE) {

th ink ( i ) ;t a k e f o r k s ( i ) ;t a k e f o r k s ( ( i +1)%N) ;eat ( i ) ;pu t f o r k s ( i ) ;pu t f o r k s ( ( i +1)%N) ;}}

5

void t a k e f o r k s ( i n t i ) {

sem wait(&mutex ) ;s t a t e [ i ]=HUNGRY;p r i n t f ( ” ph i l o sophe r %d HUNGRY\n” , i ) ;t e s t ( i ) ;sem post(&mutex ) ;sem wait(&s [ i ] ) ;

}

void pu t f o r k s ( i ) {

sem wait(&mutex ) ;s t a t e [ i ]=THINKING;p r i n t f ( ” ph i l o sophe r %d THINKING\n” , i ) ;t e s t (LEFT) ;t e s t (RIGHT) ;sem post(&mutex ) ;}

void t e s t ( i ) {

i f ( s t a t e [ i ]==HUNGRY && s ta t e [LEFT] !=EATING && s ta t e [RIGHT] !=EATING) {s t a t e [ i ]=EATING;p r i n t f ( ” ph i l o sophe r %d EATING\n” , i ) ;sem post(&s [ i ] ) ;}}

void th ink ( i n t i ) {s l e e p (1 ) ;r e turn ;}

void eat ( i n t i ) {s l e e p (1 ) ;r e turn ;}

void main ( ) {

i n t i r e t 1 , i r e t 2 , i r e t 3 , i r e t 4 , i r e t 5 ;i n t i ;i n t p [N] ;i n t s em in i t ( sem t ∗sem , i n t pshared , unsigned i n t va lue ) ;f o r ( i= 0 ; i < N ; i++ ) {s em in i t (&s [ i ] , 0 , 1) ;p [ i ] = i ;}s em in i t (&mutex , 0 , 1) ;

i r e t 1 = pthr ead c r ea t e ( &thread1 , NULL, ( void ∗) ph i lo sopher , ( i n t ∗)p [ 1 ] ) ;i r e t 2 = pthr ead c r ea t e ( &thread2 , NULL, ( void ∗) ph i lo sopher , ( i n t ∗)p [ 2 ] ) ;i r e t 3 = pthr ead c r ea t e ( &thread3 , NULL, ( void ∗) ph i lo sopher , ( i n t ∗)p [ 3 ] ) ;i r e t 4 = pthr ead c r ea t e ( &thread4 , NULL, ( void ∗) ph i lo sopher , ( i n t ∗)p [ 4 ] ) ;i r e t 5 = pthr ead c r ea t e ( &thread5 , NULL, ( void ∗) ph i lo sopher , ( i n t ∗)p [ 0 ] ) ;

p th r ead j o i n ( thread1 , NULL) ;p th r ead j o i n ( thread2 , NULL) ;p th r ead j o i n ( thread3 , NULL) ;p th r ead j o i n ( thread4 , NULL) ;p th r ead j o i n ( thread5 , NULL) ;

e x i t (0 ) ;}

6

3.2 Questao 2

Jantar dos Filosofos sem impasse

#inc lude <s t d l i b . h>#inc lude <s t d i o . h>#inc lude <pthread . h>#inc lude <semaphore . h>

# de f i n e N 5# de f i n e LEFT ( i+N−1)%N# de f i n e RIGHT ( i +1)%N# de f i n e THINKING 0# de f i n e HUNGRY 1# de f i n e EATING 2# de f i n e TRUE 1

in t s t a t e [N ] ;sem t mutex ;sem t s [N ] ;p thread t thread1 , thread2 , thread3 , thread4 , thread5 ;

void t a k e f o r k s ( i n t i ) ;void pu t f o r k s ( i n t i ) ;

void t e s t ( i n t i ) ;void th ink ( i n t i ) ;

void eat ( i n t i ) ;

void ph i l o sophe r ( i n t i ) {

whi le (TRUE) {

th ink ( i ) ;t a k e f o r k s ( i ) ;eat ( i ) ;pu t f o r k s ( i ) ;}}

void t a k e f o r k s ( i n t i ) {

sem wait(&mutex ) ;s t a t e [ i ]=HUNGRY;p r i n t f ( ” ph i l o sophe r %d HUNGRY\n” , i ) ;t e s t ( i ) ;sem post(&mutex ) ;sem wait(&s [ i ] ) ;

}

void pu t f o r k s ( i ) {

sem wait(&mutex ) ;s t a t e [ i ]=THINKING;p r i n t f ( ” ph i l o sophe r %d THINKING\n” , i ) ;t e s t (LEFT) ;t e s t (RIGHT) ;sem post(&mutex ) ;}

7

void t e s t ( i ) {

i f ( s t a t e [ i ]==HUNGRY && s ta t e [LEFT] !=EATING && s ta t e [RIGHT] !=EATING) {s t a t e [ i ]=EATING;p r i n t f ( ” ph i l o sophe r %d EATING\n” , i ) ;sem post(&s [ i ] ) ;}}

void th ink ( i n t i ) {s l e e p (1 ) ;r e turn ;}

void eat ( i n t i ) {s l e e p (1 ) ;r e turn ;}

void main ( ) {

i n t i r e t 1 , i r e t 2 , i r e t 3 , i r e t 4 , i r e t 5 ;i n t i ;i n t p [N] ;i n t s em in i t ( sem t ∗sem , i n t pshared , unsigned i n t va lue ) ;f o r ( i= 0 ; i < N ; i++ ) {s em in i t (&s [ i ] , 0 , 1) ;p [ i ] = i ;}s em in i t (&mutex , 0 , 1) ;

i r e t 1 = pthr ead c r ea t e ( &thread1 , NULL, ( void ∗) ph i lo sopher , ( i n t ∗)p [ 1 ] ) ;i r e t 2 = pthr ead c r ea t e ( &thread2 , NULL, ( void ∗) ph i lo sopher , ( i n t ∗)p [ 2 ] ) ;i r e t 3 = pthr ead c r ea t e ( &thread3 , NULL, ( void ∗) ph i lo sopher , ( i n t ∗)p [ 3 ] ) ;i r e t 4 = pthr ead c r ea t e ( &thread4 , NULL, ( void ∗) ph i lo sopher , ( i n t ∗)p [ 4 ] ) ;i r e t 5 = pthr ead c r ea t e ( &thread5 , NULL, ( void ∗) ph i lo sopher , ( i n t ∗)p [ 0 ] ) ;

p th r ead j o i n ( thread1 , NULL) ;p th r ead j o i n ( thread2 , NULL) ;p th r ead j o i n ( thread3 , NULL) ;p th r ead j o i n ( thread4 , NULL) ;p th r ead j o i n ( thread5 , NULL) ;

e x i t (0 ) ;}

8

Capıtulo 4

Analise dos Resultados

4.1 Questao 1

Observando a figura a seguir,chegamos a conclusao que ocorre deadlock.

Figura 4.1: Jantar dos Filosofos - Com Deadlock

4.2 Questao 2

Observando a figura a seguir,chegamos a conclusao que esta versao permite o maximoparalelismo a um numero arbitrario de filosofos.Ela usa um arranjo,state,para controlar seum filosofo esta comendo,pensando ou faminto(tentando pegar garfos).Assim,um filosofoso podera mudar para o estado comendo se nenhum dos vizinhos estiver comendo.O

9

programa se utiliza tambem de um arranjo de semaforos,um por filosofo,assim,filosofosfamintos podem ser bloqueados se os garfos necessarios estiverem sendo usados.

Figura 4.2: Jantar dos Filosofos - Sem Deadlock

10

Capıtulo 5

Conclusao

Concluimos a importancia da utilizacao dos semaforos e que estes podem evitar dead-locks,como no classico problema do jantar dos filosofos em que seu uso foi definitivo parao entendimento do paralelismo,o qual foi possıvel atribuir a varios processos suas respec-tivas atividades em tempo estimado e sem que um intervisse no processo do outro.

11

Referencias Bibliograficas

[1] Material disponibilizado no site da disciplina.

[2] Sistemas Operacionais Modernos ,Andrew S. Tanenbaum.

12