2nd #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> // Total allocatable memory #define PROC_MEM_SIZE 1000 void *proc_mem; struct pair { void* start ; int size; }; struct pair track[10]; int pair = 0; // Memory managemnt functions // You must implement these void *alloc_mem(int size) { if(size > PROC_MEM_SIZE) { printf("NULL"); return NULL; } else { int i; for( i = 0 ; i < pair; i++) { if(track[i].size/2 > size) { track[i].size = track[i].size/2; int j; for( j = i+1 ; j < 10 ; j++) { if(track[j].size == 0) { track[j].start = track[i].start + track[i].size; track[j].size = track[i].size; // printf("%d \n" , track[i].size); pair++; break; } } } else { pair--; // printf("%d" , track[i].size); track[i].size = 0; printf("\n %d \n" , track[i].start); return track[i].start; } } } } void free_mem(void *ptr, int size) { int i,j; bool match = true; int size2= size; int a=2; int *ptr1 =&a; void *ptr2 = ptr; while(match == true) { for(i =0 ; i < 10 ; i++) { if(track[i].size >= size2 && track[i].size < size2*2 && ptr2 != track[i].start ) { track[i].size = track[i].size + track[i].size; ptr2 = track[i].start; size2 = track[i].size; *ptr1 = 0; ptr1 =&(track[i].size); match = true; // free(ptr); break; } match = false; } } } int main(void) { int tempi; for(tempi= 0 ;tempi < 10 ;tempi++) { track[tempi].size = 0; } proc_mem = malloc(PROC_MEM_SIZE); memset(proc_mem, 0, PROC_MEM_SIZE); track[0].start = proc_mem; void *temp = proc_mem + PROC_MEM_SIZE; // printf("\n %d\n " , proc_mem); track[0].size =temp- track[0].start; pair = 1; pair = 1; char *arr1 = alloc_mem(100); char *arr2 = alloc_mem(500); // printf("%d" , arr1); memcpy(arr1, "This is the first array, it has 100 bytes of memory allocated\n\0", 100); memcpy(arr2, "This is the second array, it has 500 bytes of memory allocated\n\0", 500); printf("\n %d\n " , arr1); printf("arr1:\n %s", arr1); printf("arr2:\n %s", arr2); free_mem(arr1,100); free_mem(arr2,500); // printf("\n %d \n " , &arr1); // int i ; // for(i = 0 ; i < 10 ; i++) // printf("%d",track[i].size); free(proc_mem); } 1st #include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <unistd.h> // Thread functions void *thread1(void *); void *thread2(void *); void *thread3(void *); // global variables... int happening[3] = {0, 0, 0}; int need[3][3] = {{1, 1, 1}, {1, 1, 1}, {1, 1, 1}}; int available[3] = {1, 1, 1}; // Global mutex pthread_mutex_t m1, m2, m3; int main(void) { pthread_t t1, t2, t3; pthread_mutex_init(&m1, NULL); pthread_mutex_init(&m1, NULL); pthread_mutex_init(&m1, NULL); pthread_create(&t1, NULL, thread1, NULL); pthread_create(&t2, NULL, thread2, NULL); pthread_create(&t3, NULL, thread3, NULL); pthread_join(t1, NULL); pthread_join(t2, NULL); pthread_join(t3, NULL); pthread_mutex_destroy(&m1); pthread_mutex_destroy(&m2); pthread_mutex_destroy(&m3); return EXIT_SUCCESS; } void *thread1(void *arg) { printf("Thread 1: Started\n"); pthread_mutex_lock(&m1); need[0][0]--; // means m1 is not further required by thread-1 int f = 100000; while (f--) { } printf("Thread 1: Acquired Resource m1\n"); pthread_mutex_lock(&m2); need[0][1]--; printf("Thread 1: Acquired Resource m2\n"); pthread_mutex_lock(&m3); need[0][2]--; printf("Thread 1: Acquired Resource m3\n"); pthread_mutex_unlock(&m3); printf("Thread 1: Released Resource m3\n"); pthread_mutex_unlock(&m2); printf("Thread 1: Released Resource m2\n"); pthread_mutex_unlock(&m1); printf("Thread 1: Released Resource m1\n"); printf("Thread 1: Finished\n"); } void *thread2(void *arg) { printf("Thread 2: Started\n"); while (need[0][0] || need[0][1] || need[0][2]) { } // need for the previous should be fulfilled.. while (!available[1]) { } // if not available then no need to lock here.... pthread_mutex_lock(&m2); need[1][1]--; int i = 1000000; printf("Thread 2: Acquired Resource m2\n"); pthread_mutex_lock(&m3); need[1][2]--; printf("Thread 2: Acquired Resource m3\n"); pthread_mutex_lock(&m1); need[1][0]--; printf("Thread 2: Acquired Resource m1\n"); pthread_mutex_unlock(&m1); while (i--) { } printf("Thread 2: Released Resource m1\n"); pthread_mutex_unlock(&m3); printf("Thread 2: Released Resource m3\n"); pthread_mutex_unlock(&m2); printf("Thread 2: Released Resource m2\n"); printf("Thread 2: Finished\n"); } void *thread3(void *arg) { printf("Thread 3: Started\n"); while (need[1][0] || need[1][1] || need[1][2]) { } pthread_mutex_lock(&m3); printf("Thread 3: Acquired Resource m3\n"); pthread_mutex_lock(&m1); printf("Thread 3: Acquired Resource m1\n"); int c = 1000000; while (c--) { } pthread_mutex_lock(&m2); printf("Thread 3: Acquired Resource m2\n"); pthread_mutex_unlock(&m2); printf("Thread 3: Released Resource m2\n"); pthread_mutex_unlock(&m1); printf("Thread 3: Released Resource m1\n"); pthread_mutex_unlock(&m3); printf("Thread 3: Released Resource m3\n"); printf("Thread 3: Finished\n"); }