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

Computer Networks

The document discusses several algorithms: 1. Character stuffing takes a string as input and inserts extra characters to avoid long runs of the same character. 2. Bit stuffing takes a binary frame as input and inserts extra 0s to avoid long runs of 1s. 3. CRC (Cyclic Redundancy Check) is used to detect errors in data transmission by calculating a checksum and adding it to the end of a message. 4. Sliding window protocol controls the flow of data between sender and receiver by using a window size and waiting for acknowledgements.

Uploaded by

kavyasri phani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

Computer Networks

The document discusses several algorithms: 1. Character stuffing takes a string as input and inserts extra characters to avoid long runs of the same character. 2. Bit stuffing takes a binary frame as input and inserts extra 0s to avoid long runs of 1s. 3. CRC (Cyclic Redundancy Check) is used to detect errors in data transmission by calculating a checksum and adding it to the end of a message. 4. Sliding window protocol controls the flow of data between sender and receiver by using a window size and waiting for acknowledgements.

Uploaded by

kavyasri phani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

1. 1.

CHARACTER STUFFING
2. #include <stdio.h>
3. #include<string.h>
4.
5. int main(void) {
6.
7. int i=0,j,n;
8. char a[30],b[50];
9. scanf("%s",&a);
10. n=strlen(a);
11. b[0]='D';
12. b[1]='L';
13. b[2]='E';
14. b[3]='S';
15. b[4]='T';
16. b[5]='X';
17. j=6;
18. while(i<n)
19. {
20. if(a[i]=='D' && a[i+1]=='L' && a[i+2]=='E')
21. {
22. b[j]='D';
23. b[j+1]='L';
24. b[j+2]='E';
25. j=j+3;
26. }
27. b[j]=a[i];
28. j++;
29. i++;
30. }
31. b[j]='D';
32. b[j+1]='L';
33. b[j+2]='E';
34. b[j+3]='E';
35. b[j+4]='T';
36. b[j+5]='X';
37. b[j+6]='\0';
38. printf("%s",b);
39. return 0;
40. }
41.
1. 2.BIT STUFFINg
2. #include<stdio.h>
3. int main()
4. {
5.
6. int i,j,k,count,n,a[20],b[30];
7. printf("Enter the frame size");
8. scanf("%d",&n);
9. printf("Enter the frame(in 0s and 1s)\n");
10. for(int i=0;i<n;i++)
11. scanf("%d",&a[i]);
12.
13. i=0;
14. j=0;
15. count=1;
16. while(i<n)
17. {
18. if(a[i]==1)
19. {
20. b[j]=a[i];
21. for(k=i+1;k<n && count<5 && a[k]==1;k++)
22. {
23. j++;
24. b[j]=a[k];
25. count++;
26.
27. if(count==5)
28. {
29. j++;
30. b[j]=0;
31. }i=k;
32. }
33. }
34. else
35. {
36. b[j]=a[i];
37. }
38. i++;
39. j++;
40. }
41. printf("Frames after stuffing is : \n");
42. for(i=0;i<j;i++)
43. printf("%d",b[i]);
44. return 0;
45. }
46. 3.CRC
47. 3.CRC

#include<stdio.h>

48. char data[20],div[20],temp[4],total[100];


49. int i,j,datalen,divlen,len,flag=1;
50. void check();
51. int main()
52. {
53. printf("Enter the total bit of data:");
54. scanf("%d",&datalen);
55. printf("\nEnter the total bit of divisor");
56. scanf("%d",&divlen);
57. len=datalen+divlen-1;
58. printf("\nEnter the data:");
59. scanf("%s",&data);
60. printf("\nEnter the divisor");
61. scanf("%s",div);
62.
63. for(i=0;i<datalen;i++)
64. {
65. total[i]=data[i];
66. temp[i]=data[i];
67. }
68. for(i=datalen;i<len;i++)
69. total[i]='0';
70. check();
71. for(i=0;i<divlen;i++)
72. temp[i+datalen]=data[i];
73. printf("\ntransmitted Code Word:%s",temp);
74. printf("\n\nEnter the received code word:");
75. scanf("%s",total);
76. check();
77. for(i=0;i<divlen-1;i++)
78. if(data[i]=='1')
79. {
80. flag=0;
81. break;
82. }
83. if(flag==1)
84. printf("\nsuccessful!!");
85. else
86. printf("\nreceived code word contains errors...\n");
87. }
88. void check()
89. {
90. for(j=0;j<divlen;j++)
91. data[j]=total[j];
92. while(j<=len)
93. {
94. if(data[0]=='1')
95. for(i = 1;i <divlen ; i++)
96. data[i] = (( data[i] == div[i])?'0':'1');
97. for(i=0;i<divlen-1;i++)
98. data[i]=data[i+1];
99. data[i]=total[j++];
100. }
101. }
1. 4.SLIDING WINDOW PROTOCOL
2.
3. #include <stdio.h>
4. int main()
5. {
6. int i, w, f, frames[50];
7. printf("Enter the window size");
8. scanf("%d", &w);
9. printf("\n Enter the number of frames to be transmitted");
10. scanf("%d", &f);
11. printf("\n Enter the %d frames", f);
12. for (i = 1; i<= f; i++)
13. scanf("%d", &frames[i]);
14. printf("\n With sliding window protocol the frames will be sent in following
manner (assuming no corruotion of frames)\n\n");
15. printf("After sending %d frames at each stages sender waits for
acknowledgeent sent by receiver\n\n", w);
16. for (i = 1; i<= f; i++)
17. {
18. if (i % w == 0)
19. {
20. printf("%d", frames[i]);
21. printf("Acknowledgement of abouve frames sent is received by sender\n\n");
22. }
23. else
24. printf("%d", frames[i]);
25. }
26. if (f % w != 0)
27. printf("Acknowledgement of above frames sent is received by sender\n\n");
28.
29. return 0;
30. }
1. 5.DIJIKSTRAS ALGORITHM
2.
3. # include <stdio.h>
4. #define INFINITY 9999
5. #define MAX 10
6.
7.
8. void dijkstra(int G[MAX][MAX],int n,intstartnode);
9. int main()
10. {
11. int G[MAX][MAX],i,j,n,u;
12. printf("Enter the number of vertices\n");
13. scanf("%d",&n);
14. printf("Ener the adjecency matrix \n");
15. for(i=0;i<n;i++)
16. for(j=0;j<n;j++)
17. scanf("%d",&G[i][j]);
18.
19. printf("\n Enter the starting node");
20. scanf("%d",&u);
21. dijkstra(G,n,u);
22. return 0;
23. }
24. void dijkstra(int G[MAX][MAX],int n,intstartnode)
25. {
26. int cost[MAX][MAX],distance[MAX],pred[MAX];
27. int visited[MAX],count,mindistance,nextnode,i,j;
28. for(i=0;i<n;i++)
29. for(j=0;j<n;j++)
30. if(G[i][j]==0)
31. cost[i][j]=INFINITY;
32. else
33. cost[i][j]=G[i][j];
34. for(i=0;i<n;i++)
35. {
36. distance[i]=cost[startnode][i];
37. pred[i]=startnode;
38. visited[i]=0;
39. }
40. distance[startnode]=0;
41. visited[startnode]=1;
42. count=1;
43. while(count< n-1)
44. {
45. mindistance = INFINITY;
46. for(i=0;i<n;i++)
47. if(distance[i]<mindistance&& !visited[i])
48. {
49. mindistance=distance[i];
50. nextnode=1;
51. }visited[nextnode]=1;
52. for(i=0;i<n;i++)
53. if(!visited[i])
54. if(mindistance + cost[nextnode][i]<distance[i])
55. {
56. distance[i]=mindistance+cost[nextnode][i];
57. pred[i]=nextnode;
58. }
59. count++;
60. }for(i=0;i<n;i++)
61. if(i!=startnode)
62. {
63. printf("\n Distance of node %d = %d",i,distance[i]);
64. printf("\n path= %d",i);
65. j=i;
66. do
67. {
68. j=pred[j];
69. printf("<-%d",j);
70. }
71. while(j!=startnode);
72. }
73. }
1. 6.DISTANCE VECTOR ALGORITHM
2.
3. #include<stdio.h>
4. struct node
5. {
6. unsigned dist[20];
7. unsigned from[20];
8. }rt[10];
9. int main()
10. {
11. int dmat[20][20];
12. int i,j,k,count=0,n;
13. printf("Enter the number of nodes\n");
14. scanf("%d",&n);
15. printf("\n Enter the cost matrix ;\n");
16. for(i=0;i<n;i++)
17. for(j=0;j<n;j++)
18. {
19. scanf("%d",&dmat[i][j]);
20. dmat[i][i]=0;
21. rt[i].dist[j]=dmat[i][j];
22. rt[i].from[j]=j;
23. }
24. do
25. {
26. count=0;
27. for(i=0;i<n;i++)
28. for(j=0;j<n;j++)
29. for(k=0;k<n;k++)
30. if(rt[i].dist[j] >dmat[i][k]+rt[k].dist[j])
31. {
32. rt[i].dist[j]=rt[i].dist[k]+rt[k].dist[j];
33. rt[i].from[j]=k;
34. count++;
35. }
36. } while(count!=0);
37. for(i=0;i<n;i++)
38. {
39. printf("\n\n state value for router %d id \n",i+1);
40. for(j=0;j<n;j++)
41. {
42. printf("\t\n node %d via %d Distance %d
",j+1,rt[i].from[j]+1,rt[i].dist[j]);
43. }
44. }
45. printf("\n\n");}
46. Week 7
47. #include<stdio.h>
48. int main()
49. {
50. int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp;
51. float avg_wt,avg_tat;
52. printf("Enter number of process:");
53. scanf("%d",&n);
54.
55. printf("nEnter Burst Time:n");
56. for(i=0;i<n;i++)
57. {
58. printf("p%d:",i+1);
59. scanf("%d",&bt[i]);
60. p[i]=i+1;
61. }
62.
63. //sorting of burst times
64. for(i=0;i<n;i++)
65. {
66. pos=i;
67. for(j=i+1;j<n;j++)
68. {
69. if(bt[j]<bt[pos])
70. pos=j;
71. }
72.
73. temp=bt[i];
74. bt[i]=bt[pos];
75. bt[pos]=temp;
76.
77. temp=p[i];
78. p[i]=p[pos];
79. p[pos]=temp;
80. }
81.
82. wt[0]=0;
83.
84.
85. for(i=1;i<n;i++)
86. {
87. wt[i]=0;
88. for(j=0;j<i;j++)
89. wt[i]+=bt[j];
90.
91. total+=wt[i];
92. }
93.
94. avg_wt=(float)total/n;
95. total=0;
96.
97. printf("\nProcesst Burst Time tWaitingTimetTurnaround Time");
98. for(i=0;i<n;i++)
99. {
100. tat[i]=bt[i]+wt[i];
101. total+=tat[i];
102. printf("\np%d\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
103. }
104.
105. avg_tat=(float)total/n;
106. printf("nnAverage Waiting Time=%f",avg_wt);
107. printf("nAverage Turnaround Time=%fn",avg_tat);
108. }
109. 8 SJF
110.
111. #include <stdio.h>
112. #include <string.h>
113. //shortest job first ............................................
114. struct process{
115.
116. int pid,bt,wt,tt;
117. }p[10],temp;
118.
119. int main(void) {
120. // your code goes here
121. int i,j,n,totwt,tottt,a1,a2;
122.
123. printf("Enter the no of process\n");
124. scanf("%d",&n);
125. for(int i=1;i<=n;i++)
126. {
127. p[i].pid=i;
128. printf("Enter the burst time\n");
129. scanf("%d",&p[i].bt);
130.
131. }
132. for(i=1;i<=n;i++)
133. {
134. for(j=i+1;j<=n;j++)
135. {
136. if(p[i].bt>p[j].bt)
137. {
138. temp.pid=p[i].pid;
139. p[i].pid=p[j].pid;
140. p[j].pid=temp.pid;
141. temp.bt=p[i].bt;
142. p[i].bt=p[j].bt;
143. p[j].bt=temp.bt;
144. }
145. }
146. }
147. p[1].wt=0;
148. p[1].tt=p[1].bt+p[1].wt;
149. i=2;
150. while(i<=n)
151. {
152. p[i].wt=p[i-1].bt+p[i-1].wt;
153. p[i].tt=p[i].bt+p[i].wt;
154. i++;
155.
156. }
157. i=1;
158. totwt=tottt=0;
159. printf("\n processid\tbt\twt\ttt\n");
160. while(i<=n){
161.
162. printf("\n %d\t%d\t%d\t%d\n",p[i].pid,p[i].bt,p[i].wt,p[i].tt);
163. totwt=p[i].wt+totwt;
164. tottt=p[i].tt+tottt;
165. i++;
166.
167. }
168. a1=totwt/n;
169. a2=tottt/n;
170. printf("\n avgwt=%d avgtt=%d\n",a1,a2);
171.
172.
173. return 0;
174. }
175.
176.
177.
178.
179.
180.
181.
182.
183.
184.
185.
186.
187.
188.
189.
190.
191.
192.
193.
194.
195.
196.
197.
198.
199.
200.
9.PRIORITY SCHEDULING

201. #include<stdio.h>
202. #include<math.h>
203. struct Job {
204. int no;
205. int bt,wt,tat;
206. int priority;
207. };
208. int main()
209. {
210. int n,i,j,k;
211. int com[100]={0};
212. struct Job J[100];
213. printf("Enter the number of Jobs");
214. scanf("%d",&n);
215. for(i=0;i<n;i++)
216. {
217. printf("Enter Burst Time and priority of Job %d",(i+1));
218. J[i].no=i+1;
219. scanf("%d%d",&J[i].bt,&J[i].priority);
220. }
221. int current=0;
222. int completed=0;
223. int time=0;
224. while(completed<n)
225. {
226. current=n+1;
227. k=n+1;
228. for(i=0;i<n;i++)
229. {
230. if(com[i]==0 && k>J[i].priority)
231. {
232. k=J[i].priority;
233. current=i;
234. }
235. }
236. if(current!=n+1)
237. {
238. i=current;
239. com[current]=1;
240. J[i].wt=time;
241. J[i].tat=time+J[i].bt;
242. time=time+J[i].bt;
243. completed++;
244. }
245. else
246. {
247. break;
248. }
249. }
250. int sumwt,sumtat;
251. sumwt=0;
252. sumtat=0;
253. printf("Job no, Burst Time, Waiting Time, Turn Around Time,
Priority\n");
254. for(i=0;i<n;i++)
255. {
256. printf("%d %d %d %d
%d\n",J[i].no,J[i].bt,J[i].wt,J[i].tat,J[i].priority);
257. sumwt=sumwt+J[i].wt;
258. sumtat=sumtat+J[i].tat;
259. }
260. float avgwt,avgtat;
261. avgwt=sumwt;
262. avgtat=sumtat;
263. avgwt=avgwt/n;
264. avgtat=avgtat/n;
265. printf("The average waiting time is %fsec\n and the average turn
around time is %fsec\n",avgwt,avgtat);
266. return 0;
267. }
268.
269.
270.
271.
272.
273.
274.
275.
276.
277.
278.
279.
280.
281.
282.
283.
284.
285.
286.
287.
10 ROUND ROBIN

288.
289. #include <stdio.h>
290. #include <string.h>
291. //round robin based...........................................
292. struct process{
293.
294. int pid,bt,wt,tt;
295. }p[10],x[10];
296.
297. int main(void) {
298. // your code goes here
299. int i,j,n,tot=0,m,k;
300. float wttime=0.0,tottime=0.0,a1,a2;
301.
302. printf("Enter the no of process\n");
303. scanf("%d",&n);
304. for(int i=1;i<=n;i++)
305. {
306. x[i].pid=i;
307. printf("Enter the burst time\n");
308. scanf("%d",&x[i].bt);
309. tot=tot+x[i].bt;
310. }
311. printf("Total burst time : %d",tot);
312. p[0].tt=0;
313. k=1;
314. printf("\nEnter time slice : ");
315. scanf("%d",&m);
316. for(j=1;j<=tot;j++)
317. {
318. for(i=1;i<=n;i++)
319. {
320. if(x[i].bt !=0)
321. {
322. p[k].pid=i;
323. if(x[i].bt-m <0)
324. {
325. p[k].wt=p[k-1].tt;
326. p[k].bt=x[i].bt;
327. p[k].tt=p[k].wt+x[i].bt;
328. x[i].bt=0;
329. k++;
330. }
331. else
332. {
333. p[k].wt=p[k-1].tt;
334. p[k].tt=p[k].wt+m;
335. x[i].bt=x[i].bt-m;
336. k++;
337. }
338. }
339. }
340. }
341. printf("\n Process_id\t Wt\t Tt");
342. for(i=1;i<=k;i++)
343. {
344. printf("\n%d\t%d\t%d",p[i].pid,p[i].wt,p[i].tt);
345. wttime=wttime+p[i].wt;
346. tottime=tottime+p[i].tt;
347. a1=wttime/n;
348. a2=tottime/n;
349. }
350. printf("\nAvg Waiting Time :\t%f ",a1);
351. printf("\nAvg Turn Around Time :\t%f ",a2);
352. return 0;
353. }
354.
355.
356.
357.
358.
359.
360.
361.
362.
363.
364.
365.
366.
367.
368.
369.
370.
371.
372.
373.
374.
375.
376.
377.
378. WEEK 3
379. 11 PAGING
380. #include<stdio.h>
381. int main()
382. {
383. int ms, ps, nop, np, rempages, i, j, x, y, pa, offset;
384. int s[10], fno[10][20];
385. printf("\nEnter the memory size -- ");
386.
387. scanf("%d",&ms);
388.
389. printf("\nEnter the page size -- ");
390.
391. scanf("%d",&ps);
392.
393. nop = ms/ps;
394. printf("\nThe no. of pages available in memory are -- %d ",nop);
395.
396. printf("\nEnter number of processes -- ");
397. scanf("%d",&np);
398.
399. rempages = nop;
400.
401. for(i=1;i<=np;i++)
402.
403. {
404.
405. printf("\nEnter no. of pages required for p[%d]-- ",i);
406.
407. scanf("%d",&s[i]);
408.
409. if(s[i] >rempages)
410. {printf("\nMemory is Full");
411.
412. break;}
413. rempages = rempages - s[i];
414.
415. printf("\nEnterpagetable for p[%d] --- ",i);
416. for(j=0;j<s[i];j++)
417.
418. scanf("%d",&fno[i][j]);
419.
420. }
421.
422.
423.
424.
425. printf("\nEnter Logical Address to find Physical Address ");
426.
427. printf("\nEnter process no. and pagenumber and offset -- ");
428.
429. scanf("%d %d %d",&x,&y, &offset);
430. if(x>np || y>=s[i] || offset>=ps)
431. printf("\nInvalid Process or Page Number or offset");
432.
433. else
434.
435. {
436.
437. pa=fno[x][y]*ps+offset;
438. printf("\nThe Physical Address is -- %d",pa);
439.
440. }
441.
442. }
443.
444.
445.
446.
447.
448.
449.
450.
451.
452.
453.
454.
455.
456.
457.
458.
459.
460.
461.
462.
463.
464.
465.
466.
467.
468.
469.
12 SEGMENTATION

470.
471. #include <stdio.h>
472. #include <math.h>
473. int sost;
474. void gstinfo();
475. void ptladdr();
476.
477. struct segtab
478. {
479. int sno;
480. int baddr;
481. int limit;
482. int val[10];
483. }st[10];
484.
485. void gstinfo()
486. {
487. int i,j;
488. printf("\n\tEnter the size of the segment table: ");
489. scanf("%d",&sost);
490.
491. for(i=1;i<=sost;i++)
492. {
493. printf("\n\tEnter the information about segment: %d",i);
494. st[i].sno = i;
495.
496.
497.
498.
499. printf("\n\tEnter the base Address: ");
500. scanf("%d",&st[i].baddr);
501. printf("\n\tEnter the Limit: ");
502. scanf("%d",&st[i].limit);
503. for(j=0;j<st[i].limit;j++)
504. {
505. printf("Enter the %d address Value: ",(st[i].baddr + j));
506. scanf("%d",&st[i].val[j]);
507. }
508. }
509. }
510.
511. void ptladdr()
512. {
513. int i,swd,d=0,n,s,disp,paddr;
514.
515. printf("\n\n\t\t\t SEGMENT TABLE \n\n");
516. printf("\n\t SEG.NO\tBASE ADDRESS\t LIMIT \n\n");
517. for(i=1;i<=sost;i++)
518. printf("\t\t%d \t\t%d\t\t%d\n\n",st[i].sno,st[i].baddr,st[i].limit);
519. printf("\n\nEnter the logical Address: ");
520. scanf("%d",&swd);
521. n=swd;
522. while (n != 0)
523. {
524. n=n/10;
525. d++;
526. }
527.
528. s = swd / pow(10,d-1);
529. disp = swd % (int)pow(10,d-1);
530.
531. if(s<=sost)
532. {
533. if(disp<st[s].limit)
534. {
535. paddr = st[s].baddr + disp;
536. printf("\n\t\tLogical Address is: %d",swd);
537. printf("\n\t\tMapped Physical address is: %d",paddr);
538. printf("\n\tThe value is: %d",( st[s].val[disp] ) );
539. }
540. else
541. printf("\n\t\tLimit of segment %d is high\n\n",s);
542. }
543.
544. else
545. printf("\n\t\tInvalid Segment Address \n");
546. }
547.
548. void main()
549. {
550. char ch;
551. gstinfo();
552. do
553. {
554. ptladdr();
555. printf("\n\t Do U want to Continue(Y/N)");
556. // flushall();
557. scanf("%c",&ch);
558. }while (ch == 'Y' || ch == 'y' );
559. }
Week 5

560. 13 sequential file allocattion


561.
562. //SEQUENTIAL FILE ALLOCATION
563. #include<stdio.h>
564.
565. struct fileTable
566. {
567. char name[20];
568. int sb,nob;
569. }ft[30];
570.
571. void main()
572. {
573. int i,j,n;
574. char s[20];
575. printf("Enter no of files :");
576. scanf("%d",&n);
577. for(int i=0;i<n;i++)
578. {
579. printf("\n Enter the file name %d",i+1);
580. scanf("%s",&ft[i].name);
581. printf("Enter starting block of file %d",i+1);
582. scanf("%d",&ft[i].sb);
583. printf("Enter no of blocks in file %d",i+1);
584. scanf("%d",&ft[i].nob);
585. }
586. printf("Enter the file name to be searched--");
587. scanf("%s",s);
588. for(int i=0;i<n;i++)
589. {
590. if(strcmp(s,ft[i].name)==0)
591. {
592. printf("\nFILE NAME\t START BLOCK\t NO.OF.BLOCKS\t OCCUPIED\n");
593. printf("%s\t\t\t %d\t\t\t\t %d\t\t\t\t
",ft[i].name,ft[i].sb,ft[i].nob);
594. for(int j=0;j<ft[i].nob;j++)
595. {
596. printf("%d ",ft[i].sb+j);
597. }
598. }
599. else if(i==n)
600. printf("File not found\n");}
601.
602. }
603.
604. 14 Linked list file allocation
605.
606. //LINKED LIST FILE ALLOCATION
607. #include<stdio.h>
608. #include<stdlib.h>
609. #include<string.h>
610.
611. struct fileTable
612. {
613. char name[20];
614. int nob;
615. struct block *sb;
616. }ft[30];
617.
618. struct block
619. {
620. int bno;
621. struct block *next;
622. };
623. void main()
624. {
625. int i,j,n;
626. char s[20];
627. struct block *temp;
628. printf("Enter no of files :");
629. scanf("%d",&n);
630. for(int i=0;i<n;i++)
631. {
632. printf("\n Enter the file name %d",i+1);
633. scanf("%s",&ft[i].name);
634. printf("Enter no of blocks in file %d",i+1);
635. scanf("%d",&ft[i].nob);
636. ft[i].sb=(struct block*)malloc(sizeof(struct block));
637. temp=ft[i].sb;
638. printf("Enter blocks of file :");
639. scanf("%d",&temp->bno);
640. temp->next=NULL;
641. for(int j=1;j<ft[i].nob;j++)
642. {
643. temp->next=(struct block*)malloc(sizeof(struct block));
644. temp=temp->next;
645. scanf("%d",&temp->bno);
646. }
647. temp->next=NULL;
648. }
649. printf("Enter the file name to be searched--");
650. scanf("%s",&s);
651. for(int i=0;i<n;i++)
652. {
653. if(strcmp(s,ft[i].name)==0)
654. {
655. printf("\nFILE NAME\t NO.OF.BLOCKS\t OCCUPIED\n");
656. printf("%s\t\t\t %d\t\t\t\t ",ft[i].name);
657. temp=ft[i].sb;
658. for(int j=0;j<ft[i].nob-1;j++)
659. {
660. printf("%d->",temp->bno);
661. temp=temp->next;
662. }
663. }
664. else if(i==n)
665. printf("File not found\n");
666. }
667.
668. }
669.
670.
671.
672.
673.
674.
675.
676.
677.
678.
679.
680.
681.
682.
683.
684.
685.
686.
687.
688.
689.
690.
691.
692.
693.
694.
15 Indexed file allocation

695. #include<stdio.h>
696. #include<string.h>
697. struct fileTable
698. {
699. char name[20];
700. int nob,blocks[30];
701. }ft[30];
702.
703. void main()
704. {
705. int i,j,n;
706. char s[20];
707. printf("Enter no of files :");
708. scanf("%d",&n);
709. for(int i=0;i<n;i++)
710. {
711. printf("\n Enter the file name %d",i+1);
712. scanf("%s",&ft[i].name);
713. printf("Enter no of blocks in file %d",i+1);
714. scanf("%d",&ft[i].nob);
715. printf("Enter the blocks of file :");
716. for(j=0;j<ft[i].nob;j++)
717. {
718. scanf("%d",&ft[i].blocks[j]);
719. }
720. }
721. printf("Enter the file name to be searched--");
722. scanf("%s",s);
723. for(int i=0;i<n;i++)
724. {
725. if(strcmp(s,ft[i].name)==0)
726. {
727. printf("\nFILE NAME\t NO.OF.BLOCKS\t OCCUPIED\n");
728. printf("%s\t\t\t %d\t\t\t\t ",ft[i].name,ft[i].nob);
729. for(j=0;j<ft[i].nob-1;j++)
730. {
731. printf("%d,",ft[i].blocks[j]);
732. }
733. printf("%d",ft[i].blocks[ft[i].nob-1]);
734. }
735. else if(i==n)
736. printf("File not found\n");
737. }
738. }
16. //week-6

739. #include <stdio.h>


740. int main() {
741. int n;
742. // printf("enter the np of tracks");
743. // scanf("%d",&n);
744. int p,s[100],a;
745. printf("enter the starting pos and no of tracks to traverse\n");
746. scanf("%d%d",&p,&a);
747. printf("enter the tracks:");
748. int dummy=p,c=0,b=0;
749. for(int i=0;i<a;i++)
750. {
751. scanf("%d",&s[i]);
752. if(s[i]-dummy<0)
753. {
754. c=(-1)*(s[i]-dummy);
755. }
756. else
757. {
758. c=s[i]-dummy;
759. }
760. dummy=s[i];
761. printf("Tracks traversed:%d \t difference is:%d\n",s[i],c);
762. b+=c;
763. }
764. printf("The avg seek time is:%f\n",(float)b/a*1.0);
765. return 0;
766. }
767.
768.
769.
770.
771.
772.
773.
774.
775.
776.
777.
778.
779.
780.
781.
782.
783. 17. //SCAN
784. // Online C compiler to run C program online
785. #include <stdio.h>
786.
787. int main() {
788. int n;
789. printf("enter the no of tracks");
790. scanf("%d",&n);
791. int p,s[100],a;
792. printf("enter the starting pos and no of tracks to traverse\n");
793. scanf("%d%d",&p,&a);
794. printf("enter the tracks:");
795. int dummy=p,c=0,b=0;
796. for(int i=0;i<a;i++)
797. {
798. scanf("%d",&s[i]);
799.
800. }
801. //sorting
802. for(int i=0;i<a;i++)
803. {
804. for(int j=0;j<a;j++)
805. {
806. if(s[i]<s[j])
807. {
808. int temp=s[i];
809. s[i]=s[j];
810. s[j]=temp;
811. }
812. }
813. }
814. //traversing towards right first;
815. for(int i=0;i<a;i++)
816. printf("%d\n",s[i]);
817. for(int i=0;i<a;i++)
818. {
819. if(s[i]>p)
820. {
821. dummy=s[i];
822. c=i;
823. break;
824. }
825.
826. }
827.
828. b+=dummy-p;
829. int d=dummy-p;
830. for(int i=c;i<a;i++)
831. {
832. printf("traversing:%d ",s[i]);
833. printf("diff :%d\n",d);
834. dummy=s[i];
835. d=s[i+1]-s[i];
836. b+=d;
837. }
838. b+=n-s[a-1];//in scan until last track is traversed
839. d=s[a-1];
840. for(int i=c-1;i>=0;i--)
841. {printf("traversing:%d ",s[i]);
842. printf("diff :%d\n ",d-s[i]);
843.
844. b+=d-s[i];
845. d=s[i];
846.
847. }
848.
849. printf("The avg seek time is:%f\n",(float)b/a*1.0);
850. return 0;
851. }
852.
853.
854.
855.
856.
857.
858.
859.
860.
861.
862.
863.
864.
865.
866.
867.
868.
869.
870.
871.
872.
873.
874.
875. 18. //CSCAN(circular scan)
876.
877. // Online C compiler to run C program online
878. #include <stdio.h>
879.
880. int main() {
881. int n;
882. printf("enter the no of tracks");
883. scanf("%d",&n);
884. int p,s[100],a;
885. printf("enter the starting pos and no of tracks to traverse\n");
886. scanf("%d%d",&p,&a);
887. printf("enter the tracks:");
888. int dummy=p,c=0,b=0;
889. for(int i=0;i<a;i++)
890. {
891. scanf("%d",&s[i]);
892.
893. }
894. //sorting
895. for(int i=0;i<a;i++)
896. {
897. for(int j=0;j<a;j++)
898. {
899. if(s[i]<s[j])
900. {
901. int temp=s[i];
902. s[i]=s[j];
903. s[j]=temp;
904. }
905. }
906. }
907. //traversing towards right first;
908. for(int i=0;i<a;i++)
909. printf("%d\n",s[i]);
910. for(int i=0;i<a;i++)
911. {
912. if(s[i]>p)
913. {
914. dummy=s[i];
915. c=i;
916. break;
917. }
918.
919. }
920.
921. b+=dummy-p;
922. int d=dummy-p;
923. for(int i=c;i<a;i++)
924. {
925. printf("traversing:%d ",s[i]);
926. printf("diff :%d\n",d);
927. dummy=s[i];
928. d=s[i+1]-s[i];
929. b+=d;
930. }
931. b+=n;
932. //in cscan until last track is traversed and
933. //again until the first track is traversed
934. d=0;
935. for(int i=0;i<c;i++)
936. {printf("traversing:%d ",s[i]);
937. printf("diff :%d\n ",s[i]-d);
938.
939. b+=s[i]-d;
940. d=s[i];
941.
942. }
943.
944. printf("The avg seek time is:%f\n",(float)b/a*1.0);
945. return 0;
946. }

You might also like