Skip to content

Commit

Permalink
Finished posix and win32 code with unsynchronized versions.
Browse files Browse the repository at this point in the history
  • Loading branch information
JP Villanueva committed Apr 5, 2012
1 parent 639d095 commit 8d57be1
Show file tree
Hide file tree
Showing 5 changed files with 329 additions and 37 deletions.
3 changes: 2 additions & 1 deletion 04/Makefile
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
lab4: lab4.c
lab4: lab4.c lab4_unsynchronized.c
gcc -o lab4 lab4.c -l pthread
gcc -o lab4_unsynchronized lab4_unsynchronized.c -l pthread
56 changes: 33 additions & 23 deletions 04/lab4.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ int c_time; /* # of cycles to spin-wait between each call to dequeue. */
int size; /* Size of the queue. */
int* queue; /* Queue to hold values. */
pthread_mutex_t lock; /* Mutex semaphore. */
sem_t empty; /* Count semaphore increment. */
sem_t full; /* Count semaphore decrement. */
sem_t empty; /* Count semaphore decrement. */
sem_t full; /* Count semaphore increment. */

void producer();
void consumer();
Expand All @@ -37,13 +37,13 @@ int main(int argc, char** argv) {
pthread_t producers[p];
pthread_t consumers[c];

//sem_init(&empty, 0, n);
//sem_init(&full, 0, 0);
sem_init(&empty, 0, n);
sem_init(&full, 0, 0);
pthread_mutex_init(&lock, NULL);

begin = time(NULL);

printf("%s", asctime(localtime(&begin)));
printf("%s\n", asctime(localtime(&begin)));

/* Create threads. */
for(i = 0; i < p; i++) {
Expand All @@ -66,71 +66,81 @@ int main(int argc, char** argv) {
end = time(NULL);
elapsed = end - begin;

printf("%s", asctime(localtime(&end)));
printf("\n%s", asctime(localtime(&end)));
printf("Time elapsed: %d seconds.\n", elapsed);

/* Memory deallocation. */
free(queue);
pthread_mutex_destroy(&lock);
//sem_destroy(&full);
//sem_destroy(&empty);
sem_destroy(&full);
sem_destroy(&empty);

return 0;
}

void producer(void* arg) {
void producer() {
int i;

printf("producer:> begin\n");
fprintf(stderr, "producer:> begin\n");

do {
//sem_wait(&empty);
/* P() */
sem_wait(&empty);
pthread_mutex_lock(&lock);

/* Induce race conditions. */
sleep(1);

/* Grow queue. */
for(i = 0; i < p; i++) {
if(n > size) {
size += 1;
queue[size] = rand();
printf("producer:> enqueue queue[%d] = %d\n", size, queue[size]);
queue[size-1] = rand();
fprintf(stderr, "producer:> enqueue queue[%d] = %d\n", size, queue[size-1]);
}
}

printf("producer:> size = %d\n", size);
fprintf(stderr, "producer:> size = %d\n", size);

/* V() */
pthread_mutex_unlock(&lock);
//sem_post(&full);
sem_post(&full);

sleep(p_time);
} while(n > size);

printf("producer:> end\n");
fprintf(stderr, "producer:> end\n");
}

void consumer(void* arg) {
void consumer() {
int i;

printf("consumer:> begin\n");
fprintf(stderr, "consumer:> begin\n");

do {
//sem_wait(&full);
/* P() */
sem_wait(&full);
pthread_mutex_lock(&lock);

/* Induce race conditions. */
sleep(1);

/* Shrink queue. */
for(i = 0; i < (int)p*(x/c); i++) {
if(size > 0) {
fprintf(stderr, "consumer:> dequeue queue[%d] = %d\n", size, queue[size-1]);
size -= 1;
printf("consumer:> dequeue queue[%d] = %d\n", size, queue[size]);
}
}

printf("consumer:> size = %d\n", size);
fprintf(stderr, "consumer:> size = %d\n", size);

/* V() */
pthread_mutex_unlock(&lock);
//sem_wait(&empty);
sem_post(&empty);

sleep(c_time);
} while(size > 0);

printf("consumer:> end\n");
fprintf(stderr, "consumer:> end\n");
}
119 changes: 119 additions & 0 deletions 04/lab4_unsynchronized.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

int n; /* # of elements for bounded buffer. */
int p; /* # of producer threads. */
int c; /* # of consumer threads. */
int x; /* # of different number that go into the queue. */
int p_time; /* # of cycles to spin-wait between each call to enqueue. */
int c_time; /* # of cycles to spin-wait between each call to dequeue. */
int size; /* Size of the queue. */
int* queue; /* Queue to hold values. */

void producer();
void consumer();

int main(int argc, char** argv) {
int elapsed;
int i;
time_t begin;
time_t end;
n = atoi(argv[1]);
p = atoi(argv[2]);
c = atoi(argv[3]);
x = atoi(argv[4]);
p_time = atoi(argv[5]);
c_time = atoi(argv[6]);
size = 0;
queue = malloc(n * sizeof(int));
pthread_t producers[p];
pthread_t consumers[c];

begin = time(NULL);

printf("%s\n", asctime(localtime(&begin)));

/* Create threads. */
for(i = 0; i < p; i++) {
pthread_create(&producers[i], NULL, (void *)producer, NULL);
}

for(i = 0; i < c; i++) {
pthread_create(&consumers[i], NULL, (void *)consumer, NULL);
}

/* Join threads. */
for(i = 0; i < p; i++) {
pthread_join(producers[i], NULL);
}

for(i = 0; i < c; i++) {
pthread_join(consumers[i], NULL);
}

end = time(NULL);
elapsed = end - begin;

printf("\n%s", asctime(localtime(&end)));
printf("Time elapsed: %d seconds.\n", elapsed);

/* Memory deallocation. */
free(queue);

return 0;
}

void producer() {
int i;

fprintf(stderr, "producer:> begin\n");

do {
/* Induce race conditions. */
sleep(1);

/* Grow queue. */
for(i = 0; i < p; i++) {
if(n > size) {
size += 1;
queue[size-1] = rand();
printf("producer:> enqueue queue[%d] = %d\n", size, queue[size-1]);
}
}

printf("producer:> size = %d\n", size);

sleep(p_time);
} while(n > size);

fprintf(stderr, "producer:> end\n");
}

void consumer() {
int i;

fprintf(stderr, "consumer:> begin\n");

do {
/* Induce race conditions. */
sleep(1);

/* Shrink queue. */
for(i = 0; i < (int)p*(x/c); i++) {
if(size > 0) {
printf("consumer:> dequeue queue[%d] = %d\n", size, queue[size-1]);
size -= 1;
}
}

printf("consumer:> size = %d\n", size);

sleep(c_time);
} while(size > 0);

fprintf(stderr, "consumer:> end\n");
}
37 changes: 24 additions & 13 deletions 04/lab4_win.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ int c_time; /* # of cycles to spin-wait between each call to dequeue. */
int size; /* Size of the queue. */
int* queue; /* Queue to hold values. */
HANDLE lock; /* Mutex semaphore. */
HANDLE empty; /* Count semaphore increment. */
HANDLE full; /* Count semaphore decrement. */
HANDLE empty; /* Count semaphore decrement. */
HANDLE full; /* Count semaphore increment. */

DWORD WINAPI producer();
DWORD WINAPI consumer();
Expand All @@ -38,9 +38,9 @@ int main(int argc, char** argv) {
producers = malloc(p * sizeof(HANDLE));
consumers = malloc(c * sizeof(HANDLE));


begin = time(NULL);

/* Beginning timestamp. */
printf("%s\n", asctime(localtime(&begin)));

/* Create threads. */
Expand All @@ -66,16 +66,17 @@ int main(int argc, char** argv) {
end = time(NULL);
elapsed = end - begin;

/* Ending timestamp. */
printf("\n%s", asctime(localtime(&end)));
printf("Time elapsed: %d seconds.\n", elapsed);

/* Memory deallocation. */
CloseHandle(empty);
CloseHandle(full);
CloseHandle(lock);
free(queue);
free(producers);
free(consumers);
CloseHandle(full);
CloseHandle(empty);
CloseHandle(lock);

return 0;
}
Expand All @@ -86,22 +87,27 @@ DWORD WINAPI producer() {
printf("producer:> begin\n");

do {
//WaitForSingleObject(empty, 0);
/* P() */
WaitForSingleObject(empty, 0);
WaitForSingleObject(lock, INFINITE);

/* Induce race conditions. */
Sleep(1);

/* Grow queue. */
for(i = 0; i < p; i++) {
if(n > size) {
size += 1;
queue[size] = rand();
printf("producer:> enqueue queue[%d] = %d\n", size, queue[size]);
queue[size-1] = rand();
printf("producer:> enqueue queue[%d] = %d\n", size, queue[size-1]);
}
}

printf("producer:> size = %d\n", size);

/* V() */
ReleaseMutex(lock);
//ReleaseSemaphore(full);
ReleaseSemaphore(full);

Sleep(p_time);
} while(n > size);
Expand All @@ -117,21 +123,26 @@ DWORD WINAPI consumer() {
printf("consumer:> begin\n");

do {
//WaitForSingleObject(full, 0);
/* P() */
WaitForSingleObject(full, 0);
WaitForSingleObject(lock, INFINITE);

/* Induce race conditions. */
Sleep(1);

/* Shrink queue. */
for(i = 0; i < (int)p*(x/c); i++) {
if(size > 0) {
printf("consumer:> dequeue queue[%d] = %d\n", size-1, queue[size-1]);
size -= 1;
printf("consumer:> dequeue queue[%d] = %d\n", size, queue[size]);
}
}

printf("consumer:> size = %d\n", size);

/* V() */
ReleaseMutex(lock);
//ReleaseSemaphore(empty);
ReleaseSemaphore(empty);

Sleep(c_time);
} while(size > 0);
Expand Down
Loading

0 comments on commit 8d57be1

Please sign in to comment.