0% found this document useful (0 votes)
24 views15 pages

Oslab 10 To 15

Uploaded by

anaschaudry0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views15 pages

Oslab 10 To 15

Uploaded by

anaschaudry0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Week 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:
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_t resource1, resource2;

void *threadFunc1(void *arg) {


pthread_mutex_lock(&resource1);
printf("Thread 1: Locked resource1\n");

sleep(1); // Simulate work


pthread_mutex_lock(&resource2);
printf("Thread 1: Locked resource2\n");

pthread_mutex_unlock(&resource2);
pthread_mutex_unlock(&resource1);

return NULL;
}

void *threadFunc2(void *arg) {


pthread_mutex_lock(&resource2);
printf("Thread 2: Locked resource2\n");

sleep(1); // Simulate work

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_create(&thread1, NULL, threadFunc1, NULL);


pthread_create(&thread2, NULL, threadFunc2, 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;
}

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>
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;
}

for (int i = 0; i < 5; i++) {


arr[i] = i + 1;
printf("arr[%d] = %d\n", i, arr[i]);
}

return 0;
}
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]);
}

free(arr); // Free allocated memory


printf("Memory has been freed.\n");

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;
}

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:
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);

printf("Write data to shared memory: ");


fgets(str, 1024, stdin);

printf("Data written in memory: %s\n", str);

shmdt(str);

return 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:

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);

printf("Write data to shared memory: ");


fgets(str, 1024, stdin);

printf("Data in shared memory: %s\n", str);

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;
}

fprintf(fp, "Hello, file handling in C!\n");


fclose(fp);

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!

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy