0% found this document useful (0 votes)
19 views

Os Practicals

The document describes several OS practical assignments involving processes and scheduling in C programs. The first practical involves creating a new process using a system call, and getting and displaying the process ID and parent process ID of the current process. The second practical identifies the system calls to copy the contents of one file to another, and provides a C program example. The third practical designs a CPU scheduling program using the First Come First Served technique, with all processes starting at time 0 and no waiting for I/O. Subsequent practicals involve non-preemptive shortest job first scheduling and other CPU scheduling algorithms.
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)
19 views

Os Practicals

The document describes several OS practical assignments involving processes and scheduling in C programs. The first practical involves creating a new process using a system call, and getting and displaying the process ID and parent process ID of the current process. The second practical identifies the system calls to copy the contents of one file to another, and provides a C program example. The third practical designs a CPU scheduling program using the First Come First Served technique, with all processes starting at time 0 and no waiting for I/O. Subsequent practicals involve non-preemptive shortest job first scheduling and other CPU scheduling algorithms.
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/ 16

OS PRACTICALS

NAME:S.Jaswanth varma

REG N0: 192211389

1.Create a new process by invoking the appropriate system


call. Get the process identifier of the currently running
process and its respective parent using system calls and
display the same using a C program.

AIM: Create a new process by invoking the appropriate system call. Get
the process identifier of the currently running process and its respective
parent using system calls and display the same using a C program.

ALGORITHM:
1. Include necessary headers: Include the necessary header files like
<stdio.h> and <unistd.h> for using system calls.
2. Declare variables: Declare variables to hold the process ID (pid_t pid)
and the parent process ID (pid_t ppid).
3. Get the current process ID: Use the getpid() system call to retrieve the
process ID of the current process.
4. Get the parent process ID: Use the getppid() system call to retrieve the
parent process ID.
5. Display the process IDs: Print the process IDs to the console.
6. Create a new process: Use the fork() system call to create a new
process. Check the return value of fork() to determine whether the code
is running in the parent or child process.
7. Display process IDs in child and parent processes: Depending on
whether the process is the parent or the child, display the respective
process IDs.

PROGRAM:
#include<stdio.h>
#include<unistd.h>
int main()
{
printf("Process ID: %d\n", getpid() );
printf("Parent Process ID: %d\n", getpid() );
return 0;
}

2. Identify the system calls to copy the content of one file to


another and illustrate the same using a C program
AIM: Identify the system calls to copy the content of one file to another
and illustrate the same using a C program
ALGORITHM:
 Include necessary headers:
 Include the necessary header files like <stdio.h>, <fcntl.h>, and
<unistd.h> for working with file-related system calls.
 Declare variables:
 Declare variables to hold file descriptors, buffer, and other
necessary information.
 Open the source and destination files:
 Use the open system call to open the source and destination files,
obtaining file descriptors for each.
 Read from the source file:
 Use the read system call to read data from the source file into a
buffer.
 Write to the destination file:
 Use the write system call to write the data read from the source file
into the destination file.
 Close the files:
Use the close system call to close the source and destination files.

PROGRAM:

#include <stdio.h>
#include <stdlib.h>

int main()
{
FILE *fptr1, *fptr2;
char filename[100], c;

printf("Enter the filename to open for reading \n");


scanf("%s", filename);
fptr1 = fopen(filename, "r");
if (fptr1 == NULL)
{
printf("Cannot open file %s \n", filename);
exit(0);
}

printf("Enter the filename to open for writing \n");


scanf("%s", filename);
fptr2 = fopen(filename, "w");
if (fptr2 == NULL)
{
printf("Cannot open file %s \n", filename);
exit(0);
}
c = fgetc(fptr1);
while (c != EOF)
{
fputc(c, fptr2);
c = fgetc(fptr1);
}

printf("\nContents copied to %s", filename);

fclose(fptr1);
fclose(fptr2);
return 0;
}

3. Design a CPU scheduling program with C using First


Come First Served technique with the following
considerations.
a. All processes are activated at time 0.
b. Assume that no process waits on I/O devices.
AIM: Design a CPU scheduling program with C using First Come
First Served technique with the following considerations.
a. All processes are activated at time 0.
b. Assume that no process waits on I/O devices.
ALGORITHM:
1. Include necessary headers:
2. Include the necessary header files like <stdio.h> for input/output
operations.
3. Define the process structure:
4. Define a structure to hold the process information, such as process
ID, arrival time, burst time, waiting time, and turnaround time.
5.Input the number of processes and their details:
Input the number of processes and their arrival time and burst time.
6.Sort processes by arrival time (if not given):
If the processes are not already sorted by arrival time, sort them based
7.Calculate waiting time and turnaround time.
8. Display the scheduling information:
Display the process details including process ID, arrival time, burst
time, waiting time, and turnaround time.
PROGRAM:
#include <stdio.h>
int main()
{
int A[100][4];
int i, j, n, total = 0, index, temp;
float avg_wt, avg_tat;
printf("Enter number of process: ");
scanf("%d", &n);
printf("Enter Burst Time:\n");
for (i = 0; i < n; i++) {
printf("P%d: ", i + 1);
scanf("%d", &A[i][1]);
A[i][0] = i + 1;
}
for (i = 0; i < n; i++) {
index = i;
for (j = i + 1; j < n; j++)
if (A[j][1] < A[index][1])
index = j;
temp = A[i][1];
A[i][1] = A[index][1];
A[index][1] = temp;

temp = A[i][0];
A[i][0] = A[index][0];
A[index][0] = temp;
}
A[0][2] = 0;
for (i = 1; i < n; i++) {
A[i][2] = 0;
for (j = 0; j < i; j++)
A[i][2] += A[j][1];
total += A[i][2];
}
avg_wt = (float)total / n;
total = 0;
printf("P BT WT TAT\n");

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


A[i][3] = A[i][1] + A[i][2];
total += A[i][3];
printf("P%d %d %d %d\n", A[i][0],A[i][1], A[i]
[2], A[i][3]);
}
avg_tat = (float)total / n;
printf("Average Waiting Time= %f", avg_wt);
printf("\nAverage Turnaround Time= %f", avg_tat);
}

4.Construct a scheduling program with C that selects the


waiting process with the smallest execution time to execute
next.
AIM: Construct a scheduling program with C that selects the
waiting process with the smallest execution time to execute next.
ALGORITHM:
1.Include necessary headers:
Include the necessary header files like <stdio.h> for input/output
operations.
2. Define the process structure:
Define a structure to hold the process information, such as process
ID, arrival time, burst time, waiting time, and turnaround time.
3.Input the number of processes and their details:
Input the number of processes and their arrival time and burst time.
4.Sort processes by burst time:
Sort the processes based on burst time in ascending order.
5.Calculate waiting time and turnaround time:
Calculate the waiting time and turnaround time for each process
based on the SJF scheduling algorithm.
6.Display the scheduling information:
Display the process details including process ID, arrival time, burst
time, waiting time, and turnaround time.
PROGRAM:
#include<stdio.h>
int main()
{
int bt[20],p[20],wt[20],tat[20],i,j,n,total=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;
}
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;
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];

total+=wt[i];
}
avg_wt=(float)total/n;
total=0;
printf("nProcesst Burst Time tWaiting TimetTurnaround Time\n");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
total+=tat[i];
printf("np%dtt %dtt %dttt%d",p[i],bt[i],wt[i],tat[i]);
}
avg_tat=(float)total/n;
printf("nnAverage Waiting Time=%f",avg_wt);
printf("nAverage Turnaround Time=%fn",avg_tat);
}

7. Construct a C program to implement non-preemptive SJF


algorithm
AIM: Construct a C program to implement non-preemptive SJF algorithm
ALGORITHM:
1.Include necessary headers:
Include the necessary header files like <stdio.h> for input/output
operations.
2. Define the process structure:
Define a structure to hold the process information, such as
process ID, arrival time, burst time, waiting time, and
turnaround time.
3. Input the number of processes and their details:
Input the number of processes and their arrival time and burst
time.
4.Sort processes by burst time:
Sort the processes based on burst time in ascending order.
5. Calculate waiting time and turnaround time:
Calculate the waiting time and turnaround time for each process
based on the non-preemptive SJF scheduling algorithm.
6. Display the scheduling information:
Display the process details including process ID, arrival time,
burst time, waiting time, and turnaround time.
PROGRAM:
#include<stdio.h>
int main()
{
int at[10],bt[10],pr[10];
int
n,i,j,temp,time=0,count,over=0,sum_wait=0,sum_turnaround=0,start;
float avgwait,avgturn;
printf("Enter the number of processes\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the arrival time and execution time for
process %d\n",i+1);
scanf("%d%d",&at[i],&bt[i]);
pr[i]=i+1;
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(at[i]>at[j])
{
temp=at[i];
at[i]=at[j];
at[j]=temp;
temp=bt[i];
bt[i]=bt[j];
bt[j]=temp;
temp=pr[i];
pr[i]=pr[j];
pr[j]=temp;
}
}
}
printf("\n\nProcess\t|Arrival time\t|Execution time\t|Start time\t|
End time\t|waiting time\t|Turnaround time\n\n");
while(over<n)
{
count=0;
for(i=over;i<n;i++)
{
if(at[i]<=time)
count++;
else
break;
}
if(count>1)
{
for(i=over;i<over+count-1;i++)
{
for(j=i+1;j<over+count;j++)
{
if(bt[i]>bt[j])
{
temp=at[i];
at[i]=at[j];
at[j]=temp;
temp=bt[i];
bt[i]=bt[j];
bt[j]=temp;
temp=pr[i];
pr[i]=pr[j];
pr[j]=temp;
}
}
}
}
start=time;
time+=bt[over];
printf("p[%d]\t|\t%d\t|\t%d\t|\t%d\t|\t%d\t|\t%d\t|\t%d\
n",pr[over],
at[over],bt[over],start,time,time-at[over]-
bt[over],time-at[over]);
sum_wait+=time-at[over]-bt[over];

sum_turnaround+=time-at[over];
over++;
}
avgwait=(float)sum_wait/(float)n;
avgturn=(float)sum_turnaround/(float)n;
printf("Average waiting time is %f\n",avgwait);
printf("Average turnaround time is %f\n",avgturn);
return 0;
}

8. Construct a C program to simulate Round Robin


scheduling algorithm with C.
AIM: Construct a C program to simulate Round Robin scheduling
algorithm with C.
ALGORITHM:
1.Include necessary headers:
Include the necessary header files like <stdio.h> for input/output
operations.
2.Define the process structure:
Define a structure to hold the process information, such as process ID,
arrival time, burst time, waiting time, and turnaround time.
3.Input the number of processes and their details:
Input the number of processes and their arrival time and burst time.
4.Input the time quantum for Round Robin:
Input the time quantum to be used in the Round Robin scheduling.
5.Simulate Round Robin scheduling:
Implement the Round Robin scheduling algorithm, including a queue to
keep track of the processes.
6.Display the scheduling information:
Display the process details including process ID, arrival time, burst time,
waiting time, and turnaround time.

PROGRAM:
#include<stdio.h>
#include<conio.h>
int main()
{
int i, NOP, sum=0,count=0, y, quant, wt=0, tat=0, at[10], bt[10],
temp[10];
float avg_wt, avg_tat;
printf(" Total number of process in the system: ");
scanf("%d", &NOP);
y = NOP;
for(i=0; i<NOP; i++)
{
printf("\n Enter the Arrival and Burst time of the Process[%d]\n", i+1);
printf(" Arrival time is: \t");
scanf("%d", &at[i]);
printf(" \nBurst time is: \t");
scanf("%d", &bt[i]);
temp[i] = bt[i];
}
printf("Enter the Time Quantum for the process: \t");
scanf("%d", &quant);
printf("\n Process No \t\t Burst Time \t\t TAT \t\t Waiting Time ");
for(sum=0, i = 0; y!=0; )
{
if(temp[i] <= quant && temp[i] > 0)
{
sum = sum + temp[i];
temp[i] = 0;
count=1;
}
else if(temp[i] > 0)
{
temp[i] = temp[i] - quant;
sum = sum + quant;
}
if(temp[i]==0 && count==1)
{
y--;
printf("\nProcess No[%d] \t\t %d\t\t\t\t %d\t\t\t %d", i+1, bt[i], sum-
at[i], sum-at[i]-bt[i]);
wt = wt+sum-at[i]-bt[i];
tat = tat+sum-at[i];
count =0;
}
if(i==NOP-1)
{
i=0;
}
else if(at[i+1]<=sum)
{
i++;
}
else
{
i=0;
}
}
avg_wt = wt * 1.0/NOP;
avg_tat = tat * 1.0/NOP;
printf("\n Average Turn Around Time: \t%f", avg_wt);
printf("\n Average Waiting Time: \t%f", avg_tat);
getch();
}

You might also like