28.11.2014 Views

System call - Processi, thread

System call - Processi, thread

System call - Processi, thread

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

M-fosset - Sistemi Operativi 2004-2005<br />

Esempio<br />

#include <br />

#include <br />

#include <br />

#define MAX 10<br />

p<strong>thread</strong>_mutex_t M; /* def.mutex condiviso tra <strong>thread</strong>s */<br />

int DATA=0; /* variabile condivisa */<br />

int accessi1=0; /*num. di accessi del <strong>thread</strong> 1 alla sez critica */<br />

int accessi2=0; /*num. di accessi del <strong>thread</strong> 2 alla sez critica */<br />

void *<strong>thread</strong>1_process (void * arg)<br />

{ int k=1;<br />

while(k)<br />

{<br />

p<strong>thread</strong>_mutex_lock(&M); /*prologo */<br />

accessi1++;<br />

DATA++;<br />

k=(DATA>=MAX?0:1);<br />

printf("accessi di T1: %d\n", accessi1);<br />

p<strong>thread</strong>_mutex_unlock(&M); /*epilogo */<br />

}<br />

p<strong>thread</strong>_exit (0);<br />

}

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!