You are on page 1of 23

LAB-FAT

NAME: SHUBH KAPIL


(20BCI0265)

SUBJECT: OPERATING SYSTEM

COURSE CODE: CSE2005


1. Write a program to create a thread to find the
factorial of a natural number ‘n’.

CODE:

// C CODE TO FIND FACTORIAL OF A NUMBER USING THREADS


// CODE BY SHUBH KAPIL

#include<stdio.h>
#include<pthread.h>
#include<tgmath.h>
void *factorial(void *p); //Defining the function
int fact(int n);

int main(){ //Driver Code


pthread_t tid1;
pthread_t tid2;
pthread_attr_t attr; // set of thread attributes

//get the default attributes


pthread_attr_init(&attr);
pthread_create(&tid1,&attr,factorial,NULL);
pthread_join(tid1,NULL);
}
int fact(int n){ //Defining fact function which was declared above
if(n==0 || n==1)
return 1;
else
return n*fact(n-1);
}

void *factorial(void *p){


int i,num1;
printf("Thread 1 (factorial) : ");
printf("Enter Number: ");
scanf("%d",&num1);
printf("Factorial is: %d\n",fact(num1));
pthread_exit(0);

}
OUTPUT:
2. Write a multithreaded program that calculates
various statistical values for a list of numbers. This
program will be passed a series of numbers on the
command line and will then create three separate
worker threads. One thread will determine the
average of the numbers, the second will determine
the maximum value, and the third will determine
the minimum value. For example, suppose your
program is passed the integers 90 81 78 95 79 72 85 ,
the program will report the average value as 82.
The minimum value as 72. The maximum value as
95. The variables representing the average,
minimum, and maximum values will be stored
globally. The worker threads will set these values,
and the parent thread will output the values once
the workers have exited.

CODE:

#include <pthread.h>
#include <stdio.h>

int avg=0; //Declaring Global variables


int minValue=0, maxValue=0; //Declaring Global variables
int len = 7; //Set length of array to 7 as the given input is of
length 7 [ 90 81 78 95 79 72 85 ]

void *calcAvg(void *arg) /* Function to calculates average of the


values in the given array */
{
int i = 0, sum = 0;
int *values;

values = (int*)arg; //Fetching array

for(i=0; i<len; i++) //Iterating over array


{

sum = sum + values[i]; //Accumulating sum


}

avg = sum / (double)len; //Calculating average

return NULL;
}
void *calcMin(void *arg) /* Function that calculates minimum value in
from the given array */
{
int i = 0;
int *values;

values = (int*)arg; //Fetching array

minValue = values[0]; //Initializing minimum value as starting element

for(i=0; i<len; i++) //Iterating over array


{

if(values[i] < minValue) //Comparing value


{

minValue = values[i]; //Updating minimum value


}
}

return NULL;
}
void *calcMax(void *arg) /* Function that calculate minimum value in
from the given array */
{
int i = 0;
int *values;

values = (int*)arg; //Fetching array

maxValue = values[0]; //Initializing maximum value as starting


element

for(i=0; i<len; i++) //Iterating over array


{

if(values[i] > maxValue) //Comparing value


{

maxValue = values[i]; //Updating maximum value


}
}

return NULL;
}
int main(void) /* Main function */
{
pthread_t avgPth, minPth, maxPth; //Thread identifiers
int i = 0;

int values[] = {90, 81, 78, 95, 79, 72, 85}; //Array declaration

pthread_create(&avgPth,NULL,calcAvg, values); //Creating three


threads one for each operation
pthread_create(&minPth,NULL,calcMin, values);
pthread_create(&maxPth,NULL,calcMax, values);

pthread_join(avgPth,NULL); //Waiting till all threads finish their work


pthread_join(minPth,NULL);
pthread_join(maxPth,NULL);

printf("\n Average : %d \n", avg); //Printing results


printf("\n Minimum : %d \n", minValue);
printf("\n Maximum : %d \n", maxValue);

return 0;
}
OUTPUT:
3. Write a program to implement the page
replacement algorithms.
i. FIFO ii. LRU iii. OPT

i. FIFO:

CODE:

// C++ implementation of FIFO page replacement


// CODE BY SHUBH KAPIL (20BCI0265)

#include<bits/stdc++.h>
using namespace std;

int pageFaults(int pages[], int n, int capacity) // Function to find page


faults using FIFO
{
// To represent set of current pages. We use
// an unordered_set so that we quickly check
// if a page is present in set or not
unordered_set<int> s;

queue<int> indexes; // To store the pages in FIFO manner


int page_faults = 0; // Start from initial page
for (int i=0; i<n; i++)
{

if (s.size() < capacity) // Check if the set can hold more pages
{
// Insert it into set if not present
// already which represents page fault

if (s.find(pages[i])==s.end())
{

s.insert(pages[i]); // Insert the current page into the set

page_faults++; // increment page fault

indexes.push(pages[i]); // Push the current page into the


queue
}
}

// If the set is full then need to perform FIFO


// i.e. remove the first page of the queue from
// set and queue both and insert the current page
else
{
// Check if current page is not already
// present in the set

if (s.find(pages[i]) == s.end())
{
// Store the first page in the
// queue to be used to find and
// erase the page from the set
int val = indexes.front();

indexes.pop(); // Pop the first page from the queue

s.erase(val); // Remove the indexes page from the set

s.insert(pages[i]); // insert the current page in the set

indexes.push(pages[i]); // push the current page into


// the queue
page_faults++; // Increment page faults
}
}
}

return page_faults;
}

int main()
{
int pages[] = {2,1,4,5,2,11,3,5,10,1,3,7,2,4,5,21,3,1,3,5,7,9,4,2};
int n = sizeof(pages)/sizeof(pages[0]);
int capacity = 4;
cout << pageFaults(pages, n, capacity);
return 0;
}
OUTPUT:
ii. LRU:

CODE:

//C++ CODE FOR IMPLIMENTING lRU


// CODE BY SHUBH KAPIL
#include<bits/stdc++.h>
using namespace std;

// Function to find page faults using indexes


int pageFaults(int pages[], int n, int capacity)
{
// To represent set of current pages. We use
// an unordered_set so that we quickly check
// if a page is present in set or not
unordered_set<int> s;

// To store least recently used indexes


// of pages.
unordered_map<int, int> indexes;

// Start from initial page


int page_faults = 0;
for (int i=0; i<n; i++)
{
// Check if the set can hold more pages
if (s.size() < capacity)
{
// Insert it into set if not present
// already which represents page fault
if (s.find(pages[i])==s.end())
{
s.insert(pages[i]);

// increment page fault


page_faults++;
}

// Store the recently used index of


// each page
indexes[pages[i]] = i;
}

// If the set is full then need to perform lru


// i.e. remove the least recently used page
// and insert the current page
else
{
// Check if current page is not already
// present in the set
if (s.find(pages[i]) == s.end())
{
// Find the least recently used pages
// that is present in the set
int lru = INT_MAX, val;
for (auto it=s.begin(); it!=s.end(); it++)
{
if (indexes[*it] < lru)
{
lru = indexes[*it];
val = *it;
}
}

// Remove the indexes page


s.erase(val);

// insert the current page


s.insert(pages[i]);

// Increment page faults


page_faults++;
}

// Update the current page index


indexes[pages[i]] = i;
}
}

return page_faults;
}

// Driver code
int main()
{
int pages[] = {2,1,4,5,2,11,3,5,10,1,3,7,2,4,5,21,3,1,3,5,7,9,4,2};
int n = sizeof(pages)/sizeof(pages[0]);
int capacity = 4;
cout << pageFaults(pages, n, capacity);
return 0;
}

OUTPUT:
iii. OPTIMAL:

CODE:

// C++ CODE FRO IMPLIMENTING optimal page REPLACEMENT


ALGORITHM
// CODE BY SHUBH KAPIL

#include <bits/stdc++.h>
using namespace std;

// Function to check whether a page exists


// in a frame or not
bool search(int key, vector<int>& fr)
{
for (int i = 0; i < fr.size(); i++)
if (fr[i] == key)
return true;
return false;
}

// Function to find the frame that will not be used


// recently in future after given index in pg[0..pn-1]
int predict(int pg[], vector<int>& fr, int pn, int index)
{
// Store the index of pages which are going
// to be used recently in future
int res = -1, farthest = index;
for (int i = 0; i < fr.size(); i++) {
int j;
for (j = index; j < pn; j++) {
if (fr[i] == pg[j]) {
if (j > farthest) {
farthest = j;
res = i;
}
break;
}
}

// If a page is never referenced in future,


// return it.
if (j == pn)
return i;
}

// If all of the frames were not in future,


// return any of them, we return 0. Otherwise
// we return res.
return (res == -1) ? 0 : res;
}
void optimalPage(int pg[], int pn, int fn)
{
// Create an array for given number of
// frames and initialize it as empty.
vector<int> fr;

// Traverse through page reference array


// and check for miss and hit.
int hit = 0;
for (int i = 0; i < pn; i++) {

// Page found in a frame : HIT


if (search(pg[i], fr)) {
hit++;
continue;
}

// Page not found in a frame : MISS

// If there is space available in frames.


if (fr.size() < fn)
fr.push_back(pg[i]);

// Find the page to be replaced.


else {
int j = predict(pg, fr, pn, i + 1);
fr[j] = pg[i];
}
}
cout << "No. of hits = " << hit << endl;
cout << "No. of misses = " << pn - hit << endl;
}

// Driver Function
int main()
{
int pg[] = {2,1,4,5,2,11,3,5,10,1,3,7,2,4,5,21,3,1,3,5,7,9,4,2};
int pn = sizeof(pg) / sizeof(pg[0]);
int fn = 4;
optimalPage(pg, pn, fn);
return 0;
}

OUTPUT:

You might also like