AndreiL
Welcome to AndreiL.
Enjoy your time.
Please log in.

Join the forum, it's quick and easy

AndreiL
Welcome to AndreiL.
Enjoy your time.
Please log in.
AndreiL
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Unix threads

Go down

Unix threads Empty Unix threads

Post by Lucaci Andrei Thu Nov 22, 2012 5:07 am

VERSION 1.1 is out! Check below.
  1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <unistd.h>
#include <semaphore.h>
#define ON 1
#define OFF 0

typedef struct node{
double value;
int worker;
struct node *next;
}node;

sem_t sm;
pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t avmtx = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t avcond = PTHREAD_COND_INITIALIZER;
pthread_rwlock_t rw = PTHREAD_RWLOCK_INITIALIZER;
node* head;
int max, counter, status, pauseAll;

node* create(int max){
node* head=(node*) malloc(sizeof(node));
head->value=rand();
head->worker=0;
head->next=NULL;
node* temp=head;
int i;
printf("Max:%d\n", max);
for (i=1;i<max;i++){
node* elem=(node*) malloc(sizeof(node));
elem->value=rand();
elem->worker=i;
elem->next=NULL;
temp->next=elem;
temp=temp->next;
}
return head;
}

void print(){
node* it=head;
while(it!=NULL){
printf("Value: %lf\t\t|\t\tWorker: %d\n", it->value, it->worker);
it=it->next;
}
}

void removes(){
while(head!=NULL && head->value<2){
node* it=head->next;
printf("Value: %lf\t\t|\t\tWorker: %d\n", head->value, head->worker);
free(head);
head=it;
}
if (head==NULL) return;
node* it=head;
while(it->next!=NULL){
if (it->next->value<2){
printf("Value: %lf\t\t|\t\tWorker: %d\n", it->next->value, it->next->worker);
node* temp=it->next->next;
free(it->next);
it->next=temp;
}
else it=it->next;
}
}

void sleepy(){
while(pauseAll!=1) usleep(100);
pauseAll=0;
}

void* sqrtList(void* p){
sem_wait(&sm);
int i=*(int*)&p;
node* pointer=head;
while(status==ON && pointer!=NULL){
if (pointer->worker==i){
pthread_rwlock_wrlock(&rw);
counter++;
pthread_rwlock_unlock(&rw);
while(pointer->value>=2)
pointer->value=sqrt(pointer->value);
if (counter==max){
printf("Signaled - end of the list.\n");
pthread_rwlock_wrlock(&rw);
status=OFF;
pthread_rwlock_unlock(&rw);
pthread_cond_signal(&avcond);
}
else if (counter!=0 && counter%5==0){
printf("Signaled - 5 elements.\n");
pthread_cond_signal(&avcond);
}
break;
}
else pointer=pointer->next;
}
sem_post(&sm);
pthread_exit(NULL);
}

void* wite(void* p){
pthread_mutex_lock(&mtx);
while(status==ON){
printf("Waiting for signal: \n");
pthread_cond_wait(&avcond, &mtx);
printf("Signal received.\nDeleted items: \n");
removes();
printf("Items remaining: %d.\n", max-counter);
pauseAll=1;
if(counter==max) break;
}
pthread_mutex_unlock(&mtx);
return NULL;
}

int main(int argc, char* argv[]){
if (sem_init(&sm, 0, 3)<0){
printf("Error at initializing the sempahore.\n");
return 1;
}
srand(time(NULL));
max=atoi(argv[1]);
pthread_t t[max];
pthread_t wt;
head=create(max);
printf("Elements: \n");
print();
status=ON;
counter=0;
int i;
pthread_create(&wt, NULL, wite, NULL);
usleep(1);
for (i=0;i<max;i++){
pthread_create(&t[i], NULL, sqrtList, i);
}
while(status==ON) usleep(1000);
for (i=0;i<max;i++){
pthread_join(t[i], NULL);
}
pthread_join(wt, NULL);
if (head==NULL) printf("List is empty.\n");
else {
printf("workre: %d\n", head->worker);
printf("Remaining itmes in the list: \n");
print();
}
pthread_mutex_destroy(&mtx);
pthread_mutex_destroy(&avmtx);
pthread_cond_destroy(&avcond);
pthread_rwlock_destroy(&rw);
sem_destroy(&sm);
return 0;
}

Lucaci Andrei
Lucaci Andrei
"Tata Lor"
Unix threads 138

Number of messages : 222
Points : 2266743
Reputation : 1007
Registration date : 2008-08-15
Age : 31
Location : Cluj-Napoca

http://www.andreil.wgz.ro

Back to top Go down

Unix threads Empty Re: Unix threads

Post by Lucaci Andrei Fri Nov 23, 2012 4:03 am

Version 1.1
NOTE: I've added the semaphores in the thread-creating loop for an ease on the sqrtList function and I have added some synchronization to the threads, so now they go in order, and the result is swell and welcomed. cheers

  1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <unistd.h>
#include <semaphore.h>
#define ON 1
#define OFF 0

typedef struct node{
double value;
int worker;
struct node *next;
}node;

sem_t sm;
pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t avmtx = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t avcond = PTHREAD_COND_INITIALIZER;
pthread_rwlock_t rw = PTHREAD_RWLOCK_INITIALIZER;
node* head;
int max, counter, status, pauseAll;

node* create(int max){
node* head=(node*) malloc(sizeof(node));
head->value=rand();
head->worker=0;
head->next=NULL;
node* temp=head;
int i;
printf("Max:%d\n", max);
for (i=1;i<max;i++){
node* elem=(node*) malloc(sizeof(node));
elem->value=rand();
elem->worker=i;
elem->next=NULL;
temp->next=elem;
temp=temp->next;
}
return head;
}

void print(){
node* it=head;
while(it!=NULL){
printf("Value: %lf\t\t|\t\tWorker: %d\n", it->value, it->worker);
it=it->next;
}
}

void removes(){
while(head!=NULL && head->value<2){
node* it=head->next;
printf("Value: %lf\t\t|\t\tWorker: %d\n", head->value, head->worker);
free(head);
head=it;
}
if (head==NULL) return;
node* it=head;
while(it->next!=NULL){
if (it->next->value<2){
printf("Value: %lf\t\t|\t\tWorker: %d\n", it->next->value, it->next->worker);
node* temp=it->next->next;
free(it->next);
it->next=temp;
}
else it=it->next;
}
}

void sleepy(){
while(pauseAll!=1) usleep(100);
pauseAll=0;
}

void* sqrtList(void* p){
// sem_wait(&sm);
int i=*(int*)&p;
node* pointer=head;
while(status==ON && pointer!=NULL){
if (pointer->worker==i){
pthread_rwlock_wrlock(&rw);
counter++;
pthread_rwlock_unlock(&rw);
while(pointer->value>=2)
pointer->value=sqrt(pointer->value);
if (counter==max){
printf("Signaled - end of the list.\n");
pthread_rwlock_wrlock(&rw);
status=OFF;
pthread_rwlock_unlock(&rw);
pthread_cond_signal(&avcond);
}
else if (counter!=0 && counter%5==0){
printf("Signaled - 5 elements.\n");
pthread_cond_signal(&avcond);
}
break;
}
else pointer=pointer->next;
}
// sem_post(&sm);
pthread_exit(NULL);
}

void* wite(void* p){
pthread_mutex_lock(&mtx);
while(status==ON){
printf("Waiting for signal: \n");
pthread_cond_wait(&avcond, &mtx);
printf("Signal received.\nDeleted items: \n");
removes();
printf("Items remaining: %d.\n", max-counter);
pauseAll=1;
if(counter==max) break;
}
pthread_mutex_unlock(&mtx);
return NULL;
}

int main(int argc, char* argv[]){
if (sem_init(&sm, 0, 3)<0){
printf("Error at initializing the sempahore.\n");
return 1;
}
srand(time(NULL));
max=atoi(argv[1]);
pthread_t t[max];
pthread_t wt;
head=create(max);
printf("Elements: \n");
print();
status=ON;
counter=0;
int i;
pthread_create(&wt, NULL, wite, NULL);
usleep(1);
for (i=0;i<max;i++){
sem_wait(&sm);
usleep(1);
pthread_create(&t[i], NULL, sqrtList, i);
sem_post(&sm);
}
while(status==ON) usleep(1000);
for (i=0;i<max;i++){
pthread_join(t[i], NULL);
}
pthread_join(wt, NULL);
if (head==NULL) printf("List is empty.\n");
else {
printf("workre: %d\n", head->worker);
printf("Remaining itmes in the list: \n");
print();
}
pthread_mutex_destroy(&mtx);
pthread_mutex_destroy(&avmtx);
pthread_cond_destroy(&avcond);
pthread_rwlock_destroy(&rw);
sem_destroy(&sm);
return 0;
}

Lucaci Andrei
Lucaci Andrei
"Tata Lor"
Unix threads 138

Number of messages : 222
Points : 2266743
Reputation : 1007
Registration date : 2008-08-15
Age : 31
Location : Cluj-Napoca

http://www.andreil.wgz.ro

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum