OS MANUAL
OS MANUAL
EXPERIMENT NO 1B
PROGRAM 1
OUTPUT:
OUTPUT:
3) Display current logged user and login name.
OUTPUT:
OUTPUT:
5) Display os name,release number,kernel version.
OUTPUT:
EXPERIMENT NO.3
PRIORITY:
PROGRAM:-
#include <stdio.h>
int main() {
int pn = 0;
int CPU = 0;
int allTime = 0;
printf("Enter Processes Count: ");
scanf("%d", &pn);
int AT[pn];
int ATt[pn];
int NoP = pn;
int PT[pn];
int PP[pn];
int waitingTime[pn];
int turnaroundTime[pn];
for (int i = 0; i < pn; i++) {
printf("\nProcessing time for P%d: ", i + 1);
scanf("%d", &PT[i]);
printf("Priority for P%d: ", i + 1);
scanf("%d", &PP[i]);
printf("Arrival Time for P%d: ", i + 1);
scanf("%d", &AT[i]);
ATt[i] = AT[i];
}
int LAT = 0;
for (int i = 0; i < pn; i++) {
if (AT[i] > LAT) {
LAT = AT[i];
}
}
int ATv = AT[0];
int ATi = 0;
int P1 = PP[0];
int P2 = PP[0];
while (NoP > 0 && CPU <= 1000) {
for (int i = 0; i < pn; i++) {
if (ATt[i] < ATv) {
ATi = i;
ATv = ATt[i];
P1 = PP[i];
P2 = PP[i];
}
else if (ATt[i] == ATv || ATt[i] <= CPU) {
if (PP[i] != (pn + 1)) {
P2 = PP[i];
if (P2 < P1) {
ATi = i;
ATv = ATt[i];
P1 = PP[i];
P2 = PP[i];
}
}
}
}
if (CPU < ATv) {
CPU = CPU + 1;
continue;
} else {
waitingTime[ATi] = CPU - ATt[ATi];
CPU = CPU + PT[ATi];
turnaroundTime[ATi] = CPU - ATt[ATi];
ATt[ATi] = LAT + 10;
ATv = LAT + 10;
ATi = 0;
PP[ATi] = pn + 1;
P1 = PP[0];
P2 = PP[0];
NoP = NoP - 1;
}
}
printf("\nPN\tPT\tPP\tWT\tTT\n\n");
for (int i = 0; i < pn; i++) {
printf("P%d\t%d\t%d\t%d\t%d\n", i + 1, PT[i], PP[i], waitingTime[i], turnaroundTime[i]);
}
int AvgWT = 0;
int AVGTaT = 0;
for (int i = 0; i < pn; i++) {
AvgWT = waitingTime[i] + AvgWT;
AVGTaT = turnaroundTime[i] + AVGTaT;
}
printf("AvgWaitingTime: %d\nAvgTurnaroundTime: %d\n", AvgWT / pn, AVGTaT / pn);
return 0;
}
OUTPUT:-
EXPERIMENT NO 4.
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main(void) {
pid_t pid = fork();
if(pid == 0) {
printf("Child => PPID: %d PID: %d\n", getppid(), getpid());
exit(EXIT_SUCCESS);
}
else if(pid > 0) {
printf("Parent => PID: %d\n", getpid());
printf("Waiting for child process to finish.\n");
wait(NULL);
printf("Child process finished.\n");
}
else {
printf("Unable to create child process.\n");
}
return EXIT_SUCCESS;
}
OUTPUT:
EXPERIMENT NO 5
PROGRAM
1)PRIORITY:
#include <stdio.h>
//Function to swap two variables
void swap(int *a,int *b)
{
int temp=*a;
*a=*b;
*b=temp;
}
int main()
{
int n;
printf("Enter Number of Processes: ");
scanf("%d",&n);
// b is array for burst time, p for priority and index for process id
int b[n],p[n],index[n];
for(int i=0;i<n;i++)
{
printf("Enter Burst Time and Priority Value for Process %d: ",i+1);
scanf("%d %d",&b[i],&p[i]);
index[i]=i+1;
}
for(int i=0;i<n;i++)
{
int a=p[i],m=i;
//Finding out highest priority element and placing it at its desired position
for(int j=i;j<n;j++)
{
if(p[j] > a)
{
a=p[j];
m=j;
}
}
//Swapping processes
swap(&p[i], &p[m]);
swap(&b[i], &b[m]);
swap(&index[i],&index[m]);
}
// T stores the starting time of process
int t=0;
//Printing scheduled process
printf("Order of process Execution is\n");
for(int i=0;i<n;i++)
{
printf("P%d is executed from %d to %d\n",index[i],t,t+b[i]);
t+=b[i];
}
printf("\n");
printf("Process Id Burst Time Wait Time TurnAround Time\n");
int wait_time=0;
for(int i=0;i<n;i++)
{
printf("P%d %d %d %d\n",index[i],b[i],wait_time,wait_time + b[i]);
wait_time += b[i];
}
return 0;
}
OUTPUT:
2)ROUND ROBBIN:
#include <stdio.h>
int main() {
// Input number of processes
int n;
printf("Enter Total Number of Processes: ");
scanf("%d", &n);
int wait_time = 0, ta_time = 0, arr_time[n], burst_time[n], temp_burst_time[n];
int x = n;
// Input details of processes
for (int i = 0; i < n; i++) {
printf("Enter Details of Process %d \n", i + 1);
printf("Arrival Time: ");
scanf("%d", &arr_time[i]);
printf("Burst Time: ");
scanf("%d", &burst_time[i]);
temp_burst_time[i] = burst_time[i];
}
// Input time slot
int time_slot;
printf("Enter Time Slot: ");
scanf("%d", &time_slot);
// Total indicates total time
// counter indicates which process is executed
int total = 0, i = 0;
printf("Process ID Burst Time Turnaround Time Waiting Time\n");
// Round Robin Scheduling
for (total = 0; x != 0;) {
if (temp_burst_time[i] > 0) {
// Process execution in the given time slot
if (temp_burst_time[i] <= time_slot) {
total += temp_burst_time[i]; // Add remaining burst time to total
temp_burst_time[i] = 0; // Mark process as completed
x--; // Decrement remaining processes
printf("\nProcess No %d \t\t %d\t\t\t\t %d\t\t\t %d", i + 1, burst_time[i],
total - arr_time[i], total - arr_time[i] - burst_time[i]);
wait_time += total - arr_time[i] - burst_time[i]; // Waiting time calculation
ta_time += total - arr_time[i]; // Turnaround time calculation
} else {
total += time_slot; // Process is executed for time_slot
temp_burst_time[i] -= time_slot;
}
}
// Move to next process, if needed
if (i == n - 1) {
i = 0;
} else if (arr_time[i + 1] <= total) {
i++;
} else {
i = 0; }
}
// Calculate averages
float average_wait_time = (float)wait_time / n;
float average_turnaround_time = (float)ta_time / n;
printf("\nAverage Waiting Time: %f", average_wait_time);
printf("\nAvg Turnaround Time: %f", average_turnaround_time);
return 0;
}
OUTPUT:
3)SJF
#include<stdio.h>
int main()
{
int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,totalT=0,pos,temp;
float avg_wt,avg_tat;
printf("Enter number of process:");
scanf("%d",&n);
printf("\nEnter Burst Time:\n");
for(i=0;i<n;i++)
{
printf("p%d:",i+1);
scanf("%d",&bt[i]);
p[i]=i+1;
}
//sorting of burst times
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(bt[j]<bt[pos])
pos=j;
}
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0;
//finding the waiting time of all the processes
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
//individual WT by adding BT of all previous completed processes
wt[i]+=bt[j];
//total waiting time
total+=wt[i];
}
//average waiting time
avg_wt=(float)total/n;
printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");
for(i=0;i<n;i++)
{
//turnaround time of individual processes tat[i]=bt[i]+wt[i];
EXPERIMENT NO 6
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
int mutex = 1;
int full = 0;
int empty = 10, data = 0;
void producer()
{
--mutex;
++full;
--empty;
data++;
printf("\nProducer produces item number: %d\n", data);
++mutex;
}
void consumer()
{
--mutex;
--full;
++empty;
printf("\nConsumer consumes item number: %d.\n", data);
data--;
++mutex;
}
int main()
{
int n, i;
printf("\n1. Enter 1 for Producer"
"\n2. Enter 2 for Consumer"
"\n3. Enter 3 to Exit");
for (i = 1; i > 0; i++)
{
printf("\nEnter your choice: ");
scanf("%d", &n);
switch (n)
{
case 1:
if ((mutex == 1) && (empty != 0))
{
producer();
}
else
{
printf("The Buffer is full. New data cannot be produced!");
}
break;
case 2:
if ((mutex == 1) && (full != 0))
{
consumer();
}
else
{
printf("The Buffer is empty! New data cannot be consumed!");
}
break;
case 3:
exit(0);
break;
}
}
}
OUTPUT:
EXPERIMENT NO 7
PROGRAM:
#include <stdio.h>
int main() {
/* array will store at most 5 processes with 3 resources
if your process or resources are greater than 5 and 3, then increase the size of the array
*/
int p, c, count = 0, i, j, alc[5][3], max[5][3], need[5][3], safe[5], available[3], done[5],
terminate = 0;
printf("Enter the number of processes and resources: ");
scanf("%d %d", &p, &c);
// Validate input size
if (p > 5 || c > 3) {
printf("Error: Maximum processes = 5, Maximum resources = 3. Adjust array size
accordingly.\n");
return 1;
}
// Input allocation matrix
printf("Enter allocation of resources for all processes (%dx%d matrix):\n", p, c);
for (i = 0; i < p; i++) {
for (j = 0; j < c; j++) {
scanf("%d", &alc[i][j]);
}
}
// Input max resource requirement matrix
printf("Enter the max resources required for all processes (%dx%d matrix):\n", p, c);
for (i = 0; i < p; i++) {
for (j = 0; j < c; j++) {
scanf("%d", &max[i][j]);
}
}
// Input available resources
printf("Enter the available resources (%d values):\n", c);
for (i = 0; i < c; i++) {
scanf("%d", &available[i]);
}
// Calculate the need matrix
printf("\nNeed resources matrix:\n");
for (i = 0; i < p; i++) {
for (j = 0; j < c; j++) {
need[i][j] = max[i][j] - alc[i][j];
printf("%d\t", need[i][j]);
}
printf("\n");
}
// Initialize done array
for (i = 0; i < p; i++) {
done[i] = 0;
}
return 0;
}
OUTPUT:
EXPERIMENT NO 8
PROGRAM:
1)MFT:
#include <stdio.h>
int main() {
int ms, bs, nob, ef, n, mp[10], tif = 0;
int i, p = 0;
// Input memory size and block size
printf("Enter the total memory available (in Bytes): ");
scanf("%d", &ms);
printf("Enter the block size (in Bytes): ");
scanf("%d", &bs);
2)MVT:
#include <stdio.h>
int main() {
int ms, mp[10], i, temp, n = 0;
char ch = 'y';
// Input total memory available
printf("\nEnter the total memory available (in Bytes): ");
scanf("%d", &ms);
temp = ms;
for (i = 0; ch == 'y' || ch == 'Y'; i++, n++) {
printf("\nEnter memory required for process %d (in Bytes): ", i + 1);
scanf("%d", &mp[i]);
PROGRAM:
FIFO
#include<stdio.h>
int main()
{
int pageFaults = 0;
int frames;
int m, n, s, pages;
printf("\n Enter frame size: ");
scanf("%d",&frames);
printf("\n Enter the no of pages: ");
scanf("%d",&pages);
int incomingStream[pages];
printf("\n Enter the String: ");
for(m=0;m<pages;m++){
scanf("%d",&incomingStream[m]);
}
printf("Incoming \t frame1 \t frame2 \t frame3");
int temp[frames];
for(m=0;m<frames;m++){
temp[m] = -1;
}
for(m=0;m<pages;m++){
s = 0;
for(n=0;n<frames;n++){
if(incomingStream[m] == temp[n]){
s++;
pageFaults--;
}
}
pageFaults++;
if((pageFaults<=frames) && (s == 0)){
temp[m] = incomingStream[m];
}
else if(s == 0){
temp[(pageFaults-1)%frames] = incomingStream[m];
}
printf("\n");
printf("%d\t\t\t",incomingStream[m]);
for(n=0;n<frames;n++){
if(temp[n]!=-1){
printf(" %d\t\t\t",temp[n]);
}
else{
printf(" - \t\t\t");
}
}
}
printf("\n Total page Faults: \t%d\n",pageFaults);
printf("\n Total page Hits: \t%d\n",pages-pageFaults);
return 0;
}
OUTPUT:
LRU
#include<stdio.h>
#include<limits.h>
int checkHit(int incomingPage, int queue[], int occupied) {
for (int i = 0; i < occupied; i++) {
if (incomingPage == queue[i])
return 1;
}
return 0;
}
void printFrame(int queue[], int occupied) {
for (int i = 0; i < occupied; i++)
printf("%d\t\t\t", queue[i]);
}
int main() {
int n, frames, pagefault = 0, pageHits = 0;
printf("Enter number of frames: ");
scanf("%d", &frames);
printf("Enter the number of pages in the reference string: ");
scanf("%d", &n);
int incomingStream[n];
int queue[frames];
int distance[n];
int occupied = 0;
printf("Enter the page reference string (space-separated): ");
for (int i = 0; i < n; i++) {
scanf("%d", &incomingStream[i]);
}
printf("page\t Frame1 \t Frame 2\t Frame3\n");
for (int i = 0; i < n; i++) {
printf("%d: \t\t", incomingStream[i]);
if (checkHit(incomingStream[i], queue, occupied)) {
pageHits++;
printFrame(queue, occupied);
}
else if (occupied < frames) {
queue[occupied] = incomingStream[i];
pagefault++;
occupied++;
printFrame(queue, occupied);
}
else {
int max = INT_MIN;
int index;
for (int j = 0; j < frames; j++) {
distance[j] = 0;
for (int k = i - 1; k >= 0; k--) {
++distance[j];
if (queue[j] == incomingStream[k])
break;
}
if (distance[j] > max) {
max = distance[j];
index = j;
}
}
queue[index] = incomingStream[i];
printFrame(queue, occupied);
pagefault++;
}
printf("\n");
}