Advertisement
Guest User

Untitled

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