Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 30

ASSIGNMENT 1.

1 : Introduction to Unix/Linux architecture, features,


Unix File System.
ASSIGNMENT 1.2 : Familiarization with basic Unix/Linux commands,
creating a script.
UNIX
UNIX is an operating system which was first developed in the 1960s,
and has been under constant development ever since. By operating
system, we mean the suite of programs which make the computer
work. It is a stable, multi-user, multi-tasking system for servers,
desktops and laptops.
UNIX systems also have a graphical user interface (GUI) similar to
Microsoft Windows which provides an easy to use environment.
However, knowledge of UNIX is required for operations which aren't
covered by a graphical program, or for when there is no windows
interface available, for example, in a telnet session.

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

Fig: Approximate structure of Generic Unix Kernel

The kernel of UNIX is the hub of the operating system: it allocates


time and memory to programs and handles the filestore and
communications in response to system calls.
As an illustration of the way that the shell and the kernel work
together, suppose a user types rm myfile (which has the effect of
removing the file myfile). The shell searches the file store for the
file containing the program rm, and then requests the kernel,
through system calls, to execute the program rm on myfile. When
the process rm myfile has finished running, the shell then returns
the UNIX prompt % to the user, indicating that it is waiting for
further commands.

UNIX FILE SYSTEM


The UNIX filesystem contains several different types of files:
Ordinary Files
o Used to store your information, such as some text you have
written or an image you have drawn. This is the type of file
that you usually work with.
o Always located within/under a directory file
o Do not contain other files
Directories
o Branching points in the hierarchical tree
o Used to organize groups of files
o May contain ordinary files, special files or other directories
o Never contain "real" information which you would work with
(such as text). Basically, just used for organizing files.
o All files are descendants of the root directory, ( named / )
located at the top of the tree.

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

UNIX SYSTEM FILE NAMES


UNIX permits file names to use most characters, but avoid
spaces, tabs and characters that have a special meaning to the
shell, such as:
&;()|?\'"`[]{}<>$-!/
Case Sensitivity: uppercase and lowercase are not the same!
These are three different files:
NOVEMBER
November
november
Length: can be up to 256 characters

Extensions: may be used to identify types of files


libc.a
- archive, library file
program.c - C language source file
alpha2.f
- Fortran source file
xwd2ps.o
- Object/executable code
mygames.Z - Compressed file
Hidden Files: have names that begin with a dot (.) For example:
.cshrc
.login
.mailrc
.mwmrc
Uniqueness: as children in a family, no two files with the same
parent directory can have the same name. Files located in
separate directories can have identical names.
Reserved Filenames:
/ - the root directory (slash)
. - current directory (period)
.. - parent directory (double period)
~ - your home directory (tilde)
UNIX SYSTEM PATH NAMES
Specify where a file is located in the hierarchically organized file
system
Must know how to use pathnames to navigate the UNIX file
system
Absolute Pathname: tells how to reach a file beginning from the
root; always begins with / (slash). For example:
/usr/local/doc/training/sample.f
Relative Pathname: tells how to reach a file from the directory
you are currently in ( current or working directory); never begins
with / (slash). For example:
training/sample.f
../bin
~/projects/report.001
For example, if your current directory is /usr/home/johnson and
you wanted to change to the directory /usr/home/quattro, you
could use either of these commands:

cd ../quattro
cd /usr/home/quattro

- relative pathname
- absolute pathname

UNIX FILE PARENT-CHILD RELATIONSHIP


In UNIX all files are related to each other. The file system on UNIX is the
collection of all these related files organized in a hierarchical structure.
It is represented by the
following:

FILE & DIRECTORY COMMANDS


DIRECTORY COMMANDS
1. pwd - print working directory. Tells you which directory you
are currently in.
pwd
2. mkdir - make directory. Will create the new directory in your
working directory by default.
mkdir data2
3. cd - change to specified directory. May specify either the
absolute or relative pathname. cd with no pathname
changes to your home directory.
cd ..
- change to parent directory
cd
- change to home directory
4. rmdir - remove directory. Directories must be empty before
you remove them. rmdir project1
To recursively remove nested directories, use the rm
command with the -r option:
rm -r dirctory_name
FILE COMMANDS
1. ls - lists files
ls -l - show contents of working directory
ls -d - show all directory
2. cat - This command is useful for viewing as well as for
concatenating (hence the name "cat") files together.

3.
4.

5.
6.

cat test test1


- shows contents of both files on terminal.
cat file1 file2 > file3 - adds file1 and file2 to make file3
cp - copies files. Will overwrite unless otherwise specified.
cp sample.f sample2.f - copies sample.f to sample2.f
mv - moves files. Will overwrite unless otherwise specified. Must
also have write permission in the destination directory.
mv sample.f sample2.f - moves sample.f to sample2.f
mv dir1 newdir/dir2
- moves contents of directory dir1 to
newdir/dir2
mv *.txt chapt1
- moves all files with .txt suffix to
directory chapt1
date
shows the system date.
cat
shows the calendar.
cat<month> <year> - Show calendar for the month of the
specific year.

ASSIGNMENT 2.1 : Permission handling concepts.


ASSIGNMENT 2.2 : Introduction to VI Editor.
FILE PERMISSION

|
Others (o)

Permission
|
____________________________
|
User (u)

Group (g)

CHARACTER NOTIFICATION OF FILE ACCESS MODES


r for read i.e. view the contents of the file
w for write i.e. modify or remove contents of the file
x for executing a program
OCTAL NOTATION

FILE ACCESS MODE OPERATORS


+ - adds the specified mode to the specified class
- - removes the specified mode from the specified class
= - the modes specified are to be made the exact modes for the
specified class

PROCESS TO MODIFY ACCESS PERMISSIONS


E.g. Remove write permissions (w) for all classes (a), preventing
anyone from writing to the file:
$ls -l ourBestReferenceFile
-rw-rw-r-- 2 teamleader usguys 96 Apr 8 12:53
ourBestReferenceFile
$chmod a-w ourBestReferenceFile
$ls -l ourBestReferenceFile
-r--r--r-- 2 teamleader usguys 96 Apr 8 12:53
ourBestReferenceFile
E.g. Set the permissions for the owner and the group (ug) to read and
execute (rx) only (no write permission) on referenceLib, preventing
anyone to add files.
$ls -ld referenceLib
drwxr----- 2 teamleader usguys 96 Apr 8 12:53 referenceLib
$chmod ug=rx referenceLib
$ls -ld referenceLib
dr-xr-x--- 2 teamleader usguys 96 Apr 8 12:53 referenceLib
VI EDITOR
The vi editor (short for visual editor) is a screen editor which is
available on almost all Unix systems.
It has three operation modes:
1. Command Mode
2. Insert Mode
3. Ex Mode
The syntax used to create or open a file in vi editor is:
vi <filename.extension>

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

3.3. Write a shell program to check if a given number is palindrome or


not.
Code:
echo "Enter the number:"
read num
temp=$num
s=0
while [ $temp != 0 ]
do
r=`expr $temp % 10`
s=`expr $s \* 10 + $r`
temp=`expr $temp / 10`
done
if [ $s == $num ]
then
echo "The number is a palindrome"
else
echo "The number is NOT a palindrome"
fi
Output:
Enter the number:
121
The number is a palindrome
Enter the number:
221
The number is NOT a palindrome

ASSIGNMENT 4.1: Sum of the Series of Natural Numbers where the


Limit is given by the user

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

ASSIGNMENT 4.2:Bubble Sort an array of user inputs


Code:
#!/bin/bash
echo -n "Enter the number of inputs: "
read n
for((i=0; i<n; i++))
do
echo -n "Enter input: "
read a[i]
done
for((i=0; i<n-1; i++))
do
for((j=0; j<n-i-1; j++))
do
if [ ${a[j]} -gt ${a[j+1]} ]
then
temp=${a[j]}
a[j]=${a[j+1]}
a[j+1]=$temp
fi
done
done
echo "The sorted array is:" ${a[*]}
Output:
Enter the number of inputs: 5
Enter input: 4
Enter input: 2
Enter input: 3
Enter input: 6
Enter input: 1
The sorted array is: 1 2 3 4 6

ASSIGNMENT 4.3:Calculator from the terminal


Code:
#!/bin/bash
echo -n "Enter first number: "
read a
echo -n "Enter second number: "
read b
echo -n "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: 4
Enter second number: 2
Enter an operation (+,-,*,/,%,^): ^
4 ^ 2 = 16
Enter first number: 55
Enter second number: 5
Enter an operation (+,-,*,/,%,^): %
55 % 5 = 0

ASSIGNMENT 4.4:Binary search


Code:
#!/bin/bash
echo -n "Enter the number of inputs: "
read n
for((i=0; i<n; i++))
do
echo -n "Enter input: "
read arr[i]
done
echo -n "Enter the number to search: "
read search
# insertion sort
for((i=1; i<n; i++))
do
temp=${arr[i]}
for((j=i-1; j>=0; j--))
do
if [ ${arr[j]} -gt $temp ]
then
arr[j+1]=${arr[j]}
else
break
fi
done
arr[j+1]=$temp
done
echo "The sorted array is" ${arr[*]}
# binary search on sorted array
lower=0
upper=`expr $n - 1`
while [ $lower -le $upper ]
do
sum=`expr $lower + $upper`
mid=`expr $sum / 2`
if [ ${arr[mid]} == $search ]
then
echo "The element exists at index" $mid
exit

elif [ ${arr[mid]} -gt $search ]


then
upper=`expr $mid - 1`
else
lower=`expr $mid + 1`
fi

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

Enter the number of inputs: 8


Enter input: 43
Enter input: 62
Enter input: 31
Enter input: 67
Enter input: 3
Enter input: 876
Enter input: 23
Enter input: 65
Enter the number to search: 67
The sorted array is 3 23 31 43 62 65 67 876
The element exists at index 6

ASSIGNMENT 5.1:Check if a given word is a Palindrome or not


Code:
#!/bin/bash
echo -n "Enter the word: "
read str
len=`expr length $str`
i=1
p='true'
while [ $i -le $len ]
do
if [ `expr $str | cut -c $i` != `expr $str | cut -c $len` ]
then
p='false'
break
fi
i=`expr $i + 1`
len=`expr $len - 1`
done
if [ $p = 'true' ]
then
echo "The word is a palindrome"
else
echo "The word is NOT a palindrome"
fi
Output:
Enter the word: denmark
The word is NOT a palindrome
Enter the word: madam
The word is a palindrome

ASSIGNMENT 5.2: Count the number of vowels in a word


Code:
#!/bin/bash
echo -n "Enter the word: "
read str
len=`expr length $str`
count=0
for ((i=1; i<=len; i++))
do
c=`expr $str | cut -c $i`
if [ $c = 'a' -o $c = 'e' -o $c = 'i' -o $c = 'o' -o $c = 'u' ]
then
count=`expr $count + 1`
fi
done
echo "The number of vowels is" $count
Output:
Enter the word: umbrella
The number of vowels is 3
Enter the word: apple
The number of vowels is 2

ASSIGNMENT 5.3:Accept or Reject a string based on its length


Code:
#!/bin/bash
echo -n "Enter the string: "
read str
len=`echo "$str" | wc -m`
len=`expr $len - 1`
echo "The number of characters is" $len
if [ $len -ge 10 ]
then
echo "Accepted"
else
echo "Rejected"
fi
Output:
Enter the string: Hello is there anybody in there
The number of characters is 31
Accepted
Enter the string: apple delivery
The number of characters is 14
Accepted
Enter the string: madam
The number of characters is 5
Rejected

ASSIGNMENT 6.1:Print the process information of the current


process(program)
Code:
#include <stdio.h>
#include <unistd.h>
int main(void)
{
int pid, ppid;
pid = getpid();
ppid = getppid();
printf("PID=%d PPID=%d\n",pid,ppid);
return 0;
}
Output:
PID=9086 PPID=4853
PID=9094 PPID=4853
PID=9098 PPID=4853

ASSIGNMENT 6.2: Create a child process of the current process


Code:
#include <stdio.h>
#include <unistd.h>
int main(void)
{
int id;
id = fork();
printf("ID=%d PID=%d PPID=%d\n",id,getpid(),getppid());
return 0;
}
Output:
ID=9408 PID=9407 PPID=4853
ID=0 PID=9408 PPID=9407
ID=9413 PID=9412 PPID=4853
ID=0 PID=9413 PPID=9412
ID=9419 PID=9418 PPID=4853
ID=0 PID=9419 PPID=9418

ASSIGNMENT 6.3:Demonstrate an orphan process


Code:
#include <stdio.h>
#include <unistd.h>
int main(void)
{
int id;
id = fork();
if (id == 0)
sleep(5);
printf("ID=%d PID=%d PPID=%d\n",id,getpid(),getppid());
if (getppid() == 1)
printf("Orphan Process\n");
return 0;
}
Output:
ID=9764 PID=9763 PPID=4853
ID=0 PID=9764 PPID=1803
ID=9929 PID=9928 PPID=4853
ID=0 PID=9929 PPID=1803
ID=9994 PID=9993 PPID=4853
ID=0 PID=9994 PPID=1803

ASSIGNMENT 6.4:Demonstrate a zombie process


Code:
#include <stdio.h>
#include <unistd.h>
int main(void)
{
int id;
id = fork();
if (id > 0) {
sleep(5);
printf("PARENT: ID=%d PID=%d PPID=
%d\n",id,getpid(),getppid());
}
return 0;
}
Output:
PARENT: ID=10262 PID=10261 PPID=4853
PARENT: ID=10276 PID=10275 PPID=4853
PARENT: ID=10288 PID=10287 PPID=4853

ASSIGNMENT 6.5:Distinct parent and child process logic

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

ASSIGNMENT 7.1:Create Multiple Processes From One Parent


Code:

#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

1 with PID=11191 PPID=11189


2 with PID=11199 PPID=11189
3 with PID=11208 PPID=11189
4 with PID=11214 PPID=11189
5 with PID=11223 PPID=11189
6 with PID=11231 PPID=11189
7 with PID=11240 PPID=11189
8 with PID=11248 PPID=11189
9 with PID=11257 PPID=11189
10 with PID=11265 PPID=11189

ASSIGNMENT 7.2:Create Multiple Child Processes


Code:
#include <stdio.h>
#include <unistd.h>
int main()
{
int pid1, pid2;
int pvalue=getpid();
pid1=fork();
if (pid1 > 0)
pid2 = fork();
if (pid1==-1 || pid2==-1)
printf("ERROR\n");
if (pid1==0)
printf("FIRST CHILD PID=%d PPID=%d\n",getpid(),pvalue);
else if (pid2==0)
printf("SECOND CHILD PID=%d PPID=
%d\n",getpid(),pvalue);
if (pid1>0 && pid2>0)
printf("PARENT PID=%d Child1=%d Child2=
%d\n",pvalue,pid1,pid2);
}

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

You might also like