Friday, 4 January 2019

Program on CPU SCHEDULING - First Come First Serve (NON PREEMPTIVE)

/*  Program to Implement FIRST COME FIRST SERVE CPU Scheduling */

#include<stdio.h>
void main()
{
   int p[10],bt[10],st[10],ct[10],tat[10],wt[10],i,n;
   float sumtat=0,sumwt=0,avwt,avtat;
   printf("\n enter the no of processes");
   scanf("%d",&n);
   for(i=0;i<n;i++)
   {
     printf("\n enter the process no and burst time ");
     scanf("%d %d",&p[i],&bt[i]);
   }
   st[0]=0;
   wt[0]=0;
   ct[0]=st[0]+bt[0];
   for(i=0;i<n;i++)
   {
     st[i+1]=st[i] + bt[i];
     ct[i+1]=st[i+1]+ bt[i+1];
   }
   for(i=0;i<n;i++)
   {
     tat[i]=ct[i]-0;
     sumtat=sumtat + tat[i];
   }
   for(i=0;i<n;i++)
   {
     wt[i]=tat[i]-bt[i];
     sumwt=sumwt + wt[i];
   }
  printf("\n\t Pno\tbt\tst\tct\ttat\twt\n");
  for(i=0;i<n;i++)
  {
   printf("\n\t%d\t%d\t%d\t%d\t%d\t%d",p[i],bt[i],st[i],ct[i],tat[i],wt[i]);
  }
  avtat=sumtat/n;
  avwt=sumwt/n;
  printf("\n avearge waiting time is %f\n",avwt);
  printf("\n average turnaroundtime is %f\n",avtat);
}

output:

Thursday, 3 January 2019

UNIX COMMANDS - BC worked on UBUNTU Operating System PAGE 2 (CONT.).


bc: it is used to operate all the operators
Description: The bc command evaluates all the operators in c .
  1. Arithmetic operators
  2. Increment and decrement operators
  3. Assignment operators
  4. Relational Operators
  5. Logical operators
  6. Conditional statements
  7. Iterative statements
1.Arithmetic operator Examples:
1. Finding Sum of Two expressions
> echo "2+5" | bc
7
2. Difference of Two numbers
> echo "10-4" | bc
6
3. Multiplying two numbers
> echo "3*8" | bc
24
4. Dividing two numbers
> echo "2/3" | bc
0
> echo "5/4" | bc
1
Use the scale function to specify the number of decimal digits that the bc command should return.
> echo "scale=2;2/3" | bc
.66
5. Finding the remainder using modulus operator
> echo "6%4" | bc
2
6. Using exponent operator
> echo "10^2" | bc
100
Here the expression is evaluated as 10 to the power of 2.


2.Increment Operator Examples:
There are two kinds of increment operators. They are pre increment and post increment operators.
++var : Pre increment operator. The variable is incremented first and then the result of the variable is used.
var++ : Post increment operator. The result of the variable is used first and then the variable is incremented.
> echo "var=5;++var" | bc
6
> echo "var=5;var++" | bc
5
Here, in the second example the value of var is printed first and then it is incremented. See the below example, to see the complete incremental effect.
> echo "var=5;var++;var" | bc
5
6

Decrement Operator Examples:
Similar to the increment operators, there are two types of decrement operators.
--var : Pre decrement operator. The variable is decremented first and then the result of the variable is used.
var-- : Post decrement operator. The result of the variable is used first and then the variable is decremented.
> echo "var=5;--var"| bc
4
> echo "var=5;var--"| bc
5


3.Assignment Operator Examples:
Assigns 10 to the variable and prints the value on the terminal.
> echo "var=10;var" | bc
Increment the value of the variable by 5
> echo "var=10; var+=5;var | bc
15
The lists of assignment operators supported are:
var = value : Assign the value to the variable
var += value : similar to var = var + value
var -= value : similar to var = var - value
var *= value : similar to var = var * value
var /= value : similar to var = var / value
var ^= value : similar to var = var ^ value
var %= value : similar to var = var % value

4.Relational Operators Examples:
Relational operators are used to compare two numbers. If the comparison is true, then it returns 1. Otherwise (false), it returns 0.
expr1 < expr2 : Result is 1 if expr1 is strictly less than expr2.
expr1 <= expr2 : Result is 1 if expr1 is less than or equal to expr2.
expr1 > expr2 : Result is 1 if expr1 is strictly greater than expr2.
expr1 >= expr2 : Result is 1 if expr1 is greater than or equal to expr2.
expr1 == expr2 : Result is 1 if expr1 is equal to expr2.
expr1 != expr2 : Result is 1 if expr1 is not equal to expr2.
Examples:
> echo "10 > 5" | bc
1
> echo "1 == 2" | bc
0



5.Logical Operator Examples:
Logical operators are also mostly used in conditional statements. The result of the logical operators is either 1 (True) or 0 (false) ! expr : Result is 1 if expr is 0.
expr && expr : Result is 1 if both expressions are non-zero.
expr || expr : Result is 1 if either expression is non-zero.
> echo "4 && 10" | bc
1
> echo "0 || 0" | bc
0



6.Conditional Statement
Examples:
The syntax of if statement is
if(condition)
{
Statements
}
else
{
Statements
}
The following example shows show to use the if condition
> echo 'if(1 == 2) print "true" else print "false"' | bc
false


7.Iterative Statements:
Bc command supports the for and while loop for doing iterations. The syntax of for and while loop are shown below:
for (assignment; condition; increment)
{
statements
}
while (condition)
{
statements
}
The following examples prints numbers from 1 to 10 using the for and while loops
> echo "for(i=1;i<=10;i++) {i;}" | bc
> echo "i=1; while(i<=10) { i; i+=1}" | bc