OSLAB1
OSLAB1
Syntax:ls -l
Options:
Example with output:
Name of the command:cd
Description: cd command in Linux known as the change directory command. It is used
to move efficiently from the current working directory to different directories in our System
Syntax: cd [directory]
Example with output:
Syntax:man ls
Examples with outputs:
No output
Name of the command: rm
Description: rm (short for remove) is a basic command on Unix and Unix-like operating
systems used to remove objects such as computer files, directories and symbolic links from
file systems and also special files such as device nodes, pipes and sockets, similar to the del
command in MS-DOS, OS/2, and Microsoft Windows
Description: The echo command is used to display text or messages on the terminal.
Syntax:echo option
Examples with outputs:
Syntax: ps [options]
Example with output:
Syntax:finger <options><user>
Example with output:
No output
Output:
Output:
7.Example Program:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
int main() {
int fd; // File descriptor
// Create a new file named "sample.txt" with write permissions
fd = open("sample.txt", O_CREAT | O_WRONLY, 0644);
if (fd < 0) {
perror("open");
exit(1);
}
// Write data to the file
write(fd, "Hello, World!\n", 14);
// Close the file
close(fd);
// Reopen the file in read-only mode
fd = open("sample.txt", O_RDONLY);
if (fd < 0) {
perror("open");
exit(1);
}
char buffer[20];
// Read data from the file
read(fd, buffer, 20);
printf("Read from file: %s", buffer);
// Move the file pointer to the beginning
lseek(fd, 0, SEEK_SET);
// Close the file
close(fd);
// Remove the file
if (unlink("sample.txt") != 0) {
perror("unlink");
exit(1);
}
return 0;
}
Output:
LAB-3
Aim: Implementation of fork(), wait(), exec() and exit() process system calls.
1.Name of the system call: fork()
Description:The use of the fork() system call is to create a new process by duplicating the
calling process. The fork() system call is made by the parent process, and if it is successful, a
child process is created. The fork() system call does not accept any parameters.
Output:
Output:
Output:
Output:
Lab-04
Aim:Implementation of FCFS,SJF scheduling algorithms.
1.Name of the algorithm:First Come First Serve(FCFS) without arrival time
Description: (FCFS), is the simplest scheduling algorithm. FIFO simply queues
processes in the order that they arrive in the ready queue.
In this, the process that comes first will be executed first and next process starts only after
the previous gets fully executed.
Program:
#include <stdio.h>
int main() {
int bt[100], ex[100], seq[100], re[100] = {0}, wt[100], tat[100];
int n, i, k = 0, idle = 0;
float av1 = 0, av2 = 0;
printf("Enter number of processes\n");
scanf("%d", &n);
printf("Enter burst time for processes\n");
for (i = 0; i < n; i++) {
scanf("%d", &bt[i]);
}
int start = 0;
printf("Sequence of execution is\n");
for (i = 0; i < n; i++, k++) {
int pos = i + 1;
printf("[P%d] ", pos);
seq[k] = pos;
if (start < re[pos - 1]) {
idle += re[pos - 1] - start;
start = re[pos - 1];
}
re[pos - 1] = start;
start += bt[pos - 1];
ex[pos - 1] = start;
}
printf("\n");
for (i = 0; i < n; i++) {
tat[i] = ex[i];
wt[i] = re[i];
}
printf("Process Burst-time(ms) Waiting-time(ms) Turnaround-time(ms)\n");
for (i = 0; i < n; i++) {
printf("P%d %d %d %d\n", i + 1, bt[i], wt[i], tat[i]);
}
for (i = 0; i < n; i++) {
av1 += tat[i];
av2 += wt[i];
}
printf("Average waiting time(ms) %.2f\nAverage turnaround time(ms) %.2f\nCPU idle
time(ms) %d\n", av2 / n, av1 / n, idle);
return 0;
}
Output:
2.Name of the algorithm:First Come First Serve(FCFS) with arrival time
Description: (FCFS), is the simplest scheduling algorithm. FIFO simply queues
processes in the order that they arrive in the ready queue.
In this, the process that comes first will be executed first and next process starts only after
the previous gets fully executed.
Program:
#include<stdio.h>
int main()
{
int at[10],at2[10],bt[100],ex[100],seq[100],re[100],wt[100],tat[100];
int n,i,j,start,pos,max=0,min,idle=0,k=0;
float av1=0,av2=0;
printf("Enter number of process\n");
scanf("%d",&n);
printf("Enter arrival time for processess\n");
for(i=0;i<n;i++)
{
scanf("%d",&at[i]);
at2[i]=at[i];
}
printf("Enter burst time for processess\n");
for(i=0;i<n;i++)
{
scanf("%d",&bt[i]);
}
start=at[0];
for(i=1;i<n;i++)
{
if(start>at[i])
{
start=at[i];
}
}
printf("Sequence of execution is\n");
for(i=0;i<n;i++)
{
if(max<at[i])
{
max=at[i];
}
}
max=max+1;
for(i=0;i<n;i++,k++)
{ min=max;
for(j=0;j<n;j++){
if(at[j]!=-1)
{
if(at[j]<min)
{
min=at[j];
pos=j;
}
}
}
printf("[P%d] ",pos);
seq[k]=pos;
if(start<at[pos]){
re[pos]=start;
idle+=at[pos]-start;
start=at[pos];
start+=bt[pos];
at[pos]=-1;
ex[pos]=start;
}
else{
re[pos]=start;
start+=bt[pos];
at[pos]=-1;
ex[pos]=start;
}
}
printf("\n");
for(i=0;i<n;i++)
{
tat[i]=ex[i]-at2[i];
wt[i]=tat[i]-bt[i];
}
printf("Process Arrival-time(ms) Burst-time(ms) Waiting-time(ms) Turnaround-
time(ms)\n");
for(i=0;i<n;i++)
{
printf("P%d %d %d %d %d\n",i,at2[i],bt[i],wt[i],tat[i]);
}
for(i=0;i<n;i++)
{
av1+=tat[i];
av2+=wt[i];
}
printf("Average waiting time(ms) %f\nAverage turnaroundtime(ms) %f\nCPU idle
time(ms)%d\n",av2/n,av1/n,idle);
}
Output:
Program:
#include <stdio.h>
#include <limits.h>
// Structure to represent a process
struct Process {
int id;
int arrival_time;
int burst_time;
int remaining_time; // Remaining burst time
int completion_time; // Completion time
};
// Function to find the process with the shortest remaining time
int findShortestJob(struct Process processes[], int n, int currentTime) {
int shortestJob = -1;
int shortestTime = INT_MAX;
for (int i = 0; i < n; i++) {
if (processes[i].arrival_time <= currentTime && processes[i].remaining_time <
shortestTime && processes[i].remaining_time > 0) {
shortestJob = i;
shortestTime = processes[i].remaining_time;
}
}
return shortestJob;
}
// Function to perform preemptive SJF scheduling
void sjfPreemptive(struct Process processes[], int n) {
int currentTime = 0; // Current time
int completed = 0; // Number of completed processes
int totalWaitingTime = 0; // Accumulator for waiting times
int totalTurnaroundTime = 0; // Accumulator for turnaround times
printf("Gantt Chart:\n");
while (completed < n) {
int shortestJob = findShortestJob(processes, n, currentTime);
if (shortestJob == -1) {
printf("IDLE (%d-%d) | ", currentTime, currentTime + 1);
currentTime++; // No process is ready, so wait
} else {
processes[shortestJob].remaining_time--;
printf("P%d (%d-%d) | ", processes[shortestJob].id, currentTime, currentTime + 1);
currentTime++;
if (processes[shortestJob].remaining_time == 0) {
completed++;
processes[shortestJob].completion_time = currentTime;
int turnaround_time = processes[shortestJob].completion_time -
processes[shortestJob].arrival_time;
int waiting_time = turnaround_time - processes[shortestJob].burst_time;
totalTurnaroundTime += turnaround_time;
totalWaitingTime += waiting_time;
printf("P%d completed (%d-%d). Turnaround Time: %d, Waiting Time: %d\n",
processes[shortestJob].id, processes[shortestJob].completion_time - 1,
processes[shortestJob].completion_time,
turnaround_time, waiting_time);
}
}
}
printf("\n");
// Calculate and print averages
printf("Average Turnaround Time: %.2f\n", (float)totalTurnaroundTime / n);
printf("Average Waiting Time: %.2f\n", (float)totalWaitingTime / n);
}
int main() {
int n;
printf("Enter the number of processes: ");
scanf("%d", &n);
struct Process processes[n];
for (int i = 0; i < n; i++) {
processes[i].id = i + 1;
printf("Enter arrival time for process P%d: ", i + 1);
scanf("%d", &processes[i].arrival_time);
printf("Enter burst time for process P%d: ", i + 1);
scanf("%d", &processes[i].burst_time);
processes[i].remaining_time = processes[i].burst_time;
}
printf("\n");
sjfPreemptive(processes, n);
return 0;
}
Output:
4. Name of the algorithm: Shortest Job First Scheduling with non preemptive
Description: The shortest job first (SJF) or shortest job next, is a scheduling policy that
selects the waiting process with the smallest execution time to execute next
Progam:
#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("Enter Burst Time:");
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("Process Burst Time Waiting Time Turnaround Time\n");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
total+=tat[i];
printf("p%d\t%d\t\t%d\t\t%d\n",p[i],bt[i],wt[i],tat[i]);
}
avg_tat=(float)total/n;
printf("\nAverage Waiting Time=%f",avg_wt);
printf("\nAverage Turnaround Time=%f",avg_tat);
}
Output: