UNIX
UNIX
30-Jan-25 1
Systems programming
• Historically, systems programming meant programming the system (i.e., building
compilers, shells, loaders, and so on).
• It also requires knowledge of low level languages that provides direct access to
and control of system resources; this leads us to UNIX and C
30-Jan-25 2
What is UNIX?
• The UNIX operating system is a set of programs that act as a link between the computer and the user.
• The computer programs that allocate the system resources and coordinate all the details of the computer's internals is
• Users communicate with the kernel through a program known as the shell. The shell is a command line interpreter; it
translates commands entered by the user and converts them into a language that is understood by the kernel.
• Unix was originally developed in 1969 by a group of AT&T employees at Bell Labs, including Ken Thompson, Dennis
• There are various Unix variants available in the market. Solaris Unix, AIX, UP Unix and BSD are few examples.
• Several people can use a UNIX computer at the same time; hence UNIX is called a multiuser system.
• A user can also run multiple programs at the same time; hence UNIX is called multitasking.
30-Jan-25 3
Unix Architecture:
30-Jan-25 4
Unix Architecture
•Kernel: The kernel is the heart of the operating system. It interacts with hardware and most of the
tasks like memory management, task scheduling and file management.
•Shell: The shell is the utility 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 most
famous shells which are available with most of the Unix variants.
• Commands and Utilities: There are various command and utilities which you would use in your
day to day activities. cp, mv, cat and grep etc. are few examples of commands and utilities.
• Files and Directories: All data in UNIX is organized into files. All files are organized into
directories. These directories are organized into a tree-like structure called the file system.
30-Jan-25 5
Unix Architecture
UNIX has evolved over the past thirty years from its conceptual stage into a powerful and
effective operating system and the credit goes to the fundamental structure of UNIX, which
is very robust due to its layered approach comprising of the:
•Kernel
•Shell
30-Jan-25 6
Unix Architecture
30-Jan-25 7
The Kernel
>Core of the UNIX system
>Interacts directly with the hardware
>Insulates other parts of UNIX from hardware
>Performs all low level functions
>Parts of kernel deals with I/O devices, called Device Drivers
>All programs and applications interact with the kernel
30-Jan-25 8
The Kernel
30-Jan-25 9
The Kernel:
• The kernel is the Core of the UNIX system, which controls the system hardware and performs
various low level functions. Viewing the operating system as a set of layers, the kernel usually refers
to the operating system itself.
• However, the kernel does not directly deal with the user. Instead, it starts a separate program,
called shell, which actually interacts with the user and interprets the commands. The utilities and
applications add special capabilities to the operating system.
• Kernel functions:
•Memory management
•Process scheduling
•File management and security
•Interrupt handling and error reporting
•Input / Output services
•Date and Time services
•System accounting
30-Jan-25 10
The Kernel Cont...:
•Controls the execution of processes
•Allows creation, termination, suspension and communication of
process.
•Enforces the security scheme and prevents unauthorized access to
stored information like files and directories.
•Handles all low level input and output services.
•Handles the interrupts and system errors.
•Schedules the Central Processor and allocates system resources to
each user of the system.
30-Jan-25 11
The Shell:
•Is the command interpreter of UNIX
•Interface between the kernel and the user
•Provides powerful programming capabilities
• The shell is the interface between a user and the operating system.
When any user logs in, a copy of the shell program is started up,
displaying a prompt sign such as '$' (dollar), which informs the user
that the shell is waiting to accept a command.
30-Jan-25 12
The Shell
• In UNIX, there are three types of shells:
1.The Bourne Shell
2.The C Shell
3.The Korn Shell
• From the figures you can conceive the way; various parts and portions of UNIX are arranged around
the hardware. The core of UNIX is the kernel, which schedules jobs and manages data storage, and it is
very closely interfaced with hardware. The hardware functions according to the machine instructions
released ultimately by the kernel, based on the shell's interpretation of user commands.
• Surrounding the kernel are parts of software of the shell relating to:
1.Execution of commands for piping and filtering actions.
2.Tools for carrying out foreground and background processing.
3.Utilities for configuring the hardware.
4.I/O redirection and command execution utilities.
5.Filename substitution.
30-Jan-25 13
Shell functions
30-Jan-25 14
Shell functions
• The shell is a program that collects and interprets the user commands,
invokes the concerned program file from memory and directs the kernel to
execute them.
• As the name shell suggests, the shell envelops the kernel. Externally, the
UNIX operating system is made up of two parts. One part is a large set of
system programs, each one corresponding to a command, and another part is
the shell, which interprets, manages and coordinates the execution of these
programs. Most of the users are unaware of the various complexities of the
operating system or the hardware.
30-Jan-25 15
Command Execution
• Refer figure below, which illustrates the cycle of command execution.
To begin with, the kernel displays a shell prompt after authorized login.
The shell waits for the input from the user, decodes the command line
and searches for the program. If the program is found, the shell retrieves
and submits it to the kernel to execute the program.
30-Jan-25 16
Command Execution
• The kernel delivers the output. If the command is not found, it signals the kernel to
display command not found at the terminal. The shell accepts the kernel's reply, and in
both cases displays the next prompt. This cycle continues until the user with <CTRL> D or
logout terminates it. Once the shell encounters end of input, it instructs the kernel to log
out the user, and displays the login message again.
• The system directories where the command file programs are stored are the /bin and
/usr/bin. On receipt of the command, which is nothing but the executable filenames in the
system directory, the shell locates them and transfers control to the kernel to execute.
• These filenames can be verified by the command pwd and Is, after changing to the
directories Ibin and
• /usrlbin.
30-Jan-25 17
Shell Environment
• A separate shell process is apportioned to each user as stated earlier. The
separate shell once created provides an environment within which the user
operates. The shell environment consists of the following to ensure proper
interaction between the user and the system:
1.The name of the users, and a working directory (a directory allotted to the
user by the operating system).
2.A list of system directories to be searched to locate the commands given by
the user.
3.The file access permission modes.
4.A record of users’ identification (user id) and group identification number
(group id).
30-Jan-25 18
UNIX SYSTEM CALLS
• System Call Interface
• All operating systems provide service points through which programs
request services form the kernel.
• A system call is a mechanism used by an application to request for
services from the kernel. Processes can either be user or kernel
initiated and can run either in user mode or kernel mode.
30-Jan-25 19
Kernel mode
• Kernel mode processes can:
• Enable/Disable interrupts. Direct access to I/O devices.
30-Jan-25 20
Kernel mode
30-Jan-25 21
Library calls and system calls
• The difference between system calls and library calls is that:
• System calls are provided by the system and are executed in the system kernel. They are
entry points into the kernel and are therefore NOT linked into your program. These are not
portable calls.
• Library calls include the ANSI C standard library and are therefore portable. These
functions are linked into your program. (Recall linking in programming)
• Because system calls are part of the O/S, the program has to make a context switch to the
kernel when they are called and because of this, they have a high startup overhead. The
upside is that the time executing these routines is assigned to the OS and not the user
program.
• Calling standard library functions does not involve system call interface. However a
library function may initiate a system call which in turn issues a system call e.g. a call to
printf() may invoke write() system call to do output.
• A system call always involves using system call interface and using the kernel of OS.
30-Jan-25 22
Library calls and system calls
30-Jan-25 23
Anatomy of a System call
• General form of a system call is as follows:
• [Returned value] = system_call_name
(parameters); (if any) Where:
• returned_value = nonnegative integer if OK, -1 if error (in this case
error code is placed in external variable errno)
30-Jan-25 24
Handling System call
30-Jan-25 25
30-Jan-25 26
A system call Error
• When a system call discovers an error, it returns -1 and stores the reason the called
failed in an external variable named "errno".
• The "/usr/include/errno.h" file maps these error numbers to manifest constants, and it
these constants that you should use in your programs.
• When a system call returns successfully, it returns something other than -1, but it does
not clear "errno". "errno" only has meaning directly after a system call that returns an
error.
30-Jan-25 27
30-Jan-25 28