You are on page 1of 26

EXNO: DATE:

READERS WRITERS PROBLEM

AIM: To write a C program for illustrating the readers-writers problem. ALGORITHM: 1. Get the option from user A 2. Get the option from user B 3. If both the users wants to read , simply read the contents from the file. 4. If one users wants to read and the other one wants to write, it is possible for one user to read and the other to write. 5. If both the users want to write, it is not possible to write the contents of both files at the same time. 6. Stop the program.

PROGRAM CODE: #include<stdio.h> #include<conio.h> #include<string.h> void readfile(); void writefile(); int a,b,i=0; char c[40]; char buffer[89]; char *g; FILE *fr,*fw; void main() { clrscr(); a: printf("\n1.READ 2.WRITE"); printf("\n USERA enter your choice"); scanf("%d",&a); printf("\n USERB enter your choice"); scanf("%d",&b); if(a==1 & b==1) {readfile();} else if(a==1 & b==2) { writefile(); readfile(); } else if(a==2 & b==1) { readfile(); writefile(); } else if(a==2 & b==2) { printf("Both user are not allowed to write"); goto a; } getch(); }

void readfile() { fr=fopen("filename.c","r"); printf("\n File content is:"); while(!feof(fr)) { c[i]=(char)fgetc(fr); printf("%c",c[i]); i++; } fclose(fr); } void writefile() { printf("Enter your content to write:\n"); buffer[0]=100; g=cgets(buffer); fw=fopen("filename.c","w"); fwrite(g,strlen(g),1,fw); fclose(fw); }

OUTPUT: Output 1: 1.READ 2.WRITE USERA enter your choice1 USERB enter your choice1 File content is:IT Output 2: 1.READ 2.WRITE USERA enter your choice1 USERB enter your choice2 Enter your content to write: ME CC 2009 Output 3: 1.READ 2.WRITE USERA enter your choice2 USERB enter your choice1 File content is:ME CC 2009 Enter your content to write: computer communication lab File content is:ME CC 2009

Output 4: 1.READ 2.WRITE USERA enter your choice2 USERB enter your choice2 Both user are not allowed to write 1.READ 2.WRITE USERA enter your choice1 USERB enter your choice1 File content is: computer communication lab

RESULT: Thus the C program for readers writers problem was written and executed successfully.

EXNO: DATE:

LEXICAL ANALYSER

AIM: To write a C program to implement a lexical analyzer. ALGORITHM: 1. 2. 3. 4. Start the program. Input the statement with a $ symbol at the end . Identify the tokens. Display the keywords, operators, constants, variables and punctuations thereby separating the tokens. 5. Stop the program.

PROGRAM CODE: #include<stdio.h> #include<conio.h> #include<math.h> #include<ctype.h> #include<string.h> FILE *fp; char c; char s[10],k[10][10],o[10][10]; char li[10][10],v[10][10],cn[10][10],la[10][10]; int f=0,m=0,i=0,l=0,n=0,z=0,t=0,a=0,j=0; void kw(char s[10]) { strcpy(k[i],s); i++; } void op(char s[10]) { strcpy(o[m],s); m++; } void de(char s[10]) { strcpy(li[t],s); t++; } void con(char s[10]) { strcpy(cn[j],s); j++; } void var(char s[10]) { strcpy(v[l],s); l++; } void lab(char s[10]) { strcpy(la[n],s);

n++; } void check() { char c; char s[10]; printf("Enter the statement and terminate with $ symbol"); c=getchar(); fp=fopen("detail.c","w+"); while(c!='$') { fprintf(fp,"%c",c); c=getchar(); } rewind(fp); while(!feof(fp)) { fscanf(fp,"%s",s); if(strcmp(s,"if")==0)kw(s); else if(strcmp(s,"then")==0)kw(s); else if(strcmp(s,"main")==0)kw(s); else if(strcmp(s,"int")==0)kw(s); else if(strcmp(s,"char")==0)kw(s); else if(strcmp(s,"else")==0)kw(s); else if(strcmp(s,"void")==0)kw(s); else if(strcmp(s,"while")==0)kw(s); else if(strcmp(s,"do")==0)kw(s); else if(strcmp(s,"printf")==0)kw(s); else if(strcmp(s,"scanf")==0)kw(s); else if(strcmp(s,"++")==0)op(s); else if(strcmp(s,">=")==0)op(s); else if(strcmp(s,"=")==0)op(s); else if(strcmp(s,"*")==0)op(s); else if(strcmp(s,"$$")==0)op(s); else if(strcmp(s,"(")==0)de(s); else if(strcmp(s,"}")==0)de(s); else if(strcmp(s,";")==0)de(s); else if(isdigit(s[0]))con(s); else var(s); }

fclose(fp); } void main() { clrscr(); check(); printf("LEXICAL ANALYSER\n"); printf("\n keyword\t\t"); for(z=0;z<i;z++) printf("%s\t",k[z]); printf("\n operators\t\t"); for(z=0;z<m;z++) printf("%s\t",o[z]); printf("\n constants \t\t"); for(z=0;z<j;z++) printf("%s\t",cn[z]); printf("\n variables \t\t"); for(z=0;z<l;z++) printf("%s\t",v[z]); printf("\n punctuations \t\t"); for(z=0;z<t;z++) printf("%s\t",li[z]); getch(); }

OUTPUT: Enter the statement and terminate with $symbol for (j=0;j<10:j++);$ LEXICAL ANALYSER Keyword Operators Constant Variables for = 0 j < 10 j ) j ; + + %

Punctuation (

RESULT: Thus the C program to implement a lexical analyzer was written and executed successfully.

EXNO: DATE:

PASS- I ASSEMBLER

AIM: To write a C program to implement Pass- I Assembler. ALGORITHM: 1. 2. 3. 4. Start the program . Open the file and get the input. Separate the operands and store it in a structure. Operands are used and the symbol name, address and length are separated. 5. The output in the symbol table is printed. 6. Stop the program.

PROGRAM CODE: #include<stdio.h> #include<conio.h> #include<string.h> struct { char mnem[20]; int len; }m[5]={{"MOV",1},{"ADI",2},{"JMP",3},{"SUB",1},{"LXI",2}}; struct { char label[20]; int laddr; }s[20]; void main() { FILE *fp; int saddr,i=0,j=0,lcnt=0; char a[100],temp[100]; clrscr(); fp=fopen("ss.c","r"); printf("ENTER THE STARTING ADDRESS"); scanf("%d",&saddr); while(!feof(fp)) { fgets(a,80,fp); i=0; j=0; while(a[i]==' ') i++; { while(a[i]!=' ') { if(a[i]==':') { temp[j]='\0'; strcpy(s[lcnt].label,temp); s[lcnt].laddr=saddr; printf("%d\t",s[lcnt].laddr);

printf("%s\t",s[lcnt].label); strcpy(temp," "); lcnt++; j=0; printf("\n%s",temp); } temp[j]=a[i]; i++; j++; } temp[j]='\0'; for(i=0;i<5;i++) { if(strcmp(temp,m[i].mnem)==0) { saddr=saddr+m[i].len; }}}} getch(); fclose(fp); }

INPUT: Start: MOV A,B ADI 10 LOOP1: JMP start LOOP2: LXI,2000 OUTPUT: ENTER THE STARTING ADDRESS 1000 start 1003 LOOP1 1006 LOOP2

RESULT: Thus the C program to implement Pass-I of an Assembler was written and executed successfully.

EXNO: DATE:

SIMULATION OF PAGING

AIM: To write a C program to implement the simulation of page replacement techniques such as best fit, worst fit , first fit etc., ALGORITHM: 1. Consider a 10x10 matrix and assume each column of that matrix as page and matrix itself as in the main memory. 2. Full elements in each page is randomly according to the user. 3. Get the elements to be placed in the main memory. 4. For best fit, place the element where more elements are placed in the page with less space according to element size. 5. For worst fit place elements where less number of elements are placed. 6. For first fit, place the elements in the first page and so on.. according to the element size. 7. Display the matrix on the screen. 8. Stop the program.

PROGRAM CODE: #include<stdio.h> #include<conio.h> void main() { int c,a[19][10],i,j,k=0,q=0,p=0,n[10],m; int b=0,op,z,size=0,size2=0,size3=0,small; clrscr(); for(i=0;i<10;i++) { for(j=0;j<10;j++) { a[i][j]=0; } } printf("SIMULATION OF PAGING"); printf("\n random filling \n max page size=10\n"); for(i=0;i<10;i++) { printf("enter the number of elements to be inserted in page %d",i+1); scanf("%d",&k); n[i]=k; } for(i=0;i<10;i++) { for(j=0;j<n[i];j++) { a[i][j]=n[i]; } } printf("the appearance of main memory \n"); for(i=0;i<10;i++) { for(j=0;j<10;j++) { printf("%d",a[j][i]); } printf("\n"); }

do { printf("\n 1.BEST FIT"); printf("\n 2.WORST FIT"); printf("\n 3.FIRST FIT"); printf("\n 4.EXIT"); printf("\n enter your option"); scanf("%d",&op); switch(op) { case 1: { printf("enter the element size"); scanf("%d",&size); for(z=10;z>0;z--) { for(i=0;i<10;i++) { if(size==(z-n[i])) { c=n[i]; for(j=(z-size);j<(size+n[j]);j++) { a[i][j]=size; p=i; } n[p]=n[p]+size; for(i=0;i<10;i++) { for(j=0;j<10;j++) { printf("%d", a[j][i]); } printf("\n"); } } } if(size==(z-c)) { break;

} } break; } case 2: { printf("enter the element size"); scanf("%d",&size2); small=n[0]; for(i=0;i<10;i++) { if(n[i]<small) { small=n[i]; b=i; } } for(j=n[b];j<(size2+n[b]);j++) { a[b][j]=size2; } for(i=0;i<10;i++) { for(j=0;j<10;j++) { printf("%d",a[j][i]); } printf("\n"); } n[b]=n[b]+size2; break; } case 3: { printf("enter the element size\n"); scanf("%d",&size3); for(i=0;i<10;i++) { if(size3<=(10-n[i])) {

for(j=n[i];j<(size3+n[i]);j++) { a[i][j]=size3; q=i; } break; } } n[q]=n[q]+size3; for(i=0;i<10;i++) { for(j=0;j<10;j++) { printf("%d",a[j][i]); } printf("\n"); } break; } } } while(op<4); }

OUTPUT: SIMULATION OF PAGING random filling max page size=10 enter the number of elements to be inserted in page 11 enter the number of elements to be inserted in page 22 enter the number of elements to be inserted in page 33 enter the number of elements to be inserted in page 44 enter the number of elements to be inserted in page 55 enter the number of elements to be inserted in page 66 enter the number of elements to be inserted in page 77 enter the number of elements to be inserted in page 88 enter the number of elements to be inserted in page 99 enter the number of elements to be inserted in page 1010 the appearance of main memory 12345678910 02345678910 00345678910 00045678910 00005678910 00000678910 00000078910 00000008910 00000000910 00000000010 1.BEST FIT 2.WORST FIT 3.FIRST FIT 4.EXIT enter your option1 enter the element size3 12345678910 02345678910 00345678910 00045678910 00005678910 00000678910 00000078910

00000038910 00000030910 00000030010 1.BEST FIT 2.WORST FIT 3.FIRST FIT 4.EXIT enter your option2 enter the element size3 12345678910 32345678910 30345678910 30045678910 00005678910 00000678910 00000078910 00000038910 00000030910 00000030010 1.BEST FIT 2.WORST FIT 3.FIRST FIT 4.EXIT enter your option3 enter the element size 3 12345678910 32345678910 30345678910 30045678910 30005678910 30000678910 30000078910 00000038910 00000030910 00000030010

1.BEST FIT 2.WORST FIT 3.FIRST FIT 4.EXIT enter your option 4 $

RESULT: Thus the program to implement the simulation of page replacement techniques was executed successfully.

You might also like