Oslab 10 To 15
Oslab 10 To 15
a. Deadlocks in C (Demonstration)
#include <pthread.h>
#include <stdio.h>
#include <unistd.h> // Include this for the sleep() function
pthread_mutex_lock(&lock2);
printf("Thread 1: Locked lock2\n");
pthread_mutex_unlock(&lock2);
pthread_mutex_unlock(&lock1);
return NULL;
}
pthread_mutex_lock(&lock1);
printf("Thread 2: Locked lock1\n");
pthread_mutex_unlock(&lock1);
pthread_mutex_unlock(&lock2);
return NULL;
}
int main() {
pthread_t t1, t2;
pthread_mutex_init(&lock1, NULL);
pthread_mutex_init(&lock2, NULL);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
pthread_mutex_destroy(&lock1);
pthread_mutex_destroy(&lock2);
return 0;
}
Output:
b. Deadlock Avoidance Using Dijkstra's Banker's Algorithm
#include <stdio.h>
#define MAX 10
void calculateNeed() {
for (int i = 0; i < processes; i++) {
for (int j = 0; j < resources; j++) {
need[i][j] = max[i][j] - allocation[i][j];
}
}
}
int isSafe() {
int work[MAX], finish[MAX] = {0};
int safeSequence[MAX], index = 0;
int main() {
printf("Enter number of processes and resources: ");
scanf("%d %d", &processes, &resources);
calculateNeed();
isSafe();
return 0;
}
Output:
Other case check
c. Simulating Deadlock with Two Threads and Two Resources
#include <pthread.h>
#include <stdio.h>
#include <unistd.h> // Include this for the sleep() function
pthread_mutex_unlock(&resource2);
pthread_mutex_unlock(&resource1);
return NULL;
}
pthread_mutex_lock(&resource1);
printf("Thread 2: Locked resource1\n");
pthread_mutex_unlock(&resource1);
pthread_mutex_unlock(&resource2);
return NULL;
}
int main() {
pthread_t thread1, thread2;
pthread_mutex_init(&resource1, NULL);
pthread_mutex_init(&resource2, NULL);
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
pthread_mutex_destroy(&resource1);
pthread_mutex_destroy(&resource2);
return 0;
}
Output:
Week 11
a. Memory Allocation Graph and Resources in C
#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr = malloc(5 * sizeof(int)); // Allocating memory for 5 integers
if (!arr) {
printf("Memory allocation failed\n");
return 1;
}
b. Simulating malloc() in C
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
void *my_malloc(size_t size) {
void *ptr = sbrk(size);
if (ptr == (void*)-1) {
printf("Memory allocation failed\n");
return NULL;
}
return ptr;
}
int main() {
int *arr = (int*)my_malloc(5 * sizeof(int));
if (!arr) {
return 1;
}
return 0;
}
Output:
int main() {
int *arr = malloc(5 * sizeof(int));
if (!arr) {
printf("Memory allocation failed\n");
return 1;
}
return 0;
}
Output:
Week 12
a. Demonstrating calloc()
#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr = (int*)calloc(5, sizeof(int)); // Allocates memory for 5
integers and initializes to 0
if (!arr) {
printf("Memory allocation failed\n");
return 1;
}
printf("Using calloc():\n");
for (int i = 0; i < 5; i++) {
printf("arr[%d] = %d\n", i, arr[i]);
}
free(arr);
return 0;
}
Output:
b. Demonstrating realloc()
#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr = (int*)malloc(3 * sizeof(int));
if (!arr) {
printf("Memory allocation failed\n");
return 1;
}
printf("Reallocating memory...\n");
arr = realloc(arr, 5 * sizeof(int));
if (!arr) {
printf("Memory reallocation failed\n");
return 1;
}
free(arr);
return 0;
}
Output:
c. Demonstrating malloc()
#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr = (int*)malloc(5 * sizeof(int));
if (!arr) {
printf("Memory allocation failed\n");
return 1;
}
free(arr);
return 0;
}
Output:
Week 13
a. Demonstrating Shared Memory Using shmat()
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>
int main() {
key_t key = ftok("shmfile", 65);
int shmid = shmget(key, 1024, 0666|IPC_CREAT);
char *str = (char*)shmat(shmid, (void*)0, 0);
shmdt(str);
return 0;
}
Output:
int main() {
key_t key = ftok("shmfile", 65);
int shmid = shmget(key, 1024, 0666|IPC_CREAT);
char *str = (char*)shmat(shmid, (void*)0, 0);
shmdt(str);
shmctl(shmid, IPC_RMID, NULL);
return 0;
}
Output:
Week 14
Shared Memory Using shmget()
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
int main() {
key_t key = ftok("shmfile", 65);
int shmid = shmget(key, 1024, 0666|IPC_CREAT);
char *str = (char*)shmat(shmid, (void*)0, 0);
shmdt(str);
shmctl(shmid, IPC_RMID, NULL);
return 0;
}
Output:
Week 15
File Handling in C
#include <stdio.h>
int main() {
FILE *fp = fopen("example.txt", "w");
if (!fp) {
printf("Failed to open file\n");
return 1;
}
fp = fopen("example.txt", "r");
if (!fp) {
printf("Failed to open file\n");
return 1;
}
char buffer[100];
while (fgets(buffer, sizeof(buffer), fp)) {
printf("%s", buffer);
}
fclose(fp);
return 0;
}
Output:
These programs cover the requirements for all weeks. Let me know if you need further
explanations or modifications!