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

OS Practicals 1-2

The document discusses programs to implement data structures and algorithms in C language. It includes programs to implement queue, stack, 2D array, pointers, and the FCFS CPU scheduling algorithm.
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)
23 views11 pages

OS Practicals 1-2

The document discusses programs to implement data structures and algorithms in C language. It includes programs to implement queue, stack, 2D array, pointers, and the FCFS CPU scheduling algorithm.
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/ 11

ACROPOLIS INSTITUTE OF TECHNOLOGY AND

RESEARCH

SESSION: -2023-24

OPERATING SYSTEM (CSIT-502)

SUBMITTED TO: SUBMITTED BY:


Prof. Garima Joshi Jaydeep Mandloi
CSIT-2
0827CI211092
INDEX
Sno Tittle Date of Date of
. Experiment Submission
PRACTICAL -I
1. 1. Write a program to implement queue in C
language.
2. Write a program to implement stack in C
language.
3. Write a program to implement 2-dimentional
array in C language.
4. Write a program to implement concept of
pointers in C language.
2. PRACTICAL-II

Program to implement FCFS CPU scheduling


algorithm.
PRACTICAL-I
Write a program to implement queue in C language .
#include <stdio.h>
#define MAX_SIZE 100

// Structure for Queue


struct Queue {
int items[MAX_SIZE];
int front;
int rear;
};

// Initialize the queue


void initializeQueue(struct Queue* q) {
q->front = -1;
q->rear = -1;
}

// Check if the queue is empty


int isEmpty(struct Queue* q) {
return (q->front == -1 && q->rear == -1);
}

// Check if the queue is full


int isFull(struct Queue* q) {
return (q->rear == MAX_SIZE - 1);
}

// Add an element to the queue


void enqueue(struct Queue* q, int value) {
if (isFull(q)) {
printf("Queue is full\n");
return;
}
if (isEmpty(q)) {
q->front = q->rear = 0;
} else {
q->rear++;
}
q->items[q->rear] = value;
}

// Remove an element from the queue


int dequeue(struct Queue* q) {
int removedItem;
if (isEmpty(q)) {
printf("Queue is empty\n");
return -1;
} else if (q->front == q->rear) {
removedItem = q->items[q->front];
q->front = q->rear = -1;
} else {
removedItem = q->items[q->front];
q->front++;
}
return removedItem;
}

// Get the front element of the queue


int front(struct Queue* q) {
if (isEmpty(q)) {
printf("Queue is empty\n");
return -1;
}
return q->items[q->front];
}

// Display the elements of the queue


void display(struct Queue* q) {
int i;
if (isEmpty(q)) {
printf("Queue is empty\n");
Output:
2. Write a program to implement stack in C language.

#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100

typedef struct {
int data[MAX_SIZE];
int top;
} Stack;

// Function to initialize the stack


void initialize(Stack *stack) {
stack->top = -1;
}

// Function to check if the stack is empty


int isEmpty(Stack *stack) {
return (stack->top == -1);
}

// Function to check if the stack is full


int isFull(Stack *stack) {
return (stack->top == MAX_SIZE - 1);
}

// Function to push an element onto the stack


void push(Stack *stack, int value) {
if (isFull(stack)) {
printf("Stack overflow\n");
return;
}

stack->data[++(stack->top)] = value;
}

// Function to pop an element from the stack


int pop(Stack *stack) {
if (isEmpty(stack)) {
printf("Stack underflow\n");
return -1; // Return a sentinel value for underflow
}

return stack->data[(stack->top)--];
}

// Function to peek at the top element of the stack without removing it


int peek(Stack *stack) {
if (isEmpty(stack)) {
printf("Stack is empty\n");
return -1; // Return a sentinel value for empty stack
}

return stack->data[stack->top];
}
int main() {
printf("NAME: JAYDEEP MANDLOI ENROLLMENT_NO.: 0827CI211092\n");

Stack stack;
initialize(&stack);

push(&stack, 10);
push(&stack, 20);
push(&stack, 30);

printf("Top element: %d\n", peek(&stack));

while (!isEmpty(&stack)) {
printf("%d ", pop(&stack));
}

return 0;
}

Output:
3. Write a program to implement 2-dimentional array in C
language.

#include <stdio.h>
#define ROWS 3
#define COLS 3

int main() {
printf("NAME: JAYDEEP MANDLOI ENROLLMENT_NO.: 0827CI211092\n");
int matrix[ROWS][COLS];

// Taking input from the user


printf("Enter %d elements for a %dx%d matrix:\n", ROWS * COLS,
ROWS, COLS);
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
scanf("%d", &matrix[i][j]);
}
}

// Printing the matrix


printf("\nMatrix:\n");
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}

return 0;
}

Output:
4. Write a program to implement concept of pointers in C
language.

#include <stdio.h>

int main() {

printf("NAME: JAYDEEP MANDLOI ENROLLMENT_NO.: 0827CI211092\


n");

int num = 10; // Declare an integer variable


int *ptr; // Declare a pointer to an integer

ptr = &num; // Assign the address of 'num' to the pointer

printf("Value of num: %d\n", num);


printf("Address of num: %p\n", &num);
printf("Value of ptr (address of num): %p\n", ptr);
printf("Value at address pointed by ptr: %d\n", *ptr);

return 0;
}

Output:
PRACTICAL-II
Program to implement FCFS CPU scheduling algorithm.

#include<stdio.h>
// Function to find the waiting time for all processes
void findWaitingTime(int processes[], int n,
int bt[], int wt[])
{
// waiting time for first process is 0
wt[0] = 0;

// calculating waiting time


for (int i = 1; i < n ; i++ )
wt[i] = bt[i-1] + wt[i-1] ;
}

// Function to calculate turn around time


void findTurnAroundTime( int processes[], int n,
int bt[], int wt[], int tat[])
{
// calculating turnaround time by adding bt[i] + wt[i]
for (int i = 0; i < n ; i++)
tat[i] = bt[i] + wt[i];
}

//Function to calculate average time


void findavgTime( int processes[], int n, int bt[])
{
int wt[n], tat[n], total_wt = 0, total_tat = 0;

//Function to find waiting time of all processes


findWaitingTime(processes, n, bt, wt);

//Function to find turn around time for all processes


findTurnAroundTime(processes, n, bt, wt, tat);

//Display processes along with all details


printf("Processes Burst time Waiting time Turn around time\
n");

// Calculate total waiting time and total turn around time


for (int i=0; i<n; i++)
{
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
printf(" %d ",(i+1));
printf(" %d ", bt[i] );
printf(" %d",wt[i] );
printf(" %d\n",tat[i] );
}
float s=(float)total_wt / (float)n;
float t=(float)total_tat / (float)n;
printf("Average waiting time = %f",s);
printf("\n");
printf("Average turn around time = %f ",t);
}

// Driver code
int main()
{
printf("NAME: JAYDEEP MANDLOI ENROLLMENT_NO.: 0827CI211092\n");

//process id's
int processes[] = { 1, 2, 3};
int n = sizeof processes / sizeof processes[0];

//Burst time of all processes


int burst_time[] = {10, 5, 8};

findavgTime(processes, n, burst_time);
return 0;
}

Output:

You might also like