0% found this document useful (0 votes)
150 views47 pages

History of Unix, Uses and Unix Features History of Unix

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 47

History of Unix, Uses and Unix Features

History of Unix
The Unix operating system found its beginnings in MULTICS, which
stands for Multiplexed Operating and Computing System. The
MULTICS project began in the mid 1960s as a joint effort by General
Electric, Massachusetts Institute for Technology and Bell Laboratories.
In 1969 Bell Laboratories pulled out of the project.
One of Bell Laboratories people involved in the project was Ken
Thompson. He liked the potential MULTICS had, but felt it was too
complex and that the same thing could be done in simpler way.
In 1969 he wrote the first version of Unix, called UNICS. UNICS
stood for Uniplexed Operating and Computing System. Although the
operating system has changed, the name stuck and was eventually
shortened to Unix.
Ken Thompson teamed up with Dennis Ritchie, who wrote the first C
compiler. In 1973 they rewrote the Unix kernel in C. The following
year a version of Unix known as the Fifth Edition was first licensed to
universities.
The Seventh Edition, released in 1978, served as a dividing point for
two divergent lines of Unix development. These two branches are
known as SVR4 (System V) and BSD.
Ken Thompson spent a year's sabbatical with the University of
California at Berkeley. While there he and two graduate students, Bill
Joy and Chuck Haley, wrote the first Berkely version of Unix, which
was distributed to students. This resulted in the source code being
worked on and developed by many different people.
The Berkeley version of Unix is known as BSD, Berkeley Software
Distribution. From BSD came the vi editor, C shell, virtual memory,
Sendmail, and support for TCP/IP.
For several years SVR4 was the more conservative, commercial, and
well supported. Today SVR4 and BSD look very much alike. Probably
the biggest cosmetic difference between them is the way the ps
command functions.
The Linux operating system was developed as a Unix look alike and
has a user command interface that resembles SVR4.
UNIX FEATURES :
The following are the advantages of Unix Features
1. Portability
2. Machine-independence
3. Multi-Tasking
4. Multi-User Operations
5. Hierarchical File System
6. UNIX shell
7. Pipes and Filters
8. Utilities
The Architecture of Unix Operating System

The Architecture of Unix Operating System


Unix has a graphical user interface similar to the Windows operating
system that makes it easy for navigation and a good supportive
environment. The internal design view of this operating system can be
known from its architecture.
The architecture of this operating system is four layered. It consists of
Hardware, Kernel, System Call interface(shell) and application
libraries/tools, utilities, etc…The kernel controls the hardware of the
computer and resides at the core of the architecture. System calls acts
as the interface between the kernel and other libraries. These libraries
include general functions and built on top of the system calls. Shell is
a special application that provides an interface to the other applications
of the architecture.
Kernel
For this operating system, Kernel is the central core that interacts
directly with the hardware of the system.
Main functions of Kernal are-
 Computer hardware such as memory, disc, printers, etc.. are
controlled by the kernel.
 The kernel schedules the processes, control and executes various
user-defined tasks.
 Manages the data storage and control the computer accesses by
several users.
 The kernel is composed of several sub-components such as
configurations including boot code, device drivers to control
hardware, header files.

Shell
It is the interface between the user and the kernel. Users can interact
with the shell using shell commands. Shell has two main
responsibilities which include interpreting the commands given by the
users and execute them using the kernel, providing programming
ability to the users to write shell commands for a shell script to perform
specific tasks.
Commands
Some of the major categories of commands used by the Unix operating
system are – ‘sh’ – shell commands providing a primary user interface,
‘utilities’ forming the core toolkit of Unix commands includes sub-
categories such as system utilities supporting administrative tools and
User utilities for environment management tools.

It also has commands for general purpose applications such as


document-formatting and typesetting. Some Unix systems also include
packages such as TeX and Ghostscript. This system also provides the
facility for producing device-independent simple vector plots. It also
supports inter-system communication as well as inter-user
communication.
Properties
From the time of its development, Unix has been adopted by many
companies. It still plays a big role in many data centers and research
labs. Some of the properties of Unix that make it so preferable over
other systems are-
Multiuser accessing – Multiple users can work on the system
simultaneously by connecting through a point known as terminal.
Multi-tasking-It provides the facility to run multiple programs or
processes by multiple users on one system.
Portability – It provides the flexibility to be used on multiple hardware
architectures. As it uses a high-level language, it is easy to comprehend
and modify the Unix code according to the hardware architecture of the
computer. Thus to work on new hardware architecture, the user has to
simply modify the Unix code and run on the system.
Processes– Files are the collection of data created by the user. It
includes documents, programming instructions, etc…Processes are the
executions of the programs or files. Unix follows the hierarchical file
structure that starts with a root directory followed by the subdirectories
ending with the file name.
Communication – Requests and commands given by the user are
carried out by the kernel and shell together. The user communicates
with the system using Shell. It also provides inter-system
communication through UUCP. This operating system also obeys the
TCP/IP protocol.
It also provides easy maintenance of the files
This operating system provides Pipes and Filters to the user to design
complex programs from simple programs.
It provides a wide variety of tools for software development and
software maintenance.
It is very popularly used on Desktop computers, laptops, mainframes,
and other computing hardware.
Layout of the UNIX file system
Layout of the UNIX file system
Files in Unix System are organized into multi-level hierarchy structure
known as a directory tree. At the very top of the file system is a
directory called “root” which is represented by a “/”. All other files are
“descendants” of root.

Each physical drive can be divided into several partitions


 Each partition can contain one file system
 Each file system contains:
1. boot block(s);
2. superblock;
3. inode list;
4. data blocks.
A boot block may contain the bootstrap code that is read into the
machine upon booting.
A superblock describes the state of the file system:
 how large it is;
 how many files it can store;
 where to find free space on the file system;
 who has ownership of it;
 and more.
 The inode list is an array of "information nodes" analogous to the
FAT (File Allocation Table) system in MS-DOS.
 Data blocks start at the end of the inode list and contain file data
and directory blocks.

The term file system can mean a single disk, or it can mean the entire
collection of devices on a system. It's held together in this second sense
by the directory structure.
The directory "tree" usually spans many disks and/or partitions by
means of mount points. For example, in Red Hat Linux, there are pre-
defined mount points for floppy disks and CD-ROMs at floppy and
cdrom in /mnt. See also fstab and mtab in /etc.
Partition Structure

Boot Block(s)
Blocks on a Linux (and often a Unix) filesystem are 1024 bytes in
length, but may be longer or shorter. The blocks are normally a power
of 2 in size (1024 is 2 to the 10th power). Some systems use 512 bytes
(2 to the 9th) but 2048 and 4096 are also seen.
The first few blocks on any partition may hold a boot program, a short
program for loading the kernel of the operating system and launching
it. Often, on a Linux system, it will be controlled by LILO or Grub,
allowing booting of multiple operating systems. It's quite simple (and
common) to have a multiple boot environment for both Linux and a
flavour of Windows.
Superblock
The boot blocks are followed by the superblock, which contains
information about the geometry of the physical disk, the layout of the
partition, number of inodes and data blocks, and much more.
Inode Blocks
Disk space allocation is managed by the inodes (information node),
which are created by the mkfs(1) (make filesystem) command. Inodes
cannot be manipulated directly, but are changed by many commands,
such as touch(1) and cp(1) and by system calls, like open(2) and
unlink(2), and can be read by stat(2). Both chmod(1) and chmod(2)
change access permissions.
Data Blocks
This is where the file data itself is stored. Since a directory is simply a
specially formatted file, directories are also contained in the data
blocks. An allocated data block can belong to one and only one file in
the system. If a data block is not allocated to a file, it is free and
available for the system to allocate when needed.
Structure of the super block
struct ext2_super_block {
__u32 s_inodes_count; /* Inodes count */
__u32 s_blocks_count; /* Blocks count */
__u32 s_r_blocks_count; /* Reserved blocks count */
__u32 s_free_blocks_count; /* Free blocks count */
__u32 s_free_inodes_count; /* Free inodes count */
__u32 s_first_data_block; /* First Data Block */
__u32 s_log_block_size; /* Block size */
__s32 s_log_frag_size; /* Fragment size */
__u32 s_blocks_per_group; /* # Blocks per group */
__u32 s_frags_per_group; /* # Fragments per group */
__u32 s_inodes_per_group; /* # Inodes per group */
__u32 s_mtime; /* Mount time */
__u32 s_wtime; /* Write time */
__u16 s_mnt_count; /* Mount count */
__s16 s_max_mnt_count; /* Maximal mount count */
__u16 s_magic; /* Magic signature */
__u16 s_state; /* File system state */
__u16 s_errors; /* Behaviour if error detected */
__u16 s_minor_rev_level; /* minor revision level */
__u32 s_lastcheck; /* time of last check */
__u32 s_checkinterval; /* max. time between checks */
__u32 s_creator_os; /* OS */
__u32 s_rev_level; /* Revision level */
__u16 s_def_resuid; /* Def uid for reserved blocks */
__u16 s_def_resgid; /* Def gid for reserved blocks */
__u32 s_first_ino; /* First non-reserved inode */
__u16 s_inode_size; /* size of inode structure */
__u16 s_block_group_nr; /* block grp # of this s'block */
__u32 s_feature_compat; /* compatible feature set */
__u32 s_feature_incompat; /* incompatible feature set */
__u32 s_feature_ro_compat; /* readonly-compat feature set */
__u8 s_uuid[16]; /* 128-bit uuid for volume */
char s_volume_name[16]; /* volume name */
char s_last_mounted[64]; /* dir where last mounted */
[some compression stuff]
__u16 s_padding1; /* Padding for alignment */
[some journaling stuff]
__u32 s_reserved[197]; /* Padding to end of the block */
};
Most of this structure, which is defined fully in
/usr/include/ext2fs/ext2_fs.h, is only used by the operating system, and
is neither readable nor modifiable by a user or a program.
Structure of an inode on the disk
Each file (a unique collection of data blocks) has only 1 inode, which
completely defines the file except for its name(s). The filenames are
actually links in the directory structure to the inode for the file.
The representation of the inode to use is the stat(2) structure, which can
be seen in /usr/include/bits/stat.h
struct ext2_inode {
__u16 i_mode; /* File mode */
__u16 i_uid; /* Low bits of Uid */
__u32 i_size; /* Size in bytes */
__u32 i_atime; /* Access time */
__u32 i_ctime; /* Creation time */
__u32 i_mtime; /* Modification time */
__u32 i_dtime; /* Deletion Time */
__u16 i_gid; /* Low bits of Gid */
__u16 i_links_count; /* Links count */
__u32 i_blocks; /* Blocks count */
__u32 i_flags; /* File flags */
union {

...

} osd1; /* OS dependent 1 */
__u32 i_block[EXT2_N_BLOCKS];/* Data */
__u32 i_generation;/* Version (NFS) */
[ACLs and stuff]
union {

...

} osd2; /* OS dependent 2 */
};
Directory entry
Only ever use the d_name field in the directory entry, for use if you are
searching through a directory for a filename. Use the filename in a call
to stat(2) or lstat(2) to get the struct stat information.
struct dirent {
long d_ino; /* don't use */

__kernel_off_t d_off; /* don't use */


unsigned short d_reclen; /* don't */
char d_name[256];/* OK */
};
Special inode numbers
There are several pre-defined inodes with special purposes. Because of
the way the superblock and inodes are defined, you do not need to know
these unless you are creating or modifying filesystem software.
EXT2_BAD_INO 1 Bad blocks inode
EXT2_ROOT_INO 2 Root inode
EXT2_ACL_IDX_INO 3 ACL index inode
EXT2_ACL_DATA_INO 4 ACL data inode
EXT2_BOOT_LOADER_INO 5 Boot loader inode
EXT2_UNDEL_DIR_INO 6 Undelete dir inode
EXT2_RESIZE_INO 7 Res grp desc inode
EXT2_JOURNAL_INO 8 Journal inode
First non-reserved inode for ext2 filesystems
EXT2_GOOD_OLD_FIRST_INO 11
Inode Contents
Disk inodes contain the following information:
 Owner identifier
 Type of file (regular, directory, character or block device)
 Access permissions
 Times and dates
· file creation time

· last file access time


· last inode modification time
 Number of links to the file
 Array of pointers to data blocks on disk
 File size (in bytes, sometimes also in blocks)
File Ownership and File Permissions
Access Permissions
permissions are made up of "r", "w", "x", for read, write and executable
(access for directories), in 3 categories:
user, group, and other
permissions are changed by thechmod(1)program or the chmod(2)
function.
Directories
Always contain at least two entries: "." (self) and ".." (parent)
Have at least as many links as the total number of subdirectories
The root directory is different in that self and parent are identical
Two categories of Users
1. Superuser, or root: virtually unrestricted in any operation
2. Normal user: subject to all normal restrictions.
Linking files
In Linux and Unix, a data file is a bunch of data blocks on a disk,
managed by an inode. Its name is stored only in the directory. Or in
many directories. Both "soft" (symbolic) links and "hard" links can be
made using the ln(1) command or the link(2) and symlink(2) system
calls.
Original file
System Prompt: ls -l

-rw-r--r-- 1 allisor staff 0 D/T abc


Create hard link
System Prompt: ln abc h-abc
System Prompt: ls -l
-rw-r--r-- 2 allisor staff 0 D/T abc
-rw-r--r-- 2 allisor staff 0 D/T h-abc
Create symbolic (soft) link
System Prompt: ln -s abc s-abc
System Prompt: ls -l
-rw-r--r-- 2 allisor staff 0 D/T abc
lrwxr-xr-x 1 allisor staff 3 D/T s-abc -> abc
-rw-r--r-- 2 allisor staff 0 D/T h-abc
Examine inodes
System Prompt: ls -i1
25263 abc
25263 h-abc
25265 s-abc
Remove original file
System Prompt: rm abc
System Prompt: ls -l
-rw-r--r-- 1 allisor staff 0 D/T h-abc
lrwxr-xr-x 1 allisor staff 3 D/T s-abc -> abc

File ownership is an important component of Unix that provides a


secure method for storing files. Every file in Unix has the following
attributes −
Owner permissions − The owner's permissions determine what
actions the owner of the file can perform on the file.
Group permissions − The group's permissions determine what actions
a user, who is a member of the group that a file belongs to, can perform
on the file.
Other (world) permissions − The permissions for others indicate what
action all other users can perform on the file.
The Permission Indicators
While using ls -l command, it displays various information related to
file permission as follows −
$ls -l /home/amrood
-rwxr-xr-- 1 amrood users 1024 Nov 2 00:10 myfile
drwxr-xr--- 1 amrood users 1024 Nov 2 00:10 mydir
Here, the first column represents different access modes, i.e., the
permission associated with a file or a directory.
The permissions are broken into groups of threes, and each position in
the group denotes a specific permission, in this order: read (r), write
(w), execute (x) −
The first three characters (2-4) represent the permissions for the file's
owner. For example, -rwxr-xr-- represents that the owner has read (r),
write (w) and execute (x) permission.
The second group of three characters (5-7) consists of the permissions
for the group to which the file belongs. For example, -rwxr-xr--
represents that the group has read (r) and execute (x) permission, but
no write permission.

The last group of three characters (8-10) represents the permissions for
everyone else. For example, -rwxr-xr-- represents that there is read (r)
only permission.
File Access Modes
The permissions of a file are the first line of defense in the security of
a Unix system. The basic building blocks of Unix permissions are the
read, write, and execute permissions, which have been described below
Read
Grants the capability to read, i.e., view the contents of the file.
Write
Grants the capability to modify, or remove the content of the file.
Execute
User with execute permissions can run a file as a program.

Directory Access Modes


Directory access modes are listed and organized in the same manner as
any other file. There are a few differences that need to be mentioned −

Read
Access to a directory means that the user can read the contents. The
user can look at the filenames inside the directory.

Write
Access means that the user can add or delete files from the directory.

Execute
Executing a directory doesn't really make sense, so think of this as a
traverse permission.
A user must have execute access to the bin directory in order to execute
the ls or the cd command.

Changing Permissions
To change the file or the directory permissions, you use the chmod
(change mode) command. There are two ways to use chmod — the
symbolic mode and the absolute mode.
Using chmod in Symbolic Mode
The easiest way for a beginner to modify file or directory permissions
is to use the symbolic mode. With symbolic permissions you can add,
delete, or specify the permission set you want by using the operators in
the following table.delete, or specify the permission set you want by
using the operators in the following table.
Sr.No. Chmod operator & Description
1 +
Adds the designated permission(s) to a file or directory.
2 -
Removes the designated permission(s) from a file or directory.
3 =
Sets the designated permission(s).
Here's an example using testfile. Running ls -1 on the testfile shows
that the file's permissions are as follows −
$ls -l testfile
-rwxrwxr-- 1 amrood users 1024 Nov 2 00:10 testfile
Then each example chmod command from the preceding table is run
on the testfile, followed by ls –l, so you can see the permission changes
$chmod o+wx testfile
$ls -l testfile
-rwxrwxrwx 1 amrood users 1024 Nov 2 00:10 testfile
$chmod u-x testfile
$ls -l testfile
-rw-rwxrwx 1 amrood users 1024 Nov 2 00:10 testfile
$chmod g = rx testfile
$ls -l testfile
-rw-r-xrwx 1 amrood users 1024 Nov 2 00:10 testfile
Here's how you can combine these commands on a single line −
$chmod o+wx,u-x,g = rx testfile
$ls -l testfile
-rw-r-xrwx 1 amrood users 1024 Nov 2 00:10 testfile

Using chmod with Absolute Permissions


The second way to modify permissions with the chmod command is to
use a number to specify each set of permissions for the file.
Each permission is assigned a value, as the following table shows, and
the total of each set of permissions provides a number for that set.
Number Octal Permission Representation Ref
0 No permission ---
1 Execute permission --x
2 Write permission -w-
3 Execute and write permission: 1 (execute) + 2 (write) -wx
=3
4 Read permission r--
5 Read and execute permission: 4 (read) + 1 (execute) r-x
=5
6 Read and write permission: 4 (read) + 2 (write) = 6 rw-
7 All permissions: 4 (read) + 2 (write) + 1 (execute) = rwx
7
Here's an example using the testfile. Running ls -1 on the testfile shows
that the file's permissions are as follows −
$ls -l testfile
-rwxrwxr-- 1 amrood users 1024 Nov 2 00:10 testfile
Then each example chmod command from the preceding table is run
on the testfile, followed by ls –l, so you can see the permission changes
$ chmod 755 testfile
$ls -l testfile
-rwxr-xr-x 1 amrood users 1024 Nov 2 00:10 testfile
$chmod 743 testfile
$ls -l testfile
-rwxr---wx 1 amrood users 1024 Nov 2 00:10 testfile
$chmod 043 testfile
$ls -l testfile
----r---wx 1 amrood users 1024 Nov 2 00:10 testfile

Changing Owners and Groups


While creating an account on UNIX, it assigns a owner ID and a group
ID to each user. All the permissions mentioned above are also assigned
based on the Owner and the Groups.

Two commands are available to change the owner and the group of files
· chown − The chown command stands for "change owner" and is
used to change the owner of a file.

· chgrp − The chgrp command stands for "change group" and is used
to change the group of a file.

Changing Ownership
The chown command changes the ownership of a file. The basic syntax
is as follows −

$ chown user filelist


The value of the user can be either the name of a user on the system or
the user id (uid) of a user on the system.

The following example will help you understand the concept −


$ chown amrood testfile
$ Changes the owner of the given file to the user amrood.

NOTE − The super user, root, has the unrestricted capability to change
the ownership of any file but normal users can change the ownership
of only those files that they own.

Changing Group Ownership


The chgrp command changes the group ownership of a file. The basic
syntax is as follows −

$ chgrp group filelist


The value of group can be the name of a group on the system or the
group ID (GID) of a group on the system.

Following example helps you understand the concept −

$ chgrp special testfile


$Changes the group of the given file to special group.

SUID and SGID File Permission


Often when a command is executed, it will have to be executed with
special privileges in order to accomplish its task.

As an example, when you change your password with the passwd


command, your new password is stored in the file /etc/shadow.

As a regular user, you do not have read or write access to this file for
security reasons, but when you change your password, you need to have
the write permission to this file. This means that the passwd program
has to give you additional permissions so that you can write to the file
/etc/shadow.

Additional permissions are given to programs via a mechanism known


as the Set User ID (SUID) and Set Group ID (SGID) bits.
When you execute a program that has the SUID bit enabled, you inherit
the permissions of that program's owner. Programs that do not have the
SUID bit set are run with the permissions of the user who started the
program.

This is the case with SGID as well. Normally, programs execute with
your group permissions, but instead your group will be changed just for
this program to the group owner of the program.

The SUID and SGID bits will appear as the letter "s" if the permission
is available. The SUID "s" bit will be located in the permission bits
where the owners’ execute permission normally resides.

For example, the command −

$ ls -l /usr/bin/passwd
-r-sr-xr-x 1 root bin 19031 Feb 7 13:47 /usr/bin/passwd*
$
Shows that the SUID bit is set and that the command is owned by the
root. A capital letter S in the execute position instead of a lowercase s
indicates that the execute bit is not set.

If the sticky bit is enabled on the directory, files can only be removed
if you are one of the following users −

The owner of the sticky directory


The owner of the file being removed
The super user, root
To set the SUID and SGID bits for any directory try the following
command −

$ chmod ug+s dirname


$ ls -l
drwsr-sr-x 2 root root 4096 Jun 19 06:45 dirname
Directory and file related commands
 The directory is a file the solo job of which is to store the file
names and the related information.
 All the files, whether ordinary, special, or directory, are contained
in directories.
 UNIX uses a hierarchical structure for organizing files and
directories. This structure is often referred to as a directory tree.
 The tree has a single root node, the slash character (/), and all
other directories are contained below it.

Home Directory
The directory in which you find yourself when you first login is called
your home directory.
You will be doing much of your work in your home directory and
subdirectories that you'll be creating to organize your files.
You can go in your home directory anytime using the following
command −

$cd ~
$
Here ~ indicates the home directory. Suppose you have to go in any
other user's home directory, use the following command −

$cd ~username
$
To go in your last directory, you can use the following command −

$cd -
$
Absolute/Relative Pathnames
Directories are arranged in a hierarchy with root (/) at the top. The
position of any file within the hierarchy is described by its pathname.
Elements of a pathname are separated by a /. A pathname is absolute,
if it is described in relation to root, thus absolute pathnames always
begin with a /.

Following are some examples of absolute filenames.

/etc/passwd
/users/sjones/chem/notes
/dev/rdsk/Os3
A pathname can also be relative to your current working directory.
Relative pathnames never begin with /. Relative to user amrood's home
directory, some pathnames might look like this −

chem/notes
personal/res
To determine where you are within the filesystem hierarchy at any
time, enter the command pwd to print the current working
directory −

$pwd
/user0/home/amrood
$
Listing Directories
To list the files in a directory, you can use the following syntax −

$ls dirname
Example
Following is the example to list all the files contained in /usr/local
directory −
$ls /usr/local
X11 bin gimp jikes sbin
ace doc include lib share
atalk etc info man ami
Creating Directories
We will now understand how to create directories. Directories are
created by the following command −

$mkdir dirname
Here, directory is the absolute or relative pathname of the directory you
want to create. For example, the command −

$mkdir mydir
$
Creates the directory mydir in the current directory. Here is another
example −

$mkdir /tmp/test-dir
$
This command creates the directory test-dir in the /tmp directory. The
mkdir command produces no output if it successfully creates the
requested directory.

If you give more than one directory on the command line, mkdir creates
each of the directories. For example, −

$mkdir docs pub


$
Creates the directories docs and pub under the current directory.

Creating Parent Directories


We will now understand how to create parent directories. Sometimes
when you want to create a directory, its parent directory or directories
might not exist. In this case, mkdir issues an error message as follows

$mkdir /tmp/amrood/test
mkdir: Failed to make directory "/tmp/amrood/test";
No such file or directory
$
In such cases, you can specify the -p option to the mkdir command. It
creates all the necessary directories for you. For example −

$mkdir -p /tmp/amrood/test
$
The above command creates all the required parent directories.

Removing Directories
Directories can be deleted using the rmdir command as follows −

$rmdir dirname
$
Note − To remove a directory, make sure it is empty which means there
should not be any file or sub-directory inside this directory.

You can remove multiple directories at a time as follows −

$rmdir dirname1 dirname2 dirname3


$
The above command removes the directories dirname1, dirname2, and
dirname3, if they are empty. The rmdir command produces no output
if it is successful.

Changing Directories
You can use the cd command to do more than just change to a home
directory. You can use it to change to any directory by specifying a
valid absolute or relative path. The syntax is as given below −

$cd dirname
$
Here, dirname is the name of the directory that you want to change to.
Example
$cd /usr/local/bin
$
Changes to the directory /usr/local/bin. From this directory, you can cd
to the directory /usr/home/amrood using the following relative path −

$cd ../../home/amrood
$
Renaming Directories
The mv (move) command can also be used to rename a directory. The
syntax is as follows −

$mv olddir newdir


$
You can rename a directory mydir to yourdir as follows −

$mv mydir yourdir


$
The directories . (dot) and .. (dot dot)
The filename . (dot) represents the current working directory; and the
filename .. (dot dot) represents the directory one level above the current
working directory, often referred to as the parent directory.

If we enter the command to show a listing of the current working


directories/files and use the -a option to list all the files and the -l option
to provide the long listing, we will receive the following result.

$ls -la
drwxrwxr-x 4 teacher class 2048 Jul 16 17.56 .
drwxr-xr-x 60 root 1536 Jul 13 14:18 ..
---------- 1 teacher class 4210 May 1 08:27 .profile
-rwxr-xr-x 1 teacher class 1948 May 12 13:42 memo
$
Filters

Filters are programs that take plain text (either stored in a file or
produced by another program) as standard input, transforms it into a
meaningful format, and then returns it as standard output. Some of the
most commonly used filters are explained below:

1. cat : Displays the text of the file line by line.


Syntax: cat [path]

2. head : Displays the first n lines of the specified text files. If the
number of lines is not specified then by default prints first 10 lines.
Syntax: head [-number_of_lines_to_print] [path]

3. tail : It works the same way as head, just in reverse order. The only
difference in tail is, it returns the lines from bottom to up.
Syntax: tail [-number_of_lines_to_print] [path]

4. sort : Sorts the lines alphabetically by default but there are many
options available to modify the sorting mechanism.
Syntax: sort [-options] [path]

5. uniq : Removes duplicate lines. uniq has a limitation that it can only
remove continuous duplicate lines(although this can be fixed by the use
of piping).
Syntax: uniq [options] [path]

6. wc : wc command gives the number of lines, words and characters


in the data.
Syntax: wc [-options] [path]

7. grep : grep is used to search a particular information from a text file.


The grep filter searches a file for a particular pattern of characters, and
displays all lines that contain that pattern. The pattern that is searched
in the file is referred to as the regular expression (grep stands for
globally search for regular expression and print out).
Syntax:

Syntax: grep [options] pattern [path]


Options Description
-c : This prints only a count of the lines that match a pattern
-h : Display the matched lines, but do not display the filenames.
-i : Ignores, case for matching
-l : Displays list of a filenames only.
-n : Display the matched lines and their line numbers.
-v : This prints out all the lines that do not matches the pattern
-e exp : Specifies expression with this option. Can use multiple times.
-f file : Takes patterns from file, one per line.
-E : Treats pattern as an extended regular expression (ERE)
-w : Match whole word
-o : Print only the matched parts of a matching line,
with each such part on a separate output line.

-A n : Prints searched line and nlines after the result.


-B n : Prints searched line and n line before the result.
-C n : Prints searched line and n lines after before the result.

8. tac : tac is just the reverse of cat and it works the same way, i.e.,
instead of printing from lines 1 through n, it prints lines n through 1. It
is just reverse of cat command.
Syntax: tac [path]

9. sed : sed stands for stream editor. It allows us to apply search and
replace operation on our data effectively. sed is quite an advanced filter
and all its options can be seen on its man page.
Syntax: sed [path]
Replacing or substituting string : Sed command is mostly used to
replace the text in a file. The below simple sed command replaces the
word “unix” with “linux” in the file.

$sed 's/unix/linux/' geekfile.txt

Replacing the nth occurrence of a pattern in a line :


Use the /1, /2 etc flags to replace the first, second occurrence of a
pattern in a line. The below command replaces the second occurrence
of the word “unix” with “linux” in a line.
$sed 's/unix/linux/2' geekfile.txt

Replacing all the occurrence of the pattern in a line :


The substitute flag /g (global replacement) specifies the sed command
to replace all the occurrences of the string in the line.
$sed 's/unix/linux/g' geekfile.txt

10. nl : nl is used to number the lines of our text data.


Syntax: nl [-options] [path]
Pipe

The pipe command lets you sends the output of one command to
another.
Piping, as the term suggests, can redirect the standard output, input, or
error of one process to another for further processing.

The syntax for the pipe or unnamed pipe command is the | character
between any two commands:

Syntax
Command-1 | Command-2 | …| Command-N

A Unix pipe provides a one-way flow of data.


Example
who | sort | lpr

Then the Unix shell would create three processes with two pipes
between them:

A pipe can be explicitly created in Unix using the pipe system call. Two
file descriptors are returned--fildes[0] and fildes[1], and they are both
open for reading and writing. A read from fildes[0] accesses the data
written to fildes[1] on a first-in-first-out (FIFO) basis and a read from
fildes[1] accesses the data written to fildes[0] also on a FIFO basis.

When a pipe is used in a Unix command line, the first process is


assumed to be writing to stdout and the second is assumed to be reading
from stdin. So, it is common practice to assign the pipe write device
descriptor to stdout in the first process and assign the pipe read device
descriptor to stdin in the second process. This is elaborated below in
the discussion of multiple command pipelines.

UNIX process creation

UNIX process creation

• process equivalent to task, as in multiprocessing or multitasking

• A new process is created by the fork call

• Child and parent are identical

– child returns a 0

– parent returns nonzero

• Both parent and child execute next line

• Often the child executes an exec

– creates a brand new process in its space

Parent can execute a wait


Example: Process Creation in Unix
fork()
fork(): System call to create a child process.

wait()
wait() system call suspends execution of current process until a child has exited
or until a signal has delivered whose action is to terminate the current process or
call signal handler.

The interface between a process and an operating system is provided by system


calls. In general, system calls are available as assembly language instructions.
They are also included in the manuals used by the assembly level programmers.
Unix System Calls
System calls in Unix are used for file system control, process control,
interprocess communication etc. Access to the Unix kernel is only available
through these system calls. Generally, system calls are similar to function calls,
the only difference is that they remove the control from the user process.

There are around 80 system calls in the Unix interface currently. Details about
some of the important ones are given as follows -

System calls are functions that transfer control from the user process to the
operating system kernel. Functions such as read() and write() are system calls.
The process invokes them with the appropriate arguments, control transfers to the
kernel where the system call is executed, results are passed back to the calling
process, and finally, control is passed back to the user process.
Library functions typically provide a richer set of features. For example, the
fread() library function reads a number of elements of data of specified size from
a file. While presenting this formatted data to the user, internally it will call the
read() system call to actually read data from the file.

Library functions are implemented on top of system calls. The decision


whether to use system calls or library functions is largely dependent on the
application being written. Applications wishing to have much more control over
how they perform I/O in order to optimize for performance may well invoke
system calls directly. If an application writer wishes to use many of the features
that are available at the library level, this could save a fair amount of
programming effort. System calls can consume more time than invoking library
functions because they involve transferring control of the process from user mode
to kernel mode.
Data Structure of a Process and Context Switch in UNIX process

Data Structures of a Process

· At any time, there are many processes, each in its particular state.
· The OS must have data structures representing each process

This data structure is called the PCB (Process Control Block)

The PCB contains all of the info about a process, plus hardware state (PC, regs)
when the process is not running

Process Control Block (PCB)

Every process is represented in the operating system by a process control block,


which is also called a task control block.
Important components of PCB
Process state: A process can be new, ready, running, waiting, etc.

Program counter: The program counter lets you know the address of the next
instruction, which should be executed for that process.

CPU registers: This component includes accumulators, index and general-


purpose registers, and information of condition code.

CPU scheduling information: This component includes a process priority,


pointers for scheduling queues, and various other scheduling parameters.

Accounting and business information: It includes the amount of CPU and time
utilities like real time used, job or process numbers, etc.

Memory-management information: This information includes the value of the


base and limit registers, the page, or segment tables. This depends on the memory
system, which is used by the operating system.

I/O status information: This block includes a list of open files, the list of I/O
devices that are allocated to the process, etc.

Context Switch in Unix


A context switch occurs when the kernel transfers control of the CPU from
an executing process to another that is ready to run.
The context is the set of CPU register values and other data that describes the
process' state. The kernel then loads the context of the new process which then
starts to execute.
There are 4 situations under which kernel permits a context switch are:
1. When a process is in sleep mode
2. When a process goes to exit
3. When a process returns from a system call to user mode but is not the most
eligible process to run
4. When a process returns from an interrupt handler to user mode but is not
the most eligible process to run

Process Scheduler

When a process is created, the system assigns a lightweight process (LWP) to the
process. If the process is multithreaded, more LWPs might be assigned to the
process. An LWP is the object that is scheduled by the UNIX system scheduler,
which determines when processes run. The scheduler maintains process priorities
that are based on configuration parameters, process behavior, and user requests.
The scheduler uses these priorities to determine which process runs next. The six
priority classes are real-time, system, interactive (IA), fixed-priority (FX), fair-
share (FSS), and time-sharing (TS).

The default scheduling is a time-sharing policy. This policy dynamically adjusts


process priorities to balance the response time of interactive processes. The policy
also dynamically adjusts priorities to balance the throughput of processes that use
a lot of CPU time. The time-sharing class has the lowest priority.

Process States

Process

A process is an instance of a program in execution. A set of processes combined


together make a complete program.

There are two categories of processes in Unix, namely

· User processes: They are operated in user mode.

· Kernel processes: They are operated in kernel mode.

Process States

The states that a Process enters in working from start till end are known as Process
states. These are listed below as:

· Created-Process is newly created by system call, is not ready to run

· User running-Process is running in user mode which means it is a user


process.

· Kernel Running-Indicates process is a kernel process running in kernel mode.

· Zombie- Process does not exist/ is terminated.


· Preempted- When process runs from kernel to user mode, it is said to be
preempted.

· Ready to run in memory- It indicated that process has reached a state where
it is ready to run in memory and is waiting for kernel to schedule it.

· Ready to run, swapped– Process is ready to run but no empty main memory
is present

· Sleep, swapped- Process has been swapped to secondary storage and is at a


blocked state.

· Asleep in memory- Process is in memory(not swapped to secondary storage)


but is in blocked state.

Process State Diagram

The working of Process is explained in following steps:


1. User-running: Process is in user-running.
2. Kernel-running: Process is allocated to kernel and hence, is in kernel mode.
3. Ready to run in memory: Further, after processing in main memory process is
rescheduled to the Kernel.i.e.The process is not executing but is ready to run as
soon as the kernel schedules it.
4. Asleep in memory: Process is sleeping but resides in main memory. It is waiting
for the task to begin.
5. Ready to run, swapped: Process is ready to run and be swapped by the processor
into main memory, thereby allowing kernel to schedule it for execution.
6. Sleep, Swapped: Process is in sleep state in secondary memory, making space for
execution of other processes in main memory. It may resume once the task is
fulfilled.
7. Pre-empted: Kernel preempts an on-going process for allocation of another
process, while the first process is moving from kernel to user mode.
8. Created: Process is newly created but not running. This is the start state for all
processes.
9. Zombie: Process has been executed thoroughly and exit call has been enabled.
The process, thereby, no longer exists. But, it stores a statistical record for the
process.
This is the final state of all processes.

A process can be run in two ways:

1. Foreground Process: Every process when started runs in foreground by


default, receives input from the keyboard and sends output to the screen.
When issuing pwd command

$ ls pwd

Output:

$ /home/geeksforgeeks/root

When a command/process is running in the foreground and is taking a lot of time,


no other processes can be run or started because the prompt would not be
available until the program finishes processing and comes out.

2. Background Process : It runs in the background without keyboard input and


waits till keyboard input is required. Thus, other processes can be done in parallel
with the process running in background since they do not have to wait for the
previous process to be completed.
Adding & along with the command starts it as a background process

$ pwd &
Since pwd does not wants any input from the keyboard, it goes to the stop state
until moved to the foreground and given any data input. Thus, on pressing Enter,
:
Output:

[1] + Done pwd

That first line contains information about the background process – the job
number and the process ID. It tells you that the ls command background process
finishes successfully. The se The second is a prompt for another command.

Tracking ongoing processes

ps (Process status) can be used to see/list all the running processes.

$ ps

PID TTY TIME CMD

19 pts/1 00:00:00 sh

24 pts/1 00:00:00 ps

For more information -f (full) can be used along with ps

$ ps –f

UID PID PPID C STIME TTY TIME CMD

52471 19 1 0 07:20 pts/1 00:00:00f sh

52471 25 19 0 08:04 pts/1 00:00:00 ps -f

For a single process information, ps along with process id is used

$ ps 19
PID TTY TIME CMD

19 pts/1 00:00:00 sh

For a running program (named process) Pidof finds the process id’s (pids)

Fields described by ps are described as:


UID: User ID that this process belongs to (the person running it)
PID: Process ID
PPID: Parent process ID (the ID of the process that started it)
C: CPU utilization of process
STIME: Process start time
TTY: Terminal type associated with the process
TIME: CPU time taken by the process
CMD: The command that started this process

There are other options which can be used along with ps command :
-a: Shows information about all users
-x: Shows information about processes without terminals
-u: Shows additional information like -f option
-e: Displays extended information

Stopping a process

When running in foreground, hitting Ctrl + c (interrupt character) will exit the
command. For processes running in background kill command can be used if it’s
pid is known.

$ ps –f

UID PID PPID C STIME TTY TIME CMD

52471 19 1 0 07:20 pts/1 00:00:00 sh

52471 25 19 0 08:04 pts/1 00:00:00 ps –f

$ kill 19

Terminated
If a process ignores a regular kill command, you can use kill -9 followed by the
process ID .

$ kill -9 19

Terminated

Other process commands


bg: A job control command that resumes suspended jobs while keeping them
running in the background
Syntax:

bg [ job ]

For example

bg %19

fg: It continues a stopped job by running it in the foreground.


Syntax:

fg [ %job_id ]

For example

fg 19

top: This command is used to show all the running processes within the working
environment of Linux.
Syntax:

top

nice: It starts a new process (job) and assigns it a priority (nice) value at the same
time.
Syntax:

nice [-nice value]


nice value ranges from -20 to 19, where -20 is of the highest priority.

renice : To change the priority of an already running process renice is used.


Syntax:

renice [-nice value] [process id]

df: It shows the amount of available disk space being used by file systems
Syntax:

df

Output:

Filesystem 1K-blocks Used Available Use% Mounted on

/dev/loop0 18761008 15246876 2554440 86% /

none 4 0 4 0% /sys/fs/cgroup

udev 493812 4 493808 1% /dev

tmpfs 100672 1364 99308 2% /run

none 5120 0 5120 0% /run/lock

none 503352 1764 501588 1% /run/shm

none 102400 20 102380 1% /run/user

/dev/sda3 174766076 164417964 10348112 95% /host

free: It shows the total amount of free and used physical and swap memory in the
system, as well as the buffers used by the kernel
Syntax:

free

Output:
total used free shared buffers cached

Mem: 1006708 935872 70836 0 148244 346656

-/+ buffers/cache: 440972 565736

Swap: 262140 130084 132056

Types of Processes
1. Parent and Child process: The 2nd and 3rd column of the ps –f command
shows process id and parent’s process id number. For each user process there’s a
parent process in the system, with most of the commands having shell as their
parent.

2. Zombie and Orphan process: After completing its execution a child process
is terminated or killed and SIGCHLD updates the parent process about the
termination and thus can continue the task assigned to it. But at times when the
parent process is killed before the termination of the child process, the child
processes becomes orphan processes, with the parent of all processes “init”
process, becomes their new ppid.
A process which is killed but still shows its entry in the process status or the
process table is called a zombie process, they are dead and are not used.

3. Daemon process : They are system-related background processes that often


run with the permissions of root and services requests from other processes, they
most of the time run in the background and wait for processes it can work along
with for ex print daemon.
When ps –ef is executed, the process with ? in the tty field are daemon processes

UNIX Sample Programs


Use of echo command:

You can use echo command with various options. Some useful options are
mentioned in the following example. When you use ‘echo’ command without any
option then a newline is added by default. ‘-n’ option is used to print any text
without new line and ‘-e’ option is used to remove backslash characters from the
output. Create a new bash file with a name, ‘echo_example.sh’ and add the
following script.

#!/bin/bash
echo "Printing text with newline"
echo -n "Printing text without newline"
echo -e "\nRemoving \t backslash \t characters\n"

Use of comment:

‘#’ symbol is used to add single line comment in bash script. Create a new file
named ‘comment_example.sh’ and add the following script with single line
comment.

#!/bin/bash

# Add two numeric value


((sum=25+35))

#Print the result


echo $sum

Run the file with bash command.

echo " Enter the word"

read file

c=`cat $file | wc -c`

echo no of character in $file is $c

clear

echo enter the year

read year

rem=`expr $year % 4`
if [ $rem -eq 0 ]

then

echo it is leap

else

echo it is not leap

fi

i=`expr $i + 1`

done

Write a shell script to find a given pattern in the list of files in the current directory
using grep command

clear

echo "enter the three filen names"

read file1 file3 file3

echo "enter the pattern to search"

read pattern

grep -n a "$pattern" $file1 $file2 $file3

You might also like