You are on page 1of 13

UNIX SYSTEM PROGRAMMING Module 1

MODULE 1
Getting Started & Understanding UNIX Commands: Operating System, UNIX
Operating System, UNIX architecture, Features of UNIX, The POSIX Standards.

UNIX and POSIX APIs: The POSIX APIs, The UNIX and POSIX Development
Environment, API Common Characteristics.

1.1 The Operating System(OS)

 OS is the life-giver to a computer- providing with basic intelligence to work with.


 An operating system is the software that manages the computer’s hardware and
provides a convenient and safe environment for running programs.
 It acts as an interface between programs and the hardware resources that these
programs access (like memory, hard disk and printer).

Figure 1.1: Operating System

 It is loaded into memory when a computer is booted and remains active as long as the
machine is up.
 When OS’s allow multiple programs to be in the memory they are called as
multiprogramming.
Key functionality of Operating System
1. Memory Allocation- OS allocates memory for the program and loads the
program to the allocated memory.
2. CPU Management-
a. Allocates a time slot for a program to run in CPU.
b. Loads the CPU registers with control information related to the
program.
c. The operating system keeps track of the instruction that was last
executed.
3. Hardware Management- If the program needs to access the hardware; it makes
a call to the operating system rather than attempting to do the job itself.

3rd A & C, Dept. Of CSE, NHCE 2019-20 1


UNIX SYSTEM PROGRAMMING Module 1

4. After the program has completed execution, the operating system cleans up the
memory and registers and makes them available for the next program.

1.2 The UNIX Operating System

UNIX is a giant operating system and is way ahead of other systems in sheer power. It
practically runs on every hardware and provided inspiration to the Open Source movement. It
was developed by programmers for their own use. UNIX is not written in assembler but in C.
C is a high-level language that was designed with portability considerations in mind, which
explains why UNIX systems are available on practically every hardware platform. Apart
from handling the basic operating system functions, UNIX offers a host of applications that
benefit users, programmers, and system administrators. It encourages users to combine
multiple programs to solve a complex problem. For programmers, UNIX offers a rich set of
programming tools that aid in developing, debugging, and maintaining programs. UNIX is
also more easily maintained than most systems. One of these programs is the system’s
command interpreter, called the shell. You interact with a UNIX system through the shell.
Following are some of the important features of Unix Operating System
• Portable
• Open Source 
• Multi-User 
• Multiprogramming
• Hierarchical File System 
• Shell
• Security

1.3 UNIX architecture

Figure 1.2: The Kernel-Shell relationship

1.3.1 Division of Labor: Kernel and Shell


The function of operating system is divided among:
 Kernel
 Shell
Kernel
 The kernel interacts with the machine’s hardware
 The kernel is the core of the operating system.

3rd A & C, Dept. Of CSE, NHCE 2019-20 2


UNIX SYSTEM PROGRAMMING Module 1

 The system’s bootstrap program (a small piece of program code) loads the kernel into
memory at startup.
 The kernel comprises a set of routines mostly written in C that communicate with the
hardware directly.
 User programs (the applications) that need to access the hardware (like the hard disk
or the terminal) communicate with the kernel using a set of functions called system
calls
 It manages the system’s memory, schedules processes, decides their priorities, and
performs other tasks
Shell
 Shell interact with the user
 Computers don’t have any inherent ability to translate user commands into action.
This is done by the shell which is a command interpreter
 Interface between the user and the kernel.
 There can be only kernel running on the system but there can be several shells in
action.
 The shell examines the command for special characters. If it finds any, it rebuilds a
simplified command line, and finally communicates with the kernel to see that the
command is executed.

1.3.2 The File and Process

Two simple entities support the UNIX system- the file and process.
 A file is an array of bytes that stores information
 Files are containers for storing static information.
 Even directories and devices are considered files.
 A file is related to another file by being part of a single hierarchical structure called
the file system.
 The process represents a program in execution.
 Like files, processes also form a hierarchy.

1.4 Features of UNIX

The major features of this OS are

1. Multiuser System
 It is a multiprogramming system, it permits multiple programs to remain in
memory and compete for the attention of the CPU.
 These programs can be run by different users; UNIX is also a multiuser
system.
 For cycling through multiple jobs, the kernel uses the principle of time-
sharing. It breaks up a unit of time into several slices, and a user’s job runs for
the duration of a slice. The moment the allocated time expires, the previous
job is kept on hold and the next job is taken up. This process goes on until the
clock has turned full circle and the first job is taken up once again. This
switching happens several times in one second, so every user has the feeling
that the machine is completely dedicated to the person.

3rd A & C, Dept. Of CSE, NHCE 2019-20 3


UNIX SYSTEM PROGRAMMING Module 1

2. A Multitasking System.
 A single user can also run multiple tasks concurrently
 Users edit a file, print another one on the printer, send email to a friend, and
browse the World Wide Web, all without leaving any of the applications.
 In a multitasking environment, a user sees one job running in the foreground;
the rest run in the background.
 In UNIX, it is possible to edit a C program with the vi editor and then suspend
the vi process to run the gcc compiler.
3. Inter process Communication
 UNIX tools singly are not that powerful.
 Using Interprocess communication feature, the Shell can arrange for a
command to pass on data to another command.
 Thus by interconnecting a number of tools, an enormous number of
combinations of their usage.

4. The Featureless File


 UNIX considers every directory, devices and files as members of the file
system.
 A file to UNIX is an array of bytes and can contain virtually anything-text,
object code or a directory structure.
 UNIX provides a vast array of text manipulation tools that can edit files
without using an editor.

5. Pattern Matching

UNIX features very sophisticated pattern matching features. Many commands take file name
as argument or string as argument. All C programs have the .c extension, to represent all the c
files *.c can be used. The * is a special character (known as a meta-character) that is used by
the shell to match a number of characters.
Some advanced tools (like grep, sed and awk) also use a different meta-character set for
matching strings contained in files.
e.g. ls unix* lists out all filenames with unix.

6. Programming Facility
The UNIX shell is also a programming language; it was designed for a programmer, not a
casual end user. It has all the necessary ingredients, like control structures, loops, and
variables that establish it as a powerful programming language in its own right. These
features are used to design shell scripts; programs that run UNIX commands in a batch.

7. Portability and System Calls

 In UNIX, once software has been developed in any UNIX system it can be easily
ported to any other UNIX machine, since the same system calls are available to
whoever wants to develop tools of their own.
 System Calls are special function which communicate with the kernel
 Often the same system call can access both a file and a device; the open system call
opens both.
 write system call can be used to write on disk, device and on the monitor.

3rd A & C, Dept. Of CSE, NHCE 2019-20 4


UNIX SYSTEM PROGRAMMING Module 1

 POSIX specifies the system calls that all UNIX systems must implement. Once
software has been developed on one UNIX system using the calls mandated by
POSIX, it can be easily moved to another UNIX machine.

8. Documentation
The principal online help facility available is the man command, which remains the most
important reference for commands and their configuration files. There’s a vast ocean of
UNIX resources available on the Internet, several newsgroups on UNIX to post queries. The
FAQ (Frequently Asked Questions), a document that addresses common problems is also
widely available on the Net. Then there are numerous articles published in magazines and
journals and lecture notes made available by universities on their Web sites.

1.5 Internal and External commands

Internal commands are those commands that are executed directly by the shell. These
commands will not have a separate process running for each command. Shell built-in
commands. These commands are normally not found anywhere even if they are specified in
any directory. E.g. echo, pwd, cd
External commands are those commands that are executed by the kernel. These commands
will have a process id running for it. It is stored as separate binaries in /usr/bin directory.
They are those commands that have an independent existence and is executed from the /bin
directory. E.g. ls, cal, who

1.5.1 Command Structure

A UNIX command sequence has five words. The first word is the command itself and the
remaining ones are its arguments. The ls command is specified here with four arguments.
Two of the arguments begin with a hyphen (-l and -t) and are appropriately called options.
The entire line is referred to as the command line. A command line is executed only after you
hit [Enter].
Every command has a fixed set of options. An option changes a command’s default behavior,
so if ls shows only the filenames, the -l and -t options show their attributes as well.

A command is separated from its options and arguments by whitespace.


All commands don’t compulsorily use options and arguments. E.g. clear. Some commands
may or may not be specified with arguments. E.g. who, date.

1.5.2 Flexibility of Command Usage


UNIX system provides a certain degree of flexibility in the usage of commands. A command
can often be entered in more than 1 way.
More than one command can be specified in the same command line, each command
separated by a ; (semicolon) E.g. ls executed after who
who; ls –l note

3rd A & C, Dept. Of CSE, NHCE 2019-20 5


UNIX SYSTEM PROGRAMMING Module 1

This ; is a special character is called as meta-character. Commands on either side of the ; are
processed separately.
Some commands have lengthy syntaxes, and you may often find it desirable and sometimes
necessary to spread the command line into multiple lines. The shell then issues a secondary
prompt usually >, to indicate that the command line isn’t complete. This is how the echo
command works:
$ echo “This is
>a three line
>text message”
Output will be
This is
a three line
text message

1.5.3 man Documentation

man is the most complete and authoritative guide to UNIX system. man presents the first
page and pauses, press a key (either [spacebar] or [enter]) to see next page and press q to
quit. The structure of the man page is divided into a number of compulsory and optional
sections where each section is preceded by a header.
Every command doesn’t have all the headers but has most of them; the first three (NAME,
SYNOPSIS, and DESCRIPTION) are generally seen in all man pages.
NAME: name of the command with a brief description of its function.
SYNOPSIS: the syntax- the options and arguments used with the command
DESCRIPTION: Largest section of manual page. It contains a detailed exposition of the
command with specific explanation of every option.

Sometimes other headers are there as well


EXAMPLES: examples to highlight the use of some options.
FILES: All files used by the command.
SEE ALSO: other components of the system for a fuller comprehension.
DIAGNOSTICS: explains why and when the command generates error messages.
AUTHOR(S): Shows the authors of the command.

1.5.4 whatis and apropos


The whatis command provides a one-line explanation of the command
$whatis cp
cp(1) – copy files
When you have no idea about the command to use in a given situation, you should use
apropos command with one or more keywords. apropos then gives the name and short
description from all manual sections that contains the keyword.

1.6 POSIX Standards


POSIX is acronym for Portable Operating System Interface. There are three subgroups in

POSIX. They are :

3rd A & C, Dept. Of CSE, NHCE 2019-20 6


UNIX SYSTEM PROGRAMMING Module 1

POSIX.1 :

 Committee proposes a standard for base operating system APIs.


 This standard is formally known as the IEEE standard 1003.1-1990. This standard
specifies the APIs for the file manipulation and processes Process (for Creation and
Control).
POSIX.1b:

 Committee proposes a standard for real time operating system APIs

 This standard is formally known as the IEEE standard 1003.4-1993

 This standard specifies the APIs for the interprocess communication


(Semaphores,Message Passing Shared Memory).

POSIX.1c:

 Committee proposes a standard for multithreaded programming interface


 This standard specifies the APIs for Thread Creation, Control, and Cleanup, Thread
Scheduling, Thread Synchronization and for Signal Handling.

To ensure a user program conforms to the POSIX.1 standard, the user should define the
manifested constant _POSIX_SOURCE at the beginning of each program(before the
inclusion of any header files) as:

#define _ POSIX_SOURCE or

specify the –D_ POSIX_SOURCE option to a C compiler during compilation.

$cc –D_ POSIX_SOURCE filename.c

To ensure a user program conforms to the POSIX.1b standard, the user should define the
manifested constant

#define _POSIX_C_SOURCE

With a value POSIX_C_SOURCE

value----Meaning

198808L---- First version of POSIX.1 compliance

199009L---- Second version of POSIX.1 compliance

199309L---- POSIX.1 and POSIX.1b compliance

In general a user program that must be strictly POSIX.1and POSIX.1b compliant may be
written as follows:

3rd A & C, Dept. Of CSE, NHCE 2019-20 7


UNIX SYSTEM PROGRAMMING Module 1

#define _POSIX_SOURCE

#define _POSIX_C_SOURCE 199309L

#include <stdio.h>

#include <unistd.h>

int main( )

....

Example program:

#define _POSIX_SOURCE

#define _POSIX_C_SOURCE 199309L

#include<stdio.h>

#include<unistd.h>

int main()

#ifdef _POSIX_VERSION

printf("System compatible with %ldL\n", _POSIX_VERSION);

#else

printf("System not compatible with _POSIX_VERSION\n");

#endif

return 0;

Difference between POSIX and UNIX


POSIX UNIX

C and C++ header files are just headers C and C++ header files
not header files

/usr/include need not exist Header files are included in


/usr/include

Does not support the concept of super Super user has special privilege and the
user nor the ID is 0 super user ID is always 0

3rd A & C, Dept. Of CSE, NHCE 2019-20 8


UNIX SYSTEM PROGRAMMING Module 1

POSIX Feature Test Macros


Feature Test Macro Effects if defined on a System

_POSIX_JOB_CONTROL It allow us to start multiple jobs(groups of processes)


from a single terminal and control which jobs can access
the terminal and which jobs are to run in the background.
Hence It supports BSD version Job Control Feature.
Each process running on the system keeps the saved.

_POSIX_SAVED_IDS set-UID and set-GID, so that it can change effective user


ID and group ID to those values via setuid and setgid
APIs respectively.

_POSIX_CHOWN_RESTRICTED If the defined value is -1, users may change ownership of


files owned by them. Otherwise only users with special
privilege may change ownership of any files on a system.

_POSIX_NO_TRUNC If the defined value is -1, any long path name passed to
an API is silently truncated to NAME_MAX bytes,
otherwise error is generated.

_POSIX_VDISABLE If the defined value is -1, there is no disabling character


for special characters for all terminal device files,
otherwise the value is the disabling character value.

Limits Checking at Compile Time and at Run Time

 The POSIX.1 and POSIX.1b standards specify a number of parameters that describe
capacity limitations of the system.
 Limits are defined in <limits.h>

 These are prefixed with the name _POSIX _

The following is a list of POSIX.1 defined constants in the <limits.h> header

3rd A & C, Dept. Of CSE, NHCE 2019-20 9


UNIX SYSTEM PROGRAMMING Module 1

The following is a list of POSIX.1b defined constants

sysconf, pathcomf and fpathconf

3rd A & C, Dept. Of CSE, NHCE 2019-20 10


UNIX SYSTEM PROGRAMMING Module 1

To find out the actual implemented configuration limits

 System wide using sysconf during run time


 On individual objects during run time using, pathconf and fpathconf.

#include <unistd.h>

long sysconf (int limit_name);

long fpathconf(int fildes, int flimit_name));

long pathconf(const char *path, int flimit_name);

 For pathconf(), the path argument points to the pathname of a file or directory.

 For fpathconf (), the fildes argument is an open file descriptor.

he limit_name argument value is manifested constant as defined in <unistd.h> header. The
possible values and the corresponding data returned by the sysconf function are

Similarly the flimit_name argument value is manifested constant as defined in <unistd.h>


header. The possible values and the corresponding data returned by either pathconf or
fpathconf for a named file object are

3rd A & C, Dept. Of CSE, NHCE 2019-20 11


UNIX SYSTEM PROGRAMMING Module 1

1.7 Unix and POSIX APIs

UNIX systems provides a set of application programming interface(API”S) functions


commonly known as system calls which may be called user’s programs to perform System
specific functions.
These functions allows user’s applications to directly to manipulate system objects such as
files and processes that cannot be done by using standard C library functions.
Functions of API”S
1. Determine system configuration and user Information
2. File Manipulation
3. Process creation & control.
4. Interprocess communication
5. Network Communication
Context Switching

A user mode is the normal execution context of any user process, and it allows the
process to access its specific data only.
A kernel mode is the protective execution environment that allows a user process to
access kernels data in a restricted manner.
When the APIs execution completes, the user process is switched back to the user mode.
This context switching for each API call ensures that process access kernels data in a
controlled manner and minimizes any chance of a runway user application may damage
an entire system. So in general calling an APIs is more time consuming than calling a
user function due to the context switching. Thus for those time critical applications, user
should call their system APIs only if it is necessary.
An APIs common Characteristics

3rd A & C, Dept. Of CSE, NHCE 2019-20 12


UNIX SYSTEM PROGRAMMING Module 1

Most system calls return a special value to indicate that they have failed. The special
value is typically -1, a null pointer, or a constant such as EOF that is defined for that
purpose.
To find out what kind of error it was, you need to look at the error code stored in the
variable errno. This variable is declared in the header file errno.h as shown below.
volatile int errno
The variable errno contains the system error number.
void perror (const char*message)

The function perror is declared in stdio.h.

Following table shows Some Error Codes and their meaning:

Errors Meaning
EPERM API was aborted because the calling process does not have the super user

privilege.

EINTR An APIs execution was aborted due to signal interruption.

EIO An Input/Output error occurred in an APIs execution.

ENOEXEC A process could not execute program via one of the Exec API.

EBADF An API was called with an invalid file descriptor.

ECHILD A process does not have any child process which it can wait on.
EAGAIN An API was aborted because some system resource it is requested was

temporarily unavailable. The API should call again later.

ENOMEM An API was aborted because it could not allocate dynamic memory.

EACCESS The process does not have enough privilege to perform the operation.

EFAULT A pointer points to an invalid address.

EPIPE An API attempted to write data to a pipe which has no reader.

ENOENT An invalid file name was specified to an API.

..................................................

3rd A & C, Dept. Of CSE, NHCE 2019-20 13

You might also like