Operating System Lab (os) shell programming cs693

in shell programming
# is a single line comment.

for comments. There is no dedicated multi-line comment mechanism in shell that is analogous to the slash-star /* */ form in C-like languages.


echo HELLO WORLD!



#Add mul sub and div

echo enter first number
read a
echo enter second number
read b
add=`expr $a + $b`
sub=`expr $a - $b`
mul=`expr $a \* $b`
div=`expr $a / $b`
echo addition is $add
echo subtracion is $sub
echo multiplication is $mul
echo division is $div


#Largest of three numbers

echo enter three numbers

read a
read b
read c

if [ $a -gt $b ] && [ $a -gt $c ]
then
echo $a is greater
fi
if [ $b -gt $a ] && [ $b -gt $c ]
then
echo $b is greater
else
echo $c is greater 
fi


# Average of three number

echo Enter three number
read a
read b
read c

sum=` expr $a + $b + $c `
avg=` expr $sum / 3 `
echo $avg

# even or odd

echo enter a number
read n

b=`expr $n % 2`
if [ $b -eq 0 ]
then
echo even 
elif [ $b -ne 0 ]
then
echo odd
fi 


#shell program to convert from celsius to Fahrenheit

echo Enter temperature in celsius
read t

mul=` expr $t \* 9 / 5 `
fah=` expr $mul + 32 `
echo "Temperture in fahrenheit " $fah


#shell program for circle and rectangle

echo Enter radius of circle
read r
a=`expr 22 / 7 \* $r `
b=`expr $a \* $r`
echo area of circle $b

c=` expr 2 \* 22 / 7 \* $r`
echo perimeter of circle $c

echo Enter length and breadth of a rectangle
read l;
read b;

d=`expr $l \* $b `
echo Area of rectangle is $d

e=`expr 2 \* $l \* $b`

echo  Perimeter of the rectangle is $e



//switch statement

echo "Enter a choice"
read a
case $a in
1)echo "Add"
;;
2)echo "Sub"
;;
3)echo "mul"
;;




#grade system makaut

echo "Enter the marks"
read m;
case $m in
4[0-9])
         echo "D"
         ;;
5[0-9])
         echo "C"
         ;;
6[0-9])
         echo "B"
         ;;
7[0-9])
         echo "A"
         ;;
8[0-9])
         echo "E"
         ;;
9[0-9])
         echo "O"
         ;;
100)
echo "o"
         ;;
*)
echo "F"
;;
esac


//c program that illustrates the parent process id and child process id.
/* The fork() System Call


System call fork() is used to create processes. It takes no arguments and returns a process ID. The purpose of fork() is to create a new process, which becomes the child process of the caller. After a new child process is created, both processes will execute the next instruction following the fork() system call. Therefore, we have to distinguish the parent from the child. This can be done by testing the returned value of fork():

If fork() returns a negative value, the creation of a child process was unsuccessful.
fork() returns a zero to the newly created child process.
fork() returns a positive value, the process ID of the child process, to the parent. The returned process ID is of type pid_t defined in sys/types.h. Normally, the process ID is an integer. Moreover, a process can use function getpid() to retrieve the process ID assigned to this process.
*/
#include<stdio.h>
#include<unistd.h>

int main()
{
int p;
p=fork();
if(p>0)
printf("Parent Process ID=%d ",p);
else
printf("Child process Id =%d",p);
return(0);
}

/* c program that illustrates the parent process id and child process id */

#include<stdio.h>
#include<unistd.h>

int main()
{

printf("%d\n",getpid());
printf("%d\n",getppid());
return(0);
}


//c program to print process id,parent processid,childid and parent child id.
#include<stdio.h>
#include<unistd.h>
int wait(int);

int main()
{
     int i,j;
    
     if(fork())
     {
         printf("\t parent process \n");
         printf("process id=%d\t parent process id=%d\n",getpid(),getppid());
         for(i=0;i<100;i+5)
         {
          for(j=0;j<10000;j++)
            {
              printf("parent=%d\n",j);
            }
         }
             wait(0);
           printf("chlid terminated");
     }
else
{
printf("\t child \n");
printf("child id=%d\t parent childid=%d\n",getpid(),getppid());
for(i=0;i<100;i+10)
{
for(j=0;j<10000;j++)
{
printf("child =%d\n",i);
}
}
}
return(0);
}

#simple calculator using shell script

echo "Enter a number"
read n1
echo "Enter second number"
read n2
echo "1.Addition"
echo "2.subtraction"
echo "3.multiplication"
echo "4.division"
echo "Enter your choice"
read ch
case $ch in
1) sum=`expr $n1 + $n2 `
echo "sum=" $sum
;;
2) sub=`expr $n1 - $n2 `
echo "sub=" $sub
;;
3)mul=`expr $n1 \* $n2 `
echo "mul=" $mul
;;
4)div=`expr $n1 / $n2 `
echo "div=" $div
;;
*)echo "invalid choice"
;;
esac


//signal handler program for SIGINT
/*A function sig_handler is used a s a signal handler. This function is registered to the kernel by passing it as the second argument of the system call ‘signal’ in the main() function. The first argument to the function ‘signal’ is the signal we intend the signal handler to handle which is SIGINT in this case.

On a side note, the use of function sleep(1) has a reason behind. This function has been used in the while loop so that while loop executes after some time (ie one second in this case). This becomes important because otherwise an infinite while loop running wildly may consume most of the CPU making the computer very very slow.

Anyways, coming back , when the process is run and we try to terminate the process using Ctrl+C:*/

#include<stdio.h>
#include<signal.h>
#include<unistd.h>

void sig_handler(int signo)
{
  if (signo == SIGINT)
    printf("received SIGINT\n");
}

int main(void)
{
  if (signal(SIGINT, sig_handler) == SIG_ERR)
  printf("\ncan't catch SIGINT\n");
  // A long long wait so that we can easily issue a signal to this process
  while(1)
    sleep(1);
  return 0;
}
//c program to demonstrate floating point error ie SIGFPE error.

#include<stdio.h>
#include<signal.h>
void errorhandler(int signalno)
{
if(signalno==SIGKILL)
printf("\n Division by zero illegal opertaion\n ");

}
void main()
{
int i=0,j=50;
j=j/i;
signal(SIGFPE,errorhandler);
}

// c program to start alarm
#include<stdio.h>
#include<signal.h>
#include<unistd.h>
void ding(int sig)
{
printf("\n alarm has gone off \n");
}
main()
{
int pid;
printf("\n alarm application starting \n");
if((pid = fork())==0)
{
sleep(20);
kill(getppid(),SIGALRM);
exit(0);
}
printf("\n waiting for alaram off \n");
(void) signal(SIGALRM,ding);
pause();
printf("\n done \n");
exit(0);
}
   

Comments

Popular posts from this blog

cpanel exam CPSP Answers

How to install zimbra collaboration suite 8.8.11 on CentOS 7

awstats installation