Professional Documents
Culture Documents
CODE:
#include<stdio.h>
#include<pthread.h>
#include<tgmath.h>
void *factorial(void *p); //Defining the function
int fact(int n);
}
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>
return NULL;
}
void *calcMin(void *arg) /* Function that calculates minimum value in
from the given array */
{
int i = 0;
int *values;
return NULL;
}
void *calcMax(void *arg) /* Function that calculate minimum value in
from the given array */
{
int i = 0;
int *values;
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
return 0;
}
OUTPUT:
3. Write a program to implement the page
replacement algorithms.
i. FIFO ii. LRU iii. OPT
i. FIFO:
CODE:
#include<bits/stdc++.h>
using namespace std;
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())
{
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();
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:
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:
#include <bits/stdc++.h>
using namespace std;
// 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: