Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.12 KB | None | 0 0
  1. /*
  2. TEST semaphore and mutex
  3. Consider binary semaphore
  4. */
  5.  
  6. #include<stdio.h>
  7. #include<stdlib.h>
  8. #include<unistd.h>
  9. #include<pthread.h>
  10. #include<semaphore.h>
  11. #include<signal.h>
  12.  
  13. #define BUFFER_SIZE 6
  14. typedef int buffer_item;
  15.  
  16. buffer_item buffer[BUFFER_SIZE];
  17.  
  18. // global variable, all threads can acess
  19. int index_counter = 0;
  20. int mmsnot=0;
  21.  
  22. void *thread_Insert(void *arg); // function for sending
  23. void *thread_Remove(void *arg); // function for receiving
  24.  
  25. sem_t bin_sem;  // semaphore
  26. pthread_mutex_t mutx;   // mutex
  27.  
  28. char thread1[]="Thread A";
  29. char thread2[]="Thread B";
  30. char thread3[]="Thread C";
  31.  
  32. int main(int argc, char **argv)
  33. {
  34.   pthread_t t1, t2, t3;
  35.   void *thread_result;
  36.   int state1, state2;
  37.   int mem_arry[1024];
  38.   int *ptr;
  39.   int sze;
  40.  
  41.   state1 = pthread_mutex_init(&mutx, NULL);
  42.   state2 = sem_init(&bin_sem, 0 ,0);
  43.   //mutex initialization
  44.   //semaphore initialization, first value = 0
  45.  
  46.   if(state1||state2!=0)
  47.     puts("Error mutex & semaphore initialization!!!");
  48.  
  49.   *ptr=malloc(1024*sizeof(int));
  50.   for(int i=0;i<1024;i++){
  51.     mem_arry[i]=0;
  52.   }
  53.  
  54.   // Create thread1, thread2, thread3
  55. //  pthread_create(&t1, NULL, thread_Insert, &thread1);
  56. //  pthread_create(&t2, NULL, thread_Remove, &thread2);
  57. //  pthread_create(&t3, NULL, thread_Remove, &thread3);
  58. pthread_create(&t3, NULL, thread_MMS, NULL);
  59. for(i = 0; i < 4; i++)// loop to produce 4 buyer threads
  60.    {
  61.        pthread_t tid;
  62.        pthread_attr_t attr;
  63.        pthread_attr_init(&attr);
  64.        pthread_create(&tid, &attr,thread_user, i);
  65.       }
  66.  
  67.   // Waiting thread to terminate
  68.   pthread_join(t1, &thread_result);
  69.   pthread_join(t2, &thread_result);
  70.   pthread_join(t3, &thread_result);
  71.  
  72.  
  73.   sem_destroy(&bin_sem);    // destroy semaphore
  74.   pthread_mutex_destroy(&mutx); // destroy mutex
  75.  
  76.   return 0;
  77. }
  78.  
  79. // Thread increases item
  80. void *thread_user(int *threadnumber)
  81. {
  82.   sleep(1);
  83.   int i;
  84.   printf("Creating Thread: %d\n", &threadnumber);
  85.   pthread_mutex_lock(&mutx);
  86.   mmsnot=1
  87.   sze=64;
  88.  
  89.   sem_post(&bin_sem);   // semaphore to increase
  90.   pthread_mutex_unlock(&mutx);
  91.  
  92. }
  93.  
  94. // Thread decreases item
  95. void *thread_MMS(void *arg, int alg)
  96. {
  97.   int i;
  98.  
  99.   printf("Creating Thread: %s\n", (char*)arg);
  100.   while(1){
  101.     sem_wait(&bin_sem); //decrease index_counter
  102.     pthread_mutex_lock(&mutx);
  103.     if(mmsnot==1) {
  104.       if(alg==1){
  105.         firstfirt(sze);
  106.       }else if(alg==2)
  107.     }else{
  108.     sleep(1);
  109.   }
  110.   pthread_mutex_unlock(&mutx);
  111. }
  112. int firstfirt(int size){
  113.   int opentrk=0;
  114.   int openstart=0;
  115.   int lpctrl=1;
  116.   for(int i=0;i<1024;i++){
  117.     if (mem_arry[i]==1){
  118.  
  119.     }else if (mem_arry[i]==0){
  120.         opentrk++;
  121.         openstart=i;
  122.         return opentrk;
  123.     }
  124.   }
  125.   while(lpctrl){
  126.   if(size<=opentrk){
  127.     for(int i=openstart;i<(openstart+opentrk);i++){
  128.       mem_arry[i]=1;
  129.       ptr=malloc(size*sizeof(int));
  130.       return 0;
  131.     }
  132.   else if(size>=opentrk){
  133.     for(int i=(openstart+opentrk);i<1024;i++){
  134.       if (mem_arry[i]==0){
  135.           opentrk++;
  136.           openstart=i;
  137.           return opentrk;
  138.       }
  139.  
  140.     }
  141.   }
  142. }
  143. }
  144. }
  145. int BestFit(){
  146.  
  147. }
  148. int Wrostfit(){
  149.  
  150. }
  151.  
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement