OS Lab Programs

Program 1.1: FCFS (First Come First Served)

Aim: To implement FCFS CPU scheduling algorithm in C.

First in, first out (FIFO). The process that comes first is executed first. Arrival time for all processes is 0.

Algorithm

1. Start
2. Declare the following array to maximum size 15.
   a. WT[ MAX ] to store waiting time of each process.
   b. BT[ MAX ] to store Burst time of each process.
   c. TT [ MAX ] to store turnaround time of each process.
3. Read the number of processes.
4. Read the Burst time for all the processes.
5. Calculate the waiting time of each process
   a. WT [ i ] = WT [ i-1] + BT [i-1]
6. Calculate the Turnaround time of each process
   a. TT [ i ] = WT [ i ] + BT [ i ]
7. Calculate average waiting time and average turnaround time.
8. Display all the values
9. Stop.

Program

#include<stdio.h>
int main()
{
 int processes[] = { 1, 2, 3};
 int n =3;
 int bt[] = {10, 5, 8};

 int wt[n], tat[n], total_wt = 0, total_tat = 0;

 wt[0] = 0;

 for (int i = 1; i < n ; i++ )
 wt[i] = bt[i-1] + wt[i-1] ;

 for (int i = 0; i < n ; i++)
 tat[i] = bt[i] + wt[i];

 printf("Processes Burst time Waiting time Turn around time\n");

 for (int i=0; i<n; i++)
 {
 total_wt = total_wt + wt[i];
 total_tat = total_tat + tat[i];
 printf(" %d ",(i+1));
 printf(" %d ", bt[i] );
 printf(" %d",wt[i] );
 printf(" %d\n",tat[i] );
 }
 int s=(float)total_wt / (float)n;
 int t=(float)total_tat / (float)n;
 printf("Average waiting time = %d",s);
 printf("\n");
 printf("Average turn around time = %d ",t);
 return 0;
}

Program 1.2: SJF (Shortest Job First)

Aim: To implement SJF CPU scheduling algorithm in C.

Process with smallest execution time is chosen next. Can be preemptive or non-preemptive.

Algorithm

1. Start
2. Declare Process structure with following members.
   a. PID : to Store Process ID
   b. WT : to store waiting time of process.
   c. BT : to store Burst time of process.
   d. TT : to store turnaround time of process.
3. Read the number of processes.
4. Read the Burst time and Process Id for all the processes.
5. Sort the Array of process structure based on Burst time.
6. Calculate the waiting time of each process
   a. After sorting first process in the list will have waiting time 0
      i. Proc[0].wt = 0
   b. Find Waiting time for all other process
      i. Proc[i].wt = Proc[i-1].wt + Proc[i-1].bt
7. Calculate the Turnaround time of each process
   i. Proc[i].tt = Proc[i].wt + Proc[i].bt
8. Calculate average waiting time
   a. Total_waitingTime / Number of Process
9. Calculate average turnaround time.
   a. Total_turnaroundTime / Number of Process
10. Display all the values
11. Stop.

Program

#include <stdio.h>
#define MAX 15
struct process
{
 int pid;
 int bt;
 int wt;
 int tt;
};
int main()
{
 struct process proc[MAX], temp;
 int n , i , j;
 int total_wt=0 , total_tt=0;
 float avg_wt, avg_tt;
 printf("enter number of process (max of 15) \n");
 scanf("%d",&n);
 printf(" Enter Process Id and Brust time of the process \n");
 for( i=0; i< n; i++)
 {
 scanf("%d%d", &proc[i].pid , &proc[i].bt );
 }
 for (i = 0; i < n-1; i++)
 for (j = 0; j < n-i-1; j++)
 if (proc[j].bt > proc[j+1].bt)
 {
 temp=proc[j];
 proc[j]= proc[j+1];
 proc[j+1]=temp;
 }

 for (i = 0; i < n; i++)
 {
 if(i==0)
 {
 proc[0].wt=0;
 proc[0].tt = proc[0].bt;
 }
 else
 {
 proc[i].wt = proc[i-1].wt + proc[i-1].bt;
 total_wt = total_wt+proc[i].wt;
 proc[i].tt = proc[i].wt+proc[i].bt ;
 total_tt = total_tt+proc[i].tt;
 }
 }

 avg_wt = (float) total_wt/n;
 avg_tt = (float) total_tt/n;

 printf(" Process ID Burst time Waiting Time Turn Around Time \n");

 for (i = 0; i < n; i++)
 printf(" %d %d %d %d \n", proc[i].pid , proc[i].bt, proc[i].wt, proc[i].tt);
 printf("Average waiting time = %f",avg_wt);
 printf("\n");
 printf("Average turn around time = %f ",avg_tt);
 return 0;
}

Program 1.3: Round Robin (RR)

Aim: To implement RR CPU scheduling algorithm in C.

Time-sharing; each process gets a fixed time quantum. Preemptive.

Algorithm

1. Start
2. Declare Process structure with following members.
   a. PID : to Store Process ID
   b. WT : to store waiting time of process.
   c. BT : to store Burst time of process.
   d. TT : to store turnaround time of process.
   e. rem_bt : to store remaining burst time
3. Read the number of processes.
4. Read the Burst time and Process Id for all the processes.
5. Read the time quantum value.
6. The process are selected from the queue in first come first serve order and following
   steps are repeated until remaining burst time for all process is zero.
   a. If Burst time > time quantum
      i. The process is preempted after time quantum and added to the end of the queue.
         Remaining burst time is recorded. CPU execution time is also recorded
      1. t = t + quantum
      2. Proc[i].rem_bt = Proc[i].rem_bt - quantum
   b. If Burst Time <= time quantum
      i. Process remaining burst time is set to zero and waiting time is calculated
      1. t = t + Proc[i].rem_bt
      2. Proc[i].rem_bt = 0
      3. Proc[i].wt = t - Proc[i].bt
7. Calculate the Turnaround time of each process
   i. Proc[i].tt = Proc[i].wt + Proc[i].bt
8. Calculate average waiting time
   a. Total_waitingTime / Number of Process
9. Calculate average turnaround time.
   a. Total_turnaroundTime / Number of Process
10. Display all the values
11. Stop.

Program

#include <stdio.h>
#define MAX 15
struct process
{
 int pid;
 int bt;
 int wt;
 int tt;
 int rem_bt;
};
int main()
{
 struct process proc[MAX] ;
 int n , i , j ,quantum , t;
 int total_wt=0 , total_tt=0;
 float avg_wt, avg_tt;
 int done=0;

 printf("enter number of process (max of 15) \n");
 scanf("%d",&n);
 printf(" Enter Process Id and Brust time of the process \n");
 for( i=0; i< n; i++)
 {
 scanf("%d%d", &proc[i].pid , &proc[i].bt );
 proc[i].rem_bt = proc[i].bt;
 }

 printf("Enter time Quantum \n");
 scanf("%d",&quantum);

 t = 0;
 printf("Order of Process Excecution is \n");

 do
 {
 done = 1;
 for (int i = 0 ; i < n; i++)
 {
 if (proc[i].rem_bt > 0)
 {
 done = 0;
 printf(" %d : ",proc[i].pid);
 if (proc[i].rem_bt > quantum)
 {
 t += quantum;
 proc[i].rem_bt= proc[i].rem_bt - quantum;
 }
 else
 {
 t = t + proc[i].rem_bt;
 proc[i].wt = t - proc[i].bt;
 proc[i].rem_bt = 0;
 }
 }
 }
 }while(done != 1);
 printf("\n");
 for (i = 0; i < n; i++)
 {
 total_wt = total_wt + proc[i].wt;
 proc[i].tt = proc[i].wt + proc[i].bt ;
 total_tt = total_tt + proc[i].tt;
 }

 avg_wt = (float) total_wt/n;
 avg_tt = (float) total_tt/n;

 printf(" | PID | BT | WT | TaT | \n");
 for (i = 0; i < n; i++)
 printf(" %d : %d : %d : %d \n", proc[i].pid , proc[i].bt , proc[i].wt , proc[i].tt );
 printf("Average waiting time %f \n", avg_wt);
 printf("Average Turn around time %f \n " ,avg_tt);
 return 0;
}

Program 1.4: Priority CPU Scheduling

Aim: To implement Priority CPU scheduling algorithm in C.

CPU is allocated to the process with the highest priority. Low number = high priority.

Algorithm

1. Start
2. Declare Process structure with following members.
   i. PID : to Store Process ID
   ii. Priority : to store priority of the process
   iii. WT : to store waiting time of the process.
   iv. BT : to store Burst time of the process.
   v. TT : to store turnaround time of the process.
3. Read the number of processes.
4. Read the Process Id, Burst time and Priority for all the processes.
5. Sort the Array of process structure based on Priority.
6. Calculate the waiting time of each process
   a. After sorting first process in the list will have waiting time 0
      i. Proc[0].wt = 0
   b. Find Waiting time for all other process
      i. Proc[i].wt = Proc[i-1].wt + Proc[i-1].bt
7. Calculate the Turnaround time of each process
   i. Proc[i].tt = Proc[i].wt + Proc[i].bt
8. Calculate average waiting time
   a. Total_waitingTime / Number of Process
9. Calculate average turnaround time.
   a. Total_turnaroundTime / Number of Process
10. Display all the values
11. Stop.

Program

#include <stdio.h>
#define MAX 15
struct process
{
 int pid;
 int priority;
 int bt;
 int wt;
 int tt;
};
int main()
{
 struct process proc[MAX], temp;
 int n , i , j;
 int total_wt=0 , total_tt=0;
 float avg_wt, avg_tt;
 printf("enter number of process (max of 15) \n");
 scanf("%d",&n);
 printf(" Enter Process Id and Brust time and priority of the process \n");
 for( i=0; i< n; i++)
 {
 scanf("%d%d%d", &proc[i].pid , &proc[i].bt , &proc[i].priority);
 }
 for (i = 0; i < n-1; i++)
 for (j = 0; j < n-i-1; j++)
 if (proc[j].priority > proc[j+1].priority)
 {
 temp=proc[j];
 proc[j]= proc[j+1];
 proc[j+1]=temp;
 }

 for (i = 0; i < n; i++)
 {
 if(i==0)
 {
 proc[0].wt=0;
 proc[0].tt = proc[0].bt;
 }
 else
 {
 proc[i].wt = proc[i-1].wt + proc[i-1].tt;
 total_wt = total_wt + proc[i].wt;
 proc[i].tt = proc[i].wt + proc[i].bt ;
 total_tt = total_tt + proc[i].tt;
 }
 }

 avg_wt = (float) total_wt/n;
 avg_tt = (float) total_tt/n;

 printf(" | Process ID | Priority | Burst time | Waiting Time | Turn Around Time | \n");
 for (i = 0; i < n; i++)
 printf(" | %d | %d | %d | %d | %d | \n", proc[i].pid , proc[i].priority, proc[i].bt, proc[i].wt, proc[i].tt);
 return 0;
}

Program 2.1: Read from file and write to another file

Aim: To write a C program to copy content from one file to another file.

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>
#define BUFF_SIZE 1000
int main(void)
{
 int n,fd1,fd2;
 char buff[BUFF_SIZE];
 fd1 = open("testfile.txt",O_RDWR,0644);
 n=read(fd1,buff,BUFF_SIZE);
 fd2=open("fileforcopy.txt", O_CREAT | O_RDWR, 0777);
 if( write(fd2, buff, n) == n)
 printf("file copying is successful. and the data is:\n");
 write(1, buff, n);
 close(fd1);
 close(fd2);
 return 0;
}

Program 2.2: lseek() – read 10 chars, skip 5, read 10

Aim: To demonstrate lseek() system call. Reads 10 characters from file "seeking", prints them, skips next 5, reads 10 more and prints.

lseek() changes the read/write pointer. SEEK_SET, SEEK_CUR, SEEK_END are in <unistd.h>.

#include<unistd.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>
int main()
{
 int n,f;
 char buff[10];
 f=open("seeking",O_RDWR);
 read(f,buff,10);
 write(1,buff,10);
 lseek(f,5,SEEK_CUR);
 read(f,buff,10);
 write(1,buff,10);
 close(f);
 return 0;
}

Program 2.3: File status using stat()

Aim: To demonstrate stat() system call – file attributes, size, timestamps, device, inode.

#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include<unistd.h>
struct stat statbuf;
char dirpath[256];
int main(int argc, char *argv[])
{
 getcwd(dirpath,256);
 DIR *dir = opendir(dirpath);
 struct dirent *dp;
 for (dp=readdir(dir); dp != NULL ; dp=readdir(dir))
 {
  stat(dp->d_name, &statbuf);
  printf("the file name is %s \n", dp->d_name);
  printf("dir = %d\n", S_ISDIR(statbuf.st_mode));
  printf("file size is %ld in bytes \n", statbuf.st_size);
  printf("last modified time is %ld in seconds \n", statbuf.st_mtime);
  printf("last access time is %ld in seconds \n", statbuf.st_atime);
  printf("The device containing the file is %ld\n", statbuf.st_dev);
  printf("File serial number is %ld\n\n", statbuf.st_ino);
 }
 closedir(dir);
 return 0;
}

Program 2.4: opendir(), readdir(), closedir()

Aim: To demonstrate opendir(), readdir() and closedir() system calls. Lists directory entries.

#include<stdio.h>
#include<stdlib.h>
#include<dirent.h>
struct dirent *dptr;
int main(int argc, char *argv[])
{
 char buff[100];
 DIR *dirp;
 printf("\n\n ENTER DIRECTORY NAME");
 scanf("%s", buff);
 if( ( dirp=opendir( buff ) ) == NULL )
 {
  printf("The given directory does not exist");
  exit(1);
 }
 while(dptr = readdir ( dirp ) )
 {
  printf("%s\n",dptr->d_name);
 }
 closedir(dirp);
 return 0;
}

Program 2.5: fork() and getpid()

Aim: To demonstrate fork() and getpid() system calls. fork() creates child process; getpid() returns process ID.

Algorithm

STEP 1: Start the program.
STEP 2: Declare the variables pid, pid1, pid2.
STEP 3: Call fork() system call to create process.
STEP 4: If pid==-1, exit.
STEP 5: If pid!=-1, get the process id using getpid().
STEP 6: Print the process id.
STEP 7: Stop the program.

Program

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main()
{
 int pid,pid1,pid2;
 pid=fork();
 if(pid== -1)
 {
  printf("ERROR IN PROCESS CREATION \n");
  exit(1);
 }
 if(pid!=0)
 {
  pid1=getpid();
  printf("\n the parent process ID is %d\n", pid1);
 }
 else
 {
  pid2=getpid();
  printf("\n the child process ID is %d\n", pid2);
 }
 return 0;
}

Program 2.6: Execute another C program using exec

Aim: To execute another C program using exec system call (execvp). Replaces current process with new program.

EXEC.c (compile: gcc EXEC.c -o EXEC)

#include<stdio.h>
#include<unistd.h>
int main()
{
 printf("I am EXEC.c called by execvp() ");
 printf("\n");
 return 0;
}

execDemo.c (compile: gcc execDemo.c -o execDemo, run: ./execDemo)

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main()
{
 char *args[]={"./EXEC",NULL};
 execvp(args[0],args);
 printf("Ending-----");
 return 0;
}