Pages

Tuesday, 5 February 2019

Intoduction to shell programs - Arithmetic and Relational Operators

Introduction to Shell Programs

 In UNIX there are two major types of shells

  1. The Bourne shell. If you are using a Bourne-type shell, the default prompt is the $ character.
  2. The C shell. If you are using a C-type shell, the default prompt is the % character.
There are various subcategories for Bourne Shell which are listed as follows:
  • Bourne shell ( sh)
  • Korn shell ( ksh)
  • Bourne Again shell ( bash)
  • POSIX shell ( sh)

     
OPERATORS
1) Arithmetic Operators:- There are following arithmetic operators supported by Bourne Shell.


Assume variable a holds 10 and variable b holds 20 then:
Operator
Description
Example
+
Addition - Adds values on either side of the operator
`expr $a + $b` will give 30
-
Subtraction - Subtracts right hand operand from left hand operand
`expr $a - $b` will give -10
*
Multiplication - Multiplies values on either side of the operator
`expr $a \* $b` will give 200
/
Division - Divides left hand operand by right hand operand
`expr $b / $a` will give 2
%
Modulus - Divides left hand operand by right hand operand and returns remainder
`expr $b % $a` will give 0
=
Assignment - Assign right operand in left operand
a=$b would assign value of b into a
==
Equality - Compares two numbers, if both are same then returns true.
[ $a == $b ] would return false.
!=
Not Equality - Compares two numbers, if both are different then returns true.
[ $a != $b ] would return true.

It is very important to note here that all the conditional expressions would be put inside square braces with one spaces around them, for example [ $a == $b ] is correct where as [$a==$b] is incorrect.
Program:
#!/bin/bash
echo -n "enter a value :"
read a
echo "enter b value :"
read b
c=`expr $a + $b`
echo "addition is : $c"
c=`expr $a - $b`
echo "substraction is : $c"
c=`expr $a \* $b`
echo "multiplication is : $c"
c=`expr $a % $b`
echo "modular division is : $c"
c=`expr $a / $b`
echo "division is : $c"
c=`expr a=$b`
echo "assignment value is : $c"
c=`expr $a == $b`
echo "equal operator is : $c"
c=`expr $a != $b`
echo "not equal operator is : $c"



Output:
 
2)Relational Operators:
Bourne Shell supports following relational operators which are specific to numeric values. These operators would not work for string values unless their value is numeric.
For example, following operators would work to check a relation between 10 and 20 as well as in between "10" and "20" but not in between "ten" and "twenty".
Assume variable a holds 10 and variable b holds 20 then:
Operator
             Description
Example
-eq
Checks if the value of two operands are equal or not, if yes then condition becomes true.
[ $a -eq $b ] is not true.
-ne
Checks if the value of two operands are equal or not, if values are not equal then condition becomes true.
[ $a -ne $b ] is true.
-gt
Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true.
[ $a -gt $b ] is not true.
-lt
Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true.
[ $a -lt $b ] is true.
-ge
Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true.
[ $a -ge $b ] is not true.
-le
Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true.
[ $a -le $b ] is true.

It is very important to note here that all the conditional expressions would be put inside square braces with one spaces around them, for example [ $a <= $b ] is correct where as [$a <= $b] is incorrect.

Program:
#!/bin/bash
echo -n " enter a value :"
read a
echo -n " enter b value :"
read b

if [ $a -gt $b ]
then
echo "$a is greater then  $b"
fi

if [ $a -lt $b ]
then
echo "$a is less then $b"
fi

if [ $a -ge $b ]
then
echo "$a is  greater then or equal to  $b"
fi

if [ $a -le $b ]
then
echo "$a is less then oe equal to  $b"
fi

if [ $a -eq $b ]
then
echo "$a is equal to $b"
fi

if [ $a -ne $b ]
then
echo "$a is not equal to  $b"
fi
Output:

POSIX thread (pthread) System call program - ubuntu

The POSIX(Portable Operating system Interface) function is  to create a new thread within the same process used header file is
#include <pthread.h>

Program :
//POSIX Portable Operating System Interface called as pthread
#include<stdio.h>
#include<pthread.h>
#include<stdlib.h>

void *thread(void *arg);
int i,sum;

void main()
{
pthread_t tid;
printf("\n sum=%d",sum);
pthread_create(&tid,NULL,thread,NULL);
pthread_join(tid,NULL);
printf("\n sum=%d\n",sum);
}

void *thread(void *arg)
{
for(i=0;i<3;i++)
sum=sum+i;
pthread_exit(0);
}
 
Output: using this command we can execute the pthread system call programs
gcc pthread.c -o thread -lpthread
    

  • gcc is the compiler command.
  • thread.c is the name of c program source file.
  • -o is option to make object file.
  • thread is the name of object file.
  • -lpthread is option to execute pthread.h library file.
 
 

exec() System call program - ubuntu

/* program for execute system call  */
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>  // for datatypes
#include<sys/types.h> // for standard symbolic constants & types
int main(int argc, char *argv[])
{
    int ret;
    printf("\n the pid is %d \n",getpid());
    ret=execl("/bin/ls","ls","-l",NULL);  // path the command (ls -l )
    printf("failed process is %d",ret);
return 0;
}

output:

wait() System call program - ubuntu

/* Wait for child termination - wait.c */
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>  // standard symbolic constant & types
#include<sys/types.h> // for datatype
#include<sys/wait.h> // for wait system call
main()
{
  int i,pid;
  pid = fork();
  if(pid < 0)
  {
   printf("\nProcess creation failure\n"); exit(-1);
  }
  else if(pid > 0)
  {
   wait(NULL);
   printf ("\nParent starts\nEven Nos:");
   for (i=2;i<=10;i+=2)
   printf("%3d",i);
   printf("\nParent ends\n");
  }
  else if(pid == 0)
  {
   printf ("\nChild starts\nOdd Nos:");
   for (i=1;i<10;i+=2)
   printf ("%3d",i);
   printf ("\nChild ends\n");
  }
}
output:


fork() System call programs - ubuntu


/* Process creation - fork.c */
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>  // for datatypes
#include<sys/types.h> // for standard symbolic constants & types
void main()
{
  int x;
  x=fork();
  printf(" \n process pid =%d", getpid());
  printf(" \n returned value =%d\n", x);
}


output



/* Process creation - fork.c */
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>  // for datatypes
#include<sys/types.h> // for standard symbolic constants & types
void main()
{
 int x;
 x=fork();
 if(x == 0)
  {
   printf("\n\n CHILD PROCESS STARTS");
   printf(" \n process pid =%d", getpid());
   printf(" \n returned value =%d\n", x);
  }
 else
  {
   printf("\n\n PARENT PROCESS STARTS ");
   printf(" \n process pid =%d", getpid());
   printf(" \n returned value =%d\n", x);
  }
}

output



/* Process creation - fork.c */
#include<stdio.h>
#include<unistd.h>  // for datatypes
#include<sys/types.h> // for standard symbolic constants & types
void main()
 {
  int x=10;
    if(fork() == 0)
    {
             sleep(10);
            x=x+10;
            printf("\n from child =%d", x);
    }
    else
        printf("\n from parent =%d", x);
 }

output



/* Process creation - fork.c */
#include<stdio.h>
#include<unistd.h>  // for datatypes
#include<sys/types.h> // for standard symbolic constants & types
void main()
{
fork();
fork();
fork();
printf("\n Hi !");
}

output:

System call programs on different process ids- ubuntu

/* program to illustrate different Process id */
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>  // for datatypes
#include<sys/types.h> // for standard symbolic constants & types
void main()
{
    int pid;
    printf("\n process id = %d", getpid());
    printf("\n parent process =%d", getppid());
    printf("\n group id=%d", getgid());
    printf("\n user id =%d\n", getuid());
}

output


Program on CPU SCHEDULING - ROUND ROBIN ( NON PREEMPTIVE)

/* Program to implement ROUND ROBIN CPU SCHEDULING */
#include<stdio.h>
void 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("Enter burst time for sequences:");
  for(i=0;i<n;i++)
   {
     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\tPno\tBT\tTAT\tWT\n");
   for(i=0;i<n;i++)
    printf("\n\t%d\t%d\t%d\t%d",i+1,bt[i],tat[i],wt[i]);
    printf("\n Avg wait time is %f \n Avg turn around time is %f\n",awt,atat);

}