SlideShare a Scribd company logo
1 
CHAPTER – 1 
Linux 
Operating System 
An operating system is software that manages computer hardware and software resources and provides 
common services for computer programs. The operating system is an essential component of the system 
software in a computer system. Application programs usually require an operating system to function. 
Time-sharing operating systems schedule tasks for efficient use of the system and may also include 
accounting software for cost allocation of processor time, mass storage, printing, and other resources. 
For hardware functions such as input and output and memory allocation, the operating system acts as an 
intermediary between programs and the computer hardware, although the application code is usually 
executed directly by the hardware and will frequently make a system call to an OS function or be 
interrupted by it. Operating systems can be found on almost any device that contains a computer— 
from cellular phones and video game consoles to supercomputers and web servers. 
Examples of popular modern operating systems include Android, BSD, iOS, Linux, OS 
X, QNX, Microsoft Windows, Windows Phone, and IBM z/OS. All these examples, except Windows, 
Windows Phone and z/OS, share roots in UNIX. 
Types of OS 
 Real-time: A real-time operating system is a multitasking operating system that aims at executing 
real-time applications. Real-time operating systems often use specialized scheduling algorithms so 
that they can achieve a deterministic nature of behavior. The main objective of real-time operating 
systems is their quick and predictable response to events. They have an event-driven or time-sharing 
design and often aspects of both. An event-driven system switches between tasks based on their 
priorities or external events while time-sharing operating systems switch tasks based on clock 
interrupts. Time-sharing operating systems schedule tasks for efficient use of the system and may 
also include accounting software for cost allocation of processor time, mass storage, printing, and 
other resources. 
 Multi-user: A multi-user operating system allows multiple users to access a computer system at the 
same time. Time-sharing systems and Internet servers can be classified as multi-user systems as they 
enable multiple-user access to a computer through the sharing of time. Single-user operating systems 
have only one user but may allow multiple programs to run at the same time. 
 Multi-tasking vs. single-tasking : A multi-tasking operating system allows more than one program 
to be running at the same time, from the point of view of human time scales. A single-tasking system 
has only one running program. Multi-tasking can be of two types: pre-emptive and co-operative. In
pre-emptive multitasking, the operating system slices the CPU time and dedicates one slot to each of 
the programs. Unix-like operating systems such as Solaris and Linux support pre-emptive 
multitasking, as does AmigaOS. Cooperative multitasking is achieved by relying on each process to 
give time to the other processes in a defined manner. 16-bit versions of Microsoft Windows used 
cooperative multi-tasking. 32-bit versions of both Windows NT and Win9x, used pre-emptive multi-tasking. 
2 
Mac OS prior to OS X used to support cooperative multitasking. 
 Distributed: A distributed operating system manages a group of independent computers and makes 
them appear to be a single computer. The development of networked computers that could be linked 
and communicate with each other gave rise to distributed computing. Distributed computations are 
carried out on more than one machine. When computers in a group work in cooperation, they make a 
distributed system. 
 Template: In an o/s, distributed and cloud computing context, templating refers to creating a single 
virtual machine image as a guest operating system, then saving it as a tool for multiple running 
virtual machines (Gagne, 2012, p. 716). The technique is used both in virtualization and cloud 
computing management, and is common in large server warehouses. 
 Embedded: Embedded operating systems are designed to be used in embedded computer systems. 
They are designed to operate on small machines like PDAs with less autonomy. They are able to 
operate with a limited number of resources. They are very compact and extremely efficient by 
design. Windows CE and Minix 3 are some examples of embedded operating systems. 
History of OS 
Early computers were built to perform a series of single tasks, like a calculator. Basic operating system 
features were developed in the 1950s, such as resident monitor functions that could automatically run 
different programs in succession to speed up processing. Operating systems did not exist in their modern 
and more complex forms until the early 1960s. Hardware features were added, that enabled use 
of runtime libraries, interrupts, and parallel processing. When personal computers became popular in the 
1980s, operating systems were made for them similar in concept to those used on larger computers. 
In the 1940s, the earliest electronic digital systems had no operating systems. Electronic systems of this 
time were programmed on rows of mechanical switches or by jumper wires on plug boards. These were 
special-purpose systems that, for example, generated ballistics tables for the military or controlled the 
printing of payroll checks from data on punched paper cards. After programmable general purpose 
computers were invented, machine languages (consisting of strings of the binary digits 0 and 1 on 
punched paper tape) were introduced that sped up the programming process (Stern, 1981). 
OS/360 was used on most IBM mainframe computers beginning in 1966, including computers utilized 
by the Apollo program.
In the early 1950s, a computer could execute only one program at a time. Each user had sole use of the 
computer for a limited period of time and would arrive at a scheduled time with program and data on 
punched paper cards and/or punched tape. The program would be loaded into the machine, and the 
machine would be set to work until the program completed or crashed. Programs could generally be 
debugged via a front panel using toggle switches and panel lights. It is said that Alan Turing was a 
master of this on the early Manchester Mark 1 machine, and he was already deriving the primitive 
conception of an operating system from the principles of the Universal Turing machine. 
Later machines came with libraries of programs, which would be linked to a user's program to assist in 
operations such as input and output and generating computer code from human-readable symbolic code. 
This was the genesis of the modern-day operating system. However, machines still ran a single job at a 
time. At Cambridge University in England the job queue was at one time a washing line from which 
tapes were hung with different colored clothes-pegs to indicate job-priority. 
3 
Linux vs. Windows 
What is it? 
Linux is an example of Open Source 
software development and Free 
Operating System (OS). 
Windows is the family of operating 
system (OS) from Microsoft, which is the 
most famous OS in the world. 
Cost 
Linux can be freely distributed, 
downloaded freely, distributed through 
magazines, Books etc. There are priced 
versions for Linux also, but they are 
normally cheaper than Windows. 
For desktop or home use, Windows can be 
expensive. A single copy can cost around 
$50 to $ 450 depending on the version of 
Windows. 
User 
Everyone. From home users to 
developers and computer enthusiasts 
alike. 
Everyone. From home users to developers 
and computer enthusiasts alike. 
Manufacturer 
Linux kernel is developed by the 
community. Linus Torvalds oversees 
things. 
Microsoft created the Windows operating 
system, but allows other computer 
manufactures to distribute their own 
computers with Windows pre-installed. 
Usage 
Linux can be installed on a wide variety 
of computer hardware, ranging from 
mobile phones, tablet computers 
and video game consoles, to mainframes 
On PC's desktops, laptops, servers and 
some phones.
4 
and supercomputers. 
Development 
and 
Distribution 
Linux is developed by Open Source 
development i.e. through sharing and 
collaboration of code and features 
through forums etc. and it is distributed 
by various vendors. 
Windows is developed and distributed by 
Microsoft. 
GUI 
Linux typically provides two GUIs, KDE 
and Gnome. But there are millions of 
alternatives such as LXDE, Xfce, Unity, 
Mate, twm, etc. 
The Windows GUI is an integral 
component of the OS and is not 
replaceable. This can be a con when it 
comes to Windows 8's Metro. 
File system 
support 
Ext2, Ext3, Ext4, Jfs, ReiserFS, Xfs, 
Btrfs, FAT, FAT32, NTFS 
FAT, FAT32, NTFS, exFAT 
Text mode 
interface 
BASH (Bourne Again SHell) is the 
Linux default shell. It can support 
multiple command interpreters. 
Windows uses a command shell and each 
version of Windows has a single 
command interpreter with dos-like 
commands, recently there is the addition 
of the optional PowerShell that uses more 
Unix-like commands. 
Price Free but support is available for a price. $50-$450 
Security 
Linux has had about 60-100 viruses 
listed till date. None of them actively 
spreading nowadays. 
According to Dr. Nic Peeling and Dr. 
Julian Satchell's “Analysis of the Impact 
of Open Source Software” there have been 
more than 60,000 viruses in Windows. 
Anti-Virus cost about $20 to $400 
Threat 
detection and 
solution 
In case of Linux, threat detection and 
solution is very fast, as Linux is mainly 
community driven and whenever any 
Linux user posts any kind of threat, 
several developers start working on it 
from different parts of the world 
After detecting a major threat in Windows 
OS, Microsoft generally releases a patch 
that can fix the problem and it can take 
more than 2/3 months. Sometimes sooner, 
Microsoft releases patches and updates 
weekly. 
Processors Dozens of different kinds. Limited but most (80%)
5 
Examples 
Ubuntu, Fedora, Red Hat, Debian, 
Archlinux, Android etc. 
Windows 8, 8.1, 7, Vista, XP 
Gaming 
Very few games available natively. 
Some games can be played through 
Wine, but often not all features are 
available. 
Almost all games are compatible with 
Windows. Some CPU intensive and 
graphics intensive games are exclusive to 
Windows PC's. 
User 
experience 
Although there are many GUI 
applications, most of the work is done 
through Terminal (a console window), 
and if a problem arises GUI is rarely 
usable to fix them. 
Everything can be controlled through GUI 
and incompatibility problems are rare. 
Graphics 
performance 
Because hardware manufacturers, such 
as NVidia, often do not provide 
documentation for Linux developers, 
drivers cannot use full card performance. 
Combined with newest DirectX versions 
and full graphics card support the 
performance is almost as good as it can 
get. 
Company / 
developer 
Linus Torvalds Microsoft 
Introduction 
(from 
Wikipedia) 
Linux is a Unix-like and POSIX-compliant 
computer operating system 
assembled under the model of free and 
open source software development and 
distribution. The defining component of 
Linux is the Linux kernel, an operating 
system kernel first released 
Microsoft Windows is a series of 
graphical interface operating systems 
developed, marketed, and sold by 
Microsoft. Microsoft introduced an 
operating environment named Windows 
on November 20, 1985 as a graphical 
operating system shell for MS-DOS. 
Available 
language(s) 
Multilingual Multilingual 
License GNU/Free Proprietary 
Supported 
platforms 
All PowerPC: versions 1.0 - NT 4.0; DEC 
Alpha: versions 1.0 - NT 4.0; MIPS 
R4000: versions 1.0 - NT 4.0; IA-32: 
versions 1.0 - 8; IA-64: version XP; x86-
6 
64: versions XP - 8; ARM: version RT; 
Default user 
interface 
Gnome or KDE (Depends on distro) Graphical (Windows Aero) 
Preceded by Basic Terminal (CLI) MS-DOS 
Source model Open Source Closed / Shared source 
Update method Many Windows Update 
OS Layers 
 Base Hardware: Computer hardware is the collection of physical elements that constitutes 
a computer system. Computer hardware refers to the physical parts or components of a computer 
such as the monitor, mouse, keyboard, computer data storage, hard drive disk (HDD), system unit 
(graphic cards, sound cards, memory, motherboard and chips), etc. all of which are physical objects 
that can be touched. In contrast, software is instructions that can be stored and run by hardware. 
Software is any set of machine-readable instructions that directs a computer's processor to perform 
specific operations. A combination of hardware and software forms a usable computing system.
 Device Driver: In computing, a device driver (commonly referred to as simply a driver) is 
a computer program that operates or controls a particular type of device that is attached to a 
computer. A driver provides a software interface to hardware devices, enabling operating systems 
and other computer programs to access hardware functions without needing to know precise details 
of the hardware being used. A driver typically communicates with the device through the computer 
bus or communications subsystem to which the hardware connects. When a calling program invokes 
a routine in the driver, the driver issues commands to the device. Once the device sends data back to 
the driver, the driver may invoke routines in the original calling program. Drivers are hardware-dependent 
and operating-system-specific. They usually provide the interrupt handling required for 
7 
any necessary asynchronous time-dependent hardware interface. 
 Shell: In computing, a shell is a user interface for access to an operating system's services. In 
general, operating system shells use either a command-line interface (CLI) or graphical user 
interface (GUI), depending on a computer's role and particular operation. The design of a shell is 
guided by cognitive ergonomics and the goal is to achieve the best workflow possible for the 
intended tasks; the design can be constricted by the available computing power (for example, of 
the GPU) or the available amount of graphics memory. The design of a shell is also dictated by the 
employed computer periphery, such as computer keyboard, pointing device (a mouse with one 
button, or one with five buttons, or a 3D mouse) or touchscreen, which is the direct human–machine 
interface. CLI shells allow some operations to be performed faster, for example rearranging large 
blocks of data. However, they require the user to memorize all commands and their calling syntax, 
and also to learn the shell-specific scripting language, for example bash script. CLIs are also easier to 
be operated via refreshable braille display and provide certain advantages to screen readers. 
Graphical shells have a low burden to start using a computer, and they are characterized as being 
simple and easy to use. With the widespread adoption of programs with GUIs, the use of graphical 
shells has gained greater adoption. Since graphical shells come with certain disadvantages (for 
example, lack of support for easy automation of operation sequences), most GUI-enabled operating 
systems also provide additional CLI shells. 
 Compilers: A compiler is a programming tool, a computer program (or set of programs), that 
transforms source code written in a programming language (the source language) into another 
computer language (the target language, often having a binary form known as object code). The most 
common reason for wanting to transform source code is to create an executable program. The name 
"compiler" is primarily used for programs that translate source code from a high-level programming 
language to a lower level language (e.g., assembly language or machine code). If the compiled 
program can run on a computer whose CPU or operating system is different from the one on which 
the compiler runs, the compiler is known as a cross-compiler. More generally, compilers are a 
specific type of translators. A program that translates from a low level language to a higher level one
is a decompiler. A program that translates between high-level languages is usually called a source-to- 
source compiler or transpiler. A language rewriter is usually a program that translates the form of 
expressions without a change of language. The term compiler-compiler is sometimes used to refer to 
a parser generator, a tool often used to help create the laxer and parser. A compiler is likely to 
perform many or all of the following operations: lexical analysis, preprocessing, parsing, semantic 
analysis (Syntax-directed translation), code generation, and code optimization. Program faults 
caused by incorrect compiler behavior can be very difficult to track down and work around; 
therefore, compiler implementers invest significant effort to ensure compiler correctness. 
 Command Interpreter: A command interpreter is the part of a computer operating system that 
understands and executes commands that are entered interactively by a human being or from a 
program. In some operating systems, the command interpreter is called the shell. 
 Daemon: In multitasking computer operating systems, a daemon is a computer program that runs as 
a background process, rather than being under the direct control of an interactive user. Traditionally 
daemon names end with the letter d: for example, syslog is the daemon that implements the system 
logging facility and sshd is a daemon that services incoming SSH connections. In 
a Unix environment, the parent process of a daemon is often, but not always, the init process. A 
daemon is usually either created by a process forking a child process and then immediately exiting, 
thus causing init to adopt the child process, or by the init process directly launching the daemon. In 
addition, a daemon launched by forking and exiting typically must perform other operations, such as 
dissociating the process from any controlling terminal (tty). Such procedures are often implemented 
in various convenience routines such as daemon(3) in Unix. Systems often start daemons 
at boot time and serve the function of responding to network requests, hardware activity, or other 
programs by performing some task. Daemons can also configure hardware (like udevd on 
some GNU/Linux systems), run scheduled tasks (like cron), and perform a variety of other tasks. 
 Utilities: Utility software is system software designed to help analyse, configure, optimize or 
maintain a computer. Utility software usually focuses on how the computer infrastructure (including 
the computer hardware, software and data storage) operates. 
 Applications: Application software is a set of one or more programs designed to carry out 
operations for a specific application. Application software cannot run on itself but is dependent on 
system software to execute. Examples of application software include MS Word, MS Excel, a 
console game, a library management system, a spread sheet system etc. The term is used to 
distinguish such software from another type of computer program referred to as system software, 
which manages and integrates a computer's capabilities but does not directly perform tasks that 
benefit the user. The system software serves the application, which in turn serves the user. Examples 
of types of application software may include accounting software, media players, and office suites. 
Many application programs deal principally with documents. Applications may be bundled with the 
8
computer and its system software or published separately, and may be coded as 
e.g. proprietary, open-source or university projects. 
9 
Linux Commands 
For a person new to Linux, finding Linux functional is still not very easy even after the emergence of 
user friendly Linux distribution like Ubuntu and Mint. The thing remains that there will always be some 
configuration on user’s part to be done manually. 
Just to start with, the first thing a user should know is the basic commands in terminal. Linux GUI runs 
on Shell. When GUI is not running but Shell is running, Linux is running. If Shell is not running, nothing 
is running. Commands in Linux are a means of interaction with Shell. For beginners some of the basic 
computational task is to: 
o View the contents of a directory: A directory may contain visible and invisible files with different 
file permissions. 
o Viewing blocks, HDD partition, External HDD 
o Checking the integrity of Downloaded/Transferred Packages 
o Converting and copying a file 
o Know your machine name, OS and Kernel 
o Viewing history 
o Being root 
o Make Directory 
o Make Files 
o Changing the file permission 
o Own a file 
o Install, Update and maintain Packages 
o Uncompressing a file 
o See current date, time and calendar 
o Print contents of a file 
o Copy and Move 
o See the working directory for easy navigation 
o Change the working directory, etc… 
And we have described the entire above basic computational task in our First Article. 
Part I – Switching From Windows to Nix or a Newbie to Linux – 20 Useful Commands for 
Linux Newbies 
This was the first article of this series. We tried to provide you with detailed description of these 
commands with explicit examples which was highly appreciated by our reader in terms of likes, 
comments and traffic.
What after these initial commands? Obviously we moved to the next part of this article where we 
provided commands for computational tasks like: 
10 
o Finding a file in a given directory 
o Searching a file with the given keywords 
o Finding online documentation 
o See the current running processes 
o Kill a running process 
o See the location of installed Binaries 
o Starting, Ending, Restarting a service 
o Making and removing of aliases 
o View the disk and space usages 
o Removing a file and/or directory 
o Print/echo a custom output on standard output 
o Changing password of on-self and others, if you are root. 
o View Printing queue 
o Compare two files 
o Download a file, the Linux way (wget) 
o Mount a block / partition / external HDD 
o Compile and Run a code written in ‘C’, ‘C++’ and ‘Java’ Programming Language 
This Second Article was again highly appreciated by the readers of Tecmint.com. The article was nicely 
elaborated with suitable examples and output. 
Part II – 20 Advanced Commands for Middle Level Linux Users 
After providing the users with the glimpse of Commands used by a Middle Level User we thought to 
give our effort in a nice write-up for a list of command used by an user of System Administrator Level. 
In our Third and last article of this series, we tried to cover the commands that would be required for the 
computational task like: 
o Configuring Network Interface 
o Viewing custom Network Related information 
o Getting information about Internet Server with customizable switches and Results 
o Digging DNS 
o Knowing Your System uptime 
o Sending an occasional Information to all other logged-in users 
o Send text messages directly to a user 
o Combination of commands
11 
o Renaming a file 
o Seeing the processes of a CPU 
o Creating newly formatted ext4 partition 
o Text File editors like vi, emacs and nano 
o Copying a large file/folder with progress bar 
o Keeping track of free and available memory 
o Backup a mysql database 
o Make difficult to guess – random password 
o Merge two text files 
o List of all the opened files 
Part III – 20 Advanced Commands for Linux Experts 
Writing this article and the list of command that needs to go with the article was a little cumbersome. We 
chose 20 commands with each article and hence gave a lot of thought for which command should be 
included and which should be excluded from the particular post. I personally selected the commands on 
the basis of their usability (as I use and get used to) from a user point of view and an Administrator point 
of view. 
This Articles aims to concatenate all the articles of its series and provide you with all the functionality in 
commands you can perform in our this very series of articles. 
There are too long lists of commands available in Linux. But we provided the list of 60 commands which 
is generally and most commonly used and a user having knowledge of these 60 commands as a whole 
can work in terminal very much smoothly. 
Command Description 
adduser Add a new user 
arch Print machine architecture 
awk Find and Replace text within file(s) 
bc An arbitrary precision calculator language 
cal Display a calendar 
Cat Concatenate files and print on the standard output 
Chdir Change working directory 
Chgrp Change the group ownership of files 
chkconfig Tool for maintaining the /etc/rc[0-6].d directory hierarchy 
chmod Change the access permissions of files and directories
12 
Chown Change the user and group ownership of files 
Chroot Change root directory 
Cksum Print CRC checksum and byte counts 
Clear Clear terminal screen 
Cmp Compare two files 
Comm Compare two sorted files line by line 
Cp Copy one or more files to another location 
Cron Daemon to execute scheduled commands 
crontab Schedule a command to run at a later time 
Csplit Split a file into context-determined pieces 
Cut Divide a file into several parts 
Date Display or change the date & time 
Dc Desk Calculator 
Dd Data Dump - Convert and copy a file 
Df Display free disk space 
Diff Display the differences between two files 
diff3 Show differences among three files 
Dir Briefly list directory contents 
dircolors Colour setup for `ls' 
dirname Convert a full pathname to just a path 
Du Estimate file space usage 
Echo Display message on screen 
Ed A line-oriented text editor (edlin) 
Egrep Search file(s) for lines that match an extended expression 
Eject Eject CD-ROM 
Env Display, set, or remove environment variables 
Expand Convert tabs to spaces 
Expr Evaluate expressions
13 
Factor Print prime factors 
False Do nothing, unsuccessfully 
fdformat Low-level format a floppy disk 
Fdisk Partition table manipulator for Linux 
Fgrep Search file(s) for lines that match a fixed string 
Find Search for files that meet a desired criteria 
Fmt Reformat paragraph text 
Fold Wrap text to fit a specified width 
Format Format disks or tapes 
Free Display memory usage 
Fsck Filesystem consistency check and repair 
Gawk Find and Replace text within file(s) 
Grep Search file(s) for lines that match a given pattern 
Groups Print group names a user is in 
Gzip Compress or decompress named file(s) 
Head Output the first part of file(s) 
hostname Print or set system name 
Id Print user and group id's 
Info Help info 
Install Copy files and set attributes 
Join Join lines on a common field 
Kill Stop a process from running 
Less Display output one screen at a time 
Ln Make links between files 
Locate Find files 
logname Print current login name 
Lpc Line printer control program 
Lpr Off line print
14 
Lprm Remove jobs from the print queue 
Ls List information about file(s) 
Man Help manual 
Mkdir Create new folder(s) 
mkfifo Make FIFOs (named pipes) 
mknod Make block or character special files 
more Display output one screen at a time 
mount Mount a file system 
mv Move or rename files or directories 
nice Set the priority of a command or job 
Nl Number lines and write files 
nohup Run a command immune to hangups 
passwd Modify a user password 
paste Merge lines of files 
pathchk Check file name portability 
pr Convert text files for printing 
printcap Printer capability database 
printenv Print environment variables 
printf Format and print data 
ps Process status 
Pwd Print Working Directory 
quota Display disk usage and limits 
quotacheck Scan a file system for disk usage 
quotactl Set disk quotas 
Ram ram disk device 
Rcp Copy files between two machines 
Rm Remove files 
rmdir Remove folder(s)
15 
Rpm Remote Package Manager 
rsync Remote file copy (Synchronize file trees) 
screen Terminal window manager 
Sdiff Merge two files interactively 
Sed Stream Editor 
select Accept keyboard input 
Seq Print numeric sequences 
shutdown Shutdown or restart linux 
sleep Delay for a specified time 
Sort Sort text files 
Split Split a file into fixed-size pieces 
Su Substitute user identity 
Sum Print a checksum for a file 
symlink Make a new name for a file 
Sync Synchronize data on disk with memory 
Tac Concatenate and write files in reverse 
Tail Output the last part of files 
Tar Tape Archiver 
Tee Redirect output to multiple files 
Test Evaluate a conditional expression 
Time Measure Program Resource Use 
touch Change file timestamps 
Top List processes running on the system 
traceroute Trace Route to Host 
Tr Translate, squeeze, and/or delete characters 
True Do nothing, successfully 
Tsort Topological sort 
Tty Print filename of terminal on stdin
16 
umount Unmount a device 
uname Print system information 
unexpand Convert spaces to tabs 
Uniq Uniquify files 
Units Convert units from one scale to another 
unshar Unpack shell archive scripts 
useradd Create new user account 
usermod Modify user account 
users List users currently logged in 
uuencode Encode a binary file 
uudecode Decode a file created by uuencode 
Vdir Verbosely list directory contents (`ls -l -b') 
watch Execute/display a program periodically 
Wc Print byte, word, and line counts 
whereis Report all known instances of a command 
Which Locate a program file in the user's path 
Who Print all usernames currently logged in 
Whoami Print the current user id and name (`id -un') 
Xargs Execute utility, passing constructed argument list(s) 
Yes Print a string until interrupted 
File Permissions: “chmod” 
In Linux-like operating systems, chmod is the command and system call which may change the access 
permissions to file system objects (files and directories). It may also alter special mode flags. The request 
is filtered by the umask. The name is an abbreviation of change mode. 
History: 
A chmod command first appeared in AT&T Unix version 1. 
As systems grew in number and types of users, access_control_lists were added to many file systems in 
addition to these most basic modes to increase flexibility.
17 
Command syntax: 
chmod [options] mode[,mode] file1 [file2 ...] 
Usual implemented options include: 
 -R recursive, i.e. include objects in subdirectories 
 -f force, forge ahead with all objects even if errors occur 
 -v verbose, show objects processed 
If a symbolic link is specified, the target object is affected. File modes directly associated with symbolic 
links themselves are typically never used. 
To view the file mode, the ls or stat commands may be used: 
$ ls -l findPhoneNumbers.sh 
-rwxr-xr-- 1 dgerman staff 823 Dec 16 15:03 findPhoneNumbers.sh 
$ stat -c %a findPhoneNumbers.sh 
The r, w, and x specify the read, write, and execute access, respectively. This script can be read, written 
to, and executed by the user, read and executed by other members of the staff group and can also be read 
by others. 
Octal modes: 
The chmod numeric format accepts up to four octal digits. The rightmost three refer to permissions for 
the file owner, the group, and other users. The optional next digit (fourth from the right) specifies 
special setuid, setgid, and sticky flags. 
Numerical permissions: 
# Permission rwx 
7 read, write and execute 111 
6 read and write 110 
5 read and execute 101 
4 read only 100 
3 write and execute 011 
2 write only 010 
1 execute only 001
18 
0 None 000 
Numeric example: 
In order to permit all users who are members of the programmers group to update a file 
$ ls -l sharedFile 
-rw-r--r-- 1 jsmith programmers 57 Jul 3 10:13 sharedFile 
$ chmod 664 sharedFile 
$ ls -l sharedFile 
-rw-rw-r-- 1 jsmith programmers 57 Jul 3 10:13 sharedFile 
Since the setuid, setgid and sticky bits are not specified, this is equivalent to: 
$ chmod 0664 sharedFile. 
Symbolic modes: 
The chmod command also accepts a finer-grained symbolic notation, which allows modifying specific 
modes while leaving other modes untouched. The symbolic mode is composed of three components, 
which are combined to form a single string of text: 
$ chmod [references][operator][modes] file ... 
The references (or classes) are used to distinguish the users to whom the permissions apply. If no 
references are specified it defaults to “all” but modifies only the permissions allowed by the unmask. The 
references are represented by one or more of the following letters: 
Reference Class Description 
U user the owner of the file 
G group users who are members of the file's group 
O others users who are neither the owner of the file nor members of the file's group 
A all all three of the above, same as ugo 
The chmod program uses an operator to specify how the modes of a file should be adjusted. The 
following operators are accepted: 
Operator Description 
+ adds the specified modes to the specified classes 
- removes the specified modes from the specified classes
19 
= the modes specified are to be made the exact modes for the specified classes 
The modes indicate which permissions are to be granted or removed from the specified classes. There are 
three basic modes which correspond to the basic permissions: 
Mode Name Description 
r read read a file or list a directory's contents 
w write write to a file or directory 
x execute execute a file or recurse a directory tree 
X 
special 
execute 
which is not a permission in itself but rather can be used instead of x. It applies 
execute permissions to directories regardless of their current permissions and applies 
execute permissions to a file which already has at least one execute permission bit 
already set (either user, group or other). It is only really useful when used with '+' and 
usually in combination with the -R option for giving group or other access to a big 
directory tree without setting execute permission on normal files (such as text files), 
which would normally happen if you just used "chmod -R a+rx .", whereas 
with 'X' you can do "chmod -R a+rX ." instead 
s setuid/gid details in Special modes section 
t sticky details in Special modes section 
Multiple changes can be specified by separating multiple symbolic modes with commas (without 
spaces). 
Symbolic examples: 
Add write permission (w) to the group's(g) access modes of a directory, 
allowing users in the same group to add files: 
$ ls -ld shared_dir # show access modes before chmod 
drwxr-xr-x 2 teamleader usguys 96 Apr 8 12:53 shared_dir 
$ chmod g+w shared_dir 
$ ls -ld shared_dir # show access modes after chmod 
drwxrwxr-x 2 teamleader usguys 96 Apr 8 12:53 shared_dir 
Remove write permissions (w) for all classes (a), 
preventing anyone from writing to the file: 
$ ls -l ourBestReferenceFile 
-rw-rw-r-- 2 teamleader usguys 96 Apr 8 12:53 ourBestReferenceFile
20 
$ chmod a-w ourBestReferenceFile 
$ ls -l ourBestReferenceFile 
-r--r--r-- 2 teamleader usguys 96 Apr 8 12:53 ourBestReferenceFile 
Set the permissions for the user and the group (ug) to read and execute (rx) only (no write 
permission)on referenceLib, 
preventing anyone other than the owner to add files. 
$ ls -ld referenceLib 
drwxr----- 2 teamleader usguys 96 Apr 8 12:53 referenceLib 
$ chmod ug=rx referenceLib 
$ ls -ld referenceLib 
dr-xr-x--- 2 teamleader usguys 96 Apr 8 12:53 referenceLib 
Special modes: 
The chmod command is also capable of changing the additional permissions or special modes of a file or 
directory. The symbolic modes use s to represent the setuid and setgid modes, and t to represent 
the sticky mode. The modes are only applied to the appropriate classes, regardless of whether or not other 
classes are specified. 
Most operating systems support the specification of special modes using octal modes, but some do not. 
On these systems, only the symbolic modes can be used. 
Command line examples: 
Command explanation 
chmod a+r publicComments.txt read is added for all classes (i.e. User, Group and Others). 
chmod +r publicComments.txt 
omitting the class defaults to all classes, but the resultant 
permissions are dependent on umask 
chmod a-x publicComments.txt execute permission is removed for all classes. 
chmod a+rx viewer.sh add read and execute for all classes. 
chmod u=rw,g=r,o= internalPlan.txt 
user(i.e. owner) can read and write, group can read, Others 
cannot access. 
chmod -R u+w,go-w docs 
add write permissions to the directory docs and all its contents 
(i.e. Recursively) for user and deny write access for everybody 
else.
21 
chmod ug=rw groupAgreements.txt User and Group members can read and write (update the file). 
chmod 664 global.txt 
sets read and write and no execution access for the user and 
group, and read, no write, no execute for all others. 
chmod 0744 myCV.txt 
equivalent to u=rwx (400+200+100),go=r (40+ 4). 
The 0 specifies no special modes. 
chmod 1755 findReslts.sh 
the 1000 specifies set sticky bit and the rest is equivalent 
to u=rwx (400+200+100),go=rx (40+10 + 4+1) This suggests 
that the script be retained in memory. 
chmod 4755 SetCtrls.sh 
the 4 specifies set user ID and the rest is equivalent to u=rwx 
(400+200+100),go=rx (40+10 + 4+1). 
chmod 2755 SetCtrls.sh 
the 2 specifies set group ID and the rest is equivalent to u=rwx 
(400+200+100),go=rx (40+10 + 4+1). 
chmod -R u+rwX,g-rwx,o-rxPersonalStuff 
Recursively set a directory tree to rwx for owner 
directories, rw for owner files, --- (i.e. no access) for group and 
others. 
chmod -R a-x+X publicDocs 
remove the execute permission on all files in a directory tree 
(i.e. Recursively), while allowing for directory browsing. 
System call: 
The POSIX standard defines the following function prototype: 
int chmod(const char *path, mode_t mode); 
The mode parameter is a bitfield composed of various flags: 
Flag Octal value Purpose 
S_ISUID 04000 Set user ID on execution 
S_ISGID 02000 Set group ID on execution 
S_ISVTX 01000 Sticky bit 
S_IRUSR, S_IREAD 00400 Read by owner 
S_IWUSR, S_IWRITE 00200 Write by owner 
S_IXUSR, S_IEXEC 00100 Execute/search by owner
22 
S_IRGRP 00040 Read by group 
S_IWGRP 00020 Write by group 
S_IXGRP 00010 Execute/search by group 
S_IROTH 00004 Read by others 
S_IWOTH 00002 Write by others 
S_IXOTH 00001 Execute/search by others 
Where alternate flag names are given, one of the pair of names might not be supported on some OSs. The 
octal values of the flags are summed or combined in a bitwise OR operation to give the desired 
permission mode. 
The function returns an error code. 
Shell Scripting 
A shell script is a computer program designed to be run by the Unix shell, a command line 
interpreter. The various dialects of shell scripts are considered to be scripting languages. 
Typical operations performed by shell scripts include file manipulation, program execution, and printing 
text. 
Shortcuts: 
In its most basic form, a shell script can provide a convenient variation of a system command where 
special environment settings, command options, or post-processing apply automatically, but in a way 
that allows the new script to still act as a fully normal linux command. 
One example would be to create a version of ls, the command to list files, giving it a shorter command 
name of l, which would be normally saved in a user's bin directory as/home/username/bin/l, and a default 
set of command options pre-supplied. 
#!/bin/sh 
LC_COLLATE=C ls -FCas "$@" 
Here, the first line (Shebang) indicates which interpreter should execute the rest of the script, and the 
second line makes a listing with options for file format indicators, columns, all files (none omitted), and 
a size in blocks. The LC_COLLATE=C sets the default collation order to not fold upper and lower case 
together, not intermix dotfiles with normal filenames as a side effect of ignoring punctuation in the 
names (dotfiles are usually only shown if an option like -a is used), and the "$@" causes any parameters 
given to l to pass through as parameters to ls, so that all of the normal options and other syntax known to 
ls can still be used.
The user could then simply use l for the most commonly used short listing. 
Another example of a shell script that could be used as a shortcut would be to print a list of all the files 
and directories within a given directory. 
23 
#!/bin/sh 
Clear: 
ls -al 
In this case, the shell script would start with its normal starting line of #!/bin/sh. Following this, the 
script executes the command clear which clears the terminal of all text before going to the next line. The 
following line provides the main function of the script. The ls -al command list the files and directories 
that are in the directory from which the script is being run. The ls command attributes could be changed 
to reflect the needs of the user. 
Note: If an implementation does not have the clear command, try using the clr command instead. 
Batch jobs: 
Shell scripts allow several commands that would be entered manually at a command-line interface to be 
executed automatically, and without having to wait for a user to trigger each stage of the sequence. For 
example, in a directory with three C source code files, rather than manually running the four commands 
required to build the final program from them, one could instead create a C shell script, here 
named build and kept in the directory with them, which would compile them automatically: 
#!/bin/csh 
echo compiling... 
cc -c foo.c 
cc -c bar.c 
cc -c qux.c 
cc -o myprog foo.o bar.o qux.o 
echo done. 
The script would allow a user to save the file being edited, pause the editor, and then just run ./build to 
create the updated program, test it, and then return to the editor. Since the 1980s or so, however, scripts 
of this type have been replaced with utilities like make which are specialized for building programs. 
Generalization: 
Simple batch jobs are not unusual for isolated tasks, but using shell loops, tests, and variables provides 
much more flexibility to users. A Bash (Linux shell) script to convert JPEG images to PNG images, 
where the image names are provided on the command line—possibly via wildcards—instead of each
being listed within the script, can be created with this file, typically saved in a file 
like /home/username/bin/jpg2png 
The jpg2png command can then be run on an entire directory full of JPEG images with 
just /home/username/bin/jpg2png *.jpg 
24 
Verisimilitude: 
A key feature of shell scripts is that the invocation of their interpreters is handled as a core operating 
system feature. So rather than a user's shell only being able to execute scripts in that shell's language, or 
a script only having its interpreter directive handled correctly if it was run from a shell (both of which 
were limitations in the early Bourne shell's handling of scripts), shell scripts are set up and executed by 
the OS itself. A modern shell script is not just on the same footing as system commands, but rather many 
system commands are actually shell scripts (or more generally, scripts, since some of them are not 
interpreted by a shell, but instead by Perl, Python, or some other language). This extends to returning 
exit codes like other system utilities to indicate success or failure, and allows them to be called as 
components of larger programs regardless of how those larger tools are implemented. 
Like standard system commands, shell scripts classically omit any kind of filename extension unless 
intended to be read into a running shell through a special mechanism for this purpose (such as sh’s “. ”, 
or csh’s source). 
Programming: 
Many modern shells also supply various features usually found only in more sophisticated general-purpose 
programming languages, such as control-flow constructs, variables, comments, 
arrays, subroutines, and so on. With these sorts of features available, it is possible to write reasonably 
sophisticated applications as shell scripts. However, they are still limited by the fact that most shell 
languages have little or no support for data typing systems, classes, threading, complex math, and other 
common full language features, and are also generally much slower than compiled code or interpreted 
languages written with speed as a performance goal. 
Advantages and disadvantages: 
Perhaps the biggest advantage of writing a shell script is that the commands and syntax are exactly the 
same as those directly entered at the command line. The programmer does not have to switch to a totally 
different syntax, as they would if the script were written in a different language, or if a compiled 
language was used. 
Often, writing a shell script is much quicker than writing the equivalent code in other programming 
languages. The many advantages include easy program or file selection, quick start, and interactive 
debugging. A shell script can be used to provide sequencing and decision-making linkage around
existing programs, and for moderately sized scripts the absence of a compilation step is an advantage. 
Interpretive running makes it easy to write debugging code into a script and re-run it to detect and fix 
bugs. Non-expert users can use scripting to tailor the behavior of programs, and shell scripting provides 
some limited scope for multiprocessing. 
On the other hand, shell scripting is prone to costly errors. Inadvertent typing errors such as rm -rf * 
/ (instead of the intended rm -rf */) are folklore in the Unix community; a single extra space converts the 
command from one that deletes everything in the sub-directories to one which deletes everything—and 
also tries to delete everything in the root directory. Similar problems can transform cp and mv into 
dangerous weapons, and misuse of the > redirect can delete the contents of a file. This is made more 
problematic by the fact that many UNIX commands differ in name by only one letter: cp, cd, dd, df, etc. 
Another significant disadvantage is the slow execution speed and the need to launch a new process for 
almost every shell command executed. When a script's job can be accomplished by setting up 
a pipeline in which efficient filter commands perform most of the work, the slowdown is mitigated, but a 
complex script is typically several orders of magnitude slower than a conventional compiled program 
that performs an equivalent task. 
There are also compatibility problems between different platforms. Larry Wall, creator of Perl, famously 
wrote that "It is easier to port a shell than a shell script." 
Similarly, more complex scripts can run into the limitations of the shell scripting language itself; the 
limits make it difficult to write quality code, and extensions by various shells to ameliorate problems 
with the original shell language can make problems worse. 
Many disadvantages of using some script languages are caused by design flaws within the language 
syntax or implementation, and are not necessarily imposed by the use of a text-based command line; 
there are a number of shells which use other shell programming languages or even full-fledged languages 
like Scsh (which uses Scheme). 
25 
Process Management 
Any application that runs on a Linux system is assigned a process ID or PID. This is a numerical 
representation of the instance of the application on the system. In most situations this information is only 
relevant to the system administrator who may have to debug or terminate processes by referencing the 
PID. Process Management is the series of tasks a System Administrator completes to monitor, manage, 
and maintain instances of running applications. 
Multitasking: 
Process Management beings with an understanding concept of Multitasking. Linux is what is referred to 
as a preemptive multitasking operating system. Preemptive multitasking systems rely on a scheduler. 
The function of the scheduler is to control the process that is currently using the CPU. In contrast,
symmetric multitasking systems such as Windows 3.1 relied on each running process to voluntary 
relinquish control of the processor. If an application in this system hung or stalled, the entire computer 
system stalled. By making use of an additional component to pre-empt each process when its “turn” is 
up, stalled programs do not affect the overall flow of the operating system. 
Each “turn” is called a time slice, and each time slice is only a fraction of a second long. It is this rapid 
switching from process to process that allows a computer to “appear’ to be doing two things at once, in 
much the same way a movie “appears” to be a continuous picture. 
26 
Types of Processes: 
There are generally two types of processes that run on Linux. Interactive processes are those processes 
that are invoked by a user and can interact with the user. VI is an example of an interactive process. 
Interactive processes can be classified into foreground and background processes. The foreground 
process is the process that you are currently interacting with, and is using the terminal as its stdin 
(standard input) and stdout (standard output). A background process is not interacting with the user and 
can be in one of two states – paused or running. 
The following exercise will illustrate foreground and background processes. 
1. Logon as root. 
2. Run [cd ] 
3. Run [vi] 
4. Press [ctrl + z]. This will pause vi 
5. Type [jobs] 
6. Notice vi is running in the background 
7. Type [fg %1]. This will bring the first background process to the foreground. 
8. Close vi. 
The second general type of process that runs on Linux is a system process or Daemon (day-mon). 
Daemon is the term used to refer to process’ that are running on the computer and provide services but 
do not interact with the console. Most server software is implemented as a daemon. Apache, Samba, and 
inn are all examples of daemons. 
Any process can become a daemon as long as it is run in the background, and does not interact with the 
user. A simple example of this can be achieved using the [ls –R] command. This will list all 
subdirectories on the computer, and is similar to the [dir /s] command on Windows. This command can 
be set to run in the background by typing [ls –R &], and although technically you have control over the 
shell prompt, you will be able to do little work as the screen displays the output of the process that you 
have running in the background. You will also notice that the standard pause (ctrl+z) and kill (ctrl+c) 
commands do little to help you.
27 
CHAPTER – 2 
C Language 
C is a programming language developed at AT & T’s Bell Laboratories of USA in 1972. It was designed 
and written by a man named Dennis Ritchie. In the late seventies C began to replace the more familiar 
languages of that time like PL/I, ALGOL, etc. No one pushed C. It wasn’t made the ‘official’ Bell Labs 
language. Thus, without any advertisement C’s reputation spread and its pool of users grew. Ritchie 
seems to have been rather surprised that so many programmers preferred C to older languages like 
FORTRAN or PL/I, or the newer ones like Pascal and APL. But, that's what happened. 
Possibly why C seems so popular is because it is reliable, simple and easy to use. Moreover, in an 
industry where newer languages, tools and technologies emerge and vanish day in and day out, a 
language that has survived for more than 3 decades has to be really good. 
Compilation and Execution 
Once you have written the program you need to type it and instruct the machine to execute it. To type 
your C program you need another program called Editor. Once the program has been typed it needs to be 
converted to machine language (0s and 1s) before the machine can execute it. To carry out this 
conversion we need another program called Compiler. Compiler vendors provide an Integrated 
Development Environment (IDE) which consists of an Editor as well as the Compiler. 
There are several such IDEs available in the market targeted towards different operating systems. For 
example, Turbo C, Turbo C++ and Microsoft C are some of the popular compilers that work under MS-DOS; 
Visual C++ and Borland C++ are the compilers that work under Windows, whereas gcc compiler 
works under Linux. Note that Turbo C++, Microsoft C++ and Borland C++ software also contain a C 
compiler bundled with them. If you are a beginner you would be better off using a simple compiler like 
Turbo C or Turbo C++. Once you have mastered the language elements you can then switch over to 
more sophisticated compilers like Visual C++ under Windows or gcc under Linux. Most of the 20 
Programs in this book would work with all the compilers. Wherever there is a deviation I would point it 
out that time. 
Assuming that you are using a Turbo C or Turbo C++ compiler here are the steps that you need to follow 
to compile and execute your first C program…
28 
Start the compiler at C> prompt. The compiler (TC.EXE is usually present in C:TCBIN directory). 
o Select New from the File menu. 
o Type the program. 
o Save the program using F2 under a proper name (say Program1.c). 
o Use Ctrl + F9 to compile and execute the program. 
o Use Alt + F5 to view the output. 
Note that on compiling the program its machine language equivalent is stored as an EXE file 
(Program1.EXE) on the disk. This file is called an executable file. If we copy this file to another machine 
we can execute it there without being required to recompile it. In fact the other machine need not even 
have a compiler to be able to execute the file. 
A word of caution! If you run this program in Turbo C++ compiler, you may get an error — “The 
function printf should have a prototype”. To get rid of this error, perform the following steps and then 
recompile the program. 
Select ‘Options’ menu and then select ‘Compiler | C++ Options’. In the dialog box that pops up, select 
‘CPP always’ in the ‘Use C++ Compiler’ options. 
Again select ‘Options’ menu and then select ‘Environment | Editor’. Make sure that the default extension 
is ‘C’ rather than ‘CPP’. 
Data Types 
Integral Types: 
 Integral data types include all whole numbers, that is numbers not having any fractional component. 
 The bits of integral types are interpreted as simple powers of two: 
o The right-most bit, known as the least significant bit, represents the number of 1s. ( 2^0 ) 
o The next bit represents the number of 2s. ( 2^1 ) 
o The next bit represents the number of 4s. ( 2^2 ) 
o The next bit represents the number of 8s. ( 2^3 ) 
o In general the nth bit from the right represents 2^(n-1) 
 For unsigned integral types, the leftmost bit, known as the most significant bit, represents 2^(N-1), 
where N is the total number of bits in the data item. 
o The range of possible values for an unsigned integer of N bits is from 0 to 2^N - 1. ( All 0s 
to all 1s ) 
o So for example, a 4-bit unsigned integer could range from 0 to 15, and an 8-bit unsigned 
integer could range from 0 to 255. 
 For signed integral types, the leftmost bit can be thought of as representing a negative 2^(N-1).
o ( The real interpretation in the computer is more complicated, but if you think of it this way 
29 
you will get the right answers. ) 
o The most negative value would be the first bit a 1 and all other bits 0s, yielding negative 
2^(N-1). 
o The most positive value would be the first bit a 0 and all other bits 1s, yielding 2^(N-1) - 1. 
o So for example, a 4-bit signed integer could range from -8 to +7, and an 8-bit signed integer 
could range from -128 to +127. 
o A signed integral type having all bits 1 is equal to -1, regardless of how many bits are in the 
number. 
 Signed and unsigned integers with the same number of total bits have the same number of different 
possible values. 
o Unsigned integers use one bit pattern ( all 0s ) to represent zero and all others to represent 
positive values. 
o Signed integers use half of the possible bit patterns to represent negative numbers, one 
pattern to represent zero, and half minus 1 to represent positive values. 
 Specific details of the integer types available on a particular implementation, along with the number 
of bits allocated to each one and their minimum and maximum allowable values can be found in the 
file limits.h 
int 
 The most basic and commonly used integral type is "int". 
 The int data type is always the "best" size for the particular computer it is running on, typically 32 
bits 
 Format specifiers for ints are either %d or %i, for either printf or scanf. 
long int 
 A long int typically uses twice as many bits as a regular int, allowing it to hold much larger 
numbers. 
o ( The C standard only specifies that a long cannot use a fewer number of bits than a 
regular int ) 
 printf and scanf replace %d or %i with %ld or %li to indicate the use of a long int. 
 long int may also be specified as just long. 
long long int 
 C99 introduces the long long int, typically having twice as many bits as a long int and 
printed/scanned using %lld or %lli format specifiers 
short int
30 
 A short int may use fewer bits than a regular int, thereby saving storage space. 
o ( The C standard only specifies that a short int cannot use more bits than a regular int. 
On many machines short ints use the same number of bits as regular ints. ) 
 printf and scanf replace %d or %i with %hd or%hi to indicate the use of a short int. 
 short int may also be specified as just short. 
unsigned ints 
 Unless otherwise specified, all of the aforementioned int types are signed numbers. 
 Any of the above may be preceded by the keyword "unsigned" to indicate that they are unsigned. 
o e.g. "unsigned int", "unsigned long int", "unsigned long", etc. 
o "unsigned" by itself implies "unsigned int" 
 The format specifier for unsigned integers in decimal form is %u. The u may be preceded by l, ll, or 
h for long, long long, and short unsigned types respectively. 
 Unsigned integers can also be printed or scanned in octal or hexidecimal form using the %o, %x, or 
%X format specifiers in place of %u. 
char 
 Normally chars are interpreted as characters ( see below ) 
 Technically the char data type is an integral type, always having exactly 8 bits, ( known as a byte. ) 
 Signed chars can have values from -128 to +127, and can be printed as integers using %d or %i 
format specifiers 
 chars can also be specified as unsigned, giving them a range from 0 to+255. 
o Unsigned chars can be printed using %u, %o, %x, or %X format specifiers. 
Integral Constants 
 int constants in decimal form must begin with a digit from 1 to 9, and can be followed by additional 
digits from 0 to 9. 
o in octal form they must begin with a 0, and following digits must be in the range from 0 to 7. 
o in hexidecimal form they begin with 0x. Followng digits must be in the range 0 to 9, or A to 
F, or a to f. 
 Any of the above may be preceded by a minus sign. 
 int constants may be followed by a u, l, or ll to indicate unsigned, long, or long long constants 
respectively. 
 Allowable formats are as follows, where the [ square brackets ] denote optional characters: 
 Decimal: [±]1-9[0-9...][Ll][Uu] 
 Octal: [±]0[0-7...][Ll][Uu] 
 Hexadecimal: [±]0x[0-9a-fA-F...][Ll][Uu] 
Integer Overflow
 When integer math yields a result that is too big ( or too negative ) to be represented by the 
31 
corresponding integer type, then overflow ( or underflow ) is said to have occurred. 
 With unsigned numbers the result is defined to "wrap around" to the other end of the integer's range, 
so if you add 1 to an integer that is at the maximum value for it's type, the result is zero. 
 With signed numbers the result of overflow is undefined. In some cases adding 1 to the maximum 
positive value will wrap around to the most negative value, but in other cases erratic behaviour or 
even a program crash may occur. 
Floating Point Types: 
 Floating point types include all types in which a number may have a fractional component. 
Fortunately there are only three that we need to worry about - float, double, and long double. 
 Specific details of the floating point types available on a particular implementation, along with the 
number of bits allocated to each one and their minimum and maximum allowable values can be 
found in the file float.h 
float 
 The most basic type of floating point number is the float type. 
 According to the IEEE standard, a single precision floating point number is exactly 32 bits long, 
comprised of: 
o one bit for indicating the sign 
o 23 bits ( plus one implied ) for recording the digits as a 24-bit integer. This works out to 
about 6 or 7 decimal digits of precision. 
o 8 bits for a binary exponent, to shift the digits left or right. This works out to an absolute 
range from about 10^-38 to 10^38 
 Note that because a float only has 24 bits available for storing the digits of the number, it can 
actually be less precise than a 32-bit int for certain large integers. 
double 
 The double precision data type uses twice as many bits as a float, yielding approximately twice the 
number of digits of precision. 
 According to the IEEE standard, a double precision floating point number is 64 bits long, comprised 
of: 
o one sign bit. 
o 52 bits ( plus one implied ) for recording digits, which works out to about 15 decimal digits 
of precision. 
o 11 bits for the exponent, which works out to a range of about 10^-308 to 10^308
 The double data type is the preferred floating point type for most scientific and engineering 
32 
calculations. 
long double 
 The long double type is guaranteed to have more bits than a double, but the exact number my 
vary from one hardware platform to another. The most typical implementations are either 80 or 128 
bits. 
 The IEEE standard for quadruple precision floating point numbers is 128 bits consisting of: 
o one sign bit 
o 112 bits ( plus one implied ) for digits, working out to about 34 decimal digits of precision 
o 15 bits for the exponent, giving a range from about 10^-4932 to 10^4932 
Floating point constants 
 Floating-point constants are normally indicated by the presence of a decimal point, and are normally 
doubles. 
o Floating point constants may be followed by either an "F" to indicate an ordinary float, or an 
"L" to indicate a long double. 
o Floating point constants can also be expressed in scientific notation 
o Allowable formats are as follows: 
o [±]1-9[0-9...].[0-9...][Ee[±]0-9...][FfLl] 
o [±][0].[0-9...][Ee[±]0-9...][FfLl] 
o [±]1-9[0-9...]Ee[±]0-9...[FfLl] 
[±]1-9[0-9...]Ff[Ll] 
Characters: 
 Although the char data type is technically a small integer ( see above ), its most common use is to 
hold numeric codes representing ( ASCII ) characters. 
 For example, the ASCII code for the capital letter 'A' is 65. 
 Note that the character '9' is not the same as the integer value 9. 
o In this case the ASCII code for the character '9' is 57. 
o The numerical value 9 in the ASCII code set happens to represent a horizontal tab character. 
Character Constants 
 Character constants are enclosed in single quotes, such as 'A'. 
 Character constants may also include escape characters such as 'n' or 't', as shown here:
33 
Escape Sequence Meaning 
a alarm ( bell ) 
b Backspace 
f Form feed ( clears screen ) 
n New line 
r Carriage return 
t Horizontal tab 
v Vertical tab 
 Backslash 
? Question mark 
' Single quote 
" Double quote 
0 Numerical zero ( null byte ) 
 The  can also be used to escape a 3-digit octal numerical constant, or a hexidecimal constant 
beginning with x 
o So '112' and 'x4A' are both a capitol 'J'. See the ASCII table to confirm. 
Character Arithmetic 
 Because chars are really small integers, it is possible to do mathematical operations on them. For 
example: 
char letter = 'G', lower, upper; 
// Presume lower has been given a value somehow 
letter = letter + 3; // letter has now been changed from 'G' to 'J' 
if( lower >= 'a' && lower <= 'z' ) // If lower is a lower-case letter 
upper = lower + ( 'A' - 'a' ) // Convert it to upper-case by adding an offset 
Character Strings 
 C stores character strings as arrays of type char, terminated by a null byte.
 Constant character strings are enclosed in double quotes, and may include escape characters, such as 
34 
"nnt Please enter X > " 
 These notes will postpone further discussion of character strings until after arrays have been covered. 
Special Types 
 The enumerated type is an integer with a restricted list of legal values, referred to by names. It will 
be covered in full details in the section on structs and unions. 
 C99 introduces new types _Bool, _Complex, and _Imaginary. 
Type Conversions 
Implicit 
 There are certain cases in which data will get automatically converted from one type to another: 
o When data is being stored in a variable, if the data being stored does not match the type of 
the variable. 
 The data being stored will be converted to match the type of the storage variable. 
o When an operation is being performed on data of two different types. 
 The "smaller" data type will be converted to match the "larger" type. 
 For example, when an int is added to a double, the computer uses a double 
version of the int and the result is a double. 
 The following example converts the value of nTotal to a double precision value 
before performing the division. 
Note that if the 3.0 were changed to a simple 3, then integer division would be 
performed, losing any fractional values in the reuslt. 
average = nTotal / 3.0; 
o When data is passed to or returned from functions. 
Explicit 
 Data may also be expressly converted, using the typecast operator 
o The following example converts the value of nTotal to a double precision value before 
performing the division. 
( nStudents will then be implicitly promoted, following the guidelines listed above. ) 
average = ( double ) nTotal / nStudents; 
o Note that nTotal itself is unaffected by this conversion. 
Variables: 
 A variable is a named storage location, where data may be stored and later changed. 
 An identifier is a more general term for a named location, which may contain either data or code. 
o Identifiers must begin with a letter or an underscore, preferable letters for user programs. 
o The remaining characters must be either alphanumeric or underscores.
o Identifiers may be of any length, but only the first 31 characters are examined in most 
35 
implementations. 
o Identifiers are case sensitive, so "NUMBER", "number", and "Number" are three 
different identifiers. 
o By convention ordinary variables begin with a lower case letter, globals with a Single 
Capital, and constants in ALL CAPS. 
 Multi-word variables may use either underscores or "camel case", such as 
"new_value" or "newValue". 
o Integers are usually assigned variable names beginning with the letters I, J, K, L, M, or 
N, and floating point variables are usually assigned names beginning with other letters. 
o Identifiers may not be the same as reserved words. ( See text for a full list. ) 
 All variables must be declared before they can be used. 
o In C, all variables must be declared before the first executable statement of the program. 
o C++ allows variables to be declared any time before they are used, but it is still normally 
good practice to declare all variables at the beginning of the program, unless there is a very 
good reason to do otherwise. 
 ( Exceptions: Loop counter variables are often declared as part of the loop 
structure. Occasionally it is beneficial to declare variables within a reduced scope, to 
be discussed later. ) 
 Variables may be given an initial value at the time they are declared. This is called 
"initialization", or "initializing the variables". 
o Initialization in C should be done using an equals sign. 
o Example: double x1 = 0.0, x2 = 0.0; 
o UNINITIALIZED VARIABLES ARE DANGEROUS, AND SHOULD BE CONSIDERED 
TO HOLD RANDOM VALUES. 
 Variables may be declared "const", meaning that their values cannot be changed. 
o const variables MUST be initialized at the time they are declared. 
o By convention, const variables are named using ALL CAPS. 
o Examples: 
 const double PI = 3.14159; 
 const int MAXROWS = 100; 
o Note: K&R C did not have the const keyword, and so the #define pre-processor macro 
was used to define constant values. The const qualifier is a better approach when it is 
available, because it allows the compiler to perform type checking among other reasons. For
CS 107 we will defer the discussion of #define until we get to the chapter on the pre-processor. 
36 
Type-Related Functions and Concepts ( Advanced, Optional ) 
 The sizeof( ) operator returns the number of bytes needed to store a variable or data type, so on most 
sytems, sizeof( int ) would yield 4, as would sizeof( number ) if number were a variable of type int. 
 The keyword typedef allows programmers to define their own type names. 
o For example, "typedef float Dollars;" would define a new type named "Dollars" that is 
really the same as float. 
o In this case the programmer can now declare variables to be of type "Dollars" instead of 
type float. 
o One advantage is for portability purposes. If the typedef is changed to "typedef double 
Dollars", then it will affect all variables of type Dollars in the entire program, with one 
small change. 
o It can also make programs more readable when complicated types are used. For 
example, "typedef unsigned long long int BigInteger;" 
o ( typedef is most commonly used to rename complicated types that we have not yet 
covered, such as structs and pointers. ) 
Enumerated Types: 
 Enumerated ( enum ) data types are basically ints, except that they are restricted to a limited set 
of values, and those values are referred to by name not by number. 
 The use of enums where applicable helps make code more readable and also limits the 
possibilities for bad values, thereby reducing bugs and making the code more maintainable and 
overall better. 
 The enum keyword is used to define a new data type, having a new data type name and list of 
acceptable named values. 
 Once the new enum type has been declared, variables can be declared of the new type, and 
assigned the named values. 
 Named values can be assigned specific numbers. Those not assigned will get successive values. 
So in the following example, minor2 will have the value 2 and major2 will have the value 101: 
enum errorType { none = 0, minor1 = 1, minor2, major1 = 100, major2, fatal1 = 1000 }; 
 Enumerated type variables can also be initialized.. For example: 
errorType errorCode = none; 
sizeType bookSize = large;
 It is sometimes a good idea to include values such as "invalid", "undefined" or "none" among the list 
37 
of enumerated values. 
 Some compilers may allow using enum variables with ordinary integers, ( e.g. using numbers instead 
of names ), but it is poor practice. 
 Printing enumerated variables prints the assigned integer value. 
 One should not attempt to do any math using enumerated variables. 
Constants and Variables 
Constants: 
Constants in C refer to fixed values that do not change during the execution of a 
program. 
Integer Constants 
An integer constant refers to a sequence of digits, There are three types integers, 
namely, decimal, octal, and hexa decimal. 
Decimal Constant 
Eg:123,-321 etc., 
Note: Embedded spaces, commas and non-digit characters are not permitted 
between digits. 
Eg: 1) 15 750 2)$1000 
Octal Constant 
An octal integer constant consists of any combination of digits from the set 0 through 
7, with a leading 0. 
Eg: 1) 037 2) 0435 
Hexadecimal Constant 
A sequence of digits preceded by 0x or 0X is considered as hexadecimal integer. 
They may also include alphabets A through F or a through f. 
Eg: 1) 0X2 2) 0x9F 3) 0Xbcd 
Program for representation of integer constants on a 16-bit computer. 
/*Integer numbers on a 16-bit machine*/ 
main() 
{ 
printf(“Integer valuesnn”); 
printf(“%d%d%dn”,32767,32767+1,32767+10); 
printf(“n”);
38 
printf(“Long integer valuesnn”); 
printf(“%ld%ld%ldn” ,32767L,32767L+1L,32767L+10L); 
} 
OUTPUT 
Integer values 
32767 -32768 -32759 
Long integer values 
32767 32768 32777 
Real Constants 
Certain quantities that vary continuously, such as distances, heights etc., are 
represented by numbers containing functional parts like 17.548.Such numbers are called real 
(or floating point)constants. 
Eg:0.0083,-0.75 etc., 
A real number may also be expressed in exponential or scientific notation. 
Eg:215.65 may be written as 2.1565e2 
Single Character Constants 
A single character constants contains a single character enclosed within a pair of 
single quote marks. 
String Constants 
A string constant is a sequence of characters enclosed in double quotes. The 
characters may be letters, numbers, special characters and blank space. 
Eg:”Hello!” 
“1987” 
“?….!” 
Backslash Character Constants 
C supports special backslash character constants that are used in output functions. 
These character combinations are known as escape sequences. 
Constant Meaning 
‘a’ audible alert 
‘b’ backspace 
‘f’ form feed 
‘n’ new line 
‘0’ null 
‘v’ vertical tab
39 
‘t’ horizontal tab 
‘r’ carriage return 
Variables: 
Definition: 
A variable is a data name that may be used to store a data value. A variable may take 
different values at different times of execution and may be chosen by the programmer in a 
meaningful way. It may consist of letters, digits and underscore character. 
Eg: 1) Average 
2) Height 
Rules for defining variables 
 They must begin with a letter. Some systems permit underscore as the first character. 
 ANSI standard recognizes a length of 31 characters. However, the length should not 
be normally more than eight characters. 
 Uppercase and lowercase are significant. 
 The variable name should not be a keyword. 
 White space is not allowed. 
Arrays in C 
In C programming, one of the frequently arising problem is to handle similar types of data. For example: 
If the user want to store marks of 100 students. This can be done by creating 100 variable individually 
but, this process is rather tedious and impracticable. These type of problem can be handled in C 
programming using arrays. 
An array is a sequence of data item of homogeneous value(same type). 
Arrays are of two types: 
1. One-dimensional arrays 
2. Multidimensional arrays 
Declaration of one-dimensional array 
data_type array_name[array_size]; 
For example: 
int age[5];
Here, the name of array is age. The size of array is 5,i.e., there are 5 items(elements) of array age. All 
element in an array are of the same type (int, in this case). 
Array elements 
Size of array defines the number of elements in an array. Each element of array can be accessed and used 
by user according to the need of program. For example: 
int age[5]; 
Note that, the first element is numbered 0 and so on. 
Here, the size of array age is 5 times the size of int because there are 5 elements. 
Suppose, the starting address of age[0] is 2120d and the size of int be 4 bytes. Then, the next address 
(address of a[1]) will be 2124d, address of a[2] will be 2128d and so on. 
Initialization of one-dimensional array: 
Arrays can be initialized at declaration time in this source code as: 
40 
int age[5]={2,4,34,3,4}; 
It is not necessary to define the size of arrays during initialization. 
int age[]={2,4,34,3,4}; 
In this case, the compiler determines the size of array by calculating the number of elements of an array. 
Accessing array elements 
In C programming, arrays can be accessed and treated like variables in C. 
For example: 
scanf("%d",&age[2]); 
/* statement to insert value in the third element of array age[]. */ 
scanf("%d",&age[i]); 
/* Statement to insert value in (i+1)th element of array age[]. */ 
/* Because, the first element of array is age[0], second is age[1], ith is age[i-1] and (i+1)th is age[i]. */ 
printf("%d",age[0]); 
/* statement to print first element of an array. */
41 
printf("%d",age[i]); 
/* statement to print (i+1)th element of an array. */ 
Example of array in C programming 
/* C program to find the sum marks of n students using arrays */ 
#include <stdio.h> 
int main(){ 
int marks[10],i,n,sum=0; 
printf("Enter number of students: "); 
scanf("%d",&n); 
for(i=0;i<n;++i){ 
printf("Enter marks of student%d: ",i+1); 
scanf("%d",&marks[i]); 
sum+=marks[i]; 
} 
printf("Sum= %d",sum); 
return 0; 
} 
Output 
Enter number of students: 3 
Enter marks of student1: 12 
Enter marks of student2: 31 
Enter marks of student3: 2 
sum=45 
Important thing to remember in C arrays 
Suppose, you declared the array of 10 students. For example: arr[10]. You can use array members 
from arr[0] to arr[9]. But, what if you want to use element arr[10], arr[13] etc. Compiler may not show 
error using these elements but, may cause fatal error during program execution. 
C programming language allows programmer to create arrays of arrays known as multidimensional 
arrays. For example: 
float a[2][6];
Here, a is an array of two dimension, which is an example of multidimensional array. This array has 2 
rows and 6 columns 
For better understanding of multidimensional arrays, array elements of above example can be thinked of 
as below: 
42 
Initialization of Multidimensional Arrays 
In C, multidimensional arrays can be initialized in different number of ways. 
int c[2][3]={{1,3,0}, {-1,5,9}}; 
OR 
int c[][3]={{1,3,0}, {-1,5,9}}; 
OR 
int c[2][3]={1,3,0,-1,5,9}; 
Initialization Of three-dimensional Array 
double cprogram[3][2][4]={ 
{{-0.1, 0.22, 0.3, 4.3}, {2.3, 4.7, -0.9, 2}}, 
{{0.9, 3.6, 4.5, 4}, {1.2, 2.4, 0.22, -1}}, 
{{8.2, 3.12, 34.2, 0.1}, {2.1, 3.2, 4.3, -2.0}} 
}; 
Suppose there is a multidimensional array arr[i][j][k][m]. Then this array can hold i*j*k*m numbers of 
data. 
Similarly, the array of any dimension can be initialized in C programming. 
Example of Multidimensional Array In C 
Write a C program to find sum of two matrix of order 2*2 using multidimensional arrays where, 
elements of matrix are entered by user. 
#include <stdio.h> 
int main(){ 
float a[2][2], b[2][2], c[2][2]; 
int i,j; 
printf("Enter the elements of 1st matrixn");
/* Reading two dimensional Array with the help of two for loop. If there was an array of 'n' dimension, 
'n' numbers of loops are needed for inserting data to array.*/ 
for(i=0;i<2;++i) 
43 
for(j=0;j<2;++j){ 
printf("Enter a%d%d: ",i+1,j+1); 
scanf("%f",&a[i][j]); 
} 
printf("Enter the elements of 2nd matrixn"); 
for(i=0;i<2;++i) 
for(j=0;j<2;++j){ 
printf("Enter b%d%d: ",i+1,j+1); 
scanf("%f",&b[i][j]); 
} 
for(i=0;i<2;++i) 
for(j=0;j<2;++j){ 
/* Writing the elements of multidimensional array using loop. */ 
c[i][j]=a[i][j]+b[i][j]; /* Sum of corresponding elements of two arrays. */ 
} 
printf("nSum Of Matrix:"); 
for(i=0;i<2;++i) 
for(j=0;j<2;++j){ 
printf("%.1ft",c[i][j]); 
if(j==1) /* To display matrix sum in order. */ 
printf("n"); 
} 
return 0; 
} 
Ouput 
Enter the elements of 1st matrix 
Enter a11: 2; 
Enter a12: 0.5; 
Enter a21: -1.1; 
Enter a22: 2; 
Enter the elements of 2nd matrix
44 
Enter b11: 0.2; 
Enter b12: 0; 
Enter b21: 0.23; 
Enter b22: 23; 
Sum Of Matrix: 
2.2 0.5 
-0.9 25.0 
CHAPTER – 3 
Embedded C 
Functions 
The functions in the embedded C library are: 
Runtime support functions 
These functions carry out operations that are not available as ARM 
instructions, such as division. These functions are provided by the 
embedded C library. 
Software floating-point library 
When the compiler compiles code for use with software floating-point, it 
generates calls to routines in the library to perform floating-point 
operations. For example, to perform a double-precision multiply, the 
compiler generates a call to _dmul. All such routines are provided as 
standard by the embedded C library. 
C library subset 
This provides a subset of the C library routines. Only functions that fulfil 
the criteria described below have been included in the embedded C 
library. Refer to C library subset on page 4-19 for a complete list of the
45 
included functions. 
Static data 
The embedded C library does not make any use of static data. Because of this, it is 
automatically fully reentrant. 
Operating-system independence 
The embedded C functions do not rely on the underlying operating system in any way. 
Many functions in the full ANSI library rely on the underlying OS to perform functions 
such as writing characters, or opening files. These functions are excluded from the 
embedded C library. For example, functions such as printf() are excluded, but 
sprintf() is not. 
Standalone functions 
Many functions in the full ANSI C library rely on a number of other functions in the C 
library to perform their operations. For example, printf() relies on functions such as 
ferror() and fputc(). This means that a single call to printf() includes a large amount 
of the C library code. 
The embedded C library breaks many of these dependencies so that only the minimum 
amount of code needed to perform the operation is included. 
C library subset 
Table 4-4 lists the C library functions that are supported in the embedded C library. 
Name From Description 
<assert.h> 
Contains the assert macro, used to assist with detecting logical errors and other 
types of bug in debugging versions of a program. 
<complex.h> C99 A set of functions for manipulating complex numbers. 
<ctype.h> 
Defines set of functions used to classify characters by their types or to convert 
between upper and lower case in a way that is independent of the used character 
set (typically ASCII or one of its extensions, although implementations 
utilizing EBCDIC are also known). 
<float.h> 
Defines macro constants specifying the implementation-specific properties of 
the floating-point library.
46 
Name From Description 
<math.h> 
Defines common mathematical functions. 
<setjmp.h> 
Declares the macros setjmp and longjmp, which are used for non-local exits. 
<stdio.h> 
Defines core input and output functions 
Unions 
Unions don't take long to explain. They are the same as structures, except that, where you would have 
written struct before, now you write union. Everything works the same way, but with one big exception. 
In a structure, the members are allocated separate consecutive chunks of storage. In a union, every 
member is allocated the same piece of storage. What would you use them for? Well, sometimes you want 
a structure to contain different values of different types at different times but to conserve space as much 
as possible. Using a union, it's up to you to keep track of whatever type you put into it and make sure that 
you retrieve the right type at the right time. Here's an example: 
#include <stdio.h> 
#include <stdlib.h> 
main(){ 
union { 
float u_f; 
int u_i; 
}var; 
var.u_f = 23.5; 
printf("value is %fn", var.u_f); 
var.u_i = 5; 
printf("value is %dn", var.u_i); 
exit(EXIT_SUCCESS); 
} 
If the example had, say, put a float into the union and then extracted it as an int, a strange value would 
have resulted. The two types are almost certainly not only stored differently, but of different lengths. 
The int retrieved would probably be the low-order bits of the machine representation of a float, and 
might easily be made up of part of the mantissa of the float plus a piece of the exponent. The Standard
says that if you do this, the behaviour is implementation defined (not undefined). The behaviour is 
defined by the Standard in one case: if some of the members of a union are structures with a ‘common 
initial sequence’ (the first members of each structure have compatible type and in the case of bitfields are 
the same length), and the union currently contains one of them, then the common initial part of each can 
be used interchangeably. Oh good. 
The C compiler does no more than work out what the biggest member in a union can be and allocates 
enough storage (appropriately aligned if necessary). In particular, no checking is done to make sure that 
the right sort of use is made of the members. That is your task, and you'll soon find out if you get it 
wrong. The members of a union all start at the same address—there is guaranteed to be no padding in 
front of any of them. 
The most common way of remembering what is in a union is to embed it in a structure, with another 
member of the structure used to indicate the type of thing currently in the union. Here is how it might be 
used: 
47 
#include <stdio.h> 
#include <stdlib.h> 
/* code for types in union */ 
#define FLOAT_TYPE 1 
#define CHAR_TYPE 2 
#define INT_TYPE 3 
struct var_type{ 
int type_in_union; 
union{ 
float un_float; 
char un_char; 
int un_int; 
}vt_un; 
}var_type; 
void 
print_vt(void){ 
switch(var_type.type_in_union){ 
default: 
printf("Unknown type in unionn"); 
break; 
case FLOAT_TYPE:
48 
printf("%fn", var_type.vt_un.un_float); 
break; 
case CHAR_TYPE: 
printf("%cn", var_type.vt_un.un_char); 
break; 
case INT_TYPE: 
printf("%dn", var_type.vt_un.un_int); 
break; 
} 
} 
main(){ 
var_type.type_in_union = FLOAT_TYPE; 
var_type.vt_un.un_float = 3.5; 
print_vt(); 
var_type.type_in_union = CHAR_TYPE; 
var_type.vt_un.un_char = 'a'; 
print_vt(); 
exit(EXIT_SUCCESS); 
} 
That also demonstrates how the dot notation is used to access structures or unions inside other structures 
or unions. Some current C compilers allow you to miss bits out of the names of embedded objects 
provided that they are not ambiguous. In the example, such an unambiguous name would 
be var_type.un_int and the compiler would work out what you meant. None the less this is not permitted 
by the Standard. 
It is because of unions that structures cannot be compared for equality. The possibility that a structure 
might contain a union makes it hard to compare such structures; the compiler can't tell what the union 
currently contains and so wouldn't know how to compare the structures. This sounds a bit hard to 
swallow and isn't 100% true—most structures don't contain unions—but there is also a philosophical 
issue at stake about just what is meant by ‘equality’ when applied to structures. Anyhow, the union 
business gives the Standard a good excuse to avoid the issue by not supporting structure comparison. 
Structures
Structures are perhaps what makes C such a powerful language for creating very complex programs with 
huge amounts of data. They are basically a way of grouping together related data items under a single 
symbolic name. 
49 
Why Use Structures? 
Here is an example: A piece of C51 software had to perform a linearization process on the raw signal 
from a variety of pressure sensors manufactured by the same company. For each sensor to be catered for 
there is an input signal with a span and offset, a temperature coefficient, the signal conditioning 
amplifier, a gain and offset. The information for each sensor type could be held in "normal" constants 
thus: 
1. unsigned char sensor_type1_gain = 0x30 ; 
2. unsigned char sensor_type1_offset = 0x50 ; 
3. unsigned char sensor_type1_temp_coeff = 0x60 ; 
4. unsigned char sensor_type1_span = 0xC4 ; 
5. unsigned char sensor_type1_amp_gain = 0x21 ; 
6. unsigned char sensor_type2_gain = 0x32 ; 
7. unsigned char sensor_type2_offset = 0x56 ; 
8. unsigned char sensor_type2_temp_coeff = 0x56 ; 
9. unsigned char sensor_type2_span = 0xC5 ; 
10. unsigned char sensor_type2_amp_gain = 0x28 ; 
11. unsigned char sensor_type3_gain = 0x20 ; 
12. unsigned char sensor_type3_offset = 0x43 ; 
13. unsigned char sensor_type3_temp_coeff = 0x61 ; 
14. unsigned char sensor_type3_span = 0x89 ; 
15. unsigned char sensor_type3_amp_gain = 0x29 ; 
As can be seen, the names conform to an easily identifiable pattern of: 
1. unsigned char sensor_typeN_gain = 0x20 ; 
2. unsigned char sensor_typeN_offset = 0x43 ; 
3. unsigned char sensor_typeN_temp_coeff = 0x61 ; 
4. unsigned char sensor_typeN_span = 0x89 ; 
5. unsigned char sensor_typeN_amp_gain = 0x29 ; 
Where 'N' is the number of the sensor type. A structure is a neat way of condensing this type of related 
and repeating data. In fact the information needed to describe a sensor can be reduced to a generalized: 
1. unsigned char gain ; 
2. unsigned char offset ;
50 
3. unsigned char temp_coeff ; 
4. unsigned char span ; 
5. unsigned char amp_gain ; 
The concept of a structure is based on this idea of generalized "template" for related data. In this case, a 
structure template (or "component list") describing any of the manufacturer's sensors would be declared: 
1. struct SENSOR_DESC 
2. { 
3. unsigned char gain ; 
4. unsigned char offset ; 
5. unsigned char temp_coeff ; 
6. unsigned char span ; 
7. unsigned char amp_gain ; 
8. } ; 
This does not physically do anything to memory. At this stage it merely creates a template which can 
now be used to put real data into memory. 
This is achieved by: 
1. struct SENSOR_DESC sensor_database ; 
This reads as "use the template SENSOR_DESC to layout an area of memory named sensor_database, 
reflecting the mix of data types stated in the template". Thus a group of 5 unsigned chars will be created 
in the form of a structure. 
The individual elements of the structure can now be accessed as: 
1. sensor_database.gain = 0x30 ; 
2. sensor_database.offset = 0x50 ; 
3. sensor_database.temp_coeff = 0x60 ; 
4. sensor_database.span = 0xC4 ; 
5. sensor_database.amp_gain = 0x21 ; 
Arrays Of Structures 
In the example though, information on many sensors is required and, as with individual chars and ints, it 
is possible to declare an array of structures. This allows many similar groups of data to have different 
sets of values. 
1. struct SENSOR_DESC sensor_database[4] ;
This creates four identical structures in memory, each with an internal layout determined by the structure 
template. Accessing this array is performed simply by appending an array index to the structure name: 
1. /*Operate On Elements In First Structure Describing */ 
51 
2. /*Sensor 0 */ 
3. sensor_database[0].gain = 0x30 ; 
4. sensor_database[0].offset = 0x50 ; 
5. sensor_database[0].temp_coeff = 0x60 ; 
6. sensor_database[0].span = 0xC4 ; 
7. sensor_database[0].amp_gain = 0x21 ; 
8. /* Operate On Elements In First Structure Describing */ 
9. /*Sensor 1 */ 
10. sensor_database[1].gain = 0x32 ; 
11. sensor_database[1].offset = 0x56 ; 
12. sensor_database[1].temp_coeff = 0x56 ; 
13. sensor_database[1].span = 0xC5 ; 
14. sensor_database[1].amp_gain = 0x28 ; 
15. // and so on... 
C Preprocessor 
Including files: 
One of the most common uses of the preprocessor is to include another file: 
#include <stdio.h> 
int main(void) 
{ 
printf("Hello, world!n"); 
return 0; 
} 
The preprocessor replaces the line #include <stdio.h> with the text of the file 'stdio.h', which declares 
the printf() function among other things. 
This can also be written using double quotes, e.g. #include "stdio.h". If the filename is enclosed within 
angle brackets, the file is searched for in the standard compiler include paths. If the filename is enclosed 
within double quotes, the search path is expanded to include the current source directory. C compilers
and programming environments all have a facility which allows the programmer to define where include 
files can be found. This can be introduced through a command line flag, which can be parameterized 
using a makefile, so that a different set of include files can be swapped in for different operating systems, 
for instance. 
By convention, include files are given a .h extension, and files not included by others are given 
a .c extension. However, there is no requirement that this be observed. Occasionally you will see files 
with other extensions included: files with a .def extension may denote files designed to be included 
multiple times, each time expanding the same repetitive content; #include "icon.xbm" is likely to refer to 
an XBM image file (which is at the same time a C source file). 
#include often compels the use of #include guards or #pragma once to prevent double inclusion. 
52 
Conditional compilation: 
The if-else directives #if, #ifdef, #ifndef, #else, #elif and #endif can be used for conditional compilation. 
#if VERBOSE >= 2 
print("trace message"); 
#endif 
Most compilers targeting Microsoft Windows implicitly define _WIN32. This allows code, including 
preprocessor commands, to compile only when targeting Windows systems. A few compilers 
define WIN32 instead. For such compilers that do not implicitly define the _WIN32 macro, it can be 
specified on the compiler's command line, using -D_WIN32. 
#ifdef __unix__ /* __unix__ is usually defined by compilers targeting Unix systems */ 
# include <unistd.h> 
#elif defined _WIN32 /* _Win32 is usually defined by compilers targeting 32 or 64 bit Windows 
systems */ 
# include <windows.h> 
#endif 
The example code tests if a macro __unix__ is defined. If it is, the file <unistd.h> is then included. 
Otherwise, it tests if a macro _WIN32 is defined instead. If it is, the file<windows.h> is then included. 
A more complex #if example can use operators, for example something like: 
#if !(defined __LP64__ || defined __LLP64__) || defined _WIN32 && !defined _WIN64
53 
// we are compiling for a 32-bit system 
#else 
// we are compiling for a 64-bit system 
#endif 
Translation can also be caused to fail by using the #error directive: 
#if RUBY_VERSION == 190 
# error 1.9.0 not supported 
#endif 
Macro definition and expansion: 
There are two types of macros, object-like and function-like. Object-like macros do not take parameters; 
function-like macros do. The generic syntax for declaring an identifier as a macro of each type is, 
respectively: 
#define <identifier> <replacement token list> // object-like macro 
#define <identifier>(<parameter list>) <replacement token list> // function-like macro, note parameters 
The function-like macro declaration must not have any whitespace between the identifier and the first, 
opening, parenthesis. If whitespace is present, the macro will be interpreted as object-like with 
everything starting from the first parenthesis added to the token list. 
A macro definition can be removed with "#undef": 
#undef <identifier> // delete the macro 
Whenever the identifier appears in the source code it is replaced with the replacement token list, which 
can be empty. For an identifier declared to be a function-like macro, it is only replaced when the 
following token is also a left parenthesis that begins the argument list of the macro invocation. The exact 
procedure followed for expansion of function-like macros with arguments is subtle. 
Object-like macros were conventionally used as part of good programming practice to create symbolic 
names for constants, e.g. 
#define PI 3.14159 
... instead of hard-coding the numbers throughout the code. An alternative in both C and C++, especially 
in situations in which a pointer to the number is required, is to apply theconst qualifier to a global 
variable. This causes the value to be stored in memory, instead of being substituted by the preprocessor. 
An example of a function-like macro is:
#define RADTODEG(x) ((x) * 57.29578) 
This defines a radians-to-degrees conversion which can be inserted in the code where required, 
i.e., RADTODEG(34). This is expanded in-place, so that repeated multiplication by the constant is not 
shown throughout the code. The macro here is written as all uppercase to emphasize that it is a macro, 
not a compiled function. 
The second x is enclosed in its own pair of parentheses to avoid the possibility of incorrect order of 
operations when it is an expression instead of a single value. For example, the 
expression RADTODEG(r + 1) expands correctly as ((r + 1) * 57.29578) — without 
parentheses, (r + 1 * 57.29578) gives precedence to the multiplication. 
Similarly, the outer pair of parentheses maintain correct order of operation. For 
example, 1 / RADTODEG(r) expands to 1 / ((r) * 57.29578) — without 
parentheses, 1 /(r) * 57.29578 gives precedence to the division. 
54 
Special macros and directives: 
Certain symbols are required to be defined by an implementation during preprocessing. These 
include __FILE__ and __LINE__, predefined by the preprocessor itself, which expand into the current 
file and line number. For instance the following: 
// debugging macros so we can pin down message origin at a glance 
#define WHERESTR "[file %s, line %d]: " 
#define WHEREARG __FILE__, __LINE__ 
#define DEBUGPRINT2(...) fprintf(stderr, __VA_ARGS__) 
#define DEBUGPRINT(_fmt, ...) DEBUGPRINT2(WHERESTR _fmt, WHEREARG, 
__VA_ARGS__) 
//... 
DEBUGPRINT("hey, x=%dn", x); 
prints the value of x, preceded by the file and line number to the error stream, allowing quick access to 
which line the message was produced on. Note that the WHERESTRargument is concatenated with the 
string following it. The values of __FILE__ and __LINE__ can be manipulated with the #line directive. 
The #line directive determines the line number and the file name of the line below. E.g.: 
#line 314 "pi.c" 
puts("line=" #__LINE__ " file=" __FILE__); 
generates the puts function:
training report
training report
training report
training report
training report
training report
training report
training report
training report
training report
training report
training report

More Related Content

What's hot

Virtualization (Distributed computing)
Virtualization (Distributed computing)Virtualization (Distributed computing)
Virtualization (Distributed computing)
Sri Prasanna
 
HYPER-THREADING TECHNOLOGY
HYPER-THREADING TECHNOLOGYHYPER-THREADING TECHNOLOGY
HYPER-THREADING TECHNOLOGY
SHASHI SHAW
 

What's hot (20)

CentOS
CentOSCentOS
CentOS
 
Unit 2
Unit 2Unit 2
Unit 2
 
Linux seminar
Linux seminarLinux seminar
Linux seminar
 
Underlying principles of parallel and distributed computing
Underlying principles of parallel and distributed computingUnderlying principles of parallel and distributed computing
Underlying principles of parallel and distributed computing
 
Lamp technology seminar final
Lamp technology seminar finalLamp technology seminar final
Lamp technology seminar final
 
Parallel Computing
Parallel Computing Parallel Computing
Parallel Computing
 
Virtualization (Distributed computing)
Virtualization (Distributed computing)Virtualization (Distributed computing)
Virtualization (Distributed computing)
 
Green cloud computing India
Green cloud computing IndiaGreen cloud computing India
Green cloud computing India
 
HYPER-THREADING TECHNOLOGY
HYPER-THREADING TECHNOLOGYHYPER-THREADING TECHNOLOGY
HYPER-THREADING TECHNOLOGY
 
Chapter 1 Introduction (Data Communication by Forouzan)
Chapter 1  Introduction (Data Communication by Forouzan)Chapter 1  Introduction (Data Communication by Forouzan)
Chapter 1 Introduction (Data Communication by Forouzan)
 
Black berry
Black berryBlack berry
Black berry
 
BOSS OS
BOSS OSBOSS OS
BOSS OS
 
Cluster Computing Seminar.
Cluster Computing Seminar.Cluster Computing Seminar.
Cluster Computing Seminar.
 
Linux command ppt
Linux command pptLinux command ppt
Linux command ppt
 
Mesh Topology
Mesh Topology Mesh Topology
Mesh Topology
 
Introduction and history of linux
Introduction and history of linuxIntroduction and history of linux
Introduction and history of linux
 
A History of Linux
A History of LinuxA History of Linux
A History of Linux
 
Computer Network Complete Introduction
Computer Network Complete IntroductionComputer Network Complete Introduction
Computer Network Complete Introduction
 
Lamp technology
Lamp technologyLamp technology
Lamp technology
 
Classful and classless addressing
Classful and classless addressingClassful and classless addressing
Classful and classless addressing
 

Similar to training report

Similar to training report (20)

Neha ict project
Neha ict projectNeha ict project
Neha ict project
 
MS-CIT Unit 4 (slide share)
MS-CIT Unit 4 (slide share)MS-CIT Unit 4 (slide share)
MS-CIT Unit 4 (slide share)
 
Operating system
Operating systemOperating system
Operating system
 
Hostel management system project report
Hostel management system project reportHostel management system project report
Hostel management system project report
 
Computer operating system
Computer operating systemComputer operating system
Computer operating system
 
Modern operating system.......
Modern operating system.......Modern operating system.......
Modern operating system.......
 
operating system - OS
operating system - OSoperating system - OS
operating system - OS
 
Linux Introduction
Linux IntroductionLinux Introduction
Linux Introduction
 
Evolution of OS (Operating System)/CS.pptx
Evolution of OS (Operating System)/CS.pptxEvolution of OS (Operating System)/CS.pptx
Evolution of OS (Operating System)/CS.pptx
 
COMPUTER SYSTEM MAINTENANCE.pptx
COMPUTER SYSTEM MAINTENANCE.pptxCOMPUTER SYSTEM MAINTENANCE.pptx
COMPUTER SYSTEM MAINTENANCE.pptx
 
Network operating systems
Network operating systems Network operating systems
Network operating systems
 
Introduction to Operating System
Introduction to Operating SystemIntroduction to Operating System
Introduction to Operating System
 
os.ppt
os.pptos.ppt
os.ppt
 
History of computer software.
History of computer software.History of computer software.
History of computer software.
 
Linux
LinuxLinux
Linux
 
Characteristic & Aplication of some popular operating system.
Characteristic & Aplication of some popular operating system.Characteristic & Aplication of some popular operating system.
Characteristic & Aplication of some popular operating system.
 
Operating systems &amp; its future
Operating systems &amp; its futureOperating systems &amp; its future
Operating systems &amp; its future
 
introduction to Operating system for computer science Program
introduction to Operating system for computer science Programintroduction to Operating system for computer science Program
introduction to Operating system for computer science Program
 
CHAPTER 1 INTRODUCTION TO UNIX.pptx
CHAPTER 1 INTRODUCTION TO UNIX.pptxCHAPTER 1 INTRODUCTION TO UNIX.pptx
CHAPTER 1 INTRODUCTION TO UNIX.pptx
 
OS , Its History and Types
OS , Its History and TypesOS , Its History and Types
OS , Its History and Types
 

Recently uploaded

ONLINE VEHICLE RENTAL SYSTEM PROJECT REPORT.pdf
ONLINE VEHICLE RENTAL SYSTEM PROJECT REPORT.pdfONLINE VEHICLE RENTAL SYSTEM PROJECT REPORT.pdf
ONLINE VEHICLE RENTAL SYSTEM PROJECT REPORT.pdf
Kamal Acharya
 
Online blood donation management system project.pdf
Online blood donation management system project.pdfOnline blood donation management system project.pdf
Online blood donation management system project.pdf
Kamal Acharya
 
Laundry management system project report.pdf
Laundry management system project report.pdfLaundry management system project report.pdf
Laundry management system project report.pdf
Kamal Acharya
 
Automobile Management System Project Report.pdf
Automobile Management System Project Report.pdfAutomobile Management System Project Report.pdf
Automobile Management System Project Report.pdf
Kamal Acharya
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
Neometrix_Engineering_Pvt_Ltd
 

Recently uploaded (20)

A CASE STUDY ON ONLINE TICKET BOOKING SYSTEM PROJECT.pdf
A CASE STUDY ON ONLINE TICKET BOOKING SYSTEM PROJECT.pdfA CASE STUDY ON ONLINE TICKET BOOKING SYSTEM PROJECT.pdf
A CASE STUDY ON ONLINE TICKET BOOKING SYSTEM PROJECT.pdf
 
Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Arya
 
ONLINE VEHICLE RENTAL SYSTEM PROJECT REPORT.pdf
ONLINE VEHICLE RENTAL SYSTEM PROJECT REPORT.pdfONLINE VEHICLE RENTAL SYSTEM PROJECT REPORT.pdf
ONLINE VEHICLE RENTAL SYSTEM PROJECT REPORT.pdf
 
Online blood donation management system project.pdf
Online blood donation management system project.pdfOnline blood donation management system project.pdf
Online blood donation management system project.pdf
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
 
Laundry management system project report.pdf
Laundry management system project report.pdfLaundry management system project report.pdf
Laundry management system project report.pdf
 
Arduino based vehicle speed tracker project
Arduino based vehicle speed tracker projectArduino based vehicle speed tracker project
Arduino based vehicle speed tracker project
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
 
Automobile Management System Project Report.pdf
Automobile Management System Project Report.pdfAutomobile Management System Project Report.pdf
Automobile Management System Project Report.pdf
 
fluid mechanics gate notes . gate all pyqs answer
fluid mechanics gate notes . gate all pyqs answerfluid mechanics gate notes . gate all pyqs answer
fluid mechanics gate notes . gate all pyqs answer
 
Event Management System Vb Net Project Report.pdf
Event Management System Vb Net  Project Report.pdfEvent Management System Vb Net  Project Report.pdf
Event Management System Vb Net Project Report.pdf
 
Pharmacy management system project report..pdf
Pharmacy management system project report..pdfPharmacy management system project report..pdf
Pharmacy management system project report..pdf
 
ENERGY STORAGE DEVICES INTRODUCTION UNIT-I
ENERGY STORAGE DEVICES  INTRODUCTION UNIT-IENERGY STORAGE DEVICES  INTRODUCTION UNIT-I
ENERGY STORAGE DEVICES INTRODUCTION UNIT-I
 
KIT-601 Lecture Notes-UNIT-3.pdf Mining Data Stream
KIT-601 Lecture Notes-UNIT-3.pdf Mining Data StreamKIT-601 Lecture Notes-UNIT-3.pdf Mining Data Stream
KIT-601 Lecture Notes-UNIT-3.pdf Mining Data Stream
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
 
İTÜ CAD and Reverse Engineering Workshop
İTÜ CAD and Reverse Engineering WorkshopİTÜ CAD and Reverse Engineering Workshop
İTÜ CAD and Reverse Engineering Workshop
 
Natalia Rutkowska - BIM School Course in Kraków
Natalia Rutkowska - BIM School Course in KrakówNatalia Rutkowska - BIM School Course in Kraków
Natalia Rutkowska - BIM School Course in Kraków
 
fundamentals of drawing and isometric and orthographic projection
fundamentals of drawing and isometric and orthographic projectionfundamentals of drawing and isometric and orthographic projection
fundamentals of drawing and isometric and orthographic projection
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
 
A case study of cinema management system project report..pdf
A case study of cinema management system project report..pdfA case study of cinema management system project report..pdf
A case study of cinema management system project report..pdf
 

training report

  • 1. 1 CHAPTER – 1 Linux Operating System An operating system is software that manages computer hardware and software resources and provides common services for computer programs. The operating system is an essential component of the system software in a computer system. Application programs usually require an operating system to function. Time-sharing operating systems schedule tasks for efficient use of the system and may also include accounting software for cost allocation of processor time, mass storage, printing, and other resources. For hardware functions such as input and output and memory allocation, the operating system acts as an intermediary between programs and the computer hardware, although the application code is usually executed directly by the hardware and will frequently make a system call to an OS function or be interrupted by it. Operating systems can be found on almost any device that contains a computer— from cellular phones and video game consoles to supercomputers and web servers. Examples of popular modern operating systems include Android, BSD, iOS, Linux, OS X, QNX, Microsoft Windows, Windows Phone, and IBM z/OS. All these examples, except Windows, Windows Phone and z/OS, share roots in UNIX. Types of OS  Real-time: A real-time operating system is a multitasking operating system that aims at executing real-time applications. Real-time operating systems often use specialized scheduling algorithms so that they can achieve a deterministic nature of behavior. The main objective of real-time operating systems is their quick and predictable response to events. They have an event-driven or time-sharing design and often aspects of both. An event-driven system switches between tasks based on their priorities or external events while time-sharing operating systems switch tasks based on clock interrupts. Time-sharing operating systems schedule tasks for efficient use of the system and may also include accounting software for cost allocation of processor time, mass storage, printing, and other resources.  Multi-user: A multi-user operating system allows multiple users to access a computer system at the same time. Time-sharing systems and Internet servers can be classified as multi-user systems as they enable multiple-user access to a computer through the sharing of time. Single-user operating systems have only one user but may allow multiple programs to run at the same time.  Multi-tasking vs. single-tasking : A multi-tasking operating system allows more than one program to be running at the same time, from the point of view of human time scales. A single-tasking system has only one running program. Multi-tasking can be of two types: pre-emptive and co-operative. In
  • 2. pre-emptive multitasking, the operating system slices the CPU time and dedicates one slot to each of the programs. Unix-like operating systems such as Solaris and Linux support pre-emptive multitasking, as does AmigaOS. Cooperative multitasking is achieved by relying on each process to give time to the other processes in a defined manner. 16-bit versions of Microsoft Windows used cooperative multi-tasking. 32-bit versions of both Windows NT and Win9x, used pre-emptive multi-tasking. 2 Mac OS prior to OS X used to support cooperative multitasking.  Distributed: A distributed operating system manages a group of independent computers and makes them appear to be a single computer. The development of networked computers that could be linked and communicate with each other gave rise to distributed computing. Distributed computations are carried out on more than one machine. When computers in a group work in cooperation, they make a distributed system.  Template: In an o/s, distributed and cloud computing context, templating refers to creating a single virtual machine image as a guest operating system, then saving it as a tool for multiple running virtual machines (Gagne, 2012, p. 716). The technique is used both in virtualization and cloud computing management, and is common in large server warehouses.  Embedded: Embedded operating systems are designed to be used in embedded computer systems. They are designed to operate on small machines like PDAs with less autonomy. They are able to operate with a limited number of resources. They are very compact and extremely efficient by design. Windows CE and Minix 3 are some examples of embedded operating systems. History of OS Early computers were built to perform a series of single tasks, like a calculator. Basic operating system features were developed in the 1950s, such as resident monitor functions that could automatically run different programs in succession to speed up processing. Operating systems did not exist in their modern and more complex forms until the early 1960s. Hardware features were added, that enabled use of runtime libraries, interrupts, and parallel processing. When personal computers became popular in the 1980s, operating systems were made for them similar in concept to those used on larger computers. In the 1940s, the earliest electronic digital systems had no operating systems. Electronic systems of this time were programmed on rows of mechanical switches or by jumper wires on plug boards. These were special-purpose systems that, for example, generated ballistics tables for the military or controlled the printing of payroll checks from data on punched paper cards. After programmable general purpose computers were invented, machine languages (consisting of strings of the binary digits 0 and 1 on punched paper tape) were introduced that sped up the programming process (Stern, 1981). OS/360 was used on most IBM mainframe computers beginning in 1966, including computers utilized by the Apollo program.
  • 3. In the early 1950s, a computer could execute only one program at a time. Each user had sole use of the computer for a limited period of time and would arrive at a scheduled time with program and data on punched paper cards and/or punched tape. The program would be loaded into the machine, and the machine would be set to work until the program completed or crashed. Programs could generally be debugged via a front panel using toggle switches and panel lights. It is said that Alan Turing was a master of this on the early Manchester Mark 1 machine, and he was already deriving the primitive conception of an operating system from the principles of the Universal Turing machine. Later machines came with libraries of programs, which would be linked to a user's program to assist in operations such as input and output and generating computer code from human-readable symbolic code. This was the genesis of the modern-day operating system. However, machines still ran a single job at a time. At Cambridge University in England the job queue was at one time a washing line from which tapes were hung with different colored clothes-pegs to indicate job-priority. 3 Linux vs. Windows What is it? Linux is an example of Open Source software development and Free Operating System (OS). Windows is the family of operating system (OS) from Microsoft, which is the most famous OS in the world. Cost Linux can be freely distributed, downloaded freely, distributed through magazines, Books etc. There are priced versions for Linux also, but they are normally cheaper than Windows. For desktop or home use, Windows can be expensive. A single copy can cost around $50 to $ 450 depending on the version of Windows. User Everyone. From home users to developers and computer enthusiasts alike. Everyone. From home users to developers and computer enthusiasts alike. Manufacturer Linux kernel is developed by the community. Linus Torvalds oversees things. Microsoft created the Windows operating system, but allows other computer manufactures to distribute their own computers with Windows pre-installed. Usage Linux can be installed on a wide variety of computer hardware, ranging from mobile phones, tablet computers and video game consoles, to mainframes On PC's desktops, laptops, servers and some phones.
  • 4. 4 and supercomputers. Development and Distribution Linux is developed by Open Source development i.e. through sharing and collaboration of code and features through forums etc. and it is distributed by various vendors. Windows is developed and distributed by Microsoft. GUI Linux typically provides two GUIs, KDE and Gnome. But there are millions of alternatives such as LXDE, Xfce, Unity, Mate, twm, etc. The Windows GUI is an integral component of the OS and is not replaceable. This can be a con when it comes to Windows 8's Metro. File system support Ext2, Ext3, Ext4, Jfs, ReiserFS, Xfs, Btrfs, FAT, FAT32, NTFS FAT, FAT32, NTFS, exFAT Text mode interface BASH (Bourne Again SHell) is the Linux default shell. It can support multiple command interpreters. Windows uses a command shell and each version of Windows has a single command interpreter with dos-like commands, recently there is the addition of the optional PowerShell that uses more Unix-like commands. Price Free but support is available for a price. $50-$450 Security Linux has had about 60-100 viruses listed till date. None of them actively spreading nowadays. According to Dr. Nic Peeling and Dr. Julian Satchell's “Analysis of the Impact of Open Source Software” there have been more than 60,000 viruses in Windows. Anti-Virus cost about $20 to $400 Threat detection and solution In case of Linux, threat detection and solution is very fast, as Linux is mainly community driven and whenever any Linux user posts any kind of threat, several developers start working on it from different parts of the world After detecting a major threat in Windows OS, Microsoft generally releases a patch that can fix the problem and it can take more than 2/3 months. Sometimes sooner, Microsoft releases patches and updates weekly. Processors Dozens of different kinds. Limited but most (80%)
  • 5. 5 Examples Ubuntu, Fedora, Red Hat, Debian, Archlinux, Android etc. Windows 8, 8.1, 7, Vista, XP Gaming Very few games available natively. Some games can be played through Wine, but often not all features are available. Almost all games are compatible with Windows. Some CPU intensive and graphics intensive games are exclusive to Windows PC's. User experience Although there are many GUI applications, most of the work is done through Terminal (a console window), and if a problem arises GUI is rarely usable to fix them. Everything can be controlled through GUI and incompatibility problems are rare. Graphics performance Because hardware manufacturers, such as NVidia, often do not provide documentation for Linux developers, drivers cannot use full card performance. Combined with newest DirectX versions and full graphics card support the performance is almost as good as it can get. Company / developer Linus Torvalds Microsoft Introduction (from Wikipedia) Linux is a Unix-like and POSIX-compliant computer operating system assembled under the model of free and open source software development and distribution. The defining component of Linux is the Linux kernel, an operating system kernel first released Microsoft Windows is a series of graphical interface operating systems developed, marketed, and sold by Microsoft. Microsoft introduced an operating environment named Windows on November 20, 1985 as a graphical operating system shell for MS-DOS. Available language(s) Multilingual Multilingual License GNU/Free Proprietary Supported platforms All PowerPC: versions 1.0 - NT 4.0; DEC Alpha: versions 1.0 - NT 4.0; MIPS R4000: versions 1.0 - NT 4.0; IA-32: versions 1.0 - 8; IA-64: version XP; x86-
  • 6. 6 64: versions XP - 8; ARM: version RT; Default user interface Gnome or KDE (Depends on distro) Graphical (Windows Aero) Preceded by Basic Terminal (CLI) MS-DOS Source model Open Source Closed / Shared source Update method Many Windows Update OS Layers  Base Hardware: Computer hardware is the collection of physical elements that constitutes a computer system. Computer hardware refers to the physical parts or components of a computer such as the monitor, mouse, keyboard, computer data storage, hard drive disk (HDD), system unit (graphic cards, sound cards, memory, motherboard and chips), etc. all of which are physical objects that can be touched. In contrast, software is instructions that can be stored and run by hardware. Software is any set of machine-readable instructions that directs a computer's processor to perform specific operations. A combination of hardware and software forms a usable computing system.
  • 7.  Device Driver: In computing, a device driver (commonly referred to as simply a driver) is a computer program that operates or controls a particular type of device that is attached to a computer. A driver provides a software interface to hardware devices, enabling operating systems and other computer programs to access hardware functions without needing to know precise details of the hardware being used. A driver typically communicates with the device through the computer bus or communications subsystem to which the hardware connects. When a calling program invokes a routine in the driver, the driver issues commands to the device. Once the device sends data back to the driver, the driver may invoke routines in the original calling program. Drivers are hardware-dependent and operating-system-specific. They usually provide the interrupt handling required for 7 any necessary asynchronous time-dependent hardware interface.  Shell: In computing, a shell is a user interface for access to an operating system's services. In general, operating system shells use either a command-line interface (CLI) or graphical user interface (GUI), depending on a computer's role and particular operation. The design of a shell is guided by cognitive ergonomics and the goal is to achieve the best workflow possible for the intended tasks; the design can be constricted by the available computing power (for example, of the GPU) or the available amount of graphics memory. The design of a shell is also dictated by the employed computer periphery, such as computer keyboard, pointing device (a mouse with one button, or one with five buttons, or a 3D mouse) or touchscreen, which is the direct human–machine interface. CLI shells allow some operations to be performed faster, for example rearranging large blocks of data. However, they require the user to memorize all commands and their calling syntax, and also to learn the shell-specific scripting language, for example bash script. CLIs are also easier to be operated via refreshable braille display and provide certain advantages to screen readers. Graphical shells have a low burden to start using a computer, and they are characterized as being simple and easy to use. With the widespread adoption of programs with GUIs, the use of graphical shells has gained greater adoption. Since graphical shells come with certain disadvantages (for example, lack of support for easy automation of operation sequences), most GUI-enabled operating systems also provide additional CLI shells.  Compilers: A compiler is a programming tool, a computer program (or set of programs), that transforms source code written in a programming language (the source language) into another computer language (the target language, often having a binary form known as object code). The most common reason for wanting to transform source code is to create an executable program. The name "compiler" is primarily used for programs that translate source code from a high-level programming language to a lower level language (e.g., assembly language or machine code). If the compiled program can run on a computer whose CPU or operating system is different from the one on which the compiler runs, the compiler is known as a cross-compiler. More generally, compilers are a specific type of translators. A program that translates from a low level language to a higher level one
  • 8. is a decompiler. A program that translates between high-level languages is usually called a source-to- source compiler or transpiler. A language rewriter is usually a program that translates the form of expressions without a change of language. The term compiler-compiler is sometimes used to refer to a parser generator, a tool often used to help create the laxer and parser. A compiler is likely to perform many or all of the following operations: lexical analysis, preprocessing, parsing, semantic analysis (Syntax-directed translation), code generation, and code optimization. Program faults caused by incorrect compiler behavior can be very difficult to track down and work around; therefore, compiler implementers invest significant effort to ensure compiler correctness.  Command Interpreter: A command interpreter is the part of a computer operating system that understands and executes commands that are entered interactively by a human being or from a program. In some operating systems, the command interpreter is called the shell.  Daemon: In multitasking computer operating systems, a daemon is a computer program that runs as a background process, rather than being under the direct control of an interactive user. Traditionally daemon names end with the letter d: for example, syslog is the daemon that implements the system logging facility and sshd is a daemon that services incoming SSH connections. In a Unix environment, the parent process of a daemon is often, but not always, the init process. A daemon is usually either created by a process forking a child process and then immediately exiting, thus causing init to adopt the child process, or by the init process directly launching the daemon. In addition, a daemon launched by forking and exiting typically must perform other operations, such as dissociating the process from any controlling terminal (tty). Such procedures are often implemented in various convenience routines such as daemon(3) in Unix. Systems often start daemons at boot time and serve the function of responding to network requests, hardware activity, or other programs by performing some task. Daemons can also configure hardware (like udevd on some GNU/Linux systems), run scheduled tasks (like cron), and perform a variety of other tasks.  Utilities: Utility software is system software designed to help analyse, configure, optimize or maintain a computer. Utility software usually focuses on how the computer infrastructure (including the computer hardware, software and data storage) operates.  Applications: Application software is a set of one or more programs designed to carry out operations for a specific application. Application software cannot run on itself but is dependent on system software to execute. Examples of application software include MS Word, MS Excel, a console game, a library management system, a spread sheet system etc. The term is used to distinguish such software from another type of computer program referred to as system software, which manages and integrates a computer's capabilities but does not directly perform tasks that benefit the user. The system software serves the application, which in turn serves the user. Examples of types of application software may include accounting software, media players, and office suites. Many application programs deal principally with documents. Applications may be bundled with the 8
  • 9. computer and its system software or published separately, and may be coded as e.g. proprietary, open-source or university projects. 9 Linux Commands For a person new to Linux, finding Linux functional is still not very easy even after the emergence of user friendly Linux distribution like Ubuntu and Mint. The thing remains that there will always be some configuration on user’s part to be done manually. Just to start with, the first thing a user should know is the basic commands in terminal. Linux GUI runs on Shell. When GUI is not running but Shell is running, Linux is running. If Shell is not running, nothing is running. Commands in Linux are a means of interaction with Shell. For beginners some of the basic computational task is to: o View the contents of a directory: A directory may contain visible and invisible files with different file permissions. o Viewing blocks, HDD partition, External HDD o Checking the integrity of Downloaded/Transferred Packages o Converting and copying a file o Know your machine name, OS and Kernel o Viewing history o Being root o Make Directory o Make Files o Changing the file permission o Own a file o Install, Update and maintain Packages o Uncompressing a file o See current date, time and calendar o Print contents of a file o Copy and Move o See the working directory for easy navigation o Change the working directory, etc… And we have described the entire above basic computational task in our First Article. Part I – Switching From Windows to Nix or a Newbie to Linux – 20 Useful Commands for Linux Newbies This was the first article of this series. We tried to provide you with detailed description of these commands with explicit examples which was highly appreciated by our reader in terms of likes, comments and traffic.
  • 10. What after these initial commands? Obviously we moved to the next part of this article where we provided commands for computational tasks like: 10 o Finding a file in a given directory o Searching a file with the given keywords o Finding online documentation o See the current running processes o Kill a running process o See the location of installed Binaries o Starting, Ending, Restarting a service o Making and removing of aliases o View the disk and space usages o Removing a file and/or directory o Print/echo a custom output on standard output o Changing password of on-self and others, if you are root. o View Printing queue o Compare two files o Download a file, the Linux way (wget) o Mount a block / partition / external HDD o Compile and Run a code written in ‘C’, ‘C++’ and ‘Java’ Programming Language This Second Article was again highly appreciated by the readers of Tecmint.com. The article was nicely elaborated with suitable examples and output. Part II – 20 Advanced Commands for Middle Level Linux Users After providing the users with the glimpse of Commands used by a Middle Level User we thought to give our effort in a nice write-up for a list of command used by an user of System Administrator Level. In our Third and last article of this series, we tried to cover the commands that would be required for the computational task like: o Configuring Network Interface o Viewing custom Network Related information o Getting information about Internet Server with customizable switches and Results o Digging DNS o Knowing Your System uptime o Sending an occasional Information to all other logged-in users o Send text messages directly to a user o Combination of commands
  • 11. 11 o Renaming a file o Seeing the processes of a CPU o Creating newly formatted ext4 partition o Text File editors like vi, emacs and nano o Copying a large file/folder with progress bar o Keeping track of free and available memory o Backup a mysql database o Make difficult to guess – random password o Merge two text files o List of all the opened files Part III – 20 Advanced Commands for Linux Experts Writing this article and the list of command that needs to go with the article was a little cumbersome. We chose 20 commands with each article and hence gave a lot of thought for which command should be included and which should be excluded from the particular post. I personally selected the commands on the basis of their usability (as I use and get used to) from a user point of view and an Administrator point of view. This Articles aims to concatenate all the articles of its series and provide you with all the functionality in commands you can perform in our this very series of articles. There are too long lists of commands available in Linux. But we provided the list of 60 commands which is generally and most commonly used and a user having knowledge of these 60 commands as a whole can work in terminal very much smoothly. Command Description adduser Add a new user arch Print machine architecture awk Find and Replace text within file(s) bc An arbitrary precision calculator language cal Display a calendar Cat Concatenate files and print on the standard output Chdir Change working directory Chgrp Change the group ownership of files chkconfig Tool for maintaining the /etc/rc[0-6].d directory hierarchy chmod Change the access permissions of files and directories
  • 12. 12 Chown Change the user and group ownership of files Chroot Change root directory Cksum Print CRC checksum and byte counts Clear Clear terminal screen Cmp Compare two files Comm Compare two sorted files line by line Cp Copy one or more files to another location Cron Daemon to execute scheduled commands crontab Schedule a command to run at a later time Csplit Split a file into context-determined pieces Cut Divide a file into several parts Date Display or change the date & time Dc Desk Calculator Dd Data Dump - Convert and copy a file Df Display free disk space Diff Display the differences between two files diff3 Show differences among three files Dir Briefly list directory contents dircolors Colour setup for `ls' dirname Convert a full pathname to just a path Du Estimate file space usage Echo Display message on screen Ed A line-oriented text editor (edlin) Egrep Search file(s) for lines that match an extended expression Eject Eject CD-ROM Env Display, set, or remove environment variables Expand Convert tabs to spaces Expr Evaluate expressions
  • 13. 13 Factor Print prime factors False Do nothing, unsuccessfully fdformat Low-level format a floppy disk Fdisk Partition table manipulator for Linux Fgrep Search file(s) for lines that match a fixed string Find Search for files that meet a desired criteria Fmt Reformat paragraph text Fold Wrap text to fit a specified width Format Format disks or tapes Free Display memory usage Fsck Filesystem consistency check and repair Gawk Find and Replace text within file(s) Grep Search file(s) for lines that match a given pattern Groups Print group names a user is in Gzip Compress or decompress named file(s) Head Output the first part of file(s) hostname Print or set system name Id Print user and group id's Info Help info Install Copy files and set attributes Join Join lines on a common field Kill Stop a process from running Less Display output one screen at a time Ln Make links between files Locate Find files logname Print current login name Lpc Line printer control program Lpr Off line print
  • 14. 14 Lprm Remove jobs from the print queue Ls List information about file(s) Man Help manual Mkdir Create new folder(s) mkfifo Make FIFOs (named pipes) mknod Make block or character special files more Display output one screen at a time mount Mount a file system mv Move or rename files or directories nice Set the priority of a command or job Nl Number lines and write files nohup Run a command immune to hangups passwd Modify a user password paste Merge lines of files pathchk Check file name portability pr Convert text files for printing printcap Printer capability database printenv Print environment variables printf Format and print data ps Process status Pwd Print Working Directory quota Display disk usage and limits quotacheck Scan a file system for disk usage quotactl Set disk quotas Ram ram disk device Rcp Copy files between two machines Rm Remove files rmdir Remove folder(s)
  • 15. 15 Rpm Remote Package Manager rsync Remote file copy (Synchronize file trees) screen Terminal window manager Sdiff Merge two files interactively Sed Stream Editor select Accept keyboard input Seq Print numeric sequences shutdown Shutdown or restart linux sleep Delay for a specified time Sort Sort text files Split Split a file into fixed-size pieces Su Substitute user identity Sum Print a checksum for a file symlink Make a new name for a file Sync Synchronize data on disk with memory Tac Concatenate and write files in reverse Tail Output the last part of files Tar Tape Archiver Tee Redirect output to multiple files Test Evaluate a conditional expression Time Measure Program Resource Use touch Change file timestamps Top List processes running on the system traceroute Trace Route to Host Tr Translate, squeeze, and/or delete characters True Do nothing, successfully Tsort Topological sort Tty Print filename of terminal on stdin
  • 16. 16 umount Unmount a device uname Print system information unexpand Convert spaces to tabs Uniq Uniquify files Units Convert units from one scale to another unshar Unpack shell archive scripts useradd Create new user account usermod Modify user account users List users currently logged in uuencode Encode a binary file uudecode Decode a file created by uuencode Vdir Verbosely list directory contents (`ls -l -b') watch Execute/display a program periodically Wc Print byte, word, and line counts whereis Report all known instances of a command Which Locate a program file in the user's path Who Print all usernames currently logged in Whoami Print the current user id and name (`id -un') Xargs Execute utility, passing constructed argument list(s) Yes Print a string until interrupted File Permissions: “chmod” In Linux-like operating systems, chmod is the command and system call which may change the access permissions to file system objects (files and directories). It may also alter special mode flags. The request is filtered by the umask. The name is an abbreviation of change mode. History: A chmod command first appeared in AT&T Unix version 1. As systems grew in number and types of users, access_control_lists were added to many file systems in addition to these most basic modes to increase flexibility.
  • 17. 17 Command syntax: chmod [options] mode[,mode] file1 [file2 ...] Usual implemented options include:  -R recursive, i.e. include objects in subdirectories  -f force, forge ahead with all objects even if errors occur  -v verbose, show objects processed If a symbolic link is specified, the target object is affected. File modes directly associated with symbolic links themselves are typically never used. To view the file mode, the ls or stat commands may be used: $ ls -l findPhoneNumbers.sh -rwxr-xr-- 1 dgerman staff 823 Dec 16 15:03 findPhoneNumbers.sh $ stat -c %a findPhoneNumbers.sh The r, w, and x specify the read, write, and execute access, respectively. This script can be read, written to, and executed by the user, read and executed by other members of the staff group and can also be read by others. Octal modes: The chmod numeric format accepts up to four octal digits. The rightmost three refer to permissions for the file owner, the group, and other users. The optional next digit (fourth from the right) specifies special setuid, setgid, and sticky flags. Numerical permissions: # Permission rwx 7 read, write and execute 111 6 read and write 110 5 read and execute 101 4 read only 100 3 write and execute 011 2 write only 010 1 execute only 001
  • 18. 18 0 None 000 Numeric example: In order to permit all users who are members of the programmers group to update a file $ ls -l sharedFile -rw-r--r-- 1 jsmith programmers 57 Jul 3 10:13 sharedFile $ chmod 664 sharedFile $ ls -l sharedFile -rw-rw-r-- 1 jsmith programmers 57 Jul 3 10:13 sharedFile Since the setuid, setgid and sticky bits are not specified, this is equivalent to: $ chmod 0664 sharedFile. Symbolic modes: The chmod command also accepts a finer-grained symbolic notation, which allows modifying specific modes while leaving other modes untouched. The symbolic mode is composed of three components, which are combined to form a single string of text: $ chmod [references][operator][modes] file ... The references (or classes) are used to distinguish the users to whom the permissions apply. If no references are specified it defaults to “all” but modifies only the permissions allowed by the unmask. The references are represented by one or more of the following letters: Reference Class Description U user the owner of the file G group users who are members of the file's group O others users who are neither the owner of the file nor members of the file's group A all all three of the above, same as ugo The chmod program uses an operator to specify how the modes of a file should be adjusted. The following operators are accepted: Operator Description + adds the specified modes to the specified classes - removes the specified modes from the specified classes
  • 19. 19 = the modes specified are to be made the exact modes for the specified classes The modes indicate which permissions are to be granted or removed from the specified classes. There are three basic modes which correspond to the basic permissions: Mode Name Description r read read a file or list a directory's contents w write write to a file or directory x execute execute a file or recurse a directory tree X special execute which is not a permission in itself but rather can be used instead of x. It applies execute permissions to directories regardless of their current permissions and applies execute permissions to a file which already has at least one execute permission bit already set (either user, group or other). It is only really useful when used with '+' and usually in combination with the -R option for giving group or other access to a big directory tree without setting execute permission on normal files (such as text files), which would normally happen if you just used "chmod -R a+rx .", whereas with 'X' you can do "chmod -R a+rX ." instead s setuid/gid details in Special modes section t sticky details in Special modes section Multiple changes can be specified by separating multiple symbolic modes with commas (without spaces). Symbolic examples: Add write permission (w) to the group's(g) access modes of a directory, allowing users in the same group to add files: $ ls -ld shared_dir # show access modes before chmod drwxr-xr-x 2 teamleader usguys 96 Apr 8 12:53 shared_dir $ chmod g+w shared_dir $ ls -ld shared_dir # show access modes after chmod drwxrwxr-x 2 teamleader usguys 96 Apr 8 12:53 shared_dir Remove write permissions (w) for all classes (a), preventing anyone from writing to the file: $ ls -l ourBestReferenceFile -rw-rw-r-- 2 teamleader usguys 96 Apr 8 12:53 ourBestReferenceFile
  • 20. 20 $ chmod a-w ourBestReferenceFile $ ls -l ourBestReferenceFile -r--r--r-- 2 teamleader usguys 96 Apr 8 12:53 ourBestReferenceFile Set the permissions for the user and the group (ug) to read and execute (rx) only (no write permission)on referenceLib, preventing anyone other than the owner to add files. $ ls -ld referenceLib drwxr----- 2 teamleader usguys 96 Apr 8 12:53 referenceLib $ chmod ug=rx referenceLib $ ls -ld referenceLib dr-xr-x--- 2 teamleader usguys 96 Apr 8 12:53 referenceLib Special modes: The chmod command is also capable of changing the additional permissions or special modes of a file or directory. The symbolic modes use s to represent the setuid and setgid modes, and t to represent the sticky mode. The modes are only applied to the appropriate classes, regardless of whether or not other classes are specified. Most operating systems support the specification of special modes using octal modes, but some do not. On these systems, only the symbolic modes can be used. Command line examples: Command explanation chmod a+r publicComments.txt read is added for all classes (i.e. User, Group and Others). chmod +r publicComments.txt omitting the class defaults to all classes, but the resultant permissions are dependent on umask chmod a-x publicComments.txt execute permission is removed for all classes. chmod a+rx viewer.sh add read and execute for all classes. chmod u=rw,g=r,o= internalPlan.txt user(i.e. owner) can read and write, group can read, Others cannot access. chmod -R u+w,go-w docs add write permissions to the directory docs and all its contents (i.e. Recursively) for user and deny write access for everybody else.
  • 21. 21 chmod ug=rw groupAgreements.txt User and Group members can read and write (update the file). chmod 664 global.txt sets read and write and no execution access for the user and group, and read, no write, no execute for all others. chmod 0744 myCV.txt equivalent to u=rwx (400+200+100),go=r (40+ 4). The 0 specifies no special modes. chmod 1755 findReslts.sh the 1000 specifies set sticky bit and the rest is equivalent to u=rwx (400+200+100),go=rx (40+10 + 4+1) This suggests that the script be retained in memory. chmod 4755 SetCtrls.sh the 4 specifies set user ID and the rest is equivalent to u=rwx (400+200+100),go=rx (40+10 + 4+1). chmod 2755 SetCtrls.sh the 2 specifies set group ID and the rest is equivalent to u=rwx (400+200+100),go=rx (40+10 + 4+1). chmod -R u+rwX,g-rwx,o-rxPersonalStuff Recursively set a directory tree to rwx for owner directories, rw for owner files, --- (i.e. no access) for group and others. chmod -R a-x+X publicDocs remove the execute permission on all files in a directory tree (i.e. Recursively), while allowing for directory browsing. System call: The POSIX standard defines the following function prototype: int chmod(const char *path, mode_t mode); The mode parameter is a bitfield composed of various flags: Flag Octal value Purpose S_ISUID 04000 Set user ID on execution S_ISGID 02000 Set group ID on execution S_ISVTX 01000 Sticky bit S_IRUSR, S_IREAD 00400 Read by owner S_IWUSR, S_IWRITE 00200 Write by owner S_IXUSR, S_IEXEC 00100 Execute/search by owner
  • 22. 22 S_IRGRP 00040 Read by group S_IWGRP 00020 Write by group S_IXGRP 00010 Execute/search by group S_IROTH 00004 Read by others S_IWOTH 00002 Write by others S_IXOTH 00001 Execute/search by others Where alternate flag names are given, one of the pair of names might not be supported on some OSs. The octal values of the flags are summed or combined in a bitwise OR operation to give the desired permission mode. The function returns an error code. Shell Scripting A shell script is a computer program designed to be run by the Unix shell, a command line interpreter. The various dialects of shell scripts are considered to be scripting languages. Typical operations performed by shell scripts include file manipulation, program execution, and printing text. Shortcuts: In its most basic form, a shell script can provide a convenient variation of a system command where special environment settings, command options, or post-processing apply automatically, but in a way that allows the new script to still act as a fully normal linux command. One example would be to create a version of ls, the command to list files, giving it a shorter command name of l, which would be normally saved in a user's bin directory as/home/username/bin/l, and a default set of command options pre-supplied. #!/bin/sh LC_COLLATE=C ls -FCas "$@" Here, the first line (Shebang) indicates which interpreter should execute the rest of the script, and the second line makes a listing with options for file format indicators, columns, all files (none omitted), and a size in blocks. The LC_COLLATE=C sets the default collation order to not fold upper and lower case together, not intermix dotfiles with normal filenames as a side effect of ignoring punctuation in the names (dotfiles are usually only shown if an option like -a is used), and the "$@" causes any parameters given to l to pass through as parameters to ls, so that all of the normal options and other syntax known to ls can still be used.
  • 23. The user could then simply use l for the most commonly used short listing. Another example of a shell script that could be used as a shortcut would be to print a list of all the files and directories within a given directory. 23 #!/bin/sh Clear: ls -al In this case, the shell script would start with its normal starting line of #!/bin/sh. Following this, the script executes the command clear which clears the terminal of all text before going to the next line. The following line provides the main function of the script. The ls -al command list the files and directories that are in the directory from which the script is being run. The ls command attributes could be changed to reflect the needs of the user. Note: If an implementation does not have the clear command, try using the clr command instead. Batch jobs: Shell scripts allow several commands that would be entered manually at a command-line interface to be executed automatically, and without having to wait for a user to trigger each stage of the sequence. For example, in a directory with three C source code files, rather than manually running the four commands required to build the final program from them, one could instead create a C shell script, here named build and kept in the directory with them, which would compile them automatically: #!/bin/csh echo compiling... cc -c foo.c cc -c bar.c cc -c qux.c cc -o myprog foo.o bar.o qux.o echo done. The script would allow a user to save the file being edited, pause the editor, and then just run ./build to create the updated program, test it, and then return to the editor. Since the 1980s or so, however, scripts of this type have been replaced with utilities like make which are specialized for building programs. Generalization: Simple batch jobs are not unusual for isolated tasks, but using shell loops, tests, and variables provides much more flexibility to users. A Bash (Linux shell) script to convert JPEG images to PNG images, where the image names are provided on the command line—possibly via wildcards—instead of each
  • 24. being listed within the script, can be created with this file, typically saved in a file like /home/username/bin/jpg2png The jpg2png command can then be run on an entire directory full of JPEG images with just /home/username/bin/jpg2png *.jpg 24 Verisimilitude: A key feature of shell scripts is that the invocation of their interpreters is handled as a core operating system feature. So rather than a user's shell only being able to execute scripts in that shell's language, or a script only having its interpreter directive handled correctly if it was run from a shell (both of which were limitations in the early Bourne shell's handling of scripts), shell scripts are set up and executed by the OS itself. A modern shell script is not just on the same footing as system commands, but rather many system commands are actually shell scripts (or more generally, scripts, since some of them are not interpreted by a shell, but instead by Perl, Python, or some other language). This extends to returning exit codes like other system utilities to indicate success or failure, and allows them to be called as components of larger programs regardless of how those larger tools are implemented. Like standard system commands, shell scripts classically omit any kind of filename extension unless intended to be read into a running shell through a special mechanism for this purpose (such as sh’s “. ”, or csh’s source). Programming: Many modern shells also supply various features usually found only in more sophisticated general-purpose programming languages, such as control-flow constructs, variables, comments, arrays, subroutines, and so on. With these sorts of features available, it is possible to write reasonably sophisticated applications as shell scripts. However, they are still limited by the fact that most shell languages have little or no support for data typing systems, classes, threading, complex math, and other common full language features, and are also generally much slower than compiled code or interpreted languages written with speed as a performance goal. Advantages and disadvantages: Perhaps the biggest advantage of writing a shell script is that the commands and syntax are exactly the same as those directly entered at the command line. The programmer does not have to switch to a totally different syntax, as they would if the script were written in a different language, or if a compiled language was used. Often, writing a shell script is much quicker than writing the equivalent code in other programming languages. The many advantages include easy program or file selection, quick start, and interactive debugging. A shell script can be used to provide sequencing and decision-making linkage around
  • 25. existing programs, and for moderately sized scripts the absence of a compilation step is an advantage. Interpretive running makes it easy to write debugging code into a script and re-run it to detect and fix bugs. Non-expert users can use scripting to tailor the behavior of programs, and shell scripting provides some limited scope for multiprocessing. On the other hand, shell scripting is prone to costly errors. Inadvertent typing errors such as rm -rf * / (instead of the intended rm -rf */) are folklore in the Unix community; a single extra space converts the command from one that deletes everything in the sub-directories to one which deletes everything—and also tries to delete everything in the root directory. Similar problems can transform cp and mv into dangerous weapons, and misuse of the > redirect can delete the contents of a file. This is made more problematic by the fact that many UNIX commands differ in name by only one letter: cp, cd, dd, df, etc. Another significant disadvantage is the slow execution speed and the need to launch a new process for almost every shell command executed. When a script's job can be accomplished by setting up a pipeline in which efficient filter commands perform most of the work, the slowdown is mitigated, but a complex script is typically several orders of magnitude slower than a conventional compiled program that performs an equivalent task. There are also compatibility problems between different platforms. Larry Wall, creator of Perl, famously wrote that "It is easier to port a shell than a shell script." Similarly, more complex scripts can run into the limitations of the shell scripting language itself; the limits make it difficult to write quality code, and extensions by various shells to ameliorate problems with the original shell language can make problems worse. Many disadvantages of using some script languages are caused by design flaws within the language syntax or implementation, and are not necessarily imposed by the use of a text-based command line; there are a number of shells which use other shell programming languages or even full-fledged languages like Scsh (which uses Scheme). 25 Process Management Any application that runs on a Linux system is assigned a process ID or PID. This is a numerical representation of the instance of the application on the system. In most situations this information is only relevant to the system administrator who may have to debug or terminate processes by referencing the PID. Process Management is the series of tasks a System Administrator completes to monitor, manage, and maintain instances of running applications. Multitasking: Process Management beings with an understanding concept of Multitasking. Linux is what is referred to as a preemptive multitasking operating system. Preemptive multitasking systems rely on a scheduler. The function of the scheduler is to control the process that is currently using the CPU. In contrast,
  • 26. symmetric multitasking systems such as Windows 3.1 relied on each running process to voluntary relinquish control of the processor. If an application in this system hung or stalled, the entire computer system stalled. By making use of an additional component to pre-empt each process when its “turn” is up, stalled programs do not affect the overall flow of the operating system. Each “turn” is called a time slice, and each time slice is only a fraction of a second long. It is this rapid switching from process to process that allows a computer to “appear’ to be doing two things at once, in much the same way a movie “appears” to be a continuous picture. 26 Types of Processes: There are generally two types of processes that run on Linux. Interactive processes are those processes that are invoked by a user and can interact with the user. VI is an example of an interactive process. Interactive processes can be classified into foreground and background processes. The foreground process is the process that you are currently interacting with, and is using the terminal as its stdin (standard input) and stdout (standard output). A background process is not interacting with the user and can be in one of two states – paused or running. The following exercise will illustrate foreground and background processes. 1. Logon as root. 2. Run [cd ] 3. Run [vi] 4. Press [ctrl + z]. This will pause vi 5. Type [jobs] 6. Notice vi is running in the background 7. Type [fg %1]. This will bring the first background process to the foreground. 8. Close vi. The second general type of process that runs on Linux is a system process or Daemon (day-mon). Daemon is the term used to refer to process’ that are running on the computer and provide services but do not interact with the console. Most server software is implemented as a daemon. Apache, Samba, and inn are all examples of daemons. Any process can become a daemon as long as it is run in the background, and does not interact with the user. A simple example of this can be achieved using the [ls –R] command. This will list all subdirectories on the computer, and is similar to the [dir /s] command on Windows. This command can be set to run in the background by typing [ls –R &], and although technically you have control over the shell prompt, you will be able to do little work as the screen displays the output of the process that you have running in the background. You will also notice that the standard pause (ctrl+z) and kill (ctrl+c) commands do little to help you.
  • 27. 27 CHAPTER – 2 C Language C is a programming language developed at AT & T’s Bell Laboratories of USA in 1972. It was designed and written by a man named Dennis Ritchie. In the late seventies C began to replace the more familiar languages of that time like PL/I, ALGOL, etc. No one pushed C. It wasn’t made the ‘official’ Bell Labs language. Thus, without any advertisement C’s reputation spread and its pool of users grew. Ritchie seems to have been rather surprised that so many programmers preferred C to older languages like FORTRAN or PL/I, or the newer ones like Pascal and APL. But, that's what happened. Possibly why C seems so popular is because it is reliable, simple and easy to use. Moreover, in an industry where newer languages, tools and technologies emerge and vanish day in and day out, a language that has survived for more than 3 decades has to be really good. Compilation and Execution Once you have written the program you need to type it and instruct the machine to execute it. To type your C program you need another program called Editor. Once the program has been typed it needs to be converted to machine language (0s and 1s) before the machine can execute it. To carry out this conversion we need another program called Compiler. Compiler vendors provide an Integrated Development Environment (IDE) which consists of an Editor as well as the Compiler. There are several such IDEs available in the market targeted towards different operating systems. For example, Turbo C, Turbo C++ and Microsoft C are some of the popular compilers that work under MS-DOS; Visual C++ and Borland C++ are the compilers that work under Windows, whereas gcc compiler works under Linux. Note that Turbo C++, Microsoft C++ and Borland C++ software also contain a C compiler bundled with them. If you are a beginner you would be better off using a simple compiler like Turbo C or Turbo C++. Once you have mastered the language elements you can then switch over to more sophisticated compilers like Visual C++ under Windows or gcc under Linux. Most of the 20 Programs in this book would work with all the compilers. Wherever there is a deviation I would point it out that time. Assuming that you are using a Turbo C or Turbo C++ compiler here are the steps that you need to follow to compile and execute your first C program…
  • 28. 28 Start the compiler at C> prompt. The compiler (TC.EXE is usually present in C:TCBIN directory). o Select New from the File menu. o Type the program. o Save the program using F2 under a proper name (say Program1.c). o Use Ctrl + F9 to compile and execute the program. o Use Alt + F5 to view the output. Note that on compiling the program its machine language equivalent is stored as an EXE file (Program1.EXE) on the disk. This file is called an executable file. If we copy this file to another machine we can execute it there without being required to recompile it. In fact the other machine need not even have a compiler to be able to execute the file. A word of caution! If you run this program in Turbo C++ compiler, you may get an error — “The function printf should have a prototype”. To get rid of this error, perform the following steps and then recompile the program. Select ‘Options’ menu and then select ‘Compiler | C++ Options’. In the dialog box that pops up, select ‘CPP always’ in the ‘Use C++ Compiler’ options. Again select ‘Options’ menu and then select ‘Environment | Editor’. Make sure that the default extension is ‘C’ rather than ‘CPP’. Data Types Integral Types:  Integral data types include all whole numbers, that is numbers not having any fractional component.  The bits of integral types are interpreted as simple powers of two: o The right-most bit, known as the least significant bit, represents the number of 1s. ( 2^0 ) o The next bit represents the number of 2s. ( 2^1 ) o The next bit represents the number of 4s. ( 2^2 ) o The next bit represents the number of 8s. ( 2^3 ) o In general the nth bit from the right represents 2^(n-1)  For unsigned integral types, the leftmost bit, known as the most significant bit, represents 2^(N-1), where N is the total number of bits in the data item. o The range of possible values for an unsigned integer of N bits is from 0 to 2^N - 1. ( All 0s to all 1s ) o So for example, a 4-bit unsigned integer could range from 0 to 15, and an 8-bit unsigned integer could range from 0 to 255.  For signed integral types, the leftmost bit can be thought of as representing a negative 2^(N-1).
  • 29. o ( The real interpretation in the computer is more complicated, but if you think of it this way 29 you will get the right answers. ) o The most negative value would be the first bit a 1 and all other bits 0s, yielding negative 2^(N-1). o The most positive value would be the first bit a 0 and all other bits 1s, yielding 2^(N-1) - 1. o So for example, a 4-bit signed integer could range from -8 to +7, and an 8-bit signed integer could range from -128 to +127. o A signed integral type having all bits 1 is equal to -1, regardless of how many bits are in the number.  Signed and unsigned integers with the same number of total bits have the same number of different possible values. o Unsigned integers use one bit pattern ( all 0s ) to represent zero and all others to represent positive values. o Signed integers use half of the possible bit patterns to represent negative numbers, one pattern to represent zero, and half minus 1 to represent positive values.  Specific details of the integer types available on a particular implementation, along with the number of bits allocated to each one and their minimum and maximum allowable values can be found in the file limits.h int  The most basic and commonly used integral type is "int".  The int data type is always the "best" size for the particular computer it is running on, typically 32 bits  Format specifiers for ints are either %d or %i, for either printf or scanf. long int  A long int typically uses twice as many bits as a regular int, allowing it to hold much larger numbers. o ( The C standard only specifies that a long cannot use a fewer number of bits than a regular int )  printf and scanf replace %d or %i with %ld or %li to indicate the use of a long int.  long int may also be specified as just long. long long int  C99 introduces the long long int, typically having twice as many bits as a long int and printed/scanned using %lld or %lli format specifiers short int
  • 30. 30  A short int may use fewer bits than a regular int, thereby saving storage space. o ( The C standard only specifies that a short int cannot use more bits than a regular int. On many machines short ints use the same number of bits as regular ints. )  printf and scanf replace %d or %i with %hd or%hi to indicate the use of a short int.  short int may also be specified as just short. unsigned ints  Unless otherwise specified, all of the aforementioned int types are signed numbers.  Any of the above may be preceded by the keyword "unsigned" to indicate that they are unsigned. o e.g. "unsigned int", "unsigned long int", "unsigned long", etc. o "unsigned" by itself implies "unsigned int"  The format specifier for unsigned integers in decimal form is %u. The u may be preceded by l, ll, or h for long, long long, and short unsigned types respectively.  Unsigned integers can also be printed or scanned in octal or hexidecimal form using the %o, %x, or %X format specifiers in place of %u. char  Normally chars are interpreted as characters ( see below )  Technically the char data type is an integral type, always having exactly 8 bits, ( known as a byte. )  Signed chars can have values from -128 to +127, and can be printed as integers using %d or %i format specifiers  chars can also be specified as unsigned, giving them a range from 0 to+255. o Unsigned chars can be printed using %u, %o, %x, or %X format specifiers. Integral Constants  int constants in decimal form must begin with a digit from 1 to 9, and can be followed by additional digits from 0 to 9. o in octal form they must begin with a 0, and following digits must be in the range from 0 to 7. o in hexidecimal form they begin with 0x. Followng digits must be in the range 0 to 9, or A to F, or a to f.  Any of the above may be preceded by a minus sign.  int constants may be followed by a u, l, or ll to indicate unsigned, long, or long long constants respectively.  Allowable formats are as follows, where the [ square brackets ] denote optional characters:  Decimal: [±]1-9[0-9...][Ll][Uu]  Octal: [±]0[0-7...][Ll][Uu]  Hexadecimal: [±]0x[0-9a-fA-F...][Ll][Uu] Integer Overflow
  • 31.  When integer math yields a result that is too big ( or too negative ) to be represented by the 31 corresponding integer type, then overflow ( or underflow ) is said to have occurred.  With unsigned numbers the result is defined to "wrap around" to the other end of the integer's range, so if you add 1 to an integer that is at the maximum value for it's type, the result is zero.  With signed numbers the result of overflow is undefined. In some cases adding 1 to the maximum positive value will wrap around to the most negative value, but in other cases erratic behaviour or even a program crash may occur. Floating Point Types:  Floating point types include all types in which a number may have a fractional component. Fortunately there are only three that we need to worry about - float, double, and long double.  Specific details of the floating point types available on a particular implementation, along with the number of bits allocated to each one and their minimum and maximum allowable values can be found in the file float.h float  The most basic type of floating point number is the float type.  According to the IEEE standard, a single precision floating point number is exactly 32 bits long, comprised of: o one bit for indicating the sign o 23 bits ( plus one implied ) for recording the digits as a 24-bit integer. This works out to about 6 or 7 decimal digits of precision. o 8 bits for a binary exponent, to shift the digits left or right. This works out to an absolute range from about 10^-38 to 10^38  Note that because a float only has 24 bits available for storing the digits of the number, it can actually be less precise than a 32-bit int for certain large integers. double  The double precision data type uses twice as many bits as a float, yielding approximately twice the number of digits of precision.  According to the IEEE standard, a double precision floating point number is 64 bits long, comprised of: o one sign bit. o 52 bits ( plus one implied ) for recording digits, which works out to about 15 decimal digits of precision. o 11 bits for the exponent, which works out to a range of about 10^-308 to 10^308
  • 32.  The double data type is the preferred floating point type for most scientific and engineering 32 calculations. long double  The long double type is guaranteed to have more bits than a double, but the exact number my vary from one hardware platform to another. The most typical implementations are either 80 or 128 bits.  The IEEE standard for quadruple precision floating point numbers is 128 bits consisting of: o one sign bit o 112 bits ( plus one implied ) for digits, working out to about 34 decimal digits of precision o 15 bits for the exponent, giving a range from about 10^-4932 to 10^4932 Floating point constants  Floating-point constants are normally indicated by the presence of a decimal point, and are normally doubles. o Floating point constants may be followed by either an "F" to indicate an ordinary float, or an "L" to indicate a long double. o Floating point constants can also be expressed in scientific notation o Allowable formats are as follows: o [±]1-9[0-9...].[0-9...][Ee[±]0-9...][FfLl] o [±][0].[0-9...][Ee[±]0-9...][FfLl] o [±]1-9[0-9...]Ee[±]0-9...[FfLl] [±]1-9[0-9...]Ff[Ll] Characters:  Although the char data type is technically a small integer ( see above ), its most common use is to hold numeric codes representing ( ASCII ) characters.  For example, the ASCII code for the capital letter 'A' is 65.  Note that the character '9' is not the same as the integer value 9. o In this case the ASCII code for the character '9' is 57. o The numerical value 9 in the ASCII code set happens to represent a horizontal tab character. Character Constants  Character constants are enclosed in single quotes, such as 'A'.  Character constants may also include escape characters such as 'n' or 't', as shown here:
  • 33. 33 Escape Sequence Meaning a alarm ( bell ) b Backspace f Form feed ( clears screen ) n New line r Carriage return t Horizontal tab v Vertical tab Backslash ? Question mark ' Single quote " Double quote 0 Numerical zero ( null byte )  The can also be used to escape a 3-digit octal numerical constant, or a hexidecimal constant beginning with x o So '112' and 'x4A' are both a capitol 'J'. See the ASCII table to confirm. Character Arithmetic  Because chars are really small integers, it is possible to do mathematical operations on them. For example: char letter = 'G', lower, upper; // Presume lower has been given a value somehow letter = letter + 3; // letter has now been changed from 'G' to 'J' if( lower >= 'a' && lower <= 'z' ) // If lower is a lower-case letter upper = lower + ( 'A' - 'a' ) // Convert it to upper-case by adding an offset Character Strings  C stores character strings as arrays of type char, terminated by a null byte.
  • 34.  Constant character strings are enclosed in double quotes, and may include escape characters, such as 34 "nnt Please enter X > "  These notes will postpone further discussion of character strings until after arrays have been covered. Special Types  The enumerated type is an integer with a restricted list of legal values, referred to by names. It will be covered in full details in the section on structs and unions.  C99 introduces new types _Bool, _Complex, and _Imaginary. Type Conversions Implicit  There are certain cases in which data will get automatically converted from one type to another: o When data is being stored in a variable, if the data being stored does not match the type of the variable.  The data being stored will be converted to match the type of the storage variable. o When an operation is being performed on data of two different types.  The "smaller" data type will be converted to match the "larger" type.  For example, when an int is added to a double, the computer uses a double version of the int and the result is a double.  The following example converts the value of nTotal to a double precision value before performing the division. Note that if the 3.0 were changed to a simple 3, then integer division would be performed, losing any fractional values in the reuslt. average = nTotal / 3.0; o When data is passed to or returned from functions. Explicit  Data may also be expressly converted, using the typecast operator o The following example converts the value of nTotal to a double precision value before performing the division. ( nStudents will then be implicitly promoted, following the guidelines listed above. ) average = ( double ) nTotal / nStudents; o Note that nTotal itself is unaffected by this conversion. Variables:  A variable is a named storage location, where data may be stored and later changed.  An identifier is a more general term for a named location, which may contain either data or code. o Identifiers must begin with a letter or an underscore, preferable letters for user programs. o The remaining characters must be either alphanumeric or underscores.
  • 35. o Identifiers may be of any length, but only the first 31 characters are examined in most 35 implementations. o Identifiers are case sensitive, so "NUMBER", "number", and "Number" are three different identifiers. o By convention ordinary variables begin with a lower case letter, globals with a Single Capital, and constants in ALL CAPS.  Multi-word variables may use either underscores or "camel case", such as "new_value" or "newValue". o Integers are usually assigned variable names beginning with the letters I, J, K, L, M, or N, and floating point variables are usually assigned names beginning with other letters. o Identifiers may not be the same as reserved words. ( See text for a full list. )  All variables must be declared before they can be used. o In C, all variables must be declared before the first executable statement of the program. o C++ allows variables to be declared any time before they are used, but it is still normally good practice to declare all variables at the beginning of the program, unless there is a very good reason to do otherwise.  ( Exceptions: Loop counter variables are often declared as part of the loop structure. Occasionally it is beneficial to declare variables within a reduced scope, to be discussed later. )  Variables may be given an initial value at the time they are declared. This is called "initialization", or "initializing the variables". o Initialization in C should be done using an equals sign. o Example: double x1 = 0.0, x2 = 0.0; o UNINITIALIZED VARIABLES ARE DANGEROUS, AND SHOULD BE CONSIDERED TO HOLD RANDOM VALUES.  Variables may be declared "const", meaning that their values cannot be changed. o const variables MUST be initialized at the time they are declared. o By convention, const variables are named using ALL CAPS. o Examples:  const double PI = 3.14159;  const int MAXROWS = 100; o Note: K&R C did not have the const keyword, and so the #define pre-processor macro was used to define constant values. The const qualifier is a better approach when it is available, because it allows the compiler to perform type checking among other reasons. For
  • 36. CS 107 we will defer the discussion of #define until we get to the chapter on the pre-processor. 36 Type-Related Functions and Concepts ( Advanced, Optional )  The sizeof( ) operator returns the number of bytes needed to store a variable or data type, so on most sytems, sizeof( int ) would yield 4, as would sizeof( number ) if number were a variable of type int.  The keyword typedef allows programmers to define their own type names. o For example, "typedef float Dollars;" would define a new type named "Dollars" that is really the same as float. o In this case the programmer can now declare variables to be of type "Dollars" instead of type float. o One advantage is for portability purposes. If the typedef is changed to "typedef double Dollars", then it will affect all variables of type Dollars in the entire program, with one small change. o It can also make programs more readable when complicated types are used. For example, "typedef unsigned long long int BigInteger;" o ( typedef is most commonly used to rename complicated types that we have not yet covered, such as structs and pointers. ) Enumerated Types:  Enumerated ( enum ) data types are basically ints, except that they are restricted to a limited set of values, and those values are referred to by name not by number.  The use of enums where applicable helps make code more readable and also limits the possibilities for bad values, thereby reducing bugs and making the code more maintainable and overall better.  The enum keyword is used to define a new data type, having a new data type name and list of acceptable named values.  Once the new enum type has been declared, variables can be declared of the new type, and assigned the named values.  Named values can be assigned specific numbers. Those not assigned will get successive values. So in the following example, minor2 will have the value 2 and major2 will have the value 101: enum errorType { none = 0, minor1 = 1, minor2, major1 = 100, major2, fatal1 = 1000 };  Enumerated type variables can also be initialized.. For example: errorType errorCode = none; sizeType bookSize = large;
  • 37.  It is sometimes a good idea to include values such as "invalid", "undefined" or "none" among the list 37 of enumerated values.  Some compilers may allow using enum variables with ordinary integers, ( e.g. using numbers instead of names ), but it is poor practice.  Printing enumerated variables prints the assigned integer value.  One should not attempt to do any math using enumerated variables. Constants and Variables Constants: Constants in C refer to fixed values that do not change during the execution of a program. Integer Constants An integer constant refers to a sequence of digits, There are three types integers, namely, decimal, octal, and hexa decimal. Decimal Constant Eg:123,-321 etc., Note: Embedded spaces, commas and non-digit characters are not permitted between digits. Eg: 1) 15 750 2)$1000 Octal Constant An octal integer constant consists of any combination of digits from the set 0 through 7, with a leading 0. Eg: 1) 037 2) 0435 Hexadecimal Constant A sequence of digits preceded by 0x or 0X is considered as hexadecimal integer. They may also include alphabets A through F or a through f. Eg: 1) 0X2 2) 0x9F 3) 0Xbcd Program for representation of integer constants on a 16-bit computer. /*Integer numbers on a 16-bit machine*/ main() { printf(“Integer valuesnn”); printf(“%d%d%dn”,32767,32767+1,32767+10); printf(“n”);
  • 38. 38 printf(“Long integer valuesnn”); printf(“%ld%ld%ldn” ,32767L,32767L+1L,32767L+10L); } OUTPUT Integer values 32767 -32768 -32759 Long integer values 32767 32768 32777 Real Constants Certain quantities that vary continuously, such as distances, heights etc., are represented by numbers containing functional parts like 17.548.Such numbers are called real (or floating point)constants. Eg:0.0083,-0.75 etc., A real number may also be expressed in exponential or scientific notation. Eg:215.65 may be written as 2.1565e2 Single Character Constants A single character constants contains a single character enclosed within a pair of single quote marks. String Constants A string constant is a sequence of characters enclosed in double quotes. The characters may be letters, numbers, special characters and blank space. Eg:”Hello!” “1987” “?….!” Backslash Character Constants C supports special backslash character constants that are used in output functions. These character combinations are known as escape sequences. Constant Meaning ‘a’ audible alert ‘b’ backspace ‘f’ form feed ‘n’ new line ‘0’ null ‘v’ vertical tab
  • 39. 39 ‘t’ horizontal tab ‘r’ carriage return Variables: Definition: A variable is a data name that may be used to store a data value. A variable may take different values at different times of execution and may be chosen by the programmer in a meaningful way. It may consist of letters, digits and underscore character. Eg: 1) Average 2) Height Rules for defining variables  They must begin with a letter. Some systems permit underscore as the first character.  ANSI standard recognizes a length of 31 characters. However, the length should not be normally more than eight characters.  Uppercase and lowercase are significant.  The variable name should not be a keyword.  White space is not allowed. Arrays in C In C programming, one of the frequently arising problem is to handle similar types of data. For example: If the user want to store marks of 100 students. This can be done by creating 100 variable individually but, this process is rather tedious and impracticable. These type of problem can be handled in C programming using arrays. An array is a sequence of data item of homogeneous value(same type). Arrays are of two types: 1. One-dimensional arrays 2. Multidimensional arrays Declaration of one-dimensional array data_type array_name[array_size]; For example: int age[5];
  • 40. Here, the name of array is age. The size of array is 5,i.e., there are 5 items(elements) of array age. All element in an array are of the same type (int, in this case). Array elements Size of array defines the number of elements in an array. Each element of array can be accessed and used by user according to the need of program. For example: int age[5]; Note that, the first element is numbered 0 and so on. Here, the size of array age is 5 times the size of int because there are 5 elements. Suppose, the starting address of age[0] is 2120d and the size of int be 4 bytes. Then, the next address (address of a[1]) will be 2124d, address of a[2] will be 2128d and so on. Initialization of one-dimensional array: Arrays can be initialized at declaration time in this source code as: 40 int age[5]={2,4,34,3,4}; It is not necessary to define the size of arrays during initialization. int age[]={2,4,34,3,4}; In this case, the compiler determines the size of array by calculating the number of elements of an array. Accessing array elements In C programming, arrays can be accessed and treated like variables in C. For example: scanf("%d",&age[2]); /* statement to insert value in the third element of array age[]. */ scanf("%d",&age[i]); /* Statement to insert value in (i+1)th element of array age[]. */ /* Because, the first element of array is age[0], second is age[1], ith is age[i-1] and (i+1)th is age[i]. */ printf("%d",age[0]); /* statement to print first element of an array. */
  • 41. 41 printf("%d",age[i]); /* statement to print (i+1)th element of an array. */ Example of array in C programming /* C program to find the sum marks of n students using arrays */ #include <stdio.h> int main(){ int marks[10],i,n,sum=0; printf("Enter number of students: "); scanf("%d",&n); for(i=0;i<n;++i){ printf("Enter marks of student%d: ",i+1); scanf("%d",&marks[i]); sum+=marks[i]; } printf("Sum= %d",sum); return 0; } Output Enter number of students: 3 Enter marks of student1: 12 Enter marks of student2: 31 Enter marks of student3: 2 sum=45 Important thing to remember in C arrays Suppose, you declared the array of 10 students. For example: arr[10]. You can use array members from arr[0] to arr[9]. But, what if you want to use element arr[10], arr[13] etc. Compiler may not show error using these elements but, may cause fatal error during program execution. C programming language allows programmer to create arrays of arrays known as multidimensional arrays. For example: float a[2][6];
  • 42. Here, a is an array of two dimension, which is an example of multidimensional array. This array has 2 rows and 6 columns For better understanding of multidimensional arrays, array elements of above example can be thinked of as below: 42 Initialization of Multidimensional Arrays In C, multidimensional arrays can be initialized in different number of ways. int c[2][3]={{1,3,0}, {-1,5,9}}; OR int c[][3]={{1,3,0}, {-1,5,9}}; OR int c[2][3]={1,3,0,-1,5,9}; Initialization Of three-dimensional Array double cprogram[3][2][4]={ {{-0.1, 0.22, 0.3, 4.3}, {2.3, 4.7, -0.9, 2}}, {{0.9, 3.6, 4.5, 4}, {1.2, 2.4, 0.22, -1}}, {{8.2, 3.12, 34.2, 0.1}, {2.1, 3.2, 4.3, -2.0}} }; Suppose there is a multidimensional array arr[i][j][k][m]. Then this array can hold i*j*k*m numbers of data. Similarly, the array of any dimension can be initialized in C programming. Example of Multidimensional Array In C Write a C program to find sum of two matrix of order 2*2 using multidimensional arrays where, elements of matrix are entered by user. #include <stdio.h> int main(){ float a[2][2], b[2][2], c[2][2]; int i,j; printf("Enter the elements of 1st matrixn");
  • 43. /* Reading two dimensional Array with the help of two for loop. If there was an array of 'n' dimension, 'n' numbers of loops are needed for inserting data to array.*/ for(i=0;i<2;++i) 43 for(j=0;j<2;++j){ printf("Enter a%d%d: ",i+1,j+1); scanf("%f",&a[i][j]); } printf("Enter the elements of 2nd matrixn"); for(i=0;i<2;++i) for(j=0;j<2;++j){ printf("Enter b%d%d: ",i+1,j+1); scanf("%f",&b[i][j]); } for(i=0;i<2;++i) for(j=0;j<2;++j){ /* Writing the elements of multidimensional array using loop. */ c[i][j]=a[i][j]+b[i][j]; /* Sum of corresponding elements of two arrays. */ } printf("nSum Of Matrix:"); for(i=0;i<2;++i) for(j=0;j<2;++j){ printf("%.1ft",c[i][j]); if(j==1) /* To display matrix sum in order. */ printf("n"); } return 0; } Ouput Enter the elements of 1st matrix Enter a11: 2; Enter a12: 0.5; Enter a21: -1.1; Enter a22: 2; Enter the elements of 2nd matrix
  • 44. 44 Enter b11: 0.2; Enter b12: 0; Enter b21: 0.23; Enter b22: 23; Sum Of Matrix: 2.2 0.5 -0.9 25.0 CHAPTER – 3 Embedded C Functions The functions in the embedded C library are: Runtime support functions These functions carry out operations that are not available as ARM instructions, such as division. These functions are provided by the embedded C library. Software floating-point library When the compiler compiles code for use with software floating-point, it generates calls to routines in the library to perform floating-point operations. For example, to perform a double-precision multiply, the compiler generates a call to _dmul. All such routines are provided as standard by the embedded C library. C library subset This provides a subset of the C library routines. Only functions that fulfil the criteria described below have been included in the embedded C library. Refer to C library subset on page 4-19 for a complete list of the
  • 45. 45 included functions. Static data The embedded C library does not make any use of static data. Because of this, it is automatically fully reentrant. Operating-system independence The embedded C functions do not rely on the underlying operating system in any way. Many functions in the full ANSI library rely on the underlying OS to perform functions such as writing characters, or opening files. These functions are excluded from the embedded C library. For example, functions such as printf() are excluded, but sprintf() is not. Standalone functions Many functions in the full ANSI C library rely on a number of other functions in the C library to perform their operations. For example, printf() relies on functions such as ferror() and fputc(). This means that a single call to printf() includes a large amount of the C library code. The embedded C library breaks many of these dependencies so that only the minimum amount of code needed to perform the operation is included. C library subset Table 4-4 lists the C library functions that are supported in the embedded C library. Name From Description <assert.h> Contains the assert macro, used to assist with detecting logical errors and other types of bug in debugging versions of a program. <complex.h> C99 A set of functions for manipulating complex numbers. <ctype.h> Defines set of functions used to classify characters by their types or to convert between upper and lower case in a way that is independent of the used character set (typically ASCII or one of its extensions, although implementations utilizing EBCDIC are also known). <float.h> Defines macro constants specifying the implementation-specific properties of the floating-point library.
  • 46. 46 Name From Description <math.h> Defines common mathematical functions. <setjmp.h> Declares the macros setjmp and longjmp, which are used for non-local exits. <stdio.h> Defines core input and output functions Unions Unions don't take long to explain. They are the same as structures, except that, where you would have written struct before, now you write union. Everything works the same way, but with one big exception. In a structure, the members are allocated separate consecutive chunks of storage. In a union, every member is allocated the same piece of storage. What would you use them for? Well, sometimes you want a structure to contain different values of different types at different times but to conserve space as much as possible. Using a union, it's up to you to keep track of whatever type you put into it and make sure that you retrieve the right type at the right time. Here's an example: #include <stdio.h> #include <stdlib.h> main(){ union { float u_f; int u_i; }var; var.u_f = 23.5; printf("value is %fn", var.u_f); var.u_i = 5; printf("value is %dn", var.u_i); exit(EXIT_SUCCESS); } If the example had, say, put a float into the union and then extracted it as an int, a strange value would have resulted. The two types are almost certainly not only stored differently, but of different lengths. The int retrieved would probably be the low-order bits of the machine representation of a float, and might easily be made up of part of the mantissa of the float plus a piece of the exponent. The Standard
  • 47. says that if you do this, the behaviour is implementation defined (not undefined). The behaviour is defined by the Standard in one case: if some of the members of a union are structures with a ‘common initial sequence’ (the first members of each structure have compatible type and in the case of bitfields are the same length), and the union currently contains one of them, then the common initial part of each can be used interchangeably. Oh good. The C compiler does no more than work out what the biggest member in a union can be and allocates enough storage (appropriately aligned if necessary). In particular, no checking is done to make sure that the right sort of use is made of the members. That is your task, and you'll soon find out if you get it wrong. The members of a union all start at the same address—there is guaranteed to be no padding in front of any of them. The most common way of remembering what is in a union is to embed it in a structure, with another member of the structure used to indicate the type of thing currently in the union. Here is how it might be used: 47 #include <stdio.h> #include <stdlib.h> /* code for types in union */ #define FLOAT_TYPE 1 #define CHAR_TYPE 2 #define INT_TYPE 3 struct var_type{ int type_in_union; union{ float un_float; char un_char; int un_int; }vt_un; }var_type; void print_vt(void){ switch(var_type.type_in_union){ default: printf("Unknown type in unionn"); break; case FLOAT_TYPE:
  • 48. 48 printf("%fn", var_type.vt_un.un_float); break; case CHAR_TYPE: printf("%cn", var_type.vt_un.un_char); break; case INT_TYPE: printf("%dn", var_type.vt_un.un_int); break; } } main(){ var_type.type_in_union = FLOAT_TYPE; var_type.vt_un.un_float = 3.5; print_vt(); var_type.type_in_union = CHAR_TYPE; var_type.vt_un.un_char = 'a'; print_vt(); exit(EXIT_SUCCESS); } That also demonstrates how the dot notation is used to access structures or unions inside other structures or unions. Some current C compilers allow you to miss bits out of the names of embedded objects provided that they are not ambiguous. In the example, such an unambiguous name would be var_type.un_int and the compiler would work out what you meant. None the less this is not permitted by the Standard. It is because of unions that structures cannot be compared for equality. The possibility that a structure might contain a union makes it hard to compare such structures; the compiler can't tell what the union currently contains and so wouldn't know how to compare the structures. This sounds a bit hard to swallow and isn't 100% true—most structures don't contain unions—but there is also a philosophical issue at stake about just what is meant by ‘equality’ when applied to structures. Anyhow, the union business gives the Standard a good excuse to avoid the issue by not supporting structure comparison. Structures
  • 49. Structures are perhaps what makes C such a powerful language for creating very complex programs with huge amounts of data. They are basically a way of grouping together related data items under a single symbolic name. 49 Why Use Structures? Here is an example: A piece of C51 software had to perform a linearization process on the raw signal from a variety of pressure sensors manufactured by the same company. For each sensor to be catered for there is an input signal with a span and offset, a temperature coefficient, the signal conditioning amplifier, a gain and offset. The information for each sensor type could be held in "normal" constants thus: 1. unsigned char sensor_type1_gain = 0x30 ; 2. unsigned char sensor_type1_offset = 0x50 ; 3. unsigned char sensor_type1_temp_coeff = 0x60 ; 4. unsigned char sensor_type1_span = 0xC4 ; 5. unsigned char sensor_type1_amp_gain = 0x21 ; 6. unsigned char sensor_type2_gain = 0x32 ; 7. unsigned char sensor_type2_offset = 0x56 ; 8. unsigned char sensor_type2_temp_coeff = 0x56 ; 9. unsigned char sensor_type2_span = 0xC5 ; 10. unsigned char sensor_type2_amp_gain = 0x28 ; 11. unsigned char sensor_type3_gain = 0x20 ; 12. unsigned char sensor_type3_offset = 0x43 ; 13. unsigned char sensor_type3_temp_coeff = 0x61 ; 14. unsigned char sensor_type3_span = 0x89 ; 15. unsigned char sensor_type3_amp_gain = 0x29 ; As can be seen, the names conform to an easily identifiable pattern of: 1. unsigned char sensor_typeN_gain = 0x20 ; 2. unsigned char sensor_typeN_offset = 0x43 ; 3. unsigned char sensor_typeN_temp_coeff = 0x61 ; 4. unsigned char sensor_typeN_span = 0x89 ; 5. unsigned char sensor_typeN_amp_gain = 0x29 ; Where 'N' is the number of the sensor type. A structure is a neat way of condensing this type of related and repeating data. In fact the information needed to describe a sensor can be reduced to a generalized: 1. unsigned char gain ; 2. unsigned char offset ;
  • 50. 50 3. unsigned char temp_coeff ; 4. unsigned char span ; 5. unsigned char amp_gain ; The concept of a structure is based on this idea of generalized "template" for related data. In this case, a structure template (or "component list") describing any of the manufacturer's sensors would be declared: 1. struct SENSOR_DESC 2. { 3. unsigned char gain ; 4. unsigned char offset ; 5. unsigned char temp_coeff ; 6. unsigned char span ; 7. unsigned char amp_gain ; 8. } ; This does not physically do anything to memory. At this stage it merely creates a template which can now be used to put real data into memory. This is achieved by: 1. struct SENSOR_DESC sensor_database ; This reads as "use the template SENSOR_DESC to layout an area of memory named sensor_database, reflecting the mix of data types stated in the template". Thus a group of 5 unsigned chars will be created in the form of a structure. The individual elements of the structure can now be accessed as: 1. sensor_database.gain = 0x30 ; 2. sensor_database.offset = 0x50 ; 3. sensor_database.temp_coeff = 0x60 ; 4. sensor_database.span = 0xC4 ; 5. sensor_database.amp_gain = 0x21 ; Arrays Of Structures In the example though, information on many sensors is required and, as with individual chars and ints, it is possible to declare an array of structures. This allows many similar groups of data to have different sets of values. 1. struct SENSOR_DESC sensor_database[4] ;
  • 51. This creates four identical structures in memory, each with an internal layout determined by the structure template. Accessing this array is performed simply by appending an array index to the structure name: 1. /*Operate On Elements In First Structure Describing */ 51 2. /*Sensor 0 */ 3. sensor_database[0].gain = 0x30 ; 4. sensor_database[0].offset = 0x50 ; 5. sensor_database[0].temp_coeff = 0x60 ; 6. sensor_database[0].span = 0xC4 ; 7. sensor_database[0].amp_gain = 0x21 ; 8. /* Operate On Elements In First Structure Describing */ 9. /*Sensor 1 */ 10. sensor_database[1].gain = 0x32 ; 11. sensor_database[1].offset = 0x56 ; 12. sensor_database[1].temp_coeff = 0x56 ; 13. sensor_database[1].span = 0xC5 ; 14. sensor_database[1].amp_gain = 0x28 ; 15. // and so on... C Preprocessor Including files: One of the most common uses of the preprocessor is to include another file: #include <stdio.h> int main(void) { printf("Hello, world!n"); return 0; } The preprocessor replaces the line #include <stdio.h> with the text of the file 'stdio.h', which declares the printf() function among other things. This can also be written using double quotes, e.g. #include "stdio.h". If the filename is enclosed within angle brackets, the file is searched for in the standard compiler include paths. If the filename is enclosed within double quotes, the search path is expanded to include the current source directory. C compilers
  • 52. and programming environments all have a facility which allows the programmer to define where include files can be found. This can be introduced through a command line flag, which can be parameterized using a makefile, so that a different set of include files can be swapped in for different operating systems, for instance. By convention, include files are given a .h extension, and files not included by others are given a .c extension. However, there is no requirement that this be observed. Occasionally you will see files with other extensions included: files with a .def extension may denote files designed to be included multiple times, each time expanding the same repetitive content; #include "icon.xbm" is likely to refer to an XBM image file (which is at the same time a C source file). #include often compels the use of #include guards or #pragma once to prevent double inclusion. 52 Conditional compilation: The if-else directives #if, #ifdef, #ifndef, #else, #elif and #endif can be used for conditional compilation. #if VERBOSE >= 2 print("trace message"); #endif Most compilers targeting Microsoft Windows implicitly define _WIN32. This allows code, including preprocessor commands, to compile only when targeting Windows systems. A few compilers define WIN32 instead. For such compilers that do not implicitly define the _WIN32 macro, it can be specified on the compiler's command line, using -D_WIN32. #ifdef __unix__ /* __unix__ is usually defined by compilers targeting Unix systems */ # include <unistd.h> #elif defined _WIN32 /* _Win32 is usually defined by compilers targeting 32 or 64 bit Windows systems */ # include <windows.h> #endif The example code tests if a macro __unix__ is defined. If it is, the file <unistd.h> is then included. Otherwise, it tests if a macro _WIN32 is defined instead. If it is, the file<windows.h> is then included. A more complex #if example can use operators, for example something like: #if !(defined __LP64__ || defined __LLP64__) || defined _WIN32 && !defined _WIN64
  • 53. 53 // we are compiling for a 32-bit system #else // we are compiling for a 64-bit system #endif Translation can also be caused to fail by using the #error directive: #if RUBY_VERSION == 190 # error 1.9.0 not supported #endif Macro definition and expansion: There are two types of macros, object-like and function-like. Object-like macros do not take parameters; function-like macros do. The generic syntax for declaring an identifier as a macro of each type is, respectively: #define <identifier> <replacement token list> // object-like macro #define <identifier>(<parameter list>) <replacement token list> // function-like macro, note parameters The function-like macro declaration must not have any whitespace between the identifier and the first, opening, parenthesis. If whitespace is present, the macro will be interpreted as object-like with everything starting from the first parenthesis added to the token list. A macro definition can be removed with "#undef": #undef <identifier> // delete the macro Whenever the identifier appears in the source code it is replaced with the replacement token list, which can be empty. For an identifier declared to be a function-like macro, it is only replaced when the following token is also a left parenthesis that begins the argument list of the macro invocation. The exact procedure followed for expansion of function-like macros with arguments is subtle. Object-like macros were conventionally used as part of good programming practice to create symbolic names for constants, e.g. #define PI 3.14159 ... instead of hard-coding the numbers throughout the code. An alternative in both C and C++, especially in situations in which a pointer to the number is required, is to apply theconst qualifier to a global variable. This causes the value to be stored in memory, instead of being substituted by the preprocessor. An example of a function-like macro is:
  • 54. #define RADTODEG(x) ((x) * 57.29578) This defines a radians-to-degrees conversion which can be inserted in the code where required, i.e., RADTODEG(34). This is expanded in-place, so that repeated multiplication by the constant is not shown throughout the code. The macro here is written as all uppercase to emphasize that it is a macro, not a compiled function. The second x is enclosed in its own pair of parentheses to avoid the possibility of incorrect order of operations when it is an expression instead of a single value. For example, the expression RADTODEG(r + 1) expands correctly as ((r + 1) * 57.29578) — without parentheses, (r + 1 * 57.29578) gives precedence to the multiplication. Similarly, the outer pair of parentheses maintain correct order of operation. For example, 1 / RADTODEG(r) expands to 1 / ((r) * 57.29578) — without parentheses, 1 /(r) * 57.29578 gives precedence to the division. 54 Special macros and directives: Certain symbols are required to be defined by an implementation during preprocessing. These include __FILE__ and __LINE__, predefined by the preprocessor itself, which expand into the current file and line number. For instance the following: // debugging macros so we can pin down message origin at a glance #define WHERESTR "[file %s, line %d]: " #define WHEREARG __FILE__, __LINE__ #define DEBUGPRINT2(...) fprintf(stderr, __VA_ARGS__) #define DEBUGPRINT(_fmt, ...) DEBUGPRINT2(WHERESTR _fmt, WHEREARG, __VA_ARGS__) //... DEBUGPRINT("hey, x=%dn", x); prints the value of x, preceded by the file and line number to the error stream, allowing quick access to which line the message was produced on. Note that the WHERESTRargument is concatenated with the string following it. The values of __FILE__ and __LINE__ can be manipulated with the #line directive. The #line directive determines the line number and the file name of the line below. E.g.: #line 314 "pi.c" puts("line=" #__LINE__ " file=" __FILE__); generates the puts function: