Showing posts with label Operating Systems. Show all posts
Showing posts with label Operating Systems. Show all posts

Tuesday, December 20, 2011

C Program to Perform First Come First Serve Process Sheduling



First Come First serve is a Processes scheduling algorithm in which the process which arrives first gets System Resources First.The process with the least arrival time gets the system system resources first followed by processes with higher arrival time.


Waiting Time:
The Time Process has to wait in the ready list to get the system resources for its execution


Execution Time:
The time the process requires to perform the computation.


Turn Around Time:
The Total Time the Process Requires for completion waiting time+Execution Time


The following is a C Program Source Code of a First Come First Serve algorithm implementation.The processes are scheduled according to the arrival time and finally the waiting time and turn around time of the processes are calculated.


 #include<stdio.h>  
 void main()  
 {  
  int i,ex[20],tr[20],wt[20],n,ar[20];  
  int j,k,temp,od[20];  
  float b=0,c=0;  
  printf("\n Enter the number of processes : ");  
  scanf("%d",&n);  
  for(i=0;i<n;i++)  
  {   
  printf(" P%d = ",i+1);  
  printf("\n Excecution time : ");  
  scanf(" %d",&ex[i]);  
  printf("\n Arrival time : ");  
  scanf("%d",&ar[i]);  
  }  
 for(i=0;i<n;i++)  
  od[i]=ar[i];  
  for(i=0;i<n;i++)  
  for(j=i+1;j<n;j++)  
  {  
   if(ar[i]>ar[j])  
   {  
   temp=ar[i];  
   ar[i]=ar[j];  
   ar[j]=temp;  
   }  
  }  
  for(j=0;j<n;j++)  
 {  
  for(i=0;i<n;i++)  
  {  
  if(ar[j]==od[i])  
   {   
    if(j==0)  
    {  
     wt[i]=ar[j];  
     b=wt[i];  
     tr[i]=ex[i];  
     c=tr[i];  
     break;  
    }  
    wt[i]=temp-ar[j]+ar[j-1];  
    b=b+wt[i];  
    tr[i]=ex[i]+wt[i];  
    c=c+tr[i];  
    break;  
   }  
  }  
  temp=tr[i];  
 }   
  b=b/n;  
  c=c/n;  
  printf("\n ORDER NAME ARRIVAL-TIME EX.TIME WAITING-TIME TURN-AROUND-TIME \n");  
  for(j=0;j<n;j++)  
  for(i=0;i<n;i++)  
  {  
  if(ar[j]==od[i])  
  {  
  printf("  %d  P%d\t %d      %d\t   %d\t\t  %d ",j+1,i+1,ar[j],ex[i],wt[i],tr[i]);  
  printf("\n");  
  break;  
  }  
  }  
  printf("Average waiting time = %f \n Average turn around time = %f \n",b,c);  
 }  


Saturday, December 10, 2011

What is Abstraction?The Concept of Abstraction Simplified.



What is Abstraction in Object Oriented Programming?
Abstraction can be formally defined as the representation of an object including the specification of that object in a certain context.Abstraction is a powerful means to tackle the complexity of programming by wrapping up and thus reducing the complexity of complex operations.

Abstraction is used in OOP(Object Oriented Programming) Languages like Java,C++ etc.These OOP Languages employ an abstract entity called class from which instance of that class called object can be created.Data and Code are Binded together in a syntactic container called Object.

These statements can be little confusing for beginner's so read the rest of the post to get a better view of abstraction.




Abstraction Defined in a Simple way :

The concept of abstraction is a little confusing of beginner's.

To understand the concept of abstraction easily consider a cactus which has lots of spines placed in box.You can easily carry the box without knowing that a cactus is in it.But it is difficult to carry the cactus with bare hands.Thus Abstraction serves as a box to wrap up the complexities.


In programming subprograms are examples of abstractions,as they can be used to perform a function without knowing the internal details.eg: a function named add(int a,int b) can be used to add two integers and produce a result by simply using the code add(1,2) in the program.The internal details of the function that performs addition is not known to the programmer or to the main program.These details are abstracted and this provides a higher level view.


Abstraction simplifies programming as the programmer needs to focus only on the task he has to perform rather that the internal details of the program.Abstraction are provided in Libraries(.dll) for different operations.

Abstraction is implemented commonly in programming languages by using an data type called class.Objects are Instances of a class 

eg:Car is a class Audi,Mercedes etc are Objects of the class named car


 Each Object is an a clone of the an abstraction(class),it posses all the traits of class eg:Mercedes and Audi are two brands of cars but they posses all the characters of a the main class car.Thus they are Objects created from the Abstraction Car.The term Car is used to Abstract or wrap up the internal details like engine,wheels,brakes etc in a single container  named car.



Data Hiding is a Concept of Abstraction by which the Code and the Data in an Object(Instance of an Abstract Data type usually referred to as class) is hidden from the external entities. 

Saturday, December 3, 2011

Round Robin Process Sheduling Algorithm and Example Source Code in C

Round robin is the most widely used process scheduling algorithm .The basic strategy for round robin scheduling is that if there are n process,each of the process will receive 1/n CPU Execution Time.Each process is allotted a  time quanta, for which its is executed.The incoming processes are kept in a ready list while another one is executing.If the time quanta allotted for a process is over,then that process is moved to ready and the next process in the ready list is executed for the allotted time quanta.The Complete Example Implementation Source Code in C of Round Robin Algorithm to schedule N Processes and to calculate the Execution,wait time and turn around time is given below.
 #include<stdio.h>  
 void main()  
 {  
  int n,i,k,x=0,s=0,r=0,q=0,x=0,a[30],e[30],t[30];  
  float m,p=0;  
  printf("Enter the number of process:");  
  scanf("%d",&n);  
  printf("Enter the execution time:");  
  for(i=0;i<n;i++)  
  {  
  scanf("%d",&a[i]);  
  e[i]=a[i];  
  }  
  printf("Enter the quanta:");  
  scanf("%d",&q);  
  printf("After round robin sheduling:");  
  for(i=0;i<n;i++)  
  {  
  if(x<a[i])  
  {  
   x=a[i];  
  }  
  }  
  k=x/q;  
  while(s<=k)  
  {   
  for(i=0;i<n;i++)  
  {  
   if(a[i]>0)  
   {  
   if(a[i]>q)  
   {  
   r=r+q;  
   a[i]=a[i]-q;  
   printf("P%d\t",i+1);  
   }  
  else  
  {  
   r=r+a[i];  
   a[i]=a[i]-q;  
   printf("P%d ",i+1);  
   t[i]=r;  
  }  
  }  
 }  
 s++;  
 }  
 printf("\nPROCESS EXECUTION TIME  WAIT TIME  TURN AROUND TIME\n");  
 for(i=0;i<n;i++)  
 {  
  printf(" %d \t\t %d\t\t %d\t\t %d\t\t \n",i,e[i],x,t[i]);  
  x=x+q;  
 }  
 m=x/n;  
 printf("Average waiting time=%f",m);  
 printf("Average turn around time=");  
 for(i=0;i<n;i++)  
 p=p+t[i];  
 p=p/n;  
 printf("%f",p);  
 }  

Thursday, November 17, 2011

C Program to implement Inter Process Communication(IPC) by using Pipe

Inter Process communication is a technique by which a thread in one process shares some information with threads in other processes.A pipe is a buffer,specifically a FIFO(First in First Out) buffer that has 2 ends,a read end and a write end.A thread can write or read depending on the file reference it has.
read() is used to read data and write() is used to write data.

The following is an implementation of Pipe in C Language.There is a main process P1 and 2 child processes C1 and C2 , the main process reads a string and passes it to one of the child process ,the child process C1 receive string from P1,find its length and sends the value to P1.C1 also reads an integer array of length equal to the length of the string and sends it to C2.C2 receive integer array from C1,find the sum of the elements and sends the sum to the parent process P1.The sleep() function is used in the program to suspend the processes for a desired interval of time.
The complete C language source code of  this example program that  implements IPC(Interprocess Communication) using PIPE is given below.
 #include<stdio.h>  
 #include<unistd.h>  
 #include<string.h>  
 void main()  
 {  
 // www.c-madeeasy.blogspot.com  
  int pid[2],pid1[2],pid2[2],pid3[2],pid4[2];  
  int a[20],i,l,s=0;  
  char str[20];  
  pipe(pid);  
  pipe(pid1);  
  pipe(pid2);  
  pipe(pid3);  
  pipe(pid4);   
 if(fork()==0)  
 {  
  sleep(5);  
  close(pid[1]);  
  read(pid[0],str,sizeof(str));  
  for(i=0,l=0;str[i]!='\0';i++)  
  l=l+1;  
  close(pid3[0]);  
  write(pid3[1],&l,sizeof(l));  
  sleep(6);  
  printf("Enter %d array elementz:",l);  
  for(i=0;i<l;i++)  
  scanf("%d",&a[i]);  
  close(pid1[0]);  
  write(pid1[1],a,sizeof(a));  
  close(pid4[0]);  
  write(pid4[1],&l,sizeof(l));  
 }  
 else if(fork()==0)  
 {  
  sleep(2);  
  close(pid1[1]);  
  close(pid4[1]);  
  read(pid4[0],&l,sizeof(l));  
  read(pid1[0],a,sizeof(a));  
  for(i=0;i<l;i++)  
  s=s+a[i];  
  close(pid2[0]);  
  write(pid2[1],&s,sizeof(s));  
 }   
 else    
 {  
  printf("\nEnter string:");  
  scanf("%s",str);  
  close(pid[0]);  
  write(pid[1],str,sizeof(str));  
  sleep(7);  
  close(pid3[1]);  
  read(pid3[0],&l,sizeof(l));  
  printf("\nThe string length=%d",l);  
  sleep(8);  
  close(pid2[1]);  
  read(pid2[0],&s,sizeof(s));  
  printf("\nSum=%d",s);  
 }  
 }  

Which is the Best Photo Watermarking Software

Photo Theft is becoming more and more common in the web with the outburst of social websites like Facebook,Google Plus and Image sharing se...