Unix
Unix
System call provides an interface between user program and operating system. When the user wants to
give an instruction to the OS then it will do it through system calls. Or a user program can access the kernel
which is a part of the OS through system calls.
Types of system calls
The different system calls are as follows −
• System calls for Process management
• System calls for File management
• System calls for Directory management
System calls for Process management
A system is used to create a new process or a duplicate process called a fork.
The duplicate process consists of all data in the file description and registers common. The original process
is also called the parent process and the duplicate is called the child process.
The fork call returns a value, which is zero in the child and equal to the child’s PID (Process Identifier) in the
parent. The system calls like exit would request the services for terminating a process.
Loading of programs or changing of the original image with duplicate needs execution of exec. Pid would
help to distinguish between child and parent processes.
Example
Process management system calls in Linux.
• fork − For crea+ng a duplicate process from the parent process.
• wait − Processes are supposed to wait for other processes to complete their work.
• exec − Loads the selected program into the memory.
• exit − Terminates the process.
The pictorial representation of process management system calls is as follows −
fork() − A parent process always uses a fork for crea+ng a new child process. The child process is generally
called a copy of the parent. After execution of fork, both parent and child execute the same program in
separate processes.
exec() − This func+on is used to replace the program executed by a process. The child sometimes may use
exec after a fork for replacing the process memory space with a new program executable making the child
execute a different program than the parent.
exit() − This function is used to terminate the process.
wait() − The parent uses a wait func+on to suspend execu+on +ll a child terminates. Using wait the parent
can obtain the exit status of a terminated child.
sum=0
echo $sum
Unix Architecture
• Kernel − The kernel is the heart of the opera+ng system. It interacts with the hardware and most of
the tasks like memory management, task scheduling and file management.
• Shell − The shell is the u+lity that processes your requests. When you type in a command at your
terminal, the shell interprets the command and calls the program that you want. The shell uses
standard syntax for all commands. C Shell, Bourne Shell and Korn Shell are the most famous shells
which are available with most of the Unix variants.
• Commands and Utilities − There are various commands and u+li+es which you can make use of in
your day to day activities. cp, mv, cat and grep, etc. are few examples of commands and utilities.
There are over 250 standard commands plus numerous others provided through 3rd party software.
All the commands come along with various options.
• Files and Directories − All the data of Unix is organized into files. All files are then organized into
directories. These directories are further organized into a tree-like structure called the filesystem.
User management includes everything from creating a user to deleting a user on your system.
useradd
Syntax:
userdel
Syntax:
1. userdel -r <userName>
usermod
Syntax:
By using userdel -r option, you can delete home directory along with user account.
Syntax:
1. userdel -r <userName>
You can change the shell mode with usermod command for a user.
Syntax:
Both the command chsh and chsh -s will work to change the shell.
Syntax:
1. chsh
The following table lists out the basic commands to use the vi editor −
1 vi filename
Creates a new file if it already does not exist, otherwise opens an existing file.
2 vi -R filename
Opens an existing file in the read-only mode.
3 view filename
Opens an existing file in the read-only mode.
$vi testfile
Shell Prompt:
$date
Shell Types:
1. The Bourne shell. If you are using a Bourne-type shell, the default
prompt is the $ character.
2. The C shell. If you are using a C-type shell, the default prompt is the
% character.
There are again various subcategories for Bourne Shell which are listed as
follows:
• C shell ( csh)
• TENEX/TOPS C shell ( tcsh)
Shell Scripts:
The basic concept of a shell script is a list of commands, which are listed in
the order of execution. A good shell script will have comments, preceded by
a pound sign, #, describing the steps.
fact=1
for((i=2;i<=num;i++))
{
fact=$((fact * i)) #fact = fact * i
}
echo $fact
Sum of n numbers using for loop- Shell Script
sum=0
echo $sum
i=1
max=0
echo $max
Shell program to check the year is the leap year or not
#!/bin/sh
read year
z=`expr $year % 4`
then
else
fi
############################
OUTPUT:
$sh year.sh
enter the year
2016
entered year - 2016 is a leap year
$sh year.sh
enter the year
2013
entered year - 2013 is not a leap year
Shell script to check whether a given number is
Armstrong or not
#!/bin/bash
echo "Enter a number: "
read c
x =$c
$c
sum=0
sum
r =0
n =0
while [ $x -gt
gt 0 ]
do
r =`expr $x % 10`
n =`expr $r \* $r \* $r`
sum=`expr
sum $sum + $n`
x =`expr $x / 10`
done
if [ $sum -eq
eq $c ]
then
echo "It is an Armstrong Number."
else
echo "It is not an Armstrong Number."
fi
for((i=0;i<10;i++))
do
if($((array[i]%2==0)))
then
$even=array[i]+$even
else
$odd=array[i]+$odd
fi
done