Cs3461 Lab Manual
Cs3461 Lab Manual
Register No :
1
S.NO DATE TITLE PAGE MARK SIGN
1
10
11
12
2
13
14
15
3
PROGRAM FOR SYSTEM CALLS OF UNIX
Ex.No:1
OPERATING SYSTEM(fork, getpid, exit)
Aim:
To program for system calls of unix operating system(fork, getpid, exit)
ALGORITHM:
STEP 1: Start the program.
STEP 2: Declare the variables pid,pid1,pid2.
STEP 3: Call fork() system call to create process.
STEP 4: If pid==-1, exit.
STEP 5: Ifpid!=-1 , get the process id using getpid().
STEP 6: Print the process id.
STEP 7:Stop the program
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main() {
pid_t pid;
return 0;
}
4
Output:
Result:Thus program for system calls of unix operating system(fork, getpid, exit) executed
successfully.
5
CPU SCHEDULING ALGORITHMS
Ex.No:2
PRIORITY
AIM:
To write a C program for implementation of Priority scheduling algorithms.
ALGORITHM:
Step 1: Inside the structure declare the variables.
Step 2: Declare the variable i,j as integer, totwtime and totttime is equal to zero.
Step 3: Get the value of „n‟ assign p and allocate the memory.
Step 4: Inside the for loop get the value of burst time and priority.
Step 5: Assign wtime as zero .
Step 6: Check p[i].pri is greater than p[j].pri .
Step 7: Calculate the total of burst time and waiting time and assign as turnaround time.
Step 8: Stop the program.
PROGRAM:
#include <stdio.h>
struct Process {
int id, arrival, burst;
};
int main() {
struct Process p[] = {{1, 0, 5}, {2, 2, 3}, {3, 5, 8}, {4, 9, 2}};
int t[sizeof(p)/sizeof(p[0])]; // Array to store turnaround times
return 0;
}
6
OUTPUT:
RESULT: Thus write a C program for implementation of Priority scheduling algorithms executed
successfully..
7
Ex.No:3 Program to use IPC strategy
AIM:
To write a c program to implement IPC strategy
ALGORITHM:
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h>
int main() {
key_t key = ftok("shared_memory_key", 65);
int shmid = shmget(key, SHM_SIZE, 0666 | IPC_CREAT);
return 0;
}
8
OUTPUT:
9
IMPLEMENT MUTUAL EXCLUSION BY SEMAPHORE
Ex.No:4 USING C
AIM:
To implement mutual exclusion by semaphore using c
ALGORITHIM:
Step 1 :Initialize Variables
Step 2 :Include Thread Function (critical_section)
Step 3 :The threads compete for access to the critical section
Step 4 :Inside the critical section, the shared resource is updated in a mutually exclusive manner
Step 5 :The program exits gracefully after completing the execution of all threads
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#define NUM_THREADS 2
int shared_resource = 0;
sem_t mutex;
int main() {
pthread_t threads[NUM_THREADS];
int thread_ids[NUM_THREADS] = {0, 1};
sem_init(&mutex, 0, 1);
sem_destroy(&mutex);
return 0;
}
10
OUTPUT:
11
Ex.No:5 BANKERS ALGORITHM FOR DEADLOCK AVOIDANCE
AIM:
To write a C program to implement banker‟s algorithm for deadlock avoidance
ALGORITHM:
PROGRAM:
#include <stdio.h>
#define P 5
#define R 5
int main() {
int a[P][R] = {{0, 1, 0}, {2, 0, 0}, {3, 0, 2}, {2, 1, 1}, {0, 0, 2}};
int m[P][R] = {{7, 5, 3}, {3, 2, 2}, {9, 0, 2}, {2, 2, 2}, {4, 3, 3}};
12
printf("%s state, %s deadlock detected.\n", issafe(NULL, a, m, a) ? "safe" : "Unsafe", issafe(NULL, a, m, a) ? "no" :
"deadlock");
return 0;
}
OUTPUT:
RESULT: Thus write a C program to implement banker‟s algorithm for deadlock avoidance executed
successfully.
13
Ex.No:6 ALGORITHM FOR DEADLOCK DETECTION
AIM:
To write a C program to implement algorithm for deadlock detection.
ALGORITHM:
PROGRAM:
#include <stdio.h>
int main() {
int av[R] = {3, 3, 2}, max[P][R] = {{7, 5, 3}, {3, 2, 2}, {9, 0, 2}, {2, 2, 2}, {4, 3, 3}},
alloc[P][R] = {{0, 1, 0}, {2, 0, 0}, {3, 0, 2}, {2, 1, 1}, {0, 0, 2}}, need[P][R];
for (int i = 0; i < P; i++) for (int j = 0; j < R; j++) need[i][j] = max[i][j] - alloc[i][j];
14
isSafe(av, max, alloc, need);
return 0;
}
OUTPUT:
RESULT:Thus write a C program to implement algorithm for deadlock detection executed successfully.
15
Ex.No:7 WRITE A C PROGRAM TO IMPLEMENT THREADING
AIM:
To write a c program to implement Threading.
ALGORITHM:
PROGRAM:
#include <stdio.h>
#include <pthread.h>
int main() {
pthread_t tid[2];
char *messages[] = {"Thread 1 says: Hello from thread 1!", "Thread 2 says: Hello from thread 2!"};
16
OUTPUT:
17
Ex.No:8 PAGING TECHNIQUE OF MEMORY MANAGEMENT
AIM:
To write a c program to implement Paging technique for memory management.
ALGORITHM:
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#define P printf
#define N 10
#define F 4
int main() {
int T[N], Q[F], R = 0;
init(T);
srand(time(NULL));
for (int i = 0; i < 15; i++) ref(T, rand() % N, Q, &R);
return 0;
}
18
OUTPUT:
RESULT:Thus write a c program to implement Paging technique for memory management executed successfully.
19
MEMORY ALLOCATION METHODS FOR FIXED PARTITION
Ex.No:9
FIRST FIT
AIM:
To write a C program for implementation memory allocation methods for fixed partition
using first fit.
ALGORITHM:
PROGRAM:
#include <stdio.h>
#include <stdbool.h>
int main() {
bool mem[MEM_SIZE];
int procSize[PROCESSES] = {10, 20, 30, 40, 50};
initMemory(mem, MEM_SIZE), firstFit(mem, MEM_SIZE, procSize, PROCESSES);
return 0;
}
OUTPUT:
RESULT:Thus write a C program for implementation memory allocation methods for fixed partitionusing
first fit executed successfully.
21
MEMORY ALLOCATION METHODS FOR FIXED PARTITION
Ex.No:10
BEST FIT
AIM:
To write a C program for implementation of FCFS and SJF scheduling algorithms.
ALGORITHM:
PROGRAM:
#include <stdio.h>
#include <stdbool.h>
int main() {
22
bool mem[MEM_SIZE];
int procSize[PROCESSES] = {10, 20, 30, 40, 50};
initMemory(mem, MEM_SIZE), displayMemory(mem, MEM_SIZE), bestFit(mem, MEM_SIZE, procSize,
PROCESSES);
return 0;
}
OUTPUT:
RESULT:Thus write a C program for implementation of FCFS and SJF scheduling algorithms executed
successfully.
.
23
FILE ORGANIZATION TECHNIQUE
Ex.No:11
SINGLE LEVEL DIRECTORY
AIM:
To write C program to organize the file .
ALGORITHM:
PROGRAM:
#include <stdio.h>
#include <string.h>
#define MAX_FILES 10
#define MAX_FILENAME_LEN 20
struct File {
char filename[MAX_FILENAME_LEN];
int size;
};
struct Directory {
struct File files[MAX_FILES];
int file_count;
};
int main() {
struct Directory dir;
initDirectory(&dir);
return 0;
}
OUTPUT:
25
FILE ALLOCATION STRATEGIES
Ex.No:12
SEQUENTIAL
AIM:
To write a C program for sequential file for processing the student information.
ALGORITHM:
PROGRAM:
#include <stdio.h>
#define MAX_FILES 10
int main() {
int file_sizes[MAX_FILES] = {100, 200, 150, 300}; // File sizes
int disk_space = 0; // Current allocated disk space
for (int i = 0; i < MAX_FILES; i++) {
if (disk_space + file_sizes[i] <= 1000) { // Assuming total disk space is 1000
printf("File %d allocated at position %d\n", i + 1, disk_space);
disk_space += file_sizes[i]; // Increment disk space
} else {
printf("Disk space full. Cannot allocate File %d\n", i + 1);
break;
}
}
return 0;
}
OUTPUT:
26
RESULT:Thus write a C program for sequential file for processing the student information executed successfully.
27
PAGE REPLACE ALGORITHIM
Ex.No:13
FIFO
AIM:
ALGORITHM:
PROGRAM:
#include <stdio.h>
int main() {
int i, j, n, a, frame, no, k, avail, count = 0;
j = 0;
if (avail == 0) {
frame[j] = a[i];
j = (j + 1) % no;
count++;
}
return 0;
}
OUTPUT:
29
RESULT:Thus write a C program for page replace algorithim executed successfully.
30
MEMORY ALLOCATION METHODS FOR FIXED PARTITION
Ex.No:14
WORST FIT
AIM:
To write a C program for implementation of FCFS and SJF scheduling algorithms.
ALGORITHM:
PROGRAM:
#include <stdio.h>
block
for (int i = 0; i < processes; i++) {
int indexPlaced = -1;
for (int j = 0; j < blocks; j++) {
if (blockSize[j] >= processSize[i] &&
!occupied[j]) {
if (indexPlaced == -1) {
indexPlaced = j;
}
31
}
}
}
if (indexPlaced != -1) {
allocation[i] = indexPlaced;
occupied[indexPlaced] = 1;
blockSize[indexPlaced] -= processSize[i];
}
}
int main() {
int blockSize[] = {5, 4, 3, 6, 7};
int processSize[] = {3, 2, 4, 1, 5};
int processes = sizeof(processSize) /
sizeof(processSize[0]);
int blocks = sizeof(blockSize) /
sizeof(blockSize[0]);
return 0;
}
OUTPUT:
32
RESULT:Thus write a C program for worst fit has been executed successfully.
33
CPU SCHEDULING ALGORITHMS
Ex.No:15
FCFS
AIM:
To write a C program for implementation of FCFS and SJF scheduling algorithms.
ALGORITHM:
PROGRAM:
#include <stdio.h>
int main() {
int n, i, j, temp;
float avgWait, avgTurnaround;
wait[0] = 0;
for (i = 1; i < n; i++) {
wait[i] = wait[i - 1] + burst[i - 1];
}
avgWait = 0;
avgTurnaround = 0;
34
for (i = 0; i < n; i++) {
avgWait += wait[i];
avgTurnaround += turnaround[i];
}
avgWait /= n;
avgTurnaround /= n;
return 0;
}
OUTPUT:
RESULT:Thus write a C program for CPU scheduling on FCFS has been executed successfully.
35