Operating
System
Lab
Presented By:
Abhishek Ghosh
CSE 3rd Year
Roll - 38
Q1.Write a shell program that creates a menu as follows:
i) Show the permission mode of the files in the current directory
ii) Show all the regular files in current directory.
iii) Show all the files with size zero.
clear
echo -e "\t\t\t MAIN MENU"
echo -e "\t\t\t-----------------\n"
echo " 1. Show permission modes of the files in the current directory"
echo " 2. Show all regular files in the current directory"
echo " 3. Show all the files with size zero in the current directory"
echo -e "\nEnter your choice (1-3) : \c"
read choice
echo
case $choice in
1) for file in *
do
if [ -f $file ]
then
ls -l $file|cut -d " " -f1,8
fi
done;;
2) for file in *
do
if [ -f $file ]
then
echo $file
fi
done;;
3) for file in *
do
if [ ! -s $file ]
then
echo $file
fi
done;;
*) echo "wrong input existing";
exit;;
esac
echo
echo
Q2. Using a shell script to find the total no. of words, line and character in a given files created by the
programmer. Check if the file is regular.
clear
echo -e "Enter the filename : \c"
read f
echo
if [ ! -f $f ]
then
echo "Type of file : NON-REGULAR"
echo
echo
exit
fi
echo -e "\tType of file : REGULAR"
echo -e "\tNumber of lines : \c"
wc -l $f | cut -d " " -f1
echo -e "\tNumber of words : \c"
wc -w $f | cut -d " " -f1
echo -e "\tNumber of characters : \c"
wc -c $f | cut -d " " -f1
echo
echo
Q3. Write a shell program to sort a set of numbers read from users using bubble sort.
clear
echo -e "Enter the number of inputs : \c"
read n
echo
echo "Enter the numbers : "
i=0
while [ $i -lt $n ]
do
read arr[$i]
i=`expr $i + 1`
done
i=0
while [ $i -lt `expr $n - 1` ]
do
j=`expr $i + 1`
while [ $j -lt $n ]
do
if [ ${arr[$j]} -lt ${arr[$i]} ]
then
temp=${arr[$j]}
arr[$j]=${arr[$i]}
arr[$i]=$temp
fi
j=`expr $j + 1`
done
i=`expr $i + 1`
done
echo
echo "After sorting, numbers are now in the following sequence : "
i=0
while [ $i -lt $n ]
do
echo -e ${arr[$i]} " \c"
i=`expr $i + 1`
done
echo
echo
Q4. Write a shell script to check whether a string is palindrome or not.
clear
echo -e "Enter a string : \c"
read str
len=` expr $str : '.*' `
strt=1
while [ $len -ge 1 ]
do
echo $str|cut -c $len >m
echo $str|cut -c $strt >n
cmp m n >tmp
if [ -s tmp ]
then
echo
echo "Entered string is not a Palindrome"
exit
fi
len=` expr $len - 1 `
strt=` expr $strt + 1 `
done
echo
echo "Entered string is a Palindrome"
Q5. Write a shell program which will accept a number from the user and check whether it is prime or
not.
clear
echo -e "Enter a number : \c"
read n
if [ $n -eq 1 ]
then
echo "Not a Prime Number"
exit
fi
div=2;
while [ $div -lt $n ]
do
result=`expr $n % $div`
if [ $result -eq 0 ]
then
echo "Not a Prime Number"
exit
fi
div=`expr $div + 1`
done
echo "Prime Number"
Q6. Write a script that will return the number of parameters on the command line.
echo "The number of parameters are :"$#
Q7. Write a program to verify whether a user is authentic or not and if authentic then check logged on
to the system or not and if login then show the time of login.
clear
echo -e "Enter Username : \c"
read unm
grep $unm /etc/passwd > temp
if [ ! -s temp ]
then
echo "Not an Authentic user"
exit
else
echo "Authentic user"
who > temp2
grep $unm temp2 >temp3
if [ -s temp3 ]
then
echo "User is logged in"
echo -e "Time of logging is : \c"
cut -d "-" -f3 temp3|cut -d " " -f2
else
echo "User is not logged in:"
fi
fi
Q8. Write shell script using for loop to print the following patterns on screen
1
22
333
4444
55555
clear
echo -e "Enter the number : \c"
read n
i=0
while [ $i -lt $n ]
do
j=0
while [ $j -lt `expr $n - $i` ]
do
echo -e " "'\c'
j=`expr $j + 1`
done
k=0
while [ $k -le $i ]
do
echo -e `expr $i + 1`" "'\c'
k=`expr $k + 1`
done
echo " "
i=`expr $i + 1`
done
Q9. Write a shell script to print a given number in reverse order e.g. if the number is 143, it must be
printed as 341.
clear
echo -e "Enter number : \c"
read n
echo
echo -e "Reverse of the number is : \c"
while [ `expr $n / 10` -ne 0 ]
do
echo -e `expr $n % 10`'\c'
n=`expr $n / 10`
done
echo -e `expr $n % 10`
Q10. Write a LINUX shell script that will find all files in subdirectories and covert them to lowercase.
for i in *
do
if [ -d $i ]
then
cd $i
for j in *
do
bash ~/oslab/q10.sh
k=`echo $j | tr '[A-Z]' '[a-z]'`
if [ $j != $k ]
then
if [ ! -d $j ]
then
mv $j $k
fi
fi
done
cd ..
fi
done
Q11. Write a C program to create the orphan processes.
#include<stdio.h>
#include<stdlib.h> //for exit()
int main()
{
int p;
p=fork();
if(p==-1)
{
printf("Error");
exit(0);
}
if(p>0)
{
printf(" This is the Parent Process, PID=%d and PPID=
%d\n",getpid(),getppid());
printf(" Parent Process Terminating....\n");
}
else
{
printf(" This is the Child Process, PID=%d and PPID=
%d\n",getpid(),getppid());
sleep(5);
printf("\n This is the Child Process Again, PID=%d, but PPID=%d
now\n",getpid(),getppid());
printf(" This is because my Parent terminated, so I am an orphan
process, thats why INIT() process adopted me..\n");
printf( " Child Process Terminating....");
}
return(0);
}
Q12. Write a C program to create a Zombie Process.
#include<stdio.h>
#include<stdlib.h> //for exit()
int main()
{
int p;
p=fork();
if(p==-1)
{
printf("Error");
exit(0);
}
if(p>0)
{
printf(" This is the Parent Process, PID=%d and PPID=
%d\n",getpid(),getppid());
printf(" Parent Process : \"I will be Alive for next 5 seconds\"\n");
sleep(5);
printf("\n This is Parent Process Again...\n");
printf(" Parent Process : \"Before terminating, I will seek for status
of all Child Processes. Thus relieving them from Zombie state\"\n");
printf(" Parent Process Terminating....\n");
}
else
{
printf(" This is the Child Process, PID=%d and PPID=
%d\n",getpid(),getppid());
printf(" Child Process : \"I will be become a Zombie process after
terminating untill Parent Process again seeks to know my status\"\n");
printf( " Child Process Terminating....");
}
return(0);
}
Q13. Write a C program which shows the multitasking capabilities of your Operating System.
#include<stdio.h>
#include<stdlib.h> //for exit()
int main()
{
int p;
printf(" Parent Process prints : \"PARENT\" on the screen\n");
printf(" Child Process prints : \"CHILD\" on the screen\n\n");
printf(" Its will be clear from the pattern below that the Parent and Child
Processes are being executed in a multasking fashion..\n\n");
sleep(2);
printf(" Starting the PARENT and CHILD Processes....\n\n");
sleep(1);
p=fork();
if(p==-1)
{
printf("Error");
exit(0);
}
if(p>0)
{
int i;
for(i=0;i<100;i++)
{
printf("\n PARENT \n");
sleep(1);
}
}
else
{
int j;
for(j=0;j<100;j++)
{
printf("\n CHILD \n");
sleep(1);
}
}
return(0);
}
Q14. Write a C code to implement how a process communicates with other processes using the concept
of piping.
#include<stdio.h>
#include<stdlib.h> //for exit()
int main()
{
int k,p[2];
char *msg="Hello How R U ?";
char buff[16];
printf(" Creating a PIPE ...\n\n");
sleep(1);
pipe(p);
printf(" PIPE Created.\n\n");
k=fork();
if(k==-1)
{
printf("Error");
exit(0);
}
if(k>0)
{
printf(" Parent Process : Writting to one end of the PIPE....\n\n");
write(p[1],msg,16);
printf(" Message Written to the PIPE : \"%s\"\n\n",msg);
}
else
{
sleep(5);
printf(" \n\n Child Process : Reading from the other end of the
PIPE....\n\n");
read(p[0],buff,16);
printf(" Message Read from the PIPE : \"%s\"\n\n",buff);
return(0);
}
Q15. Implement the single producer single consumer problem.
/*Q.15 Implement single producer_single consumer problem in operating system using
semaphore variable.*/
/* Producer-Consumer Problem Using Semaphore and Shared Memory */
#include <unpipc.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/shm.h>
#define NUM_ELEM 10
#define SEM_EMPTY 0
#define SEM_FULL 1
union semun{
int val;
struct semid_ds *buf;
ushort *array;
};
int main()
{
int rc,Pid,SemID,ShmID,Status,i;
char elem;
union semun Mysemun;
struct sembuf WaitEmpty ={SEM_EMPTY, -1, SEM_UNDO};
struct sembuf SignalEmpty={SEM_EMPTY, 1, IPC_NOWAIT};
struct sembuf WaitFull ={SEM_FULL, -1, SEM_UNDO};
struct sembuf SignalFull ={SEM_FULL, 1, IPC_NOWAIT};
struct shmid_ds Myshmid_ds;
void *ShmPtr;
SemID=semget(IPC_PRIVATE,2,0666|IPC_CREAT);
Mysemun.val=NUM_ELEM;
semctl(SemID,SEM_EMPTY,SETVAL,Mysemun);
Mysemun.val=0;
semctl(SemID,SEM_FULL,SETVAL,Mysemun);
ShmID=shmget(IPC_PRIVATE,NUM_ELEM,0666|IPC_CREAT);
Pid=fork();
if(Pid==0)
{
ShmPtr=shmat(ShmID,0,SHM_R);
for(i=0;i<26;i++)
{
semop(SemID,&WaitFull,1);
elem=*((char *)ShmPtr + (i%NUM_ELEM));
printf(" CONSUMED ELEMET'%c'\n",elem);
sleep(1);
semop(SemID,&SignalEmpty,1);
}
exit(0);
}
else
{
ShmPtr=shmat(ShmID,0,SHM_W);
for(i=0;i<26;i++)
{
semop(SemID,&WaitEmpty,1);
elem='a'+i;
printf("PRODUCED ELEMENT'%c'\n ",elem);
sleep(1);
*((char *)ShmPtr + (i%NUM_ELEM))=elem;
semop(SemID,&SignalFull,1);
}
}
wait(&Status);
shmctl(ShmID,IPC_RMID,&Myshmid_ds);
semctl(SemID,SEM_EMPTY,IPC_RMID,Mysemun);
exit(0);
}