#include
#include
/*structura ce contine parametrii transmisi fiecarui threrad*/
typedef struct {
int x[100]; /*vectorul */
int n; /*nr de elemente*/
} Parametri;
int x[100];
int n,a,b;
/*functia executata de primul thread*/
void* Citire(void *param) {
int i;
printf("Dati limita inferioara (a)\n");
scanf("%d",&a);
Printf("Dati limita superioara (b)\n");
scanf("%d",&b);
printf("Dati dimensiunea vectorului\n");
scanf("%d",&n);
for (i=0;i printf("Dati element pozitie %d/n", i+1);
scanf("%d", &x[i]);
}
printf("\n");
return NULL;
}
/*functia executa ala doilea thread*/
void* Afisare(void *param) {
int i;
printf("\n Elementele din vector cuprinse intre %d si %d sunt:\n", a, b);
for (i=0;i if (x[i]>=a && x[i]<=b) printf("% ",x[i]);
}
printf("\n");
return NULL;
}
/*functia executata de al treilea thread*/
void* Suma(void * param) {
int i,m =x[0];
float S=0;
for (i=0; i S=S+x[i];
if (m }
printf("\n Media elementelor este %g", S/n);
printf("\n Maximul este %d", m)
printf("\n");
return NULL;
}
int main()
{
pthread_t f1,f2,f3;
int rc,status;
pthread_attr_t attr;
Parametri sp;
/*cream primul fir*/
pthread_att_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
rc=pthread_create(&f1, &attr,&Citire,(void*)&sp);
/*ateptam termnarea primului fir de executie*/
rc=pthread_join(f1,(void**)&status);
/*cream al doilea fir*/
pthread_create(&f2, &attr, Afisare, (void*)&sp);
rc=pthread_join(f2, (void**)&status);
/*cream al treilea fir*/
pthread_create(&f3, &attr, Suma, &sp);
rc=pthread_join(f3, (void**)&status);
pthread_attr_destroy(&attr);
return 0;
}
#include
#include
#include
#include
#include
typedef struct {
char buf[BSIZE];
sem_t occupied;
sem_t empty;
int nextin;
int nextout;
sem_t pmut;
sem_t cmut;
} buffer_t;
buffer_t buffer;
void producer(buffer_t *b, char item) {
b=&buffer;
printf("\N dati un nr ")
scanf("%d",&item);
sem_wait(&b->empty);
sem_wait(&b->pmut);
b->buf[b->nextin] = item;
printf("\n S-a pus %d ", item);
b->nextin++;
b->nextin %= BSIZE;
sem_post(&b->pmut);
sem_post(&b->occupied);
}
char consumer(buffer_t *b)
{
char item;
int i,x,y;
sem_wait(&b->cmut);
item = b->buf[b->nextout];
printf("\n a= ");
scanf("%d", &x);
printf("\n b=");
scanf("%d", &y);
for(i=2;i if ((item>x) && (item
printf("\n Numarul %d depus de producator NU este prim",item);
b->nextout++;
b->nextout %= BSIZE;
printf("\n S-a citit %d ", item);
sem_post(&b->cmut);
sem_post(&b->empty);
return item;
}
int main()
{
int i;
pthread_t t1,t2;
sem_init(&buffer.occupied, 0, 0);
sem_init(&buffer.empty,0, BSIZE);
buffer.nextin = buffer.nextout = 0;
pthread_create(&t1, NULL, producer,NULL );
pthread_create(&t2, NULL, consumer,NULL);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
return 0;
}
#include
#include
#include
#include
#include
typedef struct {
char buf[BSIZE];
sem_t occupied;
sem_t empty;
int nextin;
int nextout;
sem_t pmut;
sem_t cmut;
} buffer_t;
buffer_t buffer;
void producer(buffer_t *b, char item) {
b=&buffer;
printf("\N dati un nr ")
scanf("%d",&item);
sem_wait(&b->empty);
sem_wait(&b->pmut);
b->buf[b->nextin] = item;
printf("\n S-a pus %d ", item);
b->nextin++;
b->nextin %= BSIZE;
sem_post(&b->pmut);
sem_post(&b->occupied);
}
char consumer(buffer_t *b)
{
char item;
int i,x,y;
sem_wait(&b->cmut);
item = b->buf[b->nextout];
printf("\n a= ");
scanf("%d", &x);
printf("\n b=");
scanf("%d", &y);
for(i=2;i if ((item>x) && (item
printf("\n Numarul %d depus de producator NU este prim",item);
b->nextout++;
b->nextout %= BSIZE;
printf("\n S-a citit %d ", item);
sem_post(&b->cmut);
sem_post(&b->empty);
return item;
}
int main()
{
int i;
pthread_t t1,t2;
sem_init(&buffer.occupied, 0, 0);
sem_init(&buffer.empty,0, BSIZE);
buffer.nextin = buffer.nextout = 0;
pthread_create(&t1, NULL, producer,NULL );
pthread_create(&t2, NULL, consumer,NULL);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
return 0;
}