Posts

Showing posts with the label Operating System Lab

Write C programs to simulate the Hierarchical directory File organization technique

Write C programs to simulate the Hierarchical  directory File organization technique #include<stdio.h> #include<graphics.h> #include<string.h> struct tree_element { char name[20]; int x, y, ftype, lx, rx, nc, level; struct tree_element *link[5]; }; typedef struct tree_element node; void main() { int gd=DETECT,gm; node *root; root=NULL; create(&root,0,"root",0,639,320); clrscr(); initgraph(&gd,&gm,"c:\tc\BGI"); display(root); closegraph(); } create(node **root,int lev,char *dname,int lx,int rx,int x) { int i, gap; if(*root==NULL) { (*root)=(node *)malloc(sizeof(node)); printf("Enter name of dir/file(under %s) : ",dname); fflush(stdin); gets((*root)->name); printf("enter 1 for Dir/2 for file :"); scanf("%d",&(*root)->ftype); (*root)->level=lev; (*root)->y=50+lev*50; (*root)->x=x; (*root)->lx=lx; (*root)->rx=rx; for(i=0;i<5;i++) (*root)->li...

Write C programs to simulate the Two level directory File organization technique

Write C programs to simulate the Two level directory File organization technique #include<string.h> #include<stdlib.h> #include<stdio.h> struct { char dname[10],fname[10][10]; int fcnt; }dir[10]; void main() { int i,ch,dcnt,k; char f[30], d[30]; dcnt=0; while(1) { printf("\n\n1. Create Directory\t2. Create File\t3. Delete File"); printf("\n4. Search File\t\t5. Display\t6. Exit\tEnter your choice -- "); scanf("%d",&ch); switch(ch) { case 1: printf("\nEnter name of directory -- "); scanf("%s", dir[dcnt].dname); dir[dcnt].fcnt=0; dcnt++; printf("Directory created"); break; case 2: printf("\nEnter name of the directory -- "); scanf("%s",d); for(i=0;i<dcnt;i++) if(strcmp(d,dir[i].dname)==0) { printf("Enter name of the file -- "); scanf("%s",dir[i].fname[dir[i].fcnt]); printf("File created"); break; } if(i==dcnt) printf...

Write C programs to simulate the Single level directory File organization techniqu

Write C programs to simulate the Single level directory File organization techniqu #include<stdlib.h> #include<string.h> #include<stdio.h> struct { char dname[10],fname[10][10]; int fcnt; }dir; void main() { int i,ch; char f[30]; dir.fcnt = 0; printf("\nEnter name of directory -- "); scanf("%s", dir.dname); while(1) { printf("\n\n1. Create File\t2. Delete File\t3. Search File \n 4. Display Files\t5. Exit\nEnter your choice -- "); scanf("%d",&ch); switch(ch) { case 1: printf("\nEnter the name of the file -- "); scanf("%s",dir.fname[dir.fcnt]); dir.fcnt++; break; case 2: printf("\nEnter the name of the file -- "); scanf("%s",f); for(i=0;i<dir.fcnt;i++) { if(strcmp(f, dir.fname[i])==0) { printf("File %s is deleted ",f); strcpy(dir.fname[i],dir.fname[dir.fcnt-1]); break; } } if(i==dir.fcnt) printf("File %s not found",f); else dir...

Write a C Program To Implement LRU Page Replacement Algorithm in operating system

#include<stdio.h> int i,j,nof,nor,flag=0,ref[50],frm[50],pf=0,victim=-1; int recent[10],lrucal[50],count=0; int lruvictim(); void main() { printf("\n\t\t\t LRU PAGE REPLACEMENT ALGORITHM"); printf("\n Enter no.of Frames...."); scanf("%d",&nof); printf(" Enter no.of reference string.."); scanf("%d",&nor); printf("\n Enter reference string.."); for(i=0;i<nor;i++) scanf("%d",&ref[i]); printf("\n\n\t\t LRU PAGE REPLACEMENT ALGORITHM "); printf("\n\t The given reference string:"); printf("\n……………………………….."); for(i=0;i<nor;i++) printf("%4d",ref[i]); for(i=1;i<=nof;i++) { frm[i]=-1; lrucal[i]=0; } for(i=0;i<10;i++) recent[i]=0; printf("\n"); for(i=0;i<nor;i++) { flag=0; printf("\n\t Reference NO %d->\t",ref[i]); for(j=0;j<nof;j++) { if(frm[j]==ref[i]) { flag=1; break; } } if(flag==0)...

Write a C Program To Implement optimal Page Replacement Algorithm in operating system

#include<stdio.h> int n,page[20],f,fr[20],i; void display() {  for(i=0;i<f;i++)  {   printf("%8d",fr[i]);  }  printf("\n"); } void request() {  printf("enter no.of pages:");  scanf("%d",&n);  printf("enter no.of frames:");  scanf("%d",&f);  printf("enter no.of page no.s");  for(i=0;i<n;i++)  {   scanf("%d",&page[i]);  }  for(i=0;i<n;i++)  {   fr[i]=-1;  } } void replace() {  int j,flag=0,pf=0;  int max,lp[10],index,m;  for(j=0;j<f;j++)  {   fr[j]=page[j];   flag=1;   pf++;   display();  }  for(j=f;j<n;j++)  {   flag=0;   for(i=0;i<f;i++)   {    if(fr[i]==page[j])    {     flag=1;     break;    }   }   if(flag==0)   {    for(i=0;i<f;i++)    lp[i]=0;  ...

Write a C Program To Implement FIFO Page Replacement Algorithm in operating system

#include<stdio.h> int i,j,nof,nors,flag=0,ref[50],frm[50],pf=0,victim=-1; void main() {       printf("\n \t\t\t FIFI PAGE REPLACEMENT ALGORITHM");       printf("\n Enter no.of frames...."); // Get number of frames from user scanf("%d",&nof); printf("Enter number of reference string..\n"); scanf("%d",&nors); printf("\n Enter the reference string.."); for(i=0;i<nors;i++) scanf("%d",&ref[i]); printf("\nThe given reference string:"); for(i=0;i<nors;i++) printf("%4d",ref[i]); for(i=1;i<=nof;i++) frm[i]=-1; printf("\n"); for(i=0;i<nors;i++) // For every page { flag=0; // Flag to show availability of page in frame // Get the availability of required page in frame printf("\n\t Reference np%d->\t",ref[i]); for(j=0;j<nof;j++) { if(frm[j]==ref[i])   // If page found in frame { flag=1; // Set flag to ...

Write C programs to simulate the Paging techniques of memory management

Image
#include<stdio.h> #define MAX 50 int main() { int page[MAX],i,n,f,ps,off,pno; int choice=0; printf("\nEnter the no of  peges in memory: "); scanf("%d",&n); printf("\nEnter page size: "); scanf("%d",&ps); printf("\nEnter no of frames: "); scanf("%d",&f); for(i=0;i<n;i++) page[i]=-1; printf("\nEnter the page table\n"); printf("(Enter frame no as -1 if that page is not present in any frame)\n\n"); printf("\npageno\tframeno\n-------\t-------"); for(i=0;i<n;i++) { printf("\n\n%d\t\t",i); scanf("%d",&page[i]); } do { printf("\n\nEnter the logical address(i.e,page no & offset):"); scanf("%d%d",&pno,&off); if(page[pno]==-1) printf("\n\nThe required page is not available in any of frames"); else printf("\n\nPhysical address(i.e,frame no & offset):%d,%d",page[pno],off); prin...

Write a C program to implement the ls | sort command. (Use unnamed Pipe)

#include<sys/types.h> #include<stdio.h> #include<stdlib.h> #include<unistd.h> int main() { int fd[2]; if(pipe(fd) == -1) { perror("ERROR creating a pipe\n"); exit(1); } if (!fork()) { close(1); // close the standard output file dup(fd[1]); // now fd[1] (the writing end of the pipe) is the standard output close(fd[0]); // I am the writer. So, I don’t need this fd execlp("ls", "ls", NULL); } else { close(0); // close the standard input file dup(fd[0]); close(fd[1]); execlp("sort", "sort", "-r", NULL); } return(0); }

Write C programs to implement ipc between two unrelated processes using named pipe

#include<stdio.h> #include<stdlib.h> #include<errno.h> #include<unistd.h> int main() { int pfds[2]; char buf[30]; if(pipe(pfds)==-1) { perror("pipe"); exit(1); } printf("writing to file descriptor #%d\n", pfds[1]); write(pfds[1],"test hello",10); printf("reading from file descriptor #%d\n ", pfds[0]); read(pfds[0],buf,10); printf("read\"%s\"\n" ,buf); }output: jethusai@jethusai-Lenovo-G560:~$ gedit pipe.c jethusai@jethusai-Lenovo-G560:~$ gcc pipe.c jethusai@jethusai-Lenovo-G560:~$ ./a.out writing to file descriptor #4 reading from file descriptor #3  read"test hello"

Write a c program to implement Priority Scheduling algorithms non-preemptive

Algorithm: 1.        Start 2.       Declare the array size p[20],bt[20],pri[20], wt[20],tat[20],i, k, n, temp; float wtavg, tatavg,awt,tat; 3.       Read the number of processes to be inserted 4.       Read the Burst times of processes 5.       Sort the Burst times in ascending order and process with shortest burst time is first executed. for(i=0;i<n;i++) for(k=i+1;k<n;k++)                   if(pri[i] > pri[k])                   {                               temp=p[i];           ...

Write a c program to implement shortest job first scheduling algorithms non-preemptive

Image
Write a c program to implement shortest job first scheduling algorithms non-preemptive Algorithm: 1.        Start 2.       Declare the array size p[20], bt[20], wt[20], tat[20], i, k, n, temp; float wtavg, tatavg; 3.       Read the number of processes to be inserted 4.       Read the Burst times of processes 5.       Sort the Burst times in ascending order and process with shortest burst time is first executed. for(i=0;i<n;i++) for(k=i+1;k<n;k++) if(bt[i]>bt[k]) {    temp=bt[i]; bt[i]=bt[k]; bt[k]=temp; temp=p[i]; p[i]=p[k]; p[k]=temp; } 6.       waiting time for first process will be zero wt[0] = wtavg = 0; tat[0] = tatavg = bt[0]; 7.       calculate waiting time and turnaround time for(i=1;i<n;i++) { wt[i] = wt[i-1] +bt[i-1]; ...

write a C program to implement the RR(Round Robin) scheduling algorithm

Image
 write a C program to implement the RR(Round Robin) scheduling algorithm ALGORITHM 1. Start 2. Declare the array size 3. Read the number of processes to be inserted 4. Read the burst times of the processes 5. Read the Time Quantum 6. if (BT>TQ) then TQ-BT Else Assign the burst time to time quantum. 7.calculate the average waiting time and turn arou nd time of the processes. 8. D isplay the values 9. Stop  source code: #include<stdio.h>  int main() {       int i, n, total = 0, x, counter = 0, qt;       int wt = 0, tt = 0, at[10], bt[10], temp[10];       float average_wait_time, average_turnaround_time;       printf("\nEnter Total Number of Processes : ");       scanf("%d", &n);       x = n;       for(i = 0; i < n; i++) ...

Write a C program to implement the FCFS (FIRST COME FIRST SERVE) scheduling algorithm

Description: First-come, first-serve scheduling (FCFS): In this, which process enter the ready queue first is served first. The OS maintains DS that is ready queue. It is the simplest CPU scheduling algorithm. If a process requests the CPU then it is loaded into the ready queue, which process is the head of the ready queue, connect the CPU to that process. Algorithm for FCFS scheduling: Step 1: Start the process Step 2: Accept the number of processes in the ready Queue Step 3: For each process in the ready Queue, assign the process id and accept the CPU burst time Step 4: Set the waiting of the first process as ‘0’ and its burst time as its turnaround time Step 5: for each process in the Ready Queue calculate (a) Waiting time for process (n)= waiting time of process (n-1) + Burst time of process(n-1) (b)Turn around time for Process (n)= waiting time of Process(n)+ Burst time for process(n) Step 6: Calculate (c) Average waiting time = Total waiting Time / N...