google.com, pub-4617457846989927, DIRECT, f08c47fec0942fa0 Learn to enjoy every minute of your life.Only I can change my life.

Saturday, January 26, 2019

Sys pro program for CPU scheduling : Priority algo. - Non premptive


PPRIIORITY.C



/* CPU scheduling : Priority algo. - Non premptive ................... */
// jobQ maintained as ascending order priority Q (1st job-highest priority)
#include<stdlib.h>
#include<stdio.h>
#define newnode (struct node *)malloc(sizeof(struct node))
struct node
{ int jobno,time,proty;  // priority
  struct node *next;
}*JOBQ=NULL;

int job=0;

void erase()
{ struct node *S;
  while(JOBQ!=NULL) { S=JOBQ;  JOBQ=JOBQ->next;  free(S);  }
  job=0;
}

struct node *Append(struct node *F)
{ struct node *s,*t,*s1=NULL;
  t=newnode;        t->next=NULL;
  t->jobno=++job;   t->time=0;

  do  // validation check for job time
  {  printf("\nJOB %d - CPU burst : ",job);
     fflush(stdin); scanf("%d",&t->time);
     if(t->time<=0) printf("\nJob time should be > 0");
  }while(t->time<=0);

  do  // validation check for priority range 1-5
  {  printf("\t Priority : ",job);
     fflush(stdin);  scanf("%d",&t->proty);
     if(t->proty<1 || t->proty >5) printf("\nPriority range is 1(H) to 5(L) ");
  }while(t->proty<1 || t->proty >5);

  if(F==NULL)  F=t;    // JOBQ empty, so this is 1st job
  else    // priority wise insert (acsending oreder priority)
  {  for(s=F; s!=NULL; s1=s, s=s->next)  // find position
     { if(t->proty < s->proty) break;
     }
     t->next=s;  // insert before s
     if(s1==NULL) F=t;  // added before 1st node
     else s1->next=t; // inbetween position
  }
  return F;
}

void CreateJOBQ()
{ struct node *S;   int i,n;
  printf("\nHow many jobs? "); scanf("%d",&n);
  if(n<=0) { printf("\nError : No. of Jobs should be > 0."); return; }

  if(JOBQ!=NULL) erase(JOBQ);

  for(i=1;i<=n;i++)  JOBQ=Append(JOBQ);
}

void DisplayJOBQ()  // sorted on priority
{struct node *T;
  if(JOBQ==NULL) printf("\nJOB Q is Empty!");
  else
  { printf("\n JOB  Time  Priority");
    for(T=JOBQ;T!=NULL;T=T->next) printf("\n J%d : %3d %4d",T->jobno,T->time,T->proty);
  }
}

void delFstNode() // deletes 1st job(highest priority) from JOBQ
{struct node *t;
   t=JOBQ; JOBQ=JOBQ->next;
   free(t);
}

void ExecuteJOBQ()  // Shortest Job First
{ struct node *T,*S=NULL,*t1;  int time=0; char choice;

  if(JOBQ==NULL) { printf("\nJOB Q is Empty!"); return; }

  printf("\nclock CPU   JOBQ ");
  printf("\n%3d  -idle-",time);
  for(T=JOBQ; T!=NULL; T=T->next) printf(" J%d(%d) ",T->jobno,T->time);  // print JOBQ

  while(JOBQ!=NULL)
  {
    printf("\n%3d  J%d(%d) |--",time,JOBQ->jobno,JOBQ->time); // cpu allocated
    time+=JOBQ->time; // increment clock
    delFstNode();  // job finished
    for(T=JOBQ; T!=NULL; T=T->next) printf(" J%d(%d:%d) ",T->jobno,T->proty,T->time);  // print JOBQ

    printf(" --|  Add New Job(y/n)? ");  choice=getche();
    if(choice=='y' || choice=='Y')  JOBQ=Append(JOBQ);
  }
  printf("\n%3d  -idle-",time);
}

char menu()
{ char choice='a';
  clrscr();
  printf("\n< Priority : NonPreemptive > ");
  printf("\nC: Create \nD: Display \nE: Execute \nX: exit");
  printf("\nEnter your choice : ");  choice=getche();
  return toupper(choice);
}

void main()
{
  while(1)
  { switch(menu())
    { case 'X' : erase(); exit(0);  // release memory allocated to JOBQ
      case 'C' : CreateJOBQ();  break;
      case 'D' : DisplayJOBQ(); break;
      case 'E' : ExecuteJOBQ(); break;
      default  : printf("\7 Invalid Choice!");
    }
    printf("\npress any key to continue...");    getch();
  }
}









ASMB2.C

#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<stdlib.h>

struct symtab
{
    char name[10];
    int used,declared,addr,length,value;
}s[10];

int symtab_cnt;

int check_if_valid_opode(char tok[]);

int search_into_symtab(char *st)
{
    int i;
    for(i=0;i<symtab_cnt;i++)
        if(strcmpi(st,s[i].name)==0)
            return i;
    return -1;
}


char optab[][10]={"stop","add","sub","mult","mover","movem","comp","bc",
             "div","read","print"};
char regtab[][6]={"areg,","breg,","creg,","dreg,"};
char oprtab[][6]={"le,","lt,","eq,","gt,","ge,","any,"};
char adtab[][6]={"start","end","ltorg","origin"};

int optab_cnt=11;
int regtab_cnt=4;
int oprtab_cnt=6;
int adtab_cnt=4;



void display()
{
    int m;
    for(m=0;m<symtab_cnt;m++)
        if(s[m].used == 1 && s[m].declared == 0)
            printf("\nSymb %s used but not defined",s[m].name);
        else if(s[m].used == 0 && s[m].declared == 1)
            printf("\nSymb %s defined but not used",s[m].name);
    getch();
}



main(int argc,char *argv[])
{

    FILE *fp;
    char *w[4],*error[15];
    char str[80],line[80];
    int notok,i,p=0,k,j,cnt=0,h;
    clrscr();
    fp=fopen(argv[1],"r");
    if(fp==NULL)
    {
        printf("\nFile can not be opened...");
        getch();
        exit(1);
    }

    for(i=0;i<4;i++)           //Allocating memory to token variables
        w[i]=(char *)malloc(10);

    cnt=0;
    while(fgets(str,80,fp))
    {
        cnt++;
        printf("%d : %s",cnt,str);
        notok=sscanf(str,"%s%s%s%s",w[0],w[1],w[2],w[3]);
        switch(notok)
        {

            case 2:k=check_if_valid_opcode(w[0]);
                if(k==9||k==10)
                {
                    h=search_into_symtab(w[1]);
                    if(h==-1)
                    {
                        strcpy(s[symtab_cnt].name,w[1]);
                        s[symtab_cnt++].used=1;
                    }
                    continue;
                }
                break;

            case 3:k=check_if_valid_opcode(w[0]);.

                if(k>=1&&k<=8)
                {
                    h=search_into_symtab(w[2]);
                    if(h==-1)
                    {
                        strcpy(s[symtab_cnt].name,w[2]);
                        s[symtab_cnt++].used=1;
                    }
                    continue;
                }

                if(strcmpi(w[1],"ds")==0 || strcmpi(w[1],"dc")==0)
                {
                    h=search_into_symtab(w[0]);
                    if(h==-1)
                    {
                        strcpy(s[symtab_cnt].name,w[0]);
                        s[symtab_cnt++].declared=1;
                    }
                    else
                    {
                        if(s[h].declared==0)
                        {
                            s[h].declared=1;
                        }
                        else
                            printf("Multiple declaration");
                    }
                }
                break;

            case 4:h=search_into_symtab(w[0]);
                if(h==-1)
                {
                    strcpy(s[symtab_cnt].name,w[0]);
                    s[symtab_cnt++].declared=1;
                }
                break;
        }

    }
    display();

    getch();

}


int check_if_valid_opcode(char tok[])
{
    int i;
    for(i=0;i<optab_cnt;i++)
        if((strcmpi(tok,optab[i]))==0)
            return i;
    return -1;
}








C++ Programs Questions and answers Solve it



Solve C++ programs 
Questions and answers:
 

1)Which C++ header file(s) will be essentially required to be included to run /execute the following C++ code:
 void main()
{
     char Msg[ ]="Sunset Gardens";
     for (int I=5;I<strlen(Msg);I++)
     puts(Msg);
 }

Ans : stdio.h, string.h

 

2) Name the header files that shall be need for the following code:
  void main() 
            {
                char text[] =”Something”
               cout<<”Remaining SMS chars: ”<<160-strlen(text)<<endl; 
            }
 
 Ans: iostream.h/iomanip.h , string.h


3) Rewrite the following program after removing the syntactical error(s) if any.

 #include<iostream.h>
Class Item
{ long IId, Qty; public: void Purchase
{ cin>>IId>>Qty;}
 void Sale()
{ cout<<setw(5)<<IId<<”Old:”<< Qty<<endl; cout<< “New :”<<Qty<<endl; }
};
void main()
 { Item I; Purchase(); I.Sale() }

Ans : #include<iostream.h>
class Item // C capital
 { long IId, Qty; public: void Purchase ( ) { cin>>IId>>Qty;}
 // ( ) after function name void Sale( )
{
cout<<setw(5)<<IId<<”Old:”<< Qty<<endl; cout<< “New :”<<Qty<<endl;
}
};
 void main()
{ Item I; I. Purchase(); // object missing I.Sale() ; // ; is missing }


4) Find the output of the following program:
 #include<iostream.h>
#include<ctype.h>
typedef char Str80[80];
 void main()
 {char *Notes;
 Str80 str= “ vR2GooD”;
int L=6;
Notes =Str;
 while(L>=3)
{ Str[L]=(isupper(Str[L])? tolower(Str[L]) : toupper(Str[L]));
cout<<Notes<<endl;
L--;
 Notes++;
}
}

Either the statement is removed or header file included as #include<iomanip.h>

Ans : vR2Good R2GoOd 2GOOd gOOd


5) Observe the following program and find out, which output(s) out id (i) to (iv) will not be expected from program?
 What will be the minimum and maximum value assigned to the variables Chance?
 #include<iostream.h>
 #include<stdlib.h>
void main()
{ randomize();
 int Arr[] = {9,6};, N;
int Chance = random(2)+10;
 for(int c=0;c<2;c++)
 { N= random(2);
 cout<<Arr[N];
 }
}
i) 9#6# ii) 19#17# iii) 19#16# iv) 20#16#

Ans: The output not expected from program are (i),(ii) and (iv) Minimum value of Chance =10
Maximum value of Chance = 11

6) Find the output of the following program:
#include<iostream.h>
class METRO
{ int Mno, TripNo, PassengerCount;
public: METRO(int Tmno=1) { Mno =Tmno; PassengerCount=0;} void Trip(int PC=20) { TripNo++, PassengerCount+=PC};
void StatusShow() { cout<<Mno<< “:”<<TripNo<< “ :”<<PassengerCount<<endl;} };
 void main() { METRO M(5), T; M.Trip(); M.StatusShow(); T.StatusShow(); M.StatusShow(); }
Ans : 5: 1: 20 1: 1: 50 5: 2: 50



7) Find the output for the following program:
 #include<iostream.h>
 #include<ctype.h>
void Encript ( char T[ ])
 { for( int i=0 ; T[i] != ‘ \0’ ; i += 2)
if( T[i] = = ‘A’ || T[i] = = ‘E’ ) T[i] = ‘#’ ;
 else if (islower (T[i] )) T[i] = toupper(T[i]);
 else T[i] = ‘@’;
}
void main()
{ char text [ ] = “SaVE EArTh in 2012”;
encrypt(text);
 cout<<text<<endl;
 }

8) Find the output of the following program:
 #include<iostream.h>
void main( )
{
 int U=10,V=20;
 for(int I=1;I<=2;I++)
{
cout<<”[1]”<<U++<<”&”<<V  5 <<endl;
 cout<<”[2]”<<++V<<”&”<<U + 2 <<endl;
}
 }


9)In the following program, find the correct possible output(s)from the options:
#include<stdlib.h>
 #include<iostream.h>
void main( )
 {
randomize( );
 char City[ ][10]={“DEL”, “CHN”, “KOL”, “BOM”, “BNG”};
int Fly;
for(int I=0; I<3;I++)
{
Fly=random(2) + 1;
cout<<City[Fly]<< “:”;
 }
}
 Outputs: (i) DEL : CHN : KOL: (ii) CHN: KOL : CHN: (iii) KOL : BOM : BNG: (iv) KOL : CHN : KOL:

10)In the following C++ program what is the expected value of Myscore from options (i) to (iv) given below.
 Justify your answer.
#include<stdlib.h>
#include<iostream.h>
 void main( )
{
 randomize( );
 int Score[ ] = {25,20,34,56,72,63},Myscore;
 cout<<Myscore<<endl;
 }

 i) 25 (ii) 34 (iii) 20 (iv) Garbage Value.

Function overloading in C++
A function name having several definitions that are differentiable by the number or types of their arguments is known as function overloading.
Example :

 A same function print() is being used to print different data types:
#include <iostream.h>
class printData { public: void print(int i) { cout << "Printing int: " << i << endl; }
void print(double  f) { cout << "Printing float: " << f << endl; }
void print(char* c) { cout << "Printing character: " << c << endl; }
};
int main(void) { printData pd;
// Call print to print integer pd.print(5);
 // Call print to print float pd.print(500.263);
 // Call print to print character pd.print("Hello C++");
return 0;
}
When the above code is compiled and executed, it produces following result:
Printing int: 5 Printing float: 500.263 Printing character: Hello C++

अच्छे विचार करे विचार

  पहचान की नुमाईश, जरा कम करें... जहाँ भी "मैं" लिखा है, उसे "हम" करें... हमारी "इच्छाओं" से ज़्यादा "सुन...