Swe3001 Os Da1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

SHANMATHY SHREE AE 21MIS0498

SWE3001 – OPERATING SYSTEM


(LAB)
DIGITAL ASSESSMENT – 1

NAME: SHANMATHY SHREE AE


REG NO: 21MIS0498
FACULTY: KUMAR PJ
SLOT: L11 + L12
SHANMATHY SHREE AE 21MIS0498

a) Shell Programming
• Handling the command line arguments
CODE:
echo Multiplication table of $1
c=`expr $1 - 1`
for (( i=1 ; i<=10 ; i++ ))
do
echo $1*$i = `expr $1 \* $i`
done
c=1
for(( i=1 ; i<=$1 ; i++ ))
do
c=`expr $c \* $i`
done

OUTPUT:
SHANMATHY SHREE AE 21MIS0498

• String reversal
CODE:
echo 22-12-2022 SHANMATHY SHREEE 21MIS0498
echo "enter a number :"
read n
sum1=0
sum=0
while [ $n -ne 0 ]
do
r=`expr $n % 10`
n=`expr $n / 10`
sum=`expr $sum \* 10`
sum=`expr $sum + $r`
done
echo the reversed number is $sum

OUTPUT:
SHANMATHY SHREE AE 21MIS0498

• If-Else, Nested If Else, Switch cases in shell


CODE:
echo "Enter 1st numbner"
read a
echo "Enter 2nd number "
read b
echo "Enter 3rd number "
read c
echo "the 1st number is $a"
echo "the 2nd number is $b"
echo "the 3rd number is $c"
if [ $a -lt $b ]
then d=$a
else d=$b
fi
if [ $c -lt $d ]
then d=$c
fi
echo "The smallest number is $d"

OUTPUT:
SHANMATHY SHREE AE 21MIS0498

• Nested If Else, Switch cases in shell


CODE:
read a
read b
echo " Initially, a = $a "
echo " b = $b "
a=`expr $a + $b`
b=`expr $a - $b`
a=`expr $a - $b`
echo " After Swapping, a = $a "
echo " b = $b "
echo "Printing grade of the students "
echo "Enter name "
read name
echo "Enter subject1 marks : "
read m1
echo "Enter subject2 marks : "
read m2
echo "Enter subject3 marks : "
read m3
total=`expr $m1 + $m2 + $m3`
avg=`expr $m1 / 3`
if [ $avg -ge 90 ]
then
echo " Grade = A "
elif [ $avg -ge 80 ]
then
echo " Grade = B"
elif [ $avg -ge 70 ]
then
echo " Grade = C "
SHANMATHY SHREE AE 21MIS0498

elif [ $avg -ge 60 ]


then
echo " Grade = D "
elif [ $avg -ge 60 ]
then
echo " Grade = E+ "
elif [ $avg -ge 50 ]
then
echo " Grade = E "
else echo " Grade = F "
fi

OUTPUT:
SHANMATHY SHREE AE 21MIS0498

• Switch cases in shell


CODE:
echo Enter 2 numbers
read a b
echo " 1.Addition
2.subtraction
3.Multiplication
4.Division
Enter your choice (1-4) : "
read c
case $c in
1) echo sum is `expr $a + $b`
;;
2) echo difference `expr $a - $b`
;;
3) echo product is `expr $a \* $b`
;;
4) echo quotient is `expr $a / $b`
;;
esac

OUTPUT:
SHANMATHY SHREE AE 21MIS0498

b) Parent child process creation using fork( ) and exec()


system call Checking the Process Identifier Assigning
new task to child Providing the path name and program
name to exec() Synchronizing Parent and child process
using wait().

CODE:
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <sys/wait.h>
#include <stdlib.h>
int var_glb; /* A global variable*/
int main(void)
{
pid_t childPID;
int var_lcl = 0;
childPID = fork();
if(childPID >= 0) // fork was successful
{
if(childPID == 0) // child process
{
var_lcl++;
var_glb++;
printf("\n Child Process :: var_lcl = [%d], var_glb[%d]\n", var_lcl,
var_glb);
}
else //Parent process
{
var_lcl = 10;
var_glb = 20;
SHANMATHY SHREE AE 21MIS0498

printf("\n Parent process :: var_lcl = [%d], var_glb[%d]\n", var_lcl,


var_glb);
}
}
else // fork failed
{
printf("\n Fork failed, quitting!!!!!!\n");
return 1;
}
return 0;
}

OUTPUT:
SHANMATHY SHREE AE 21MIS0498

c) Process and Thread Management Write a program to


create a thread and perform the following (Easy)
• Create a thread runner function.
• Set the thread attributes.
• Join the parent and thread.
• Wait for the thread to complete.
CODE:
#include <stdio.h>
#include <pthread.h>
/* This is our thread function. It is like main(), but for a thread */
void *threadFunc(void *arg)
{
char *str;
int i = 0;
str=(char*)arg;
while(i < 10 )
{
printf("threadFunc says: %s\n",str);
++i;
}
return NULL;
}
int main(void)
{
pthread_t pth; // this is our thread identifier
int i = 0;
/* Create worker thread */
pthread_create(&pth,NULL,threadFunc,"processing...");
/* wait for our thread to finish before continuing */
pthread_join(pth, NULL /* void ** return value could go here */);
while(i < 10 )
SHANMATHY SHREE AE 21MIS0498

{
printf("main() is running...\n");
++i;
}
return 0;
}

OUTPUT:
SHANMATHY SHREE AE 21MIS0498

d) Write a program to create a thread to find the factorial of a


natural number ‘n’. (Medium)
CODE:
#include<stdio.h>
#include<pthread.h>
#include<tgmath.h>
void *factorial(void *p);
int fact(int n);
int main()
{
pthread_t tid1; pthread_t tid2;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_create(&tid1,&attr,factorial,NULL);
pthread_join(tid1,NULL);
}
int fact(int n){ if(n==0 || n==1) return 1;
else
return n*fact(n-1);
}
void *factorial(void *p){ int i,num1;
printf("Thread 1 (factorial) : "); printf("Enter Number: ");
scanf("%d",&num1);
printf("Factorial is: %d\n",fact(num1)); pthread_exit(0);
}
SHANMATHY SHREE AE 21MIS0498

OUTPUT:

You might also like