Operating System Lab: Presented By: Abhishek Ghosh Cse 3 Year Roll - 38
Operating System Lab: Presented By: Abhishek Ghosh Cse 3 Year Roll - 38
System
Lab
Presented By:
Abhishek Ghosh
CSE 3rd Year
Roll - 38
Q1.Write a shell program that creates a menu as follows:
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"
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
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
if [ ! -f $f ]
then
echo "Type of file : NON-REGULAR"
echo
echo
exit
fi
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 [ $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
strt=1
cmp m n >tmp
if [ -s tmp ]
then
echo
echo "Entered string is not a Palindrome"
exit
fi
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
if [ $n -eq 1 ]
then
echo "Not a Prime Number"
exit
fi
div=2;
if [ $result -eq 0 ]
then
echo "Not a Prime Number"
exit
fi
div=`expr $div + 1`
done
clear
if [ ! -s temp ]
then
echo "Not an Authentic user"
exit
else
echo "Authentic user"
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
i=0
while [ $i -lt $n ]
do
j=0
k=0
while [ $k -le $i ]
do
echo -e `expr $i + 1`" "'\c'
k=`expr $k + 1`
done
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
echo -e "Reverse of the number is : \c"
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);
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);
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;
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];
pipe(p);
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);
read(p[0],buff,16);
return(0);
}
Q15. Implement the single producer single consumer problem.
#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);
}