added more code
This commit is contained in:
29
CPE435/Lab6/D1.c
Normal file
29
CPE435/Lab6/D1.c
Normal file
@ -0,0 +1,29 @@
|
||||
/*
|
||||
Compile with gcc Lab06_D1.c -o Lab06 -lpthread
|
||||
|
||||
*/
|
||||
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#define NUM_THREADS 5
|
||||
|
||||
void* printHello(void *threadId)
|
||||
{
|
||||
printf("\n%d:Hello World!\n",threadId);
|
||||
pthread_exit(NULL);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
pthread_t threads[NUM_THREADS];
|
||||
int rc,t;
|
||||
for(t=0;t<NUM_THREADS;t++)
|
||||
{
|
||||
printf("Creating thread %d\n",t);
|
||||
rc = pthread_create(&threads[t],NULL,printHello,(void*)t);
|
||||
if(rc)
|
||||
{
|
||||
printf("ERROR:Return Code from pthread_create() is %d\n",rc);
|
||||
}
|
||||
}
|
||||
pthread_exit(NULL);
|
||||
}
|
BIN
CPE435/Lab6/D2
Executable file
BIN
CPE435/Lab6/D2
Executable file
Binary file not shown.
49
CPE435/Lab6/D2.c
Normal file
49
CPE435/Lab6/D2.c
Normal file
@ -0,0 +1,49 @@
|
||||
/*
|
||||
Written By: Prawar Poudel
|
||||
13 Feb 2018
|
||||
This is written to demonstrate simple creation and waiting for pthread to terminate
|
||||
*/
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#define NUM_THREADS 5
|
||||
|
||||
//the argument that will be sent will be the (int) id
|
||||
void *simpleThreadFunc(void* argument)
|
||||
{
|
||||
int myId = (int)argument;
|
||||
printf("My Id is %d\n",myId);
|
||||
int a = 0;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
//you can create these dynamically also
|
||||
pthread_t myThreads[NUM_THREADS];
|
||||
int status = 0;
|
||||
int i;
|
||||
for(i=0;i<NUM_THREADS;i++)
|
||||
{
|
||||
printf("Creating thread no. %d, and sending ID %d\n",i,i);
|
||||
status = pthread_create(&myThreads[i],NULL,simpleThreadFunc,(void*)i); if(status)
|
||||
{
|
||||
printf("Error in creating the threads: %d\n",i);
|
||||
return -1;
|
||||
}else
|
||||
{
|
||||
printf("Successful creation of thread..\n");
|
||||
}
|
||||
}
|
||||
//this is the area that threads will run
|
||||
//we will wait for the threads here
|
||||
for(i=0;i<NUM_THREADS;i++)
|
||||
{
|
||||
int retStatus = pthread_join(myThreads[i],NULL);
|
||||
if(!retStatus)
|
||||
{
|
||||
printf("Successful termination of thread id %d\n",i);
|
||||
}else
|
||||
printf("Well..... some problem at thread id %d, error no: %d\n",i,retStatus);
|
||||
}
|
||||
return 0;
|
||||
}
|
BIN
CPE435/Lab6/D3
Executable file
BIN
CPE435/Lab6/D3
Executable file
Binary file not shown.
88
CPE435/Lab6/D3.c
Normal file
88
CPE435/Lab6/D3.c
Normal file
@ -0,0 +1,88 @@
|
||||
/*
|
||||
Written By: Prawar Poudel
|
||||
13 Feb 2018
|
||||
This is written to demonstrate simple creation and waiting for pthread to terminate
|
||||
*/
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#define NUM_THREADS 100
|
||||
int mutexProtectedGlobalVariable;
|
||||
int unprotectedProtectedGlobalVariable;
|
||||
pthread_mutex_t myMutex;
|
||||
|
||||
//this function will update the value without any protection
|
||||
void *unprotectedThreadFunc(void* argument)
|
||||
{
|
||||
int i;
|
||||
for( i=0;i<10000;i++) unprotectedProtectedGlobalVariable++; }
|
||||
|
||||
//this function will update the value without any protection
|
||||
void *protectedThreadFunc(void* argument)
|
||||
{
|
||||
int i;
|
||||
pthread_mutex_lock (&myMutex);
|
||||
for( i=0;i<10000;i++) mutexProtectedGlobalVariable++; pthread_mutex_unlock (&myMutex);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
mutexProtectedGlobalVariable = 0;
|
||||
unprotectedProtectedGlobalVariable = 0;
|
||||
int i;
|
||||
|
||||
//you can create these dynamically also
|
||||
pthread_t myThreads[NUM_THREADS];
|
||||
int status = 0;
|
||||
printf("Calling unprotected set of threads\n");
|
||||
|
||||
//first set of five threads will call a function that will update the variable unprotected
|
||||
for(i=0;i<NUM_THREADS;i++)
|
||||
{
|
||||
status = pthread_create(&myThreads[i],NULL,unprotectedThreadFunc, (void*)i);
|
||||
if(status)
|
||||
{
|
||||
printf("Error in creating the threads: %d\n",i);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
//this is the area that threads will run
|
||||
//we will wait for the threads here
|
||||
for(i=0;i<NUM_THREADS;i++)
|
||||
{
|
||||
int retStatus = pthread_join(myThreads[i],NULL);
|
||||
if(retStatus)
|
||||
{
|
||||
printf("Well..... some problem at thread id %d, error no: %d\n",i,retStatus);
|
||||
}
|
||||
}
|
||||
printf("Unprotected sum is %d\n", unprotectedProtectedGlobalVariable); printf("\t\t...end of unprotected set of threads\n");
|
||||
printf("Calling protected set of threads\n");
|
||||
pthread_mutex_init(&myMutex, NULL);
|
||||
|
||||
//next set of five threads will call a function that will update the variable protected
|
||||
for(i=0;i<NUM_THREADS;i++)
|
||||
{
|
||||
status = pthread_create(&myThreads[i],NULL,protectedThreadFunc, (void*)i);
|
||||
if(status)
|
||||
{
|
||||
printf("Error in creating the threads: %d\n",i);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
//this is the area that threads will run
|
||||
//we will wait for the threads here
|
||||
for(i=0;i<NUM_THREADS;i++)
|
||||
{
|
||||
int retStatus = pthread_join(myThreads[i],NULL);
|
||||
if(retStatus)
|
||||
{
|
||||
printf("Well..... some problem at thread id %d, error no: %d\n",i,retStatus);
|
||||
}
|
||||
}
|
||||
pthread_mutex_destroy(&myMutex);
|
||||
printf("Protected sum is %d \n", mutexProtectedGlobalVariable); printf("\t\t...end of unprotected set of threads\n");
|
||||
return 0;
|
||||
}
|
BIN
CPE435/Lab6/D4
Executable file
BIN
CPE435/Lab6/D4
Executable file
Binary file not shown.
74
CPE435/Lab6/D4.c
Normal file
74
CPE435/Lab6/D4.c
Normal file
@ -0,0 +1,74 @@
|
||||
/*
|
||||
Written By: Prawar Poudel
|
||||
13 Feb 2018
|
||||
This is written to demonstrate the usage of Thread Local Storage Here using identifier __thread , we have made myVal and myArr[] thread local
|
||||
*/
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#define NUM_THREADS 5
|
||||
#define ARRSIZE 5
|
||||
//this value is a global variable,
|
||||
// but we will store it as thread local, meaning while it is still global the value will can be modified such that the modified value is thread specific
|
||||
__thread int myVal;
|
||||
__thread int myArr[ARRSIZE];
|
||||
|
||||
void printMyVal(int id)
|
||||
{
|
||||
int i;
|
||||
printf("My value myVal from thread %d is %d\n", id,myVal);
|
||||
printf("My arr value are\n");
|
||||
for( i=0;i<ARRSIZE;i++)
|
||||
{
|
||||
printf("%dthe element is %d\n",i,myArr[i]);
|
||||
}
|
||||
}
|
||||
|
||||
//the argument that will be sent will be the (int) id
|
||||
void *simpleThreadFunc(void* argument)
|
||||
{
|
||||
int i;
|
||||
int myId = (int)argument;
|
||||
printf("My Id is %d\n",myId);
|
||||
//just setting some thread specific value to the variable
|
||||
myVal = myId*100;
|
||||
for( i=0;i<5;i++)
|
||||
myArr[i] = (myId*100+i);
|
||||
|
||||
printMyVal(myId);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
//you can create these dynamically also
|
||||
pthread_t myThreads[NUM_THREADS];
|
||||
int status = 0;
|
||||
int i;
|
||||
for( i=0;i<NUM_THREADS;i++)
|
||||
{
|
||||
printf("Creating thread no. %d, and sending ID %d\n",i,i);
|
||||
status = pthread_create(&myThreads[i],NULL,simpleThreadFunc,(void*)i);
|
||||
if(status)
|
||||
{
|
||||
printf("Error in creating the threads: %d\n",i);
|
||||
return -1;
|
||||
}else
|
||||
{
|
||||
printf("Successful creation of thread..\n");
|
||||
}
|
||||
}
|
||||
|
||||
//this is the area that threads will run
|
||||
//we will wait for the threads here
|
||||
for( i=0;i<NUM_THREADS;i++)
|
||||
{
|
||||
int retStatus = pthread_join(myThreads[i],NULL);
|
||||
if(!retStatus)
|
||||
{
|
||||
printf("Successful termination of thread id %d\n",i);
|
||||
}
|
||||
else
|
||||
printf("Well..... some problem at thread id %d, error no: %d\n",i,retStatus);
|
||||
}
|
||||
return 0;
|
||||
}
|
BIN
CPE435/Lab6/D5
Executable file
BIN
CPE435/Lab6/D5
Executable file
Binary file not shown.
71
CPE435/Lab6/D5.c
Normal file
71
CPE435/Lab6/D5.c
Normal file
@ -0,0 +1,71 @@
|
||||
/*
|
||||
Written By: Prawar Poudel
|
||||
13 Feb 2018
|
||||
This is written to demonstrate the usage of Thread Local Storage using key
|
||||
*/
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#define NUM_THREADS 5
|
||||
#define ARRSIZE 5
|
||||
|
||||
//in this program we will use a key defined by pthread_key_t to define a key
|
||||
pthread_key_t myKey;
|
||||
|
||||
void printMyVal(int myId)
|
||||
{
|
||||
printf("Getting specific value for thread %d using key\n",myId); int *myVal = pthread_getspecific(myKey);
|
||||
printf("\t..The thread local value in thread id %d is %d\n",myId,*myVal);
|
||||
}
|
||||
|
||||
//the argument that will be sent will be the (int) id
|
||||
void *simpleThreadFunc(void* argument)
|
||||
{
|
||||
int myId = (int)argument;
|
||||
//this variable will be thread specific value that we will print from other function
|
||||
int myVal = myId*100;
|
||||
printf("Creating the variable ::%d that will be referred to by Key from threadid %d\n",myVal,myId);
|
||||
if(!pthread_setspecific(myKey,(void*)&myVal))
|
||||
{}
|
||||
else
|
||||
{
|
||||
printf("Error in setting specific key in thread id %d\n",myId);
|
||||
}
|
||||
printMyVal(myId);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
//you can create these dynamically also
|
||||
pthread_t myThreads[NUM_THREADS];
|
||||
int status = 0;
|
||||
pthread_key_create(&myKey,NULL);
|
||||
int i;
|
||||
for( i=0;i<NUM_THREADS;i++)
|
||||
{
|
||||
printf("Creating thread no. %d, and sending ID %d\n",i,i);
|
||||
status = pthread_create(&myThreads[i],NULL,simpleThreadFunc,(void*)i);
|
||||
if(status)
|
||||
{
|
||||
printf("Error in creating the threads: %d\n",i);
|
||||
return -1;
|
||||
}else
|
||||
{
|
||||
printf("Successful creation of thread %d..\n",i);
|
||||
}
|
||||
}
|
||||
|
||||
//this is the area that threads will run
|
||||
//we will wait for the threads here
|
||||
for( i=0;i<NUM_THREADS;i++)
|
||||
{
|
||||
int retStatus = pthread_join(myThreads[i],NULL);
|
||||
if(!retStatus)
|
||||
{
|
||||
printf("Successful termination of thread id %d\n",i);
|
||||
}
|
||||
else
|
||||
printf("Well..... some problem at thread id %d, error no: %d\n",i,retStatus);
|
||||
}
|
||||
return 0;
|
||||
}
|
BIN
CPE435/Lab6/Lab06.pdf
Normal file
BIN
CPE435/Lab6/Lab06.pdf
Normal file
Binary file not shown.
29
CPE435/Lab6/Lab06_D1.c
Normal file
29
CPE435/Lab6/Lab06_D1.c
Normal file
@ -0,0 +1,29 @@
|
||||
/*
|
||||
Compile with gcc Lab06_D1.c -o Lab06 -lpthread
|
||||
|
||||
*/
|
||||
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#define NUM_THREADS 5
|
||||
|
||||
void* printHello(void *threadId)
|
||||
{
|
||||
printf("\n%d:Hello World!\n",threadId);
|
||||
pthread_exit(NULL);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
pthread_t threads[NUM_THREADS];
|
||||
int rc,t;
|
||||
for(t=0;t<NUM_THREADS;t++)
|
||||
{
|
||||
printf("Creating thread %d\n",t);
|
||||
rc = pthread_create(&threads[t],NULL,printHello,(void*)t);
|
||||
if(rc)
|
||||
{
|
||||
printf("ERROR:Return Code from pthread_create() is %d\n",rc);
|
||||
}
|
||||
}
|
||||
pthread_exit(NULL);
|
||||
}
|
49
CPE435/Lab6/Lab06_D2.c
Normal file
49
CPE435/Lab6/Lab06_D2.c
Normal file
@ -0,0 +1,49 @@
|
||||
/*
|
||||
Written By: Prawar Poudel
|
||||
13 Feb 2018
|
||||
This is written to demonstrate simple creation and waiting for pthread to terminate
|
||||
*/
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#define NUM_THREADS 5
|
||||
|
||||
//the argument that will be sent will be the (int) id
|
||||
void *simpleThreadFunc(void* argument)
|
||||
{
|
||||
int myId = (int)argument;
|
||||
printf("My Id is %d\n",myId);
|
||||
int a = 0;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
//you can create these dynamically also
|
||||
pthread_t myThreads[NUM_THREADS];
|
||||
int status = 0;
|
||||
int i;
|
||||
for(i=0;i<NUM_THREADS;i++)
|
||||
{
|
||||
printf("Creating thread no. %d, and sending ID %d\n",i,i);
|
||||
status = pthread_create(&myThreads[i],NULL,simpleThreadFunc,(void*)i); if(status)
|
||||
{
|
||||
printf("Error in creating the threads: %d\n",i);
|
||||
return -1;
|
||||
}else
|
||||
{
|
||||
printf("Successful creation of thread..\n");
|
||||
}
|
||||
}
|
||||
//this is the area that threads will run
|
||||
//we will wait for the threads here
|
||||
for(i=0;i<NUM_THREADS;i++)
|
||||
{
|
||||
int retStatus = pthread_join(myThreads[i],NULL);
|
||||
if(!retStatus)
|
||||
{
|
||||
printf("Successful termination of thread id %d\n",i);
|
||||
}else
|
||||
printf("Well..... some problem at thread id %d, error no: %d\n",i,retStatus);
|
||||
}
|
||||
return 0;
|
||||
}
|
88
CPE435/Lab6/Lab06_D3.c
Normal file
88
CPE435/Lab6/Lab06_D3.c
Normal file
@ -0,0 +1,88 @@
|
||||
/*
|
||||
Written By: Prawar Poudel
|
||||
13 Feb 2018
|
||||
This is written to demonstrate simple creation and waiting for pthread to terminate
|
||||
*/
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#define NUM_THREADS 100
|
||||
int mutexProtectedGlobalVariable;
|
||||
int unprotectedProtectedGlobalVariable;
|
||||
pthread_mutex_t myMutex;
|
||||
|
||||
//this function will update the value without any protection
|
||||
void *unprotectedThreadFunc(void* argument)
|
||||
{
|
||||
int i;
|
||||
for( i=0;i<10000;i++) unprotectedProtectedGlobalVariable++; }
|
||||
|
||||
//this function will update the value without any protection
|
||||
void *protectedThreadFunc(void* argument)
|
||||
{
|
||||
int i;
|
||||
pthread_mutex_lock (&myMutex);
|
||||
for( i=0;i<10000;i++) mutexProtectedGlobalVariable++; pthread_mutex_unlock (&myMutex);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
mutexProtectedGlobalVariable = 0;
|
||||
unprotectedProtectedGlobalVariable = 0;
|
||||
int i;
|
||||
|
||||
//you can create these dynamically also
|
||||
pthread_t myThreads[NUM_THREADS];
|
||||
int status = 0;
|
||||
printf("Calling unprotected set of threads\n");
|
||||
|
||||
//first set of five threads will call a function that will update the variable unprotected
|
||||
for(i=0;i<NUM_THREADS;i++)
|
||||
{
|
||||
status = pthread_create(&myThreads[i],NULL,unprotectedThreadFunc, (void*)i);
|
||||
if(status)
|
||||
{
|
||||
printf("Error in creating the threads: %d\n",i);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
//this is the area that threads will run
|
||||
//we will wait for the threads here
|
||||
for(i=0;i<NUM_THREADS;i++)
|
||||
{
|
||||
int retStatus = pthread_join(myThreads[i],NULL);
|
||||
if(retStatus)
|
||||
{
|
||||
printf("Well..... some problem at thread id %d, error no: %d\n",i,retStatus);
|
||||
}
|
||||
}
|
||||
printf("Unprotected sum is %d\n", unprotectedProtectedGlobalVariable); printf("\t\t...end of unprotected set of threads\n");
|
||||
printf("Calling protected set of threads\n");
|
||||
pthread_mutex_init(&myMutex, NULL);
|
||||
|
||||
//next set of five threads will call a function that will update the variable protected
|
||||
for(i=0;i<NUM_THREADS;i++)
|
||||
{
|
||||
status = pthread_create(&myThreads[i],NULL,protectedThreadFunc, (void*)i);
|
||||
if(status)
|
||||
{
|
||||
printf("Error in creating the threads: %d\n",i);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
//this is the area that threads will run
|
||||
//we will wait for the threads here
|
||||
for(i=0;i<NUM_THREADS;i++)
|
||||
{
|
||||
int retStatus = pthread_join(myThreads[i],NULL);
|
||||
if(retStatus)
|
||||
{
|
||||
printf("Well..... some problem at thread id %d, error no: %d\n",i,retStatus);
|
||||
}
|
||||
}
|
||||
pthread_mutex_destroy(&myMutex);
|
||||
printf("Protected sum is %d \n", mutexProtectedGlobalVariable); printf("\t\t...end of unprotected set of threads\n");
|
||||
return 0;
|
||||
}
|
73
CPE435/Lab6/Lab06_D4.c
Normal file
73
CPE435/Lab6/Lab06_D4.c
Normal file
@ -0,0 +1,73 @@
|
||||
/*
|
||||
Written By: Prawar Poudel
|
||||
13 Feb 2018
|
||||
This is written to demonstrate the usage of Thread Local Storage Here using identifier __thread , we have made myVal and myArr[] thread local
|
||||
*/
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#define NUM_THREADS 5
|
||||
#define ARRSIZE 5
|
||||
//this value is a global variable,
|
||||
// but we will store it as thread local, meaning while it is still global the value will can be modified such that the modified value is thread specific
|
||||
__thread int myVal;
|
||||
__thread int myArr[ARRSIZE];
|
||||
|
||||
void printMyVal(int id)
|
||||
{
|
||||
int i;
|
||||
printf("My value myVal from thread %d is %d\n", id,myVal);
|
||||
printf("My arr value are\n");
|
||||
for( i=0;i<ARRSIZE;i++)
|
||||
{
|
||||
printf("%dthe element is %d\n",i,myArr[i]);
|
||||
}
|
||||
}
|
||||
|
||||
//the argument that will be sent will be the (int) id
|
||||
void *simpleThreadFunc(void* argument)
|
||||
{
|
||||
int i;
|
||||
int myId = (int)argument;
|
||||
printf("My Id is %d\n",myId);
|
||||
//just setting some thread specific value to the variable
|
||||
myVal = myId*100;
|
||||
for( i=0;i<5;i++)
|
||||
myArr[i] = (myId*100+i);
|
||||
|
||||
printMyVal(myId);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
//you can create these dynamically also
|
||||
pthread_t myThreads[NUM_THREADS];
|
||||
int status = 0;
|
||||
int i;
|
||||
for( i=0;i<NUM_THREADS;i++)
|
||||
{
|
||||
printf("Creating thread no. %d, and sending ID %d\n",i,i);
|
||||
status = pthread_create(&myThreads[i],NULL,simpleThreadFunc,(void*)i); if(status)
|
||||
{
|
||||
printf("Error in creating the threads: %d\n",i);
|
||||
return -1;
|
||||
}else
|
||||
{
|
||||
printf("Successful creation of thread..\n");
|
||||
}
|
||||
}
|
||||
|
||||
//this is the area that threads will run
|
||||
//we will wait for the threads here
|
||||
for( i=0;i<NUM_THREADS;i++)
|
||||
{
|
||||
int retStatus = pthread_join(myThreads[i],NULL);
|
||||
if(!retStatus)
|
||||
{
|
||||
printf("Successful termination of thread id %d\n",i);
|
||||
}
|
||||
else
|
||||
printf("Well..... some problem at thread id %d, error no: %d\n",i,retStatus);
|
||||
}
|
||||
return 0;
|
||||
}
|
71
CPE435/Lab6/Lab06_D5.c
Normal file
71
CPE435/Lab6/Lab06_D5.c
Normal file
@ -0,0 +1,71 @@
|
||||
/*
|
||||
Written By: Prawar Poudel
|
||||
13 Feb 2018
|
||||
This is written to demonstrate the usage of Thread Local Storage using key
|
||||
*/
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#define NUM_THREADS 5
|
||||
#define ARRSIZE 5
|
||||
|
||||
//in this program we will use a key defined by pthread_key_t to define a key
|
||||
pthread_key_t myKey;
|
||||
|
||||
void printMyVal(int myId)
|
||||
{
|
||||
printf("Getting specific value for thread %d using key\n",myId); int *myVal = pthread_getspecific(myKey);
|
||||
printf("\t..The thread local value in thread id %d is %d\n",myId,*myVal);
|
||||
}
|
||||
|
||||
//the argument that will be sent will be the (int) id
|
||||
void *simpleThreadFunc(void* argument)
|
||||
{
|
||||
int myId = (int)argument;
|
||||
//this variable will be thread specific value that we will print from other function
|
||||
int myVal = myId*100;
|
||||
printf("Creating the variable ::%d that will be referred to by Key from threadid %d\n",myVal,myId);
|
||||
if(!pthread_setspecific(myKey,(void*)&myVal))
|
||||
{}
|
||||
else
|
||||
{
|
||||
printf("Erorr in setting specific key in thread id %d\n",myId);
|
||||
}
|
||||
printMyVal(myId);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
//you can create these dynamically also
|
||||
pthread_t myThreads[NUM_THREADS];
|
||||
int status = 0;
|
||||
pthread_key_create(&myKey,NULL);
|
||||
int i;
|
||||
for( i=0;i<NUM_THREADS;i++)
|
||||
{
|
||||
printf("Creating thread no. %d, and sending ID %d\n",i,i);
|
||||
status = pthread_create(&myThreads[i],NULL,simpleThreadFunc,(void*)i);
|
||||
if(status)
|
||||
{
|
||||
printf("Error in creating the threads: %d\n",i);
|
||||
return -1;
|
||||
}else
|
||||
{
|
||||
printf("Successful creation of thread %d..\n",i);
|
||||
}
|
||||
}
|
||||
|
||||
//this is the area that threads will run
|
||||
//we will wait for the threads here
|
||||
for( i=0;i<NUM_THREADS;i++)
|
||||
{
|
||||
int retStatus = pthread_join(myThreads[i],NULL);
|
||||
if(!retStatus)
|
||||
{
|
||||
printf("Successful termination of thread id %d\n",i);
|
||||
}
|
||||
else
|
||||
printf("Well..... some problem at thread id %d, error no: %d\n",i,retStatus);
|
||||
}
|
||||
return 0;
|
||||
}
|
BIN
CPE435/Lab6/PosixThreads.pdf
Normal file
BIN
CPE435/Lab6/PosixThreads.pdf
Normal file
Binary file not shown.
BIN
CPE435/Lab6/PosixThreads.pptx
Normal file
BIN
CPE435/Lab6/PosixThreads.pptx
Normal file
Binary file not shown.
BIN
CPE435/Lab6/grap1.jpg
Normal file
BIN
CPE435/Lab6/grap1.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 39 KiB |
BIN
CPE435/Lab6/output.png
Normal file
BIN
CPE435/Lab6/output.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 24 KiB |
BIN
CPE435/Lab6/parallelDecomp
Executable file
BIN
CPE435/Lab6/parallelDecomp
Executable file
Binary file not shown.
98
CPE435/Lab6/parallelDecomp.c
Normal file
98
CPE435/Lab6/parallelDecomp.c
Normal file
@ -0,0 +1,98 @@
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/time.h>
|
||||
#include <math.h>
|
||||
|
||||
|
||||
#define TIMER_CLEAR (tv1.tv_sec = tv1.tv_usec = tv2.tv_sec = tv2.tv_usec = 0)
|
||||
#define TIMER_START gettimeofday(&tv1, (struct timezone*)0)
|
||||
#define TIMER_ELAPSED (double) (tv2.tv_usec-tv1.tv_usec)/1000000.0+(tv2.tv_sec-tv1.tv_sec)
|
||||
#define TIMER_STOP gettimeofday(&tv2, (struct timezone*)0)
|
||||
struct timeval tv1,tv2;
|
||||
|
||||
pthread_mutex_t myMutex;
|
||||
|
||||
#define MAX_THREADS 16
|
||||
|
||||
__thread double areaThread, areaThreadTotal;
|
||||
__thread double myVal;
|
||||
|
||||
int NUM_THREADS;
|
||||
double iterations;
|
||||
int a = 0, b = 1;
|
||||
double area, h;
|
||||
double sumOfRectangles;
|
||||
|
||||
// returns square root
|
||||
double func(double num)
|
||||
{
|
||||
return sqrt(1-pow(num,2));
|
||||
}
|
||||
|
||||
void *protectedThreadFunc(void* argument)
|
||||
{
|
||||
int i;
|
||||
int myId = (int)argument;
|
||||
TIMER_START;
|
||||
for(i=0;i<=iterations-1;i++)
|
||||
{
|
||||
areaThread=h*func(a+(i-1)*h);
|
||||
areaThreadTotal+=areaThread;
|
||||
}
|
||||
|
||||
pthread_mutex_lock (&myMutex);
|
||||
sumOfRectangles=4*areaThreadTotal;
|
||||
TIMER_STOP;
|
||||
pthread_mutex_unlock (&myMutex);
|
||||
|
||||
}
|
||||
|
||||
int main(int argc, char * argv[])
|
||||
{
|
||||
TIMER_CLEAR;
|
||||
if (argc < 3)
|
||||
{
|
||||
printf("Please specify the iterations or threads\n");
|
||||
return -1;
|
||||
}
|
||||
area = 0;
|
||||
int i;
|
||||
|
||||
iterations = atoi(argv[1]); // in other words this is n
|
||||
|
||||
NUM_THREADS = atoi(argv[2]);
|
||||
|
||||
h = (b-a)/iterations;
|
||||
|
||||
//creating pthread array
|
||||
pthread_t myThreads[NUM_THREADS];
|
||||
int status = 0;
|
||||
|
||||
|
||||
//printf("Calling protected set of threads\n");
|
||||
pthread_mutex_init(&myMutex, NULL);
|
||||
//next set of threads will call a function that will update the variable protected
|
||||
for(i=0;i<NUM_THREADS;i++)
|
||||
{
|
||||
status = pthread_create(&myThreads[i],NULL,protectedThreadFunc, (void*)i);
|
||||
if(status)
|
||||
{
|
||||
printf("Error in creating the threads: %d\n",i);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
for(i=0;i<NUM_THREADS;i++)
|
||||
{
|
||||
int retStatus = pthread_join(myThreads[i],NULL);
|
||||
if(retStatus)
|
||||
{
|
||||
printf("Well..... some problem at thread id %d, error no: %d\n",i,retStatus);
|
||||
}
|
||||
}
|
||||
pthread_mutex_destroy(&myMutex);
|
||||
printf("Computed Sum for Rectangular Decomposition: %f\n", sumOfRectangles);
|
||||
printf("Time elapsed = %f seconds\n", TIMER_ELAPSED);
|
||||
return 0;
|
||||
}
|
88
CPE435/Lab6/serial.c
Normal file
88
CPE435/Lab6/serial.c
Normal file
@ -0,0 +1,88 @@
|
||||
/*
|
||||
Written By: Prawar Poudel
|
||||
13 Feb 2018
|
||||
This is written to demonstrate simple creation and waiting for pthread to terminate
|
||||
*/
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#define NUM_THREADS 100
|
||||
int mutexProtectedGlobalVariable;
|
||||
int unprotectedProtectedGlobalVariable;
|
||||
pthread_mutex_t myMutex;
|
||||
|
||||
//this function will update the value without any protection
|
||||
void *unprotectedThreadFunc(void* argument)
|
||||
{
|
||||
int i;
|
||||
for( i=0;i<10000;i++) unprotectedProtectedGlobalVariable++; }
|
||||
|
||||
//this function will update the value without any protection
|
||||
void *protectedThreadFunc(void* argument)
|
||||
{
|
||||
int i;
|
||||
pthread_mutex_lock (&myMutex);
|
||||
for( i=0;i<10000;i++) mutexProtectedGlobalVariable++; pthread_mutex_unlock (&myMutex);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
mutexProtectedGlobalVariable = 0;
|
||||
unprotectedProtectedGlobalVariable = 0;
|
||||
int i;
|
||||
|
||||
//you can create these dynamically also
|
||||
pthread_t myThreads[NUM_THREADS];
|
||||
int status = 0;
|
||||
printf("Calling unprotected set of threads\n");
|
||||
|
||||
//first set of five threads will call a function that will update the variable unprotected
|
||||
for(i=0;i<NUM_THREADS;i++)
|
||||
{
|
||||
status = pthread_create(&myThreads[i],NULL,unprotectedThreadFunc, (void*)i);
|
||||
if(status)
|
||||
{
|
||||
printf("Error in creating the threads: %d\n",i);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
//this is the area that threads will run
|
||||
//we will wait for the threads here
|
||||
for(i=0;i<NUM_THREADS;i++)
|
||||
{
|
||||
int retStatus = pthread_join(myThreads[i],NULL);
|
||||
if(retStatus)
|
||||
{
|
||||
printf("Well..... some problem at thread id %d, error no: %d\n",i,retStatus);
|
||||
}
|
||||
}
|
||||
printf("Unprotected sum is %d\n", unprotectedProtectedGlobalVariable); printf("\t\t...end of unprotected set of threads\n");
|
||||
printf("Calling protected set of threads\n");
|
||||
pthread_mutex_init(&myMutex, NULL);
|
||||
|
||||
//next set of five threads will call a function that will update the variable protected
|
||||
for(i=0;i<NUM_THREADS;i++)
|
||||
{
|
||||
status = pthread_create(&myThreads[i],NULL,protectedThreadFunc, (void*)i);
|
||||
if(status)
|
||||
{
|
||||
printf("Error in creating the threads: %d\n",i);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
//this is the area that threads will run
|
||||
//we will wait for the threads here
|
||||
for(i=0;i<NUM_THREADS;i++)
|
||||
{
|
||||
int retStatus = pthread_join(myThreads[i],NULL);
|
||||
if(retStatus)
|
||||
{
|
||||
printf("Well..... some problem at thread id %d, error no: %d\n",i,retStatus);
|
||||
}
|
||||
}
|
||||
pthread_mutex_destroy(&myMutex);
|
||||
printf("Protected sum is %d \n", mutexProtectedGlobalVariable); printf("\t\t...end of unprotected set of threads\n");
|
||||
return 0;
|
||||
}
|
BIN
CPE435/Lab6/serialDecomp
Executable file
BIN
CPE435/Lab6/serialDecomp
Executable file
Binary file not shown.
47
CPE435/Lab6/serialDecomp.c
Normal file
47
CPE435/Lab6/serialDecomp.c
Normal file
@ -0,0 +1,47 @@
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/time.h>
|
||||
#include <math.h>
|
||||
|
||||
|
||||
#define TIMER_CLEAR (tv1.tv_sec = tv1.tv_usec = tv2.tv_sec = tv2.tv_usec = 0)
|
||||
#define TIMER_START gettimeofday(&tv1, (struct timezone*)0)
|
||||
#define TIMER_ELAPSED (double) (tv2.tv_usec-tv1.tv_usec)/1000000.0+(tv2.tv_sec-tv1.tv_sec)
|
||||
#define TIMER_STOP gettimeofday(&tv2, (struct timezone*)0)
|
||||
struct timeval tv1,tv2;
|
||||
|
||||
double func(double num)
|
||||
{
|
||||
return sqrt(1-pow(num,2));
|
||||
}
|
||||
|
||||
int main(int argc, char * argv[])
|
||||
{
|
||||
TIMER_CLEAR;
|
||||
if (argc < 2)
|
||||
{
|
||||
printf("Please specify the iterations\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
float iterations = atoi(argv[1]);
|
||||
// integral bounds
|
||||
float integral, sumOfRectangles;
|
||||
double a,b, area;
|
||||
a=0;
|
||||
b=1;
|
||||
double h = (b-a)/iterations;
|
||||
int i=0;
|
||||
TIMER_START;
|
||||
// sum up Trapezoids
|
||||
for (i; i <= iterations-1; i++)
|
||||
{
|
||||
area=h*func(a+(i-1)*h);
|
||||
sumOfRectangles+=area;
|
||||
}
|
||||
integral=4*sumOfRectangles;
|
||||
TIMER_STOP;
|
||||
//integral = h*((sqrt(1-pow(a,2))/2)+sumOfTrapezoids+sqrt(1-pow(a,2))/2);
|
||||
printf("Computed Sum for Rectangular Decomposition: %f\n", integral);
|
||||
printf("Time elapsed = %f seconds\n", TIMER_ELAPSED);
|
||||
}
|
25
CPE435/Lab6/timing.c
Normal file
25
CPE435/Lab6/timing.c
Normal file
@ -0,0 +1,25 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
#define TIMER_CLEAR (tv1.tv_sec = tv1.tv_usec = tv2.tv_sec = tv2.tv_usec = 0)
|
||||
#define TIMER_START gettimeofday(&tv1, (struct timezone*)0)
|
||||
#define TIMER_ELAPSED (double) (tv2.tv_usec-tv1.tv_usec)/1000000.0+(tv2.tv_sec-tv1.tv_sec)
|
||||
#define TIMER_STOP gettimeofday(&tv2, (struct timezone*)0)
|
||||
struct timeval tv1,tv2;
|
||||
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
int i;
|
||||
TIMER_CLEAR;
|
||||
TIMER_START;
|
||||
// Call to function that you want to time
|
||||
// example
|
||||
sleep(5); //Timer will give us ~5 seconds
|
||||
TIMER_STOP;
|
||||
printf("Time elapsed = %f seconds\n", TIMER_ELAPSED);
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user