0% found this document useful (0 votes)
12 views11 pages

Os 67

The document contains a series of C programming exercises conducted over several weeks at Aditya University, covering topics such as file operations, resource allocation algorithms, paging, segmentation, shared memory, and multithreading. Each section includes code snippets demonstrating the implementation of these concepts along with expected outputs. The exercises are aimed at enhancing students' understanding of operating systems and concurrent programming.

Uploaded by

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

Os 67

The document contains a series of C programming exercises conducted over several weeks at Aditya University, covering topics such as file operations, resource allocation algorithms, paging, segmentation, shared memory, and multithreading. Each section includes code snippets demonstrating the implementation of these concepts along with expected outputs. The exercises are aimed at enhancing students' understanding of operating systems and concurrent programming.

Uploaded by

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

Exp No:

Date: Page No:

Week-3
Program:
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *file;
char data[100];
file = fopen("example.txt", "w");
if (file == NULL) {
printf("Error opening file for writing!\n");
return 1; }
fprintf(file, "Hello, this is a file write operation in C!\n");
fclose(file);
printf("Data written to file successfully.\n");
file = fopen("example.txt", "r");
if (file == NULL) {
printf("Error opening file for reading!\n");
return 1; }
printf("File content:\n");
while (fgets(data, sizeof(data), file) != NULL) {
printf("%s", data); }
fclose(file);
return 0;}
Output:

ADITYA UNIVERSITY (Formerly Aditya Engineering College (A)) ROLL NO: 23A91A0567
Exp No:
Date: Page No:

Week-4
Program:
#include <stdio.h>
#include <stdbool.h>
#define P 5
#define R 3
int available[R];
int maximum[P][R];
int allocation[P][R];
int need[P][R];
void calculateNeed() {
for (int i = 0; i < P; i++) {
for (int j = 0; j < R; j++) {
need[i][j] = maximum[i][j] - allocation[i][j];
}
}
}
bool isSafe() {
bool finish[P] = {false};
int work[R];
for (int i = 0; i < R; i++) {
work[i] = available[i];
}
int safeSequence[P];
int count = 0;
while (count < P) {
bool found = false;
for (int i = 0; i < P; i++) {
if (!finish[i]) {

ADITYA UNIVERSITY (Formerly Aditya Engineering College (A)) ROLL NO: 23A91A0567
Exp No:
Date: Page No:

bool canAllocate = true;


for (int j = 0; j < R; j++) {
if (need[i][j] > work[j]) {
canAllocate = false;
break;
}
}
if (canAllocate) {
for (int j = 0; j < R; j++) {
work[j] += allocation[i][j];
}
safeSequence[count++] = i;
finish[i] = true;
found = true;
}
}
}
if (!found) {
printf("System is not in a safe state\n");
return false;
}
}
printf("System is in a safe state. Safe sequence: ");
for (int i = 0; i < P; i++)
printf("%d ", safeSequence[i]);
printf("\n");
return true;
}
int main() {
printf("Enter available resources: ");

ADITYA UNIVERSITY (Formerly Aditya Engineering College (A)) ROLL NO: 23A91A0567
Exp No:
Date: Page No:

for (int i = 0; i < R; i++)


scanf("%d", &available[i]);
printf("Enter maximum resources for each process:\n");
for (int i = 0; i < P; i++) {
for (int j = 0; j < R; j++)
scanf("%d", &maximum[i][j]);
}
printf("Enter allocated resources for each process:\n");
for (int i = 0; i < P; i++) {
for (int j = 0; j < R; j++)
scanf("%d", &allocation[i][j]);
}
calculateNeed();
isSafe();
return 0;
}
Output:

ADITYA UNIVERSITY (Formerly Aditya Engineering College (A)) ROLL NO: 23A91A0567
Exp No:
Date: Page No:

Week-8
Program:
Paging:
#include <stdio.h>
#include <stdlib.h>
#define PAGE_SIZE 4
#define TOTAL_PAGES 8
void simulatePaging(int logicalAddress) {
int pageNumber = logicalAddress / PAGE_SIZE;
int offset = logicalAddress % PAGE_SIZE;
if (pageNumber >= TOTAL_PAGES) {
printf("Invalid logical address! Out of memory bounds.\n");
return; }
printf("Logical Address: %d\n", logicalAddress);
printf("Page Number: %d\n", pageNumber);
printf("Offset: %d\n", offset);
printf("Physical Address: [Page %d][Offset %d]\n", pageNumber, offset) }
int main() {
int logicalAddress;
printf("Enter a logical address: ");
scanf("%d", &logicalAddress);
simulatePaging(logicalAddress);
return 0;}
Output:

ADITYA UNIVERSITY (Formerly Aditya Engineering College (A)) ROLL NO: 23A91A0567
Exp No:
Date: Page No:

Segmentation:
#include <stdio.h>
#define TOTAL_SEGMENTS 3
typedef struct {
int base;
int limit;
} Segment;
void simulateSegmentation(Segment segments[], int segmentId, int offset) {
if (segmentId >= TOTAL_SEGMENTS || offset >= segments[segmentId].limit) {
printf("Invalid segment ID or offset! Out of bounds.\n");
return;
}
int physicalAddress = segments[segmentId].base + offset;
printf("Segment ID: %d\n", segmentId);
printf("Offset: %d\n", offset);
printf("Physical Address: %d\n", physicalAddress);
}
int main() {
Segment segments[TOTAL_SEGMENTS] = {
{0, 100},
{100, 200},
{300, 150}
};

int segmentId, offset;


printf("Enter segment ID (0-2): ");
scanf("%d", &segmentId);
printf("Enter offset: ");
scanf("%d", &offset);
simulateSegmentation(segments, segmentId, offset);

ADITYA UNIVERSITY (Formerly Aditya Engineering College (A)) ROLL NO: 23A91A0567
Exp No:
Date: Page No:

return 0;
}
Output:

ADITYA UNIVERSITY (Formerly Aditya Engineering College (A)) ROLL NO: 23A91A0567
Exp No:
Date: Page No:

Week-10
Program:
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>
#include <unistd.h>
#define SHM_SIZE 1024
int main() {
key_t key = ftok("shmfile", 65);
int shmid = shmget(key, SHM_SIZE, 0666 | IPC_CREAT);
char *str = (char *)shmat(shmid, NULL, 0);
if (fork() == 0) {
printf("Child Process: Writing to shared memory: Hello, Shared Memory!\n");
strcpy(str, "Hello, Shared Memory!");
shmdt(str);
} else {
sleep(1);
printf("Parent Process: Reading from shared memory: %s\n", str);
shmdt(str);
shmctl(shmid, IPC_RMID, NULL); }
return 0; }
Output:

ADITYA UNIVERSITY (Formerly Aditya Engineering College (A)) ROLL NO: 23A91A0567
Exp No:
Date: Page No:

Week-11
Program:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *threadFunction(void *arg) {
printf("Thread is running!\n");
return NULL;
}
int main() {
pthread_t thread;
if (pthread_create(&thread, NULL, threadFunction, NULL) != 0) {
printf("Error creating thread\n");
return 1;
}
pthread_join(thread, NULL);
return 0;
}
Output:

ADITYA UNIVERSITY (Formerly Aditya Engineering College (A)) ROLL NO: 23A91A0567
Exp No:
Date: Page No:

Week-12
Program:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
void *threadFunction(void *arg) {
int id = *(int *)arg;
printf("Thread %d is running!\n", id);
sleep(1);
printf("Thread %d has finished execution!\n", id);
return NULL;
}
int main() {
pthread_t threads[3];
int thread_ids[3] = {1, 2, 3};
for (int i = 0; i < 3; i++) {
if (pthread_create(&threads[i], NULL, threadFunction, &thread_ids[i]) != 0) {
printf("Error creating thread %d\n", i + 1);
return 1;
}
}

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


pthread_join(threads[i], NULL);
}
return 0;
}

ADITYA UNIVERSITY (Formerly Aditya Engineering College (A)) ROLL NO: 23A91A0567
Exp No:
Date: Page No:

Output:

ADITYA UNIVERSITY (Formerly Aditya Engineering College (A)) ROLL NO: 23A91A0567

You might also like