Cn&os Lab Executed Codes
Cn&os Lab Executed Codes
1.CHARACTER STUFFING
#include <stdio.h>
#include<string.h>
int main(void) {
int i=0,j,n;
char a[30],b[50];
scanf("%s",&a);
n=strlen(a);
b[0]='D';
b[1]='L';
b[2]='E';
b[3]='S';
b[4]='T';
b[5]='X';
j=6;
while(i<n)
{
if(a[i]=='D' && a[i+1]=='L' && a[i+2]=='E')
{
b[j]='D';
b[j+1]='L';
b[j+2]='E';
j=j+3;
}
b[j]=a[i];
j++;
i++;
}
b[j]='D';
b[j+1]='L';
b[j+2]='E';
b[j+3]='E';
b[j+4]='T';
b[j+5]='X';
b[j+6]='\0';
printf("%s",b);
return 0;
}
2.BIT STUFFING
#include<stdio.h>
int main()
{
int i,j,k,count,n,a[20],b[30];
printf("Enter the frame size");
scanf("%d",&n);
printf("Enter the frame(in 0s and 1s)\n");
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
i=0;
j=0;
count=1;
while(i<n)
{
if(a[i]==1)
{
b[j]=a[i];
for(k=i+1;k<n && count<5 && a[k]==1;k++)
{
j++;
b[j]=a[k];
count++;
if(count==5)
{
j++;
b[j]=0;
}i=k;
}
}
else
{
b[j]=a[i];
}
i++;
j++;
}
printf("Frames after stuffing is : \n");
for(i=0;i<j;i++)
printf("%d",b[i]);
return 0;
}
3.CRC
#include<stdio.h>
char data[20],div[20],temp[4],total[100];
int i,j,datalen,divlen,len,flag=1;
void check();
int main()
{
printf("Enter the total bit of data:");
scanf("%d",&datalen);
printf("\nEnter the total bit of divisor");
scanf("%d",&divlen);
len=datalen+divlen-1;
printf("\nEnter the data:");
scanf("%s",&data);
printf("\nEnter the divisor");
scanf("%s",div);
for(i=0;i<datalen;i++)
{
total[i]=data[i];
temp[i]=data[i];
}
for(i=datalen;i<len;i++)
total[i]='0';
check();
for(i=0;i<divlen;i++)
temp[i+datalen]=data[i];
printf("\ntransmitted Code Word:%s",temp);
printf("\n\nEnter the received code word:");
scanf("%s",total);
check();
for(i=0;i<divlen-1;i++)
if(data[i]=='1')
{
flag=0;
break;
}
if(flag==1)
printf("\nsuccessful!!");
else
printf("\nreceived code word contains errors...\n");
}
void check()
{
for(j=0;j<divlen;j++)
data[j]=total[j];
while(j<=len)
{
if(data[0]=='1')
for(i = 1;i <divlen ; i++)
data[i] = (( data[i] == div[i])?'0':'1');
for(i=0;i<divlen-1;i++)
data[i]=data[i+1];
data[i]=total[j++];
}
}
#include <stdio.h>
int main()
{
int i, w, f, frames[50];
printf("Enter the window size");
scanf("%d", &w);
printf("\n Enter the number of frames to be transmitted");
scanf("%d", &f);
printf("\n Enter the %d frames", f);
for (i = 1; i<= f; i++)
scanf("%d", &frames[i]);
printf("\n With sliding window protocol the frames will be sent in following manner (assuming no
corruotion of frames)\n\n");
printf("After sending %d frames at each stages sender waits for acknowledgeent sent by receiver\n\
n", w);
for (i = 1; i<= f; i++)
{
if (i % w == 0)
{
printf("%d", frames[i]);
printf("Acknowledgement of abouve frames sent is received by sender\n\n");
}
else
printf("%d", frames[i]);
}
if (f % w != 0)
printf("Acknowledgement of above frames sent is received by sender\n\n");
return 0;
}
5.DIJIKSTRAS ALGORITHM
# include <stdio.h>
#define INFINITY 9999
#define MAX 10
printf("\n\n");
OPERATING SYSTEMS
WEEK 1
7.FCFS
#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);
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;
#include <stdio.h>
#include <string.h>
//shortest job first ............................................
struct process{
int pid,bt,wt,tt;
}p[10],temp;
int main(void) {
// your code goes here
int i,j,n,totwt,tottt,a1,a2;
}
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(p[i].bt>p[j].bt)
{
temp.pid=p[i].pid;
p[i].pid=p[j].pid;
p[j].pid=temp.pid;
temp.bt=p[i].bt;
p[i].bt=p[j].bt;
p[j].bt=temp.bt;
}
}
}
p[1].wt=0;
p[1].tt=p[1].bt+p[1].wt;
i=2;
while(i<=n)
{
p[i].wt=p[i-1].bt+p[i-1].wt;
p[i].tt=p[i].bt+p[i].wt;
i++;
}
i=1;
totwt=tottt=0;
printf("\n processid\tbt\twt\ttt\n");
while(i<=n){
printf("\n %d\t%d\t%d\t%d\n",p[i].pid,p[i].bt,p[i].wt,p[i].tt);
totwt=p[i].wt+totwt;
tottt=p[i].tt+tottt;
i++;
}
a1=totwt/n;
a2=tottt/n;
printf("\n avgwt=%d avgtt=%d\n",a1,a2);
return 0;
}
9.PRIORITY SCHEDULING
#include<stdio.h>
#include<math.h>
struct Job {
int no;
int bt,wt,tat;
int priority;
};
int main()
{
int n,i,j,k;
int com[100]={0};
struct Job J[100];
printf("Enter the number of Jobs");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter Burst Time and priority of Job %d",(i+1));
J[i].no=i+1;
scanf("%d%d",&J[i].bt,&J[i].priority);
}
int current=0;
int completed=0;
int time=0;
while(completed<n)
{
current=n+1;
k=n+1;
for(i=0;i<n;i++)
{
if(com[i]==0 && k>J[i].priority)
{
k=J[i].priority;
current=i;
}
}
if(current!=n+1)
{
i=current;
com[current]=1;
J[i].wt=time;
J[i].tat=time+J[i].bt;
time=time+J[i].bt;
completed++;
}
else
{
break;
}
}
int sumwt,sumtat;
sumwt=0;
sumtat=0;
printf("Job no, Burst Time, Waiting Time, Turn Around Time, Priority\n");
for(i=0;i<n;i++)
{
printf("%d %d %d %d %d\n",J[i].no,J[i].bt,J[i].wt,J[i].tat,J[i].priority);
sumwt=sumwt+J[i].wt;
sumtat=sumtat+J[i].tat;
}
float avgwt,avgtat;
avgwt=sumwt;
avgtat=sumtat;
avgwt=avgwt/n;
avgtat=avgtat/n;
printf("The average waiting time is %fsec\n and the average turn around time is %fsec\
n",avgwt,avgtat);
return 0;
}
10 ROUND ROBIN
#include <stdio.h>
#include <string.h>
//round robin based...........................................
struct process{
int pid,bt,wt,tt;
}p[10],x[10];
int main(void) {
// your code goes here
int i,j,n,tot=0,m,k;
float wttime=0.0,tottime=0.0,a1,a2;
WEEK 3
11 PAGING
#include<stdio.h>
int main()
{
scanf("%d",&ms);
scanf("%d",&ps);
nop = ms/ps;
printf("\nThe no. of pages available in memory are -- %d ",nop);
rempages = nop;
for(i=1;i<=np;i++)
scanf("%d",&s[i]);
if(s[i] >rempages)
{
printf("\nMemory is Full");
break;
}
rempages = rempages - s[i];
scanf("%d",&fno[i][j]);
else
pa=fno[x][y]*ps+offset;
printf("\nThe Physical Address is -- %d",pa);
}
12 SEGMENTATION
#include <stdio.h>
#include <math.h>
int sost;
void gstinfo();
void ptladdr();
struct segtab
{
int sno;
int baddr;
int limit;
int val[10];
}st[10];
void gstinfo()
{
int i,j;
printf("\n\tEnter the size of the segment table: ");
scanf("%d",&sost);
for(i=1;i<=sost;i++)
{
printf("\n\tEnter the information about segment: %d",i);
st[i].sno = i;
void ptladdr()
{
int i,swd,d=0,n,s,disp,paddr;
s = swd / pow(10,d-1);
disp = swd % (int)pow(10,d-1);
if(s<=sost)
{
if(disp<st[s].limit)
{
paddr = st[s].baddr + disp;
printf("\n\t\tLogical Address is: %d",swd);
printf("\n\t\tMapped Physical address is: %d",paddr);
printf("\n\tThe value is: %d",( st[s].val[disp] ) );
}
else
printf("\n\t\tLimit of segment %d is high\n\n",s);
}
else
printf("\n\t\tInvalid Segment Address \n");
}
void main()
{
char ch;
gstinfo();
do
{
ptladdr();
printf("\n\t Do U want to Continue(Y/N)");
// flushall();
scanf("%c",&ch);
}while (ch == 'Y' || ch == 'y' );
}
Week 5
13 sequential file allocattion
struct fileTable
{
char name[20];
int sb,nob;
}ft[30];
void main()
{
int i,j,n;
char s[20];
printf("Enter no of files :");
scanf("%d",&n);
for(int i=0;i<n;i++)
{
printf("\n Enter the file name %d",i+1);
scanf("%s",&ft[i].name);
printf("Enter starting block of file %d",i+1);
scanf("%d",&ft[i].sb);
printf("Enter no of blocks in file %d",i+1);
scanf("%d",&ft[i].nob);
}
printf("Enter the file name to be searched--");
scanf("%s",s);
for(int i=0;i<n;i++)
{
if(strcmp(s,ft[i].name)==0)
{
printf("\nFILE NAME\t START BLOCK\t NO.OF.BLOCKS\t OCCUPIED\n");
printf("%s\t\t\t %d\t\t\t\t %d\t\t\t\t ",ft[i].name,ft[i].sb,ft[i].nob);
for(int j=0;j<ft[i].nob;j++)
{
printf("%d ",ft[i].sb+j);
}
}
else if(i==n)
printf("File not found\n");
}
}
14 Linked list file allocation
struct fileTable
{
char name[20];
int nob;
struct block *sb;
}ft[30];
struct block
{
int bno;
struct block *next;
};
void main()
{
int i,j,n;
char s[20];
struct block *temp;
printf("Enter no of files :");
scanf("%d",&n);
for(int i=0;i<n;i++)
{
printf("\n Enter the file name %d",i+1);
scanf("%s",&ft[i].name);
printf("Enter no of blocks in file %d",i+1);
scanf("%d",&ft[i].nob);
ft[i].sb=(struct block*)malloc(sizeof(struct block));
temp=ft[i].sb;
printf("Enter blocks of file :");
scanf("%d",&temp->bno);
temp->next=NULL;
for(int j=1;j<ft[i].nob;j++)
{
temp->next=(struct block*)malloc(sizeof(struct block));
temp=temp->next;
scanf("%d",&temp->bno);
}
temp->next=NULL;
}
printf("Enter the file name to be searched--");
scanf("%s",&s);
for(int i=0;i<n;i++)
{
if(strcmp(s,ft[i].name)==0)
{
printf("\nFILE NAME\t NO.OF.BLOCKS\t OCCUPIED\n");
printf("%s\t\t\t %d\t\t\t\t ",ft[i].name);
temp=ft[i].sb;
for(int j=0;j<ft[i].nob-1;j++)
{
printf("%d->",temp->bno);
temp=temp->next;
}
}
else if(i==n)
printf("File not found\n");
}
}
15 Indexed file allocation
void main()
{
int i,j,n;
char s[20];
printf("Enter no of files :");
scanf("%d",&n);
for(int i=0;i<n;i++)
{
printf("\n Enter the file name %d",i+1);
scanf("%s",&ft[i].name);
printf("Enter no of blocks in file %d",i+1);
scanf("%d",&ft[i].nob);
printf("Enter the blocks of file :");
for(j=0;j<ft[i].nob;j++)
{
scanf("%d",&ft[i].blocks[j]);
}
}
printf("Enter the file name to be searched--");
scanf("%s",s);
for(int i=0;i<n;i++)
{
if(strcmp(s,ft[i].name)==0)
{
printf("\nFILE NAME\t NO.OF.BLOCKS\t OCCUPIED\n");
printf("%s\t\t\t %d\t\t\t\t ",ft[i].name,ft[i].nob);
for(j=0;j<ft[i].nob-1;j++)
{
printf("%d,",ft[i].blocks[j]);
}
printf("%d",ft[i].blocks[ft[i].nob-1]);
}
else if(i==n)
printf("File not found\n");
}
16. //week-6
//FCFS
// Online C compiler to run C program online
#include <stdio.h>
int main() {
int n;
// printf("enter the np of tracks");
// scanf("%d",&n);
int p,s[100],a;
printf("enter the starting pos and no of tracks to traverse\n");
scanf("%d%d",&p,&a);
printf("enter the tracks:");
int dummy=p,c=0,b=0;
for(int i=0;i<a;i++)
{
scanf("%d",&s[i]);
if(s[i]-dummy<0)
{
c=(-1)*(s[i]-dummy);
}
else
{
c=s[i]-dummy;
}
dummy=s[i];
printf("Tracks traversed:%d \t difference is:%d\n",s[i],c);
b+=c;
17. //SCAN
// Online C compiler to run C program online
#include <stdio.h>
int main() {
int n;
printf("enter the no of tracks");
scanf("%d",&n);
int p,s[100],a;
printf("enter the starting pos and no of tracks to traverse\n");
scanf("%d%d",&p,&a);
printf("enter the tracks:");
int dummy=p,c=0,b=0;
for(int i=0;i<a;i++)
{
scanf("%d",&s[i]);
}
//sorting
for(int i=0;i<a;i++)
{
for(int j=0;j<a;j++)
{
if(s[i]<s[j])
{
int temp=s[i];
s[i]=s[j];
s[j]=temp;
}
}
}
//traversing towards right first;
for(int i=0;i<a;i++)
printf("%d\n",s[i]);
for(int i=0;i<a;i++)
{
if(s[i]>p)
{
dummy=s[i];
c=i;
break;
}
b+=dummy-p;
int d=dummy-p;
for(int i=c;i<a;i++)
{
printf("traversing:%d ",s[i]);
printf("diff :%d\n",d);
dummy=s[i];
d=s[i+1]-s[i];
b+=d;
}
b+=n-s[a-1];//in scan until last track is traversed
d=s[a-1];
for(int i=c-1;i>=0;i--)
{printf("traversing:%d ",s[i]);
printf("diff :%d\n ",d-s[i]);
b+=d-s[i];
d=s[i];
int main() {
int n;
printf("enter the no of tracks");
scanf("%d",&n);
int p,s[100],a;
printf("enter the starting pos and no of tracks to traverse\n");
scanf("%d%d",&p,&a);
printf("enter the tracks:");
int dummy=p,c=0,b=0;
for(int i=0;i<a;i++)
{
scanf("%d",&s[i]);
}
//sorting
for(int i=0;i<a;i++)
{
for(int j=0;j<a;j++)
{
if(s[i]<s[j])
{
int temp=s[i];
s[i]=s[j];
s[j]=temp;
}
}
}
//traversing towards right first;
for(int i=0;i<a;i++)
printf("%d\n",s[i]);
for(int i=0;i<a;i++)
{
if(s[i]>p)
{
dummy=s[i];
c=i;
break;
}
b+=dummy-p;
int d=dummy-p;
for(int i=c;i<a;i++)
{
printf("traversing:%d ",s[i]);
printf("diff :%d\n",d);
dummy=s[i];
d=s[i+1]-s[i];
b+=d;
}
b+=n;
//in cscan until last track is traversed and
//again until the first track is traversed
d=0;
for(int i=0;i<c;i++)
{printf("traversing:%d ",s[i]);
printf("diff :%d\n ",s[i]-d);
b+=s[i]-d;
d=s[i];