c printout.doc

Embed Size (px)

Citation preview

  • 7/27/2019 c printout.doc

    1/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 A2. Get the option from user B

    3. If both the users wants to read , simply read the contents from thefile.

    4. If one users wants to read and the other one wants to write, it ispossible for one user to read and the other to write.

    5. If both the users want to write, it is not possible to write thecontents of both files at the same time.

    6. Stop the program.

  • 7/27/2019 c printout.doc

    2/26

  • 7/27/2019 c printout.doc

    3/26

    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);}

  • 7/27/2019 c printout.doc

    4/26

    OUTPUT:

    Output 1:

    1.READ 2.WRITE

    USERA enter your choice1USERB enter your choice1

    File content is:IT

    Output 2:

    1.READ 2.WRITEUSERA enter your choice1USERB enter your choice2

    Enter your content to write:ME CC 2009

    Output 3:

    1.READ 2.WRITE

    USERA enter your choice2USERB enter your choice1

    File content is:ME CC 2009Enter your content to write:

    computer communication labFile content is:ME CC 2009

  • 7/27/2019 c printout.doc

    5/26

    Output 4:

    1.READ 2.WRITE

    USERA enter your choice2USERB enter your choice2

    Both user are not allowed to write1.READ 2.WRITE

    USERA enter your choice1USERB enter your choice1

    File content is: computer communication lab

  • 7/27/2019 c printout.doc

    6/26

    RESULT:

    Thus the C program for readers writers problem was written andexecuted successfully.

  • 7/27/2019 c printout.doc

    7/26

    EXNO:

    DATE: LEXICAL ANALYSER

    AIM:

    To write a C program to implement a lexical analyzer.

    ALGORITHM:

    1. Start the program.2. Input the statement with a $ symbol at the end .

    3. Identify the tokens.4. Display the keywords, operators, constants, variables and

    punctuations thereby separating the tokens.5. Stop the program.

  • 7/27/2019 c printout.doc

    8/26

    PROGRAM CODE:

    #include#include#include#include#includeFILE *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);

  • 7/27/2019 c printout.doc

    9/26

    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);}

  • 7/27/2019 c printout.doc

    10/26

    fclose(fp);}void main(){clrscr();check();

    printf("LEXICAL ANALYSER\n");printf("\n keyword\t\t");for(z=0;z

  • 7/27/2019 c printout.doc

    11/26

    OUTPUT:

    Enter the statement and terminate with $symbol for (j=0;j

  • 7/27/2019 c printout.doc

    12/26

    RESULT:

    Thus the C program to implement a lexical analyzer was written andexecuted successfully.

  • 7/27/2019 c printout.doc

    13/26

    EXNO:

    DATE: PASS- I ASSEMBLER

    AIM:

    To write a C program to implement Pass- I Assembler.

    ALGORITHM:

    1. Start the program .2. Open the file and get the input.

    3. Separate the operands and store it in a structure.4. 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.

  • 7/27/2019 c printout.doc

    14/26

    PROGRAM CODE:

    #include#include#includestruct{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);

  • 7/27/2019 c printout.doc

    15/26

    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

  • 7/27/2019 c printout.doc

    16/26

    INPUT:

    Start: MOV A,BADI 10

    LOOP1: JMP startLOOP2: LXI,2000

    OUTPUT:

    ENTER THE STARTING ADDRESS

    1000 start1003 LOOP11006 LOOP2

  • 7/27/2019 c printout.doc

    17/26

    RESULT:

    Thus the C program to implement Pass-I of an Assembler was writtenand executed successfully.

  • 7/27/2019 c printout.doc

    18/26

    EXNO:

    DATE: SIMULATION OF PAGING

    AIM:

    To write a C program to implement the simulation of pagereplacement techniques such as best fit, worst fit , first fit etc.,

    ALGORITHM:

    1. Consider a 10x10 matrix and assume each column of that matrix aspage 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.

  • 7/27/2019 c printout.doc

    19/26

    PROGRAM CODE:

    #include#includevoid 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

  • 7/27/2019 c printout.doc

    20/26

    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

  • 7/27/2019 c printout.doc

    21/26

    }}

    break;}case 2:{

    printf("enter the element size");scanf("%d",&size2);small=n[0];for(i=0;i

  • 7/27/2019 c printout.doc

    22/26

  • 7/27/2019 c printout.doc

    23/26

    OUTPUT:

    SIMULATION OF PAGINGrandom fillingmax page size=10

    enter the number of elements to be inserted in page 11enter the number of elements to be inserted in page 22enter the number of elements to be inserted in page 33enter the number of elements to be inserted in page 44enter the number of elements to be inserted in page 55enter the number of elements to be inserted in page 66enter the number of elements to be inserted in page 77enter the number of elements to be inserted in page 88enter the number of elements to be inserted in page 99

    enter the number of elements to be inserted in page 1010the appearance of main memory12345678910023456789100034567891000045678910000056789100000067891000000078910000000089100000000091000000000010

    1.BEST FIT2.WORST FIT3.FIRST FIT4.EXITenter your option1

    enter the element size3

    12345678910023456789100034567891000045678910000056789100000067891000000078910

  • 7/27/2019 c printout.doc

    24/26

    000000389100000003091000000030010

    1.BEST FIT2.WORST FIT3.FIRST FIT4.EXIT

    enter your option2enter the element size312345678910323456789103034567891030045678910

    000056789100000067891000000078910000000389100000003091000000030010

    1.BEST FIT2.WORST FIT3.FIRST FIT4.EXITenter your option3

    enter the element size31234567891032345678910303456789103004567891030005678910

    3000067891030000078910000000389100000003091000000030010

  • 7/27/2019 c printout.doc

    25/26

    1.BEST FIT2.WORST FIT3.FIRST FIT4.EXITenter your option 4

    $

  • 7/27/2019 c printout.doc

    26/26

    RESULT:

    Thus the program to implement the simulation of page replacementtechniques was executed successfully.