Write a code to pass a simple integer to each thread and measure time taken by each thread with out and with pthread_join. The calling thread uses a unique data structure for each thread, insuring that each thread's argument remains intact throughout the program.

Solution: 1) with our pthread_join

//Write a code to pass a simple integer to each thread. The calling thread uses a unique
//data structure for each thread, insuring that each thread's argument remains intact
//throughout the program.

// we try to display the massages based on user's imput through scanf

// we try to find the time taken by each thread
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define NUM_THREADS 8 // define total no of threds as global variable
char *messages[NUM_THREADS]; // define the pointer globally to pass massage

// pthread function
void *PrintHello(void *threadid)
{
long taskid;
sleep(1);
taskid = (long) threadid;
printf("Thread %d: %s\n", taskid, messages[taskid]);
pthread_exit(NULL);
}


int main(int argc, char *argv[])
{
clock_t tm1,tm2;
double time_taken;
pthread_t threads[NUM_THREADS]; // define pthread id
long taskids[NUM_THREADS];        // define taskid
int rc, t, NUM;                    

// how many massage you want to display from 8
printf ("please enter number of thread here (num must be <=8):");
scanf ("%d/n",&NUM);

// massages
messages[0] = "English: Hello World!";
messages[1] = "French: Bonjour, le monde!";
messages[2] = "Spanish: Hola al mundo";
messages[3] = "Klingon: Nuq neH!";
messages[4] = "German: Guten Tag, Welt!";
messages[5] = "Russian: Zdravstvuyte, mir!";
messages[6] = "Japan: Sekai e konnichiwa!";
messages[7] = "Latin: Orbis, te saluto!";

// creating function and calling it
for(t=0;t<NUM;t++) {
taskids[t] = t;
printf("Creating thread %d\n", t);
tm1 = clock();
rc = pthread_create(&threads[t], NULL, PrintHello, (void *) taskids[t]);
tm2 = clock()-tm1;
time_taken = ((double) tm2-tm1)/ CLOCKS_PER_SEC;

printf("fun(%d) took %f seconds to execute \n",t, time_taken);

if (rc) {
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
pthread_exit(NULL);
}

Result:

Sony VIO@SonyVIO-PC MSYS /c/mingw/pthread_codes
$ ./passing.exe
please enter number of thread here (num must be <=8):4
Creating thread 0
fun(0) took 0.001000 seconds to execute
Creating thread 1
fun(1) took -0.031000 seconds to execute
Creating thread 2
fun(2) took -0.031000 seconds to execute
Creating thread 3
fun(3) took -0.031000 seconds to execute
Thread 0: English: Hello World!
Thread 1: French: Bonjour, le monde!
Thread 2: Spanish: Hola al mundo
Thread 3: Klingon: Nuq neH!
 

2) With pthread_join

//Write a code to pass a simple integer to each thread. The calling thread uses a unique
//data structure for each thread, insuring that each thread's argument remains intact
//throughout the program.

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define NUM_THREADS 8 // define total no of threds as global variable
char *messages[NUM_THREADS]; // define the pointer globally to pass massage

// pthread function
void *PrintHello(void *threadid)
{
long taskid;
sleep(1);
taskid = (long) threadid;
printf("Thread %d: %s\n", taskid, messages[taskid]);
pthread_exit(NULL);
}


int main(int argc, char *argv[])
{
clock_t tm1,tm2;
double time_taken;
pthread_t threads[NUM_THREADS]; // define pthread id
long taskids[NUM_THREADS];        // define taskid
int rc, t, NUM;                    

// how many massage you want to display from 8
printf ("please enter number of thread here (num must be <=8):");
scanf ("%d/n",&NUM);

// massages
messages[0] = "English: Hello World!";
messages[1] = "French: Bonjour, le monde!";
messages[2] = "Spanish: Hola al mundo";
messages[3] = "Klingon: Nuq neH!";
messages[4] = "German: Guten Tag, Welt!";
messages[5] = "Russian: Zdravstvuyte, mir!";
messages[6] = "Japan: Sekai e konnichiwa!";
messages[7] = "Latin: Orbis, te saluto!";

// creating function and calling it
for(t=0;t<NUM;t++) {
taskids[t] = t;
printf("Creating thread %d\n", t);
tm1 = clock();
rc = pthread_create(&threads[t], NULL, PrintHello, (void *) taskids[t]);
pthread_join(threads[t], NULL);
tm2 = clock()-tm1;
time_taken = ((double) tm2-tm1)/ CLOCKS_PER_SEC;

printf("fun(%d) took %f seconds to execute \n",t, time_taken);

if (rc) {
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
pthread_exit(NULL);
}

Result: 

Sony VIO@SonyVIO-PC MSYS /c/mingw/pthread_codes
$ ./passing.exe
please enter number of thread here (num must be <=8):5
Creating thread 0
Thread 0: English: Hello World!
fun(0) took 0.016000 seconds to execute
Creating thread 1
Thread 1: French: Bonjour, le monde!
fun(1) took -0.046000 seconds to execute
Creating thread 2
Thread 2: Spanish: Hola al mundo
fun(2) took -0.046000 seconds to execute
Creating thread 3
Thread 3: Klingon: Nuq neH!
fun(3) took -0.030000 seconds to execute
Creating thread 4
Thread 4: German: Guten Tag, Welt!
fun(4) took -0.062000 seconds to execute

Conclusion:

You can clearly see the difference in result where in case one thread is not wait for the massage to display in case 2 untill massage will not display next thread can not start. As well you can see the difference in the time taken by each mode. Precise time taken can see in the second case.

Comments

Popular posts from this blog

print hello world using pthread through creating 5 threads.

write simple code to create pthread and execute the code