OS_Labs[1]
OS_Labs[1]
#include <stdio.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
union semun {
};
sem_union.val = value;
exit(EXIT_FAILURE);
exit(EXIT_FAILURE);
exit(EXIT_FAILURE);
int main() {
key_t key;
int semid;
if ((key = ftok(".", 'A')) == -1) { // Use current directory as the key source
perror("ftok failed");
exit(EXIT_FAILURE);
}
perror("semget failed");
exit(EXIT_FAILURE);
initialize_semaphore(semid, 1);
if (pid < 0) {
perror("fork failed");
exit(EXIT_FAILURE);
if (pid == 0) {
// Child process
semaphore_wait(semid);
semaphore_signal(semid);
} else {
// Parent process
semaphore_wait(semid);
semaphore_signal(semid);
wait(NULL);
exit(EXIT_FAILURE);
return 0;
Output:
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#include <unistd.h>
#define NUM_PHILOSOPHERS 5
sem_t forks[NUM_PHILOSOPHERS];
pthread_mutex_t mutex;
sleep(1);
sleep(2);
while (1) {
think(philosopher);
pthread_mutex_lock(&mutex);
pthread_mutex_unlock(&mutex);
eat(philosopher);
return NULL;
int main() {
pthread_t philosophers[NUM_PHILOSOPHERS];
int philosopher_ids[NUM_PHILOSOPHERS];
sem_init(&forks[i], 0, 1);
pthread_mutex_init(&mutex, NULL);
philosopher_ids[i] = i;
return 1;
pthread_join(philosophers[i], NULL);
// Clean up resources
sem_destroy(&forks[i]);
}
pthread_mutex_destroy(&mutex);
return 0;;
Output:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#define BUFFER_SIZE 5
#define NUM_PRODUCERS 2
#define NUM_CONSUMERS 2
#define NUM_ITEMS 10
// Shared buffer
int buffer[BUFFER_SIZE];
int in = 0, out = 0;
// Producer function
int id = (int)arg;
// Produce an item
buffer[in] = item;
in = (in + 1) % BUFFER_SIZE;
return NULL;
// Consumer function
int id = (int)arg;
// Consume an item
return NULL;
int main() {
pthread_mutex_init(&mutex, NULL);
producer_ids[I] = I + 1;
exit(EXIT_FAILURE);
consumer_ids[I] = I + 1;
pthread_join(producers[I], NULL); }
pthread_join(consumers[I], NULL);
sem_destroy(&empty);
sem_destroy(&full);
pthread_mutex_destroy(&mutex);
return 0;}
Output:
Lab # 10
(a). Deadlocks in C (Demonstration)
#include <pthread.h>
#include <stdio.h>
#include <unistd.h> // Include this for the sleep() function
pthread_mutex_t lock1, lock2;
void *thread1(void *arg) {
pthread_mutex_lock(&lock1);
printf("Thread 1: Locked lock1\n");
sleep(1); // Simulate work
pthread_mutex_lock(&lock2);
printf("Thread 1: Locked lock2\n");
pthread_mutex_unlock(&lock2);
pthread_mutex_unlock(&lock1);
return NULL;
}
void *thread2(void *arg) {
pthread_mutex_lock(&lock2);
printf("Thread 2: Locked lock2\n");
sleep(1); // Simulate work
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_create(&t1, NULL, thread1, NULL);
pthread_create(&t2, NULL, thread2, 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
int available[MAX], max[MAX][MAX], allocation[MAX][MAX], need[MAX][MAX];
int processes, resources;
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;
for (int i = 0; i < resources; i++)
work[i] = available[i];
for (int count = 0; count < processes; count++) {
int found = 0;
for (int i = 0; i < processes; i++) {
if (!finish[i]) {
int j;
for (j = 0; j < resources; j++) {
if (need[i][j] > work[j])
break;
}
if (j == resources) {
for (int k = 0; k < resources; k++)
work[k] += allocation[i][k];
safeSequence[index++] = i;
finish[i] = 1;
found = 1;
}
}
}
if (!found) {
printf("System is not in a safe state\n");
return 0;
}
}
printf("System is in a safe state. Safe sequence: ");
for (int i = 0; i < processes; i++)
printf("%d ", safeSequence[i]);
printf("\n");
return 1;
}
int main() {
printf("Enter number of processes and resources: ");
scanf("%d %d", &processes, &resources);
printf("Enter available resources: ");
for (int i = 0; i < resources; i++)
scanf("%d", &available[i]);
printf("Enter maximum resource matrix: \n");
for (int i = 0; i < processes; i++)
for (int j = 0; j < resources; j++)
scanf("%d", &max[i][j]);
printf("Enter allocated resource matrix: \n");
for (int i = 0; i < processes; i++)
for (int j = 0; j < resources; j++)
scanf("%d", &allocation[i][j]);
calculateNeed();
isSafe();
return 0;
}
Output:
#include <pthread.h>
#include <stdio.h>
pthread_mutex_lock(&resource1);
pthread_mutex_lock(&resource2);
pthread_mutex_unlock(&resource2);
pthread_mutex_unlock(&resource1);
return NULL;
pthread_mutex_lock(&resource2);
pthread_mutex_lock(&resource1);
pthread_mutex_unlock(&resource1);
pthread_mutex_unlock(&resource2);
return NULL;
int main() {
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:
Lab # 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;
}
printf("Memory allocation graph:\n");
for (int i = 0; i < 5; i++) {
arr[i] = i * 10;
printf("arr[%d] = %d (Address: %p)\n", i, arr[i], (void*)&arr[i]);
}
free(arr); // Freeing the memory
printf("Memory freed.\n");
return 0;
}
Output:
b. Simulating malloc() in C
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
Output:
c. Using free() in Linux
#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr = malloc(5 * sizeof(int));
if (!arr) {
printf("Memory allocation failed\n");
return 1;
}
for (int i = 0; i < 5; i++) {
arr[i] = i * 10;
printf("arr[%d] = %d\n", i, arr[i]);
}
Output:
Lab # 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;
}
for (int i = 0; i < 3; i++) {
arr[i] = i + 1;
printf("arr[%d] = %d\n", i, arr[i]);
}
printf("Reallocating memory...\n");
arr = realloc(arr, 5 * sizeof(int));
if (!arr) {
printf("Memory reallocation failed\n");
return 1;
}
for (int i = 3; i < 5; i++) {
arr[i] = i + 1;
}
for (int i = 0; i < 5; i++) {
printf("arr[%d] = %d\n", i, arr[i]);
}
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;
}
for (int i = 0; i < 5; i++) {
arr[i] = i * 10;
printf("arr[%d] = %d\n", i, arr[i]);
}
free(arr);
return 0;
}
Output:
Lab # 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);
Output:
b. Demonstrating shmdt, shmctl, and ftok
#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);
printf("Read data from shared memory: %s\n", str);
shmdt(str);
shmctl(shmid, IPC_RMID, NULL);
return 0;
}
Output:
Lab # 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:
Lab # 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;
}
fclose(fp);
return 0;
}
Output:
These programs cover the requirements for all weeks. Let me know if you need further
explanations or modifications!