0% found this document useful (0 votes)
12 views9 pages

Os Experiment From 3 To 5 B

The document outlines various CPU scheduling algorithms, including FCFS, Priority, SJF, and Round Robin, along with their implementation in C programming. It provides code examples for each algorithm, detailing how to calculate waiting time and turnaround time for processes. Additionally, it discusses process management commands such as creation, termination, and changing process priority.

Uploaded by

Shashank S
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)
12 views9 pages

Os Experiment From 3 To 5 B

The document outlines various CPU scheduling algorithms, including FCFS, Priority, SJF, and Round Robin, along with their implementation in C programming. It provides code examples for each algorithm, detailing how to calculate waiting time and turnaround time for processes. Additionally, it discusses process management commands such as creation, termination, and changing process priority.

Uploaded by

Shashank S
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/ 9

3. Process Management commands.

● Process creation, status, Identifying process, ps -f &its options,

● Running process in background, Job control, and Process termination.

● Changing process priority, scheduling process (Usage of sleep and wait commands)
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
pid_t pid, mypid, myppid;
pid = getpid();
printf("Before fork: Process id is %d\n", pid);
pid = fork();

if (pid < 0)
{
perror("fork() failure\n");
return 1;
}

// Child process
if (pid == 0)
{
printf("This is child process\n");
mypid = getpid();
myppid = getppid();
printf("Process id is %d and PPID is %d\n", mypid, myppid);
}
else
{ // Parent process
sleep(2);
printf("This is parent process\n");
mypid = getpid();
myppid = getppid();
printf("Process id is %d and PPID is %d\n", mypid, myppid);
printf("Newly created process id or child pid is %d\n", pid);
}
return 0;
}
Before fork: Process id is 166629
This is child process
Process id is 166630 and PPID is 166629
Before fork: Process id is 166629
This is parent process
Process id is 166629 and PPID is 166628
Newly created process id or child pid is 166630
4. Design, Develop and Implementation of CPU scheduling by using

a. FCFS

b. Priority
DESCRIPTION

FCFS CPU SCHEDULING ALGORITHM: For FCFS scheduling algorithm, read the
number of processes/jobs in the system, their CPU burst times. The scheduling is performed
on the basis of arrival time of the processes irrespective of their other parameters. Each
process will be executed according to its arrival time. Calculate the waiting time and
turnaround time of each of the processes accordingly.

Program:
FCFS Scheduling:
#include<stdio.h>
#include<conio.h>
main()
{
int bt[20], wt[20], tat[20], i, n;
float wtavg, tatavg;
printf("\nEnter the number of processes -- ");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("\nEnter Burst Time for Process %d -- ", i);
scanf("%d", &bt[i]);
}
wt[0] = wtavg = 0;
tat[0] = tatavg = bt[0];
for(i=1;i<n;i++)
{
wt[i] = wt[i-1] +bt[i-1];
tat[i] = tat[i-1] +bt[i];
wtavg = wtavg + wt[i];
tatavg = tatavg + tat[i];
}
printf("\t PROCESS \tBURST TIME \t WAITING TIME\t TURNAROUND TIME\n");
for(i=0;i<n;i++)
printf("\n\t P%d \t\t %d \t\t %d \t\t %d", i, bt[i], wt[i], tat[i]);
printf("\nAverage Waiting Time -- %f", wtavg/n);
printf("\nAverage Turnaround Time -- %f", tatavg/n);
}

Output:

PRIORITY CPU SCHEDULING ALGORITHM: For priority scheduling algorithm, read


the number of processes/jobs in the system, their CPU burst times, and the priorities. Arrange
all the jobs in order with respect to their priorities. There may be two jobs in queue with the
same priority, and then FCFS approach is to be performed. Each process will be executed
according to its priority. Calculate the waiting time and turnaround time of each of the
processes accordingly.

PRIORITY CPU SCHEDULING


#include<stdio.h>
#include<conio.h>
struct process
{
int pid;
int bt;
int wt;
int tt;
int prior;
}
p[10],temp;
int main()
{
int i,j,n,totwt,tottt,arg1,arg2;
clrscr();
printf("enter the number of process");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
p[i].pid=i;
printf("enter the burst time");
scanf("%d",&p[i].bt);
printf("\n enter the priority");
scanf("%d",&p[i].prior);
}
for(i=1;i<n;i++)
{
for(j=i+1;j<=n;j++)
{
if(p[i].prior>p[j].prior)
{
temp.pid=p[i].pid;
p[i].pid=p[j].pid;
p[j].pid=temp.pid;
temp.bt=p[i].bt;
p[i].bt=p[j].bt;
p[j].bt=temp.bt;
temp.prior=p[i].prior;
p[i].prior=p[j].prior;
p[j].prior=temp.prior;
}
}
}
p[i].wt=0;
p[1].tt=p[1].bt+p[1].wt;
i=2;
while(i<=n)
{
p[i].wt=p[i-1].bt+p[i-1].wt;
p[i].tt=p[i].bt+p[i].wt;
i++;
}
i=1;
totwt=tottt=0;
printf("\n process to \t bt \t wt \t tt");
while(i<=n)
{
printf("\n%d\t %d\t %d\t %d\t",p[i].pid,p[i].bt,p[i].wt,p[i].tt);
totwt=p[i].wt+totwt;
tottt=p[i].tt+tottt;
i++;
}
arg1=totwt/n;
arg2=tottt/n;
printf("\n arg1=%d \t arg2=%d\t",arg1,arg2);
getch();
return 0;
}
Output:

5. Design, Develop and Implementation of CPU scheduling by

a. SJF

b. Round Robin
DESCRIPTION
5 a)SJF CPU SCHEDULING ALGORITHM: For SJF scheduling algorithm, read the
number of processes/jobs in the system, their CPU burst times. Arrange all the jobs in order
with respect to their burst times. There may be two jobs in queue with the same execution
time, and then FCFS approach is to be performed. Each process will be executed according to
the length of its burst time. Then calculate the waiting time and turnaround time of each of
the processes accordingly.

SJF CPU SCHEDULING:


#include<stdio.h>
#include<conio.h>
struct process
{
int pid;
int bt;
int wt;
int tt;
}p[10],temp;
int main()
{
int i,j,n,totwt,tottt;
float avg1,avg2;
clrscr();
printf("\nEnter the number of process:\t");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
p[i].pid=i;
printf("\nEnter the burst time:\t");
scanf("%d",&p[i].bt);
}
for(i=1;i<n;i++){
for(j=i+1;j<=n;j++)
{
if(p[i].bt>p[j].bt)
{
temp.pid=p[i].pid;
p[i].pid=p[j].pid;
p[j].pid=temp.pid;
temp.bt=p[i].bt;p[i].bt=p[j].bt;
p[j].bt=temp.bt;
}}}
p[1].wt=0;
p[1].tt=p[1].bt+p[1].wt;
i=2;
while(i<=n){
p[i].wt=p[i-1].bt+p[i-1].wt;
p[i].tt=p[i].bt+p[i].wt;
i++;
}
i=1;
totwt=tottt=0;
printf("\nProcess id \tbt \twt \ttt");
while(i<=n){
printf("\n\t%d \t%d \t%d t%d\n",p[i].pid,p[i].bt,p[i].wt,p[i].tt);
totwt=p[i].wt+totwt;
tottt=p[i].tt+tottt;
i++;
} avg1=totwt/n;
avg2=tottt/n;
printf("\nAVG1=%f\t AVG2=%f",avg1,avg2);
getch();
return 0; }

Output:

5 b)ROUND ROBIN CPU SCHEDULING ALGORITHM: For round robin scheduling


algorithm, read the number of processes/jobs in the system, their CPU burst times, and the
size of the time slice. Time slices are assigned to each process in equal portions and in
circular order, handling all processes execution. This allows every process to get an equal
chance. Calculate the waiting time and turnaround time of each of the processes accordingly.

Program:

Round Robin Scheduling:


#include <stdio.h>

#define MAX_PROC 10

struct Proc {
int bt; // Burst time
int rt; // Remaining time
int tt; // Turnaround time
int wt; // Waiting time
};

void roundRobin(struct Proc procs[], int n, int tq) {


int tt_time = 0;
int tt_wt = 0;
int tt_tt = 0;
for (int i = 0; i < n; ++i) {
procs[i].rt = procs[i].bt;
}

while (1) {
int comp = 1;

for (int i = 0; i < n; ++i) {


if (procs[i].rt > 0) {
comp = 0;

int exe_time = (procs[i].rt > tq) ? tq : procs[i].rt;

procs[i].rt -= exe_time;
tt_time += exe_time;

if (procs[i].rt <= 0) {
procs[i].tt = tt_time;
procs[i].wt = procs[i].tt - procs[i].bt;

tt_wt += procs[i].wt;
tt_tt += procs[i].tt;
}
}
}

if (comp)
break;
}

printf("Proc\tBT\tTT\tWT\n");
for (int i = 0; i < n; ++i) {
printf("%d\t%d\t%d\t%d\n", i + 1, procs[i].bt, procs[i].tt, procs[i].wt);
}

float avg_wt = (float)tt_wt / n;


float avg_tt = (float)tt_tt / n;

printf("\nAvg WT: %.2f units\n", avg_wt);


printf("Avg TT: %.2f units\n", avg_tt);
}

int main() {
int n, tq;
struct Proc procs[MAX_PROC];

printf("Enter number of procs: ");


scanf("%d", &n);

printf("Enter time quantum: ");


scanf("%d", &tq);

printf("Enter burst times:\n");


for (int i = 0; i < n; ++i) {
printf("Proc %d: ", i + 1);
scanf("%d", &procs[i].bt);
}

roundRobin(procs, n, tq);

return 0;
}

OUTPUT:

/tmp/X92DttPApE.o
Enter number of procs: 3
Enter time quantum: 5
Enter burst times:
Proc 1: 15
Proc 2: 20
Proc 3: 30
Proc BT TT WT
1 15 35 20
2 20 50 30
3 30 65 35

Avg WT: 28.33 units


Avg TT: 50.00 units

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy