History of Unix, Uses and Unix Features History of Unix
History of Unix, Uses and Unix Features History of Unix
History of Unix, Uses and Unix Features History of Unix
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
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.
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 */
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.
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
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 −
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.
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.
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.
$ 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 −
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 /.
/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 /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.
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 −
$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:
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]
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.
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
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.
– child returns a 0
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.
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.
· At any time, there are many processes, each in its particular state.
· The OS must have data structures representing each process
The PCB contains all of the info about a process, plus hardware state (PC, regs)
when the process is not running
Program counter: The program counter lets you know the address of the next
instruction, which should be executed for that process.
Accounting and business information: It includes the amount of CPU and time
utilities like real time used, job or process numbers, etc.
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.
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).
Process States
Process
Process States
The states that a Process enters in working from start till end are known as Process
states. These are listed below as:
· 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
$ ls pwd
Output:
$ /home/geeksforgeeks/root
$ 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:
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.
$ ps
19 pts/1 00:00:00 sh
24 pts/1 00:00:00 ps
$ ps –f
$ 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)
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
$ 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
bg [ job ]
For example
bg %19
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:
df: It shows the amount of available disk space being used by file systems
Syntax:
df
Output:
none 4 0 4 0% /sys/fs/cgroup
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
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.
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
read file
clear
read year
rem=`expr $year % 4`
if [ $rem -eq 0 ]
then
echo it is leap
else
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
read pattern