Computer Networks
Computer Networks
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>
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
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