Os 67
Os 67
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:
ADITYA UNIVERSITY (Formerly Aditya Engineering College (A)) ROLL NO: 23A91A0567
Exp No:
Date: Page No:
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}
};
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;
}
}
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