Os Experiment From 3 To 5 B
Os Experiment From 3 To 5 B
● 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:
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.
Output:
Program:
#define MAX_PROC 10
struct Proc {
int bt; // Burst time
int rt; // Remaining time
int tt; // Turnaround time
int wt; // Waiting time
};
while (1) {
int comp = 1;
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);
}
int main() {
int n, tq;
struct Proc procs[MAX_PROC];
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