Professional Documents
Culture Documents
OS Day 1 2 3
OS Day 1 2 3
UNIX ARCHITECTURE
UNIX INTERFACES
UNIX SHELL
The shell acts as an interface between the user and the kernel. When
a user logs in, the login program checks the username and password,
and then starts another program called the shell. The shell is a
command line interpreter (CLI). It interprets the commands the user
types in and arranges for them to be carried out. The commands are
themselves programs: when they terminate, the shell gives the user
another prompt (% on our systems).
The adept user can customize his/her own shell, and users can use
different shells on the same machine.
The tcsh shell has certain features to help the user inputting
commands.
Filename Completion - By typing part of the name of a command,
filename or directory and pressing the [Tab] key, the tcsh shell will
complete the rest of the name automatically. If the shell finds more
than one name beginning with those letters you have typed, it will
beep, prompting you to type a few more letters before pressing the
tab key again.
History - The shell keeps a list of the commands you have typed in. If
you need to repeat a command, use the cursor keys to scroll up and
down the list or type history for a list of previous commands.
UNIX UTILITY PROGRAMS
There are large number of utility programs which are classified into 6
categories which are
1. File and directory manipulation commands.
2. Filters.
3. Program development tools such as editors and compilers.
4. Test processing.
5. System Administration.
6. Miscellaneous.
UNIX KERNEL
Special Files
o Used to represent a real physical device such as a printer,
tape drive or terminal, used for (I/O) operations
o Unix considers any device attached to the system to be a
file - including your terminal:
By default, a command treats your terminal as the
standard input file (stdin) from which to read its input
Your terminal is also treated as the standard output file
(stdout) to which a command's output is sent
o Two types of I/O: character and block
o Usually only found under directories named /dev
Pipes
o UNIX allows you to link commands together using a pipe.
The pipe acts a temporary file which only exists to hold data
from one command until it is read by another
For example, to pipe the output from one command into
another command: who | wc -l
cd ../quattro
cd /usr/home/quattro
- relative pathname
- absolute pathname
3.
4.
5.
6.
|
Others (o)
Permission
|
____________________________
|
User (u)
Group (g)
SHELLSCRIPT PROGRAMMING
Q. Write a menu driven shell program for performing the following
operations.
Code:
echo "Enter first number: "
read a
echo "Enter second number: "
read b
echo "Enter an operation (+,-,*,/,%,^): "
read op
case $op in
+)
r=`expr $a + $b`;;
-)
r=`expr $a - $b`;;
\*)
r=`expr $a \* $b`;;
/)
r=`expr $a / $b`;;
%) r=`expr $a % $b`;;
^)
r=`echo "$a ^ $b" | bc`;;
*)
echo "Invalid Input"
exit;;
esac
echo $a $op $b "=" $r
Output:
Enter first number:
20
Enter second number:
10
Enter an operation (+,-,*,/,%,^):
+
20+10=30
Enter first number:
20
Enter second number:
10
Enter an operation (+,-,*,/,%,^):
/
20/10=2
ASSIGNMENT 3 : Maximum & Minimum number in set, Palindrome No.
and Prime No. programs.
3.1. Write a shell program to find a maximum and minimum number in
a given series of numbers.
Code:
echo "Enter the number of integers:"
read n
count=$n
echo "Enter the integers:"
while [ $count != 0 ]
do
read input
if [ $count == $n ]
then
max=$input
min=$input
fi
if [ $input -gt $max ]
then
max=$input
fi
if [ $input -lt $min ]
then
min=$input
fi
count=`expr $count - 1`
done
echo "The max value is" $max
echo "The min value is" $min
Output:
Enter the number of integers:
3
Enter the integers:
20
30
10
The max value is 30
The min value is 10
3.2. Write a shell program to find the prime numbers in a given range
of numbers.
Code:
echo "Enter the number to test:"
read num
count=2
end=`expr $num / 2`
prime=true
while [ $count -le $end ]
do
if [ `expr $num % $count` == 0 ]
then
prime=false
break
fi
count=`expr $count + 1`
done
if [ $prime == true ]
then
echo "The number is prime"
else
echo "The number is NOT prime"
fi
Output:
Enter the number to test:
17
The number is prime
Enter the number to test:
12
The number is NOT prime
Code:
#!/bin/bash
echo -n "Enter n to calculate sum of first n natural numbers: "
read n
sum=0
for((i=1;i<=n;i++))
do
sum=`expr $sum + $i`
echo -n "$i+"
done
echo -e "\b=$sum"
Output:
Enter n to calculate sum of first n natural numbers: 4
1+2+3+4=10
done
echo "The element was NOT found"
Output:
Enter the number of inputs: 5
Enter input: 44
Enter input: 22
Enter input: 33
Enter input: 11
Enter input: 7
Enter the number to search: 2
The sorted array is 7 11 22 33 44
The element was NOT found
Code:
#include <stdio.h>
#include <unistd.h>
int main(void)
{
int id;
id = fork();
if (id == 0) {
printf("CHILD PID=%d PPID=%d\n",getpid(),getppid());
sleep(5);
printf("CHILD PID=%d PPID=%d\n",getpid(),getppid());
}
else
printf("PARENT PID=%d PPID=%d\n",getpid(),getppid());
return 0;
}
Output:
CHILD PID=10662 PPID=1803
CHILD PID=10666 PPID=1803
CHILD PID=10670 PPID=1803
PARENT PID=10695 PPID=4853
CHILD PID=10696 PPID=10695
CHILD PID=10696 PPID=1803
PARENT PID=10728 PPID=4853
CHILD PID=10729 PPID=10728
CHILD PID=10729 PPID=1803
#include <stdio.h>
#include <unistd.h>
int main()
{
int pid, i;
pid = fork();
for (i=0; i<10; i++)
if (pid > 0)
{
pid = fork();
if (pid == 0)
printf("I am process %d withPID=%dPPID=
%d\n",i+1,getpid(),getppid());
sleep(5);
}
return 0;
}
Output:
I
I
I
I
I
I
I
I
I
I
am
am
am
am
am
am
am
am
am
am
process
process
process
process
process
process
process
process
process
process
return 0;
Output:
PARENT PID=11479 Child1=11480 Child2=11481
FIRST CHILD PID=11480 PPID=11479
SECOND CHILD PID=11481 PPID=11479
PARENT PID=11506 Child1=11507 Child2=11508
SECOND CHILD PID=11508 PPID=11506
FIRST CHILD PID=11507 PPID=11506