/**Jake of threads being added/**//**linked list of thread

/**Jake Hollis 3478534 CS4405 Assignment 1 – Best Fit Algorithm for Pthreads**/#include #include #include #include #include /****mutex lock for pthread**/pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;/**All of the sums of the allocated and free memory spaces as well as the total number of threadsand actual threads themselves*/pthread_t *threads;int P = 0; // total allocation operationsint X = 0; // total allocated memoryint Q = 0; // total freed operationsint Y = 0; // total freed memoryint cnt = 1;int adjustedHeap = 0;int y = 0;int heap = 0; // heap sizeint numOfThreads = 0; //number of threads being added/**//**linked list of thread information and a linked list for the Structs**/struct threadInfo{    int numThread;    char opType; //either free or alloc    int amount;    int lineNum;    struct threadInfo *next;};  struct threadInfo **head;void *bestFit(void *arg){    /**lock the thread**/    pthread_mutex_lock(&lock);    /**populating heap array**/    int heapArrayheap;    char boolean = ‘f’;    adjustedHeap = heap;    int i= 0;    for(int i = 0; i numThread == cnt && temp->opType == ‘A’){            /**this is where the magic happens :D**/            adjustedHeap = heap;            for(int i = 0; i = temp->amount){                      id = 0;                      adjustedHeap -= temp->amount;                      break;                  }                  else if(adjustedHeap amount){                      printf(“Thread %d failed to allocate %d memory on line %d
“, temp->numThread, temp->amount, temp->lineNum);                      boolean = ‘o’;                      break;                  }                  else{                      heapArrayi = temp->lineNum;                      P++;                      X = temp->amount;                      adjustedHeap -= temp->amount;                      id++;                  }               }            }        }        else if(temp->numThread == cnt && temp->opType == ‘F’){                /**this is where the magic happens :D**/                    for(int i = 0; i lineNum){                            adjustedHeap -= temp->amount;                         }                         else if(adjustedHeap amount){                            printf(“Thread %d failed to free %d memory on line %d
“, temp->numThread, temp->amount, temp->lineNum);                            boolean = ‘o’;                            break;                         }                         else{                            heapArrayi = -1;                            adjustedHeap -= temp->amount;                            id++;                         }                    }         }      temp = temp->next;      adjustedHeap = heap;    }    if(boolean != ‘o’){        printf(“Thread %d completed successfully: Total allocation operations %d, Total allocated memory %d, Total free operations %d, Total freed memory %d

“, cnt, P, X, Q, Y);    }    //counter for multithreading    P=0;    X=0;    Q=0;    Y=0;    id=0;    adjustedHeap=0;    cnt++;    pthread_mutex_unlock(&lock);    return 0;}/**pthread bestfit algorithm*//***creating and adding info to the thread list struct**/void addThreadInfo(struct threadInfo** infoThreadList, int nT, char oT, int aT, int lN){    struct threadInfo *temp;    temp=(struct threadInfo *)malloc(sizeof(struct threadInfo));    temp->numThread = nT;    temp->opType = oT;    temp->amount = aT;    temp->lineNum = lN;  //  printf(”

We Will Write a Custom Essay Specifically
For You For Only $13.90/page!

order now

temp: %d,temp: %c,temp: %d,temp: %d

“, temp->numThread, temp->opType, temp->amount, temp->lineNum);    temp->next = (*infoThreadList);    (*infoThreadList) = temp;}/***checks to see if the thread number is the largest as it comes through***/void setNumOfThreads(int nThrd){    int n;    n = nThrd;    if(n > numOfThreads){       numOfThreads = n;    }}void rev(struct threadInfo** thdList){    struct threadInfo* cR = *thdList;    struct threadInfo* nT;    struct threadInfo* pV = NULL;    while(cR != NULL){        nT  = cR->next;        cR->next = pV;        pV = cR;        cR = nT;    }    *thdList = pV;}/**Creates threads, pipes text file to the scanf sends data to the various functions.*/int main(int argc, char * argv){  struct threadInfo *infoThread = NULL;  /**the heap equals the 3rd argument in the terminal**/  heap = atoi(argv2);  printf(“Heap Size: %d

“, heap);  int j = 0;  // placeholder values to add to struct dynamic linked list  char TT; // literally pointless putting this here. I’m including this for laughs.  int numThrd;  char operType;  int amnt;  int lnNum = 0; // starts at line 0  if(argc > 0){    if(strcmp(argv1, “-s”) == 0){        while(scanf(“%c%d %c%d
“, , , , ) != EOF){              printf(“%d, %c, %d, %d
“, numThrd, operType, amnt, lnNum); //T1 A1 %s prints char arrays /// %c prints a single char              addThreadInfo(, numThrd, operType, amnt, lnNum);              setNumOfThreads(numThrd);              lnNum++;        }    }    else{        printf(“Please input the program correctly next time, idiot.
“);    }  }  rev();  /**  **  Thread calling and mutex lock exception handling  **  **/  if(pthread_mutex_init(, NULL) != 0){      printf(”
Init mutex lock has failed
“);      return 1;  }  threads = malloc(sizeof(pthread_t)*numOfThreads);  for(int i = 0; i


I'm Alejandro!

Would you like to get a custom essay? How about receiving a customized one?

Check it out