https://www.adonaimedrado.pro.br/wiki/index.php?title=C:_Starvation&feed=atom&action=history
C: Starvation - Revision history
2024-03-28T09:11:24Z
Revision history for this page on the wiki
MediaWiki 1.26.2
https://www.adonaimedrado.pro.br/wiki/index.php?title=C:_Starvation&diff=3650&oldid=prev
Adonaimedrado: Created page with '<code lang=c> #include <stdio.h> #include <pthread.h> #define BOOL int #define TRUE 1 #define FALSE 0 int x; pthread_mutex_t mutex_de_x, mutex_de_y; void *minha_thread1(void *…'
2010-08-28T07:14:46Z
<p>Created page with '<code lang=c> #include <stdio.h> #include <pthread.h> #define BOOL int #define TRUE 1 #define FALSE 0 int x; pthread_mutex_t mutex_de_x, mutex_de_y; void *minha_thread1(void *…'</p>
<p><b>New page</b></p><div><code lang=c><br />
#include <stdio.h><br />
#include <pthread.h><br />
<br />
#define BOOL int<br />
#define TRUE 1<br />
#define FALSE 0<br />
<br />
int x;<br />
pthread_mutex_t mutex_de_x, mutex_de_y;<br />
<br />
void *minha_thread1(void *null)<br />
{ <br />
BOOL consegui_lock_em_x=FALSE, consegui_lock_em_y=FALSE;<br />
<br />
while(!consegui_lock_em_x || !consegui_lock_em_y)<br />
{<br />
consegui_lock_em_x=FALSE;<br />
consegui_lock_em_y=FALSE;<br />
<br />
if (!pthread_mutex_trylock(&mutex_de_x))<br />
{<br />
consegui_lock_em_x=TRUE;<br />
printf("minha_thread1: Obtive acesso ao mutex X.\n");<br />
printf("minha_thread1: Aguardando 1 segundo...\n");<br />
sleep(1);<br />
printf("minha_thread1: Tentando mutex Y...\n"); <br />
if (!pthread_mutex_trylock(&mutex_de_y))<br />
{<br />
consegui_lock_em_y=TRUE;<br />
printf("minha_thread1: Obtive acesso ao mutex Y.\n");<br />
printf("minha_thread1: Liberando mutex Y...\n");<br />
pthread_mutex_unlock(&mutex_de_y);<br />
}<br />
else<br />
{<br />
printf("minha_thread1: Não obtive acesso ao mutex Y.\n");<br />
consegui_lock_em_y=FALSE;<br />
}<br />
printf("minha_thread1: Aguardando 1 segundo...\n");<br />
sleep(1);<br />
printf("minha_thread1: Liberando mutex X...\n");<br />
pthread_mutex_unlock(&mutex_de_x);<br />
}<br />
else<br />
consegui_lock_em_x=FALSE;<br />
<br />
printf("minha_thread1: consegui_lock_em_x=%d,consegui_lock_em_y=%d\n",consegui_lock_em_x,consegui_lock_em_y);<br />
sleep(1); <br />
}<br />
<br />
pthread_exit(NULL);<br />
}<br />
<br />
void *minha_thread2(void *null)<br />
{ <br />
BOOL consegui_lock_em_x=FALSE, consegui_lock_em_y=FALSE;<br />
<br />
while(!consegui_lock_em_x || !consegui_lock_em_y)<br />
{<br />
consegui_lock_em_x=FALSE;<br />
consegui_lock_em_y=FALSE;<br />
<br />
if (!pthread_mutex_trylock(&mutex_de_y))<br />
{<br />
consegui_lock_em_y=TRUE;<br />
printf("\tminha_thread2: Obtive acesso ao mutex Y.\n");<br />
printf("\tminha_thread2: Aguardando 1 segundo...\n");<br />
sleep(1);<br />
printf("\tminha_thread2: Tentando mutex X...\n"); <br />
if (!pthread_mutex_trylock(&mutex_de_x))<br />
{<br />
consegui_lock_em_y=TRUE;<br />
printf("\tminha_thread2: Obtive acesso ao mutex X.\n"); <br />
printf("\tminha_thread2: Liberando mutex X...\n");<br />
pthread_mutex_unlock(&mutex_de_x);<br />
}<br />
{<br />
printf("\tminha_thread2: Não obtive acesso ao mutex X.\n");<br />
consegui_lock_em_x=FALSE;<br />
}<br />
printf("\tminha_thread2: Aguardando 1 segundo...\n");<br />
sleep(1);<br />
printf("\tminha_thread2: Liberando mutex Y...\n");<br />
pthread_mutex_unlock(&mutex_de_y);<br />
}<br />
else<br />
consegui_lock_em_y=FALSE;<br />
<br />
printf("\tminha_thread2: consegui_lock_em_x=%d,consegui_lock_em_y=%d\n",consegui_lock_em_x,consegui_lock_em_y);<br />
sleep(1);<br />
}<br />
<br />
pthread_exit(NULL);<br />
}<br />
<br />
int main()<br />
{<br />
int retorno_da_thread;<br />
pthread_t thread1, thread2;<br />
pthread_attr_t atributo_de_thread;<br />
<br />
pthread_attr_init(&atributo_de_thread);<br />
pthread_attr_setdetachstate(<br />
&atributo_de_thread, PTHREAD_CREATE_JOINABLE);<br />
<br />
pthread_mutex_init(&mutex_de_x,NULL);<br />
pthread_mutex_init(&mutex_de_y,NULL);<br />
<br />
if (pthread_create(&thread1,<br />
&atributo_de_thread,<br />
minha_thread1,NULL))<br />
printf("Erro ao criar a minha_thread1.\n");<br />
<br />
if (pthread_create(&thread2,<br />
&atributo_de_thread,minha_thread2,NULL))<br />
printf("Erro ao criar a minha_thread2.\n");<br />
<br />
if (pthread_join(thread1,(void **)&retorno_da_thread))<br />
printf("Erro no join de minha_thread1.\n");<br />
else<br />
printf("minha_thread1 joined.\n");<br />
<br />
if (pthread_join(thread2,(void **)&retorno_da_thread))<br />
printf("Erro no join de minha_thread2.\n");<br />
else<br />
printf("minha_thread2 joined.\n");<br />
<br />
pthread_mutex_destroy(&mutex_de_y);<br />
pthread_mutex_destroy(&mutex_de_x);<br />
<br />
pthread_attr_destroy(&atributo_de_thread);<br />
<br />
pthread_exit(NULL);<br />
}<br />
</code></div>
Adonaimedrado