Monday, 25 March 2013

Round Robin Scheduling in C Programming

Round Robin Technique is one of the scheduling algorithm for processes in an operating system. This algorithm give the CPU access to a process for a particular time period which time know as Quantum Time. After execution once all the process again repeated and allotted for CPU execution that’s why it’s Round term us in this algorithm. I made following Program which manage all the process and calculate average turn around time and waiting time.

Source Code :

<pre class="brush : cpp">
#include<stdio.h>
#include<conio.h>
int main()
{
int st[10], bt[10], wt[10], tat[10], n, tq;
int i, count=0, swt=0, stat=0, temp, sq=0;
float awt=0.0, atat=0.0;
printf("enter number of processes : ");
scanf("%d", &n);
printf("nenter burst time for processes ");
for(i=0; i<n;i++)
{
printf("nFor process p%d : ",i+1); 
scanf("%d", &bt[i]);
st[i]=bt[i];
}
printf("nenter time quantum : ");
scanf("%d", &tq);
while(1)
{
for(i=0, count=0;i<n;i++)
{
temp=tq;
if(st[i]==0)
{
count++;
continue;
}
if(st[i]>tq)
{
st[i]=st[i]-tq;
}
else
if (st[i]>=0)
{
temp=st[i];
st[i]=0;
}
sq=sq+temp;
tat[i]=sq;
}
if(n==count)
break;
}
for(i=0;i<n;i++)
{
wt[i]=tat[i]-bt[i];
swt=swt+wt[i];
stat=stat+tat[i];
}
awt=(float)swt/n;
atat=(float)stat/n;
printf(" n process_not burst_timet wait_timet turn_around_time");
for(i=0; i<n; i++)
{
printf("n%dtt %dtt %dtt %d", i+1, bt[i], wt[i], tat[i]);
}
printf("naverage wait time is %fnaverage turn_around_time is %fn", awt, atat);
getch();
return 0;}
</pre>


Output :




Wednesday, 20 March 2013

Quick Sort program in C++

Quick Sort is a Divide-and-conquer  Sorting algorithm Because this algorithm first divides a large list into two smaller sub-lists ( low elements and high elements ). Quick Sort can then recursively sort by the sub-lists. Check my Following Link and comment me back if you can do it in other better way.


Source Code :

<pre class="brush: cpp">
#include<iostream.h>
#include<conio.h>
int main()
{
int a[10],size,m;
int qsort(int a[],int p,int r);
cout<<"enter array size : ";
cin>>size;

for(m=0;m<size;m++)
{
cout<<"nenter " <<m<<"th element : "; 
cin>>a[m];                    
}
qsort(a,0,size-1);
cout<<endl<<"sorted array is : ";
for(m=0;m<size;m++)
{

cout<<a[m]<<"t";                    
}
getch();
return 0;
}
int qsort(int a[],int p,int r)

    int partition(int a[],int x,int y);
  int q;
  if(p<r)  
  {
      q=partition(a,p,r);     

    qsort(a,p,q-1);
   qsort(a,q+1,r);
}
}
int partition(int a[],int p,int r)
{
    int i,x,temp,temp1;
    i=p-1;
    x=a[r];
    int j;
    for(j=p;j<=r-1;j++)
    {
    if(a[j]<=x)
    {
    i=i+1;  
    temp=a[i];
    a[i]=a[j];
    a[j]=temp;     
    }                   
    }
    temp1=a[r];
    a[r]=a[i+1];
    a[i+1]=temp1;
    return i+1;
}

</pre>

Output :




Wednesday, 6 March 2013

C Program to Reverse the String


Here I’m sharing a code to print any inputted sting as reverse method. For Example if we input APPS then output should be SSPA.
Strrev() function in C do the same thing. By using this function easily can print a String in reverse but here we perform Reverse opertion without using Strrev(). 

Let See our Code :

<pre class="brush: cpp">
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
   char *str;
   int i,len;
   clrscr();
   printf("Enter Any String for Reverse Print : ");
   scanf("%s",str);
   len=strlen(str)-1;
   for(i=0;i<strlen(str)/2;i++)
   {
     str[i]+=str[len];
     str[len]=str[i]-str[len];
     str[i]=str[i]-str[len--];
   }
   printf("nnReverse String is : %s",str);
   getch();
}
</pre>

Output : 








Wednesday, 6 February 2013

Program in c++ to perform First Fit , Best Fit and Worst Fit

Before learning programming concept of  First , best & Worst Fit we should know about these Operating System Concept.

First fit: The allocator places a process in the First  block of unallocated memory in which it will fit.

Best fit: The allocator places a process in the smallest block of unallocated memory in which it will fit.

Worst fit: The memory manager places a process in the largest block of unallocated memory available.


Program (Source Code) :


<pre class="brush: html">

#include<conio.h>
#include<iostream.h>
int main()
{
  int c,i,j,k,n,l,m[10],p[10],po[20],flag,z,y,temp,temp1;
      cout<<"enter memory partition:\t";
      cin>>n;
      cout<<"\nenter memory size for\n";
      for(i=1;i<=n;i++)
      {
        cout<<"\npartition "<<i<<" :\t";
        cin>>m[i];
        po[i]=i;      
      }
      cout<<"\nenter process:\t";
      cin>>j;
      cout<<"\nenter memory size for\n";
      for(i=1;i<=j;i++)
      {
      cout<<"\nprocess "<<i<<" :\t";
        cin>>p[i];                
      }        
      cout<<"\n******** welcome to menu driven program of memory management**********\n1.first fit\n2.best fit\n3.worst fit\nenter choice:\t";
      cin>>c;
      switch(c)
      {
      case 1:
            for(i=1;i<=j;i++)
      {
          flag=1;
          for(k=1;k<=n;k++)
      {
          if(p[i]<=m[k])
          {
             cout<<"\nprocess "<<i<<" whose memory size is "<<p[i]<<"KB allocated at memory partition:\t"<<po[k];
             m[k]=m[k]-p[i];
             break;          
          }
          else
         {
            flag++;  
          }
      }  
      if(flag>n)
      {
         cout<<"\nprocess "<<i<<" whose memory size is "<<p[i]<<"KB can't be allocated";      
      }          
      }
      break;
      case 2:
       for(y=1;y<=n;y++)
          {
          for(z=y;z<=n;z++)
          {
          if(m[y]>m[z])
          {
          temp=m[y];
          m[y]=m[z];
          m[z]=temp;
          temp1=po[y];
          po[y]=po[z];
          po[z]=temp1;            
          }                
          }            
          }
          for(i=1;i<=j;i++)
      {
          flag=1;
          for(k=1;k<=n;k++)
      {
          if(p[i]<=m[k])
          {
             cout<<"\nprocess "<<i<<" whose memory size is "<<p[i]<<"KB allocated at memory partition:\t"<<po[k];
             m[k]=m[k]-p[i];
             break;          
          }
          else
         {
            flag++;  
          }
      }  
      if(flag>n)
      {
         cout<<"\nprocess "<<i<<" whose memory size is "<<p[i]<<"KB can't be allocated";      
      }          
      }
          break;
          case 3:
          for(y=1;y<=n;y++)
          {
          for(z=y;z<=n;z++)
          {
          if(m[y]<m[z])
          {
          temp=m[y];
          m[y]=m[z];
          m[z]=temp;
          temp1=po[y];
          po[y]=po[z];
          po[z]=temp1;            
          }                
          }            
          }
          for(i=1;i<=j;i++)
      {
          flag=1;
          for(k=1;k<=n;k++)
      {
          if(p[i]<=m[k])
          {
             cout<<"\nprocess "<<i<<" whose memory size is "<<p[i]<<"KB allocated at memory partition:\t"<<po[k];
             m[k]=m[k]-p[i];
             break;          
          }
          else
         {
            flag++;  
          }
      }  
      if(flag>n)
      {
         cout<<"\nprocess "<<i<<" whose memory size is "<<p[i]<<"KB can't be allocated";      
      }          
      }
          break;
          }  
      getch();
      return 0;
}</pre>






Output Screen :