Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <pthread.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <sys/shm.h>
- #include <sys/ipc.h>
- #include <unistd.h>
- #include <semaphore.h>
- #include <sys/time.h>
- char tab[10];
- sem_t mtx;
- void randomize()
- {
- int i;
- for(i=0;i<10;i++)
- {
- tab[i] = (char) (rand() % 26) + 97;
- }
- }
- void printab(char* tab)
- {
- int i;
- for(i = 0; i < 10; i++)
- {
- printf("%c ", tab[i]);
- }
- }
- void Sorted()
- {
- int i;
- for (i=0; i<9; i++)
- {
- if (tab[i] > tab[i+1])
- {
- break ;
- }
- }
- if (i < 9)
- {
- printf("[X]");
- }
- else
- {
- printf("[O]");
- }
- }
- int compare(const void *x, const void *y)
- {
- return *(const char *)x - *(const char *)y;
- }
- void *ThreadFunc(void *pContext)
- {
- int tid = (intptr_t)pContext; //Thread ID
- printf("##### Thread #%d started...\n", tid + 1);
- struct timeval time1, time2;
- gettimeofday(&time1, NULL);
- sem_wait(&mtx );//check and decrement mtx value
- printf("[%d] --> ", tid + 1);
- randomize();
- qsort(tab, 10, 1, compare); //sort the tab
- printab(tab);
- Sorted(); //check is sorted
- sem_post(&mtx); //increment mtx
- gettimeofday(&time2, NULL);
- printf (" %d -> ",(int) (time1.tv_usec ));
- printf ("%d\n",(int) (time2.tv_usec ));
- printf ("Time spent %d\n",(int) (time2.tv_usec - time1.tv_usec));
- return NULL;
- }
- int main()
- {
- int i;
- struct timeval time1, time2;
- printf ("##### Thread: Start Time -> %d\n",(int) (time1.tv_usec));
- gettimeofday(&time1, NULL);
- pthread_t tab_thread[20];
- sem_init(&mtx, 0, 1 );
- for (i = 0; i < 20; ++i)
- {
- pthread_create(&(tab_thread[i]), NULL, ThreadFunc, (void *)(intptr_t)i);
- }
- for (i = 0; i < 20; ++i)
- {
- pthread_join(tab_thread[i], NULL);//wait and check
- }
- sem_destroy(&mtx); //libérer la mémoire
- gettimeofday(&time2, NULL);
- printf ("##### Thread: End Time -> %d\n",(int) (time2.tv_usec));
- return 0;
- }
- 2. Output of the
- program:
- This program was made a simple programming environment (Ubuntu 14.04 on VirtualMachine) with
- VIM and clang. I chose to use Ubuntu on Virtual Machine, although I also have it by Dual Boot for practical
- reasons. I chose VIM and gcc as text editor and compiler because I am used to use them at my home
- university in France. The program is then compiled with a basic makefile with clang.
- The fuction « randomize » fills an array with random characters.
- The fuction « printab » print the array.
- The fuction « sorted » checks if the characters in the array are sorted (alphabetic order).
- The function « compar » compar two characters and returns the difference between the ASCII code
- value of the two characters. If the value is postive the characters are well sorted else the second character
- have to be place before the fist one in the array.
- The fuction « ThreadFunc » is the fucntion that each thread have to excute.
- « tid » is the thread ID. We have to use the varible « mtx » to manage the order of execution of each thread.
- Indeed we can fill the table with only one thread at a time. For that we use the semaphore « mtx » which is
- set at 1. « sem_wait » is used to to manage it. It will decrement it. If mtx = 1 the thread is executed and mtx is
- decremented, else the thread have to wait his turn. Then we use the function randomize to fill the table.
- « qsort » is a function which sort the array. It take as argument an array, the number of elements of that
- array, the size of each elements of that array and a function which compare the two characters. Then it prints
- the table and check if it is well sorted. The semaphore « mtx » is then decremented thanks to the function
- « sem_post ».
- In the main function, we inialize the semaphore with « sem_init ». Then we creat and initialize an
- array of thread. We fill the array with threads thanks to the function « pthred_creat » which creats threads.
- Then we go through the table with « pthread_join » function which wait a specific thread to terminate and
- check if it is terminate. We release the memory of « mtx ».
- For the time we I used function of a library ( time.h).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement