SlideShare a Scribd company logo
© 2012 SysPlay eLearning Academy for You <https://sysplay.in>
All Rights Reserved.
Linux Internals (Day 2)
2
© 2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
What to Expect?
GNU OS & GNU Development Tools
Compiler, Assembler and linker
Writing Large Programs
Writing Makefiles
Linux Process & Process Control
Programs on Process control
© 2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
GNU Operating System
A Unix-like operating system that is free software – respects your
freedom
GNU is a recursive acronym for “GNU's Not Unix” because
GNU's design is Unix like, but differs by being free software and containing
no Unix Code.
A software collection of application, libraries and developer tools.
And a program to allocate resources and talk to hardware, known as Kernel
Used with the Kernel called Linux
Together known as GNU/Linux Operating System
© 2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
GNU Tools
Contains a rich set of software packages ranging from
Archiving, Audio, Business Productivity to Health,
Hobbies and so on
Archiving – cpio, gzip, tar...
Audio – EMMS, Gmediaserver
Database – Ferret, Gdbm, Guil dbi..
Games – Acm, Aetherspace
And the list continues...
© 2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
GNU Development Tools
The GNU development tools are collection of
Compiler, Assembler, Linker, Libraries, Makefile,
debugger and so on.
GCC – GNU Compiler Collection
Binutils – Linker(ld), Assembler (gas) and so on
GNU C Library – libc
Debugger – GDB, DDD
© 2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
GNU Compiler Collection (GCC)
Most widely used compiler
A full featured ANSI C compiler with support for
C, C++, java and Fortran
Contains the libraries for these languages as
well.
Historically, known as 'GNU C Compiler'
7
© 2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
W's of Building a Program
A Process to translate a program from a Higher
Level Language to an executable
Four stages for a C Program to become an
executable
Pre-processing
Compiling
Assembling
Linking
8
© 2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Stages for Building a Program
9
© 2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Pre-Processor
Accepts Source Code as input and is responsible for
Removing Comments
Interpreting special pre-processor directives denoted by #
#include – includes contents of named file
#include <stdio.h>
#define – defines symbolic name or constants
#define MAX_SIZE 100
gcc -E hello_world.c -o hello_world.i
gives hello_world.i
10
© 2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Compiler, Assembler and Linker
Compiler
Translates a pre-Processed source code to assembly code
gcc -S hello_world.i -o hello_world.s
Gives hello_world.s
Assembler
Translate assembly code to machine code with unresolved symbols
gcc -c hello_world.s -o hello_world.o
Gives hello_world.o
11
© 2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Assembler, Compiler & Linker...
Linker
Translates the object code into the executable
All the called functions named would be replaced by
address of these function.
gcc hello_world.o -o hello_world
Gives hello_world
12
© 2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Useful gcc Compiler Options
-c
Suppress the linking process and produce a .o file for each source file listed
gcc -c file1.c file2.c ...
-llibrary: Link with object libraries which are archived.
Example : math library
gcc sqrt.c -o sqrt -lm
-L<path> - Look into the non standard path for libraries
gcc -o bar bar.o -L/home/user/ -lfoo
-I<path> - Look into the non standard path for headers
Gcc -c -I/home/user foo.c
-g : invoke debugging option
-D: define symbols either as identifies or as values
© 2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
GNU Binutils
The GNU binutils are a collection of binary tools.
Most important
as – GNU assembler
ld – GNU linker
Also, includes
Addr2line, ar, gprof, nlmconv, nm, objcopy, objdump,
ranlib, readelf, size, strings, strip
© 2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Getting the Listing with Assembler
Use the '-Wa' option in gcc to pass arguments to the
assembler
The arguments must be separated from each other by
commas.
Eg:
gcc -c -g -Wa,-alh,-L file.c
-alh – emit the listing to standard output with high level and
assembly source
-L - Retain local symbols in symbol table
© 2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Exploring GCC Linking Process
gcc -c main.c func.c
Give main.o and func.o
gcc func.o main.o -o main
ldd main
Prints the shared libraries needed by program
Eg for printf,
Code Relocations
Are the entries within a binary that are left to be filled at the link time or
run time.
Use objdump and readelf
© 2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
nm command
Provides the information on the symbols being used in the an object file or
executable file
Provides
Virtual address of the Symbol
A character to depict the symbol type
Lower case – Symbol is local
Upper case – Symbol is global
Name of the symbol
nm <object file or executable name>
nm -A ./*.o - display the symbol with function which contains it
nm -u <executable> – displays all the unresolved symbols in a executable file
© 2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Strings Command
Finds and displays the printable strings in given
executable, binary or object file
strings <filename>
© 2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Building Libraries
Collection of object files can be used to form a
library
Use ar command
ar cru libfoo.a foo1.o foo2.o foo3.o
Will create a file libfoo.a from foo1.o foo2.o foo3.o
Ranlib libfoo.a
Generates and adds the symbol table to .a file
Gcc main.o libfoo.a
© 2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Writing a Larger Programs
Divide the programs into modules
Separate source file. Main() in main.c and others will
contain the functions
Can Create our own library of functions.
Can be shared amongst many programs
Advantages
The modules will divide into common group of functions
We can link each module separately and link in compiled
modules
© 2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Header Files
Each module needs to have a variable definition,
function definitions etc with itself.
Several modules need to share such some
declarations.
Centralize the PreProcessor commands function
prototypes in a file called header with an extension .h.
#include <stdio.h> - Standard Header file
#inlcude “my_head.h”
© 2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Sharing the Variables
Pass the variable as Paramters
Passing long variable list to many functions can be laborious.
Large arrays and structures are difficult to store locally -
Memory problems with stack.
External Variable and functions
Defined outside of functions
Potentially available to the whole program
We have global variable – AnotherString declared in main.c and
shared with WriteMyString.
© 2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Advantages of Using Several Files
Each programmer works on different file.
An Object orientation style can be used.
Files can contain all functions from a related group.
Code re-usability for well implemented objects or
functions.
When changes are made to a file, only that file needs
to be recompiled to rebuild the program
© 2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Makefile & make Utility
make is a utility for automatically building large
applications
Files specifying the instructions for make are
Makefile in the directory which it was invoked
Make is an expert system that tracks which files
have changed since the last time the project was
build and invokes the compiler on only those files
and their dependency
© 2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Why make?
A software project which consists of many source codes,
can have complex and long compiler commands. Using
make, it can be reduced.
Programming project sometimes need specialized
compiler options that are so rarely used they are hard to
remember; with make this can be reduced.
Maintaining a consistent development environment.
Automating the build process, because make can be
called easily from a shell script.
© 2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Makefile Components
Makefile is a set of rules. Each rule has the form
TARGET: DEPENDENCIES
<TAB> COMMAND
<TAB> COMMAND
The <TABS> are Mandatory
Target
Either a name of the file that is generated by the program or the name
of the action to carry out
Object files and executable files are example of files that are generated
by other programs. Cleaning up the object files is an example of an
action that we might need to carry
© 2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Make File Components...
Dependency
A name of the file that is used to create the target
More than one dependencies must be separated out by spaces
Commands
Must be prepended with <TAB>
If the target is the file, commands explains how to create that file
If the target is an action, then the commands describe the action
© 2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
How it works?
Works on the Timestamps
Dependency is up-to-date, if and only if the target's latest updates
happened after the dependency's latest update.
Dependency is changed, if and only if the target's latest update happened
before the dependency' s latest update
Makefile takes the action only if Dependency is changed.
Starts from the first target & chain proceeds and all the other
targets are ignored.
Type make <target> for specific target
Let's try a simple Makefile
© 2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Building a C Program
a.o : a.c
gcc -c a.c -o a.o
clean:
rm -rf *.o
29
© 2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
What is Process?
Running instance of program
Program in execution.
Executable Program Loaded -> Process
Two instances of same program -> ? Processes
30
© 2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Program Vs Process
31
© 2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Why we need a process?
Improve the system performance
Multitasking
In turn needs
Time sharing (on same processor)
Scheduling
Priority
And for all these: Process Identifier (PID)
32
© 2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Let's view
Shell Local process: ps
Console attached System processes: ps a
All system processes: ps ax
List Many other details: Add l
Observe uid, ppid, priority, nice, status, tty, time
Dynamic process status : top
Try pid.c
33
© 2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Linux Process Scheduling
Based on the time-sharing technique
Several processes are allowed to run concurrently
CPU time is divided into “slices”, one for each
runnable process.
Preemption
Based on various task priorities
Specified by its scheduling policies
34
© 2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Types of Processes
IO Bound
Spend much of the time waiting on I/O
Runs for only short duration
CPU Bound
Spend much of the time executing code
Tend to run until they are preempted.
Linux Implicitly favours I/O bound processes over CPU
bound processes
35
© 2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Process Context Switch
36
© 2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Generic Process State Diagram
37
© 2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Linux Process States
The possible states are:
TASK_RUNNING (R) – ready or running
TASK_INTERRUPTIBLE (S) – blocked (waiting for an event)
TASK_UNINTERRUPTIBLE (D) – blocked (usually for IO)
TASK_ZOMBIE (Z) – terminated, but not cleaned by its parent
TASK_STOPPED (T) : execution stopped
Mutually exclusive
Additional Information: Foreground (+), Threaded (I)
38
© 2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Process Management in Linux
Information associated with each process
Address Space (Code, Variables, stack, heap)
Processor state (PC, Registers,..)
CPU registers
Scheduling info, priority
Memory Management information
Stored in a structure of type 'task_struct'
Maintained by Linux Kernel for each process
Also, called the Process Descriptor/
Process Control Block
39
© 2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Basic Process Management
bg – Starts a suspended process in the background
fg = Starts a suspended process in the foreground
jobs – Lists the jobs running
pidof – Find the Process ID of a running process
top – Display the processes that are using the most
cpu resources
40
© 2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Process Creation
From Shell
By running a command / Program / Script
By 'exec' ing a Command / Program
By Programming
Using System()
Simple, but inefficient
Using fork and exec family function
Comparatively complex
Greater flexibility, speed and security
41
© 2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Using the System Function
Execution Flow
Creates a sub-process running the standard shell
Hands the command to that shell for execution
Used to execute the command from within a program
Subjected to the features and limitations of the system
shell
Try this example (system.c)
42
© 2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Using fork()
Creates a child process that is an copy of its parent
43
© 2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Distinguishing Parent & Child
Child has a distinct PID from that of parent
The fork provides a different return values to
parent and child.
Return Values
PID of the child in Parent process
0 in the Child process
44
© 2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Using Exec function
An exec family of functions replace the current
program by new one
Before exec(),
PID
program
code
Program a.c
program
counter
r e g i s t e r
stack
data
section
45
© 2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Using exec function...
program
code
ProgramA.c
PID
program
counter
stack
data
section
r e g i s t e r
heap
Preserved Reset
Overwritten by the
new program
Change program code
46
© 2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Exec Family of Functions
execve()
execv() execvp()
execlp()
execle()
execl()
47
© 2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Cons of fork & exec
Fork
Forked child process typically executes the copy of
parent process' program
Can't execute the program external to current
executable
Exec
Doesn't creates a new process
Calling program ceases to execute
48
© 2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
fork complementing exec
New
Copy of
Parent
Initial process
Fork
Original
process
Continues
new_Program
(replacement)
execv(new_program)
fork() returns pid=0 and runs as a
cloned parent until execv is called
Returns a
new PID
49
© 2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Copy On Write (COW)
Parent and Child shares the Address space (ro)
Data & other resources are marked COW
If written to, a duplicate is make and each process receives a unique
copy
Consequently, the duplication of resources occurs only when they
are written to
Avoids copy in case of immediate exec
fork()'s only overheads
Duplication of parent's page table
Creation of unique PCB for child
50
© 2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Process Termination
Parent and Child process terminate as usual
Success Exit – Normal Success
Error Exit – Normal failure
Fatal Exit – Signaled from Kernel space for a bug
Kill Exit – Signaled by process
But which one of them terminates first?
If it matters, parent can wait for their children using
wait family of system calls.
51
© 2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Wait family of system calls
Four different system calls in the wait family
wait() : Block until one of the child process terminates
waitpid() : Wait for a specific child to exit/stop/resume.
wait3() : Along with, return the resource usage info about
exiting/stopping/resuming child process.
Wait4() : wait3() for specific child or parent.
All of these fill a status code
In an integer pointer argument
About how the child process exited
Which can decoded using...
52
© 2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
wait status macros
WEXITSTATUS : Extracts the child process exit code
WIFEXITED: Determine whether child process exited normally or
died from unhandled signal
WIFSIGNALED : Determines if the child process was signaled
WTERMSIG : Extracts from the process' exit status, the signal by
which it died
Other
WIFSTOPPED, WCOREDUMP, WSTOPSIG, WIFCONTINUED
53
© 2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Zombie Process
A process that is terminated but the resources
are not yet cleaned up is called zombie process
Reason
The child process exited before the parent
The parent didn't call the wait
Even if a parent does a wait later, it remains a
Zombie till then.
54
© 2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Who cleans up a Zombie?
Typically, again a Parent
By doing a wait on it
What if the parent exits without “wait”?
Does it stay around in the system?
Not really. It gets inherited by init
which then cleans it up right there
55
© 2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Orphan Process
If the parent dies before the children
Child become orphan
And are adopted by init
which then does the clean up on their exit
56
© 2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
What all we have learnt?
GNU OS & GNU Development Tools
Compiler, Assembler and linker
Writing Large Programs
Writing Makefiles
Linux Process & Process Control
Programs on Process control
57
© 2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Any Queries?

More Related Content

What's hot

POSIX Threads
POSIX ThreadsPOSIX Threads
SPI Drivers
SPI DriversSPI Drivers
Bootloaders
BootloadersBootloaders
Bootloaders
Anil Kumar Pugalia
 
Kernel Programming
Kernel ProgrammingKernel Programming
Kernel Programming
Anil Kumar Pugalia
 
Functional Programming with LISP
Functional Programming with LISPFunctional Programming with LISP
Functional Programming with LISPAnil Kumar Pugalia
 
Kernel Debugging & Profiling
Kernel Debugging & ProfilingKernel Debugging & Profiling
Kernel Debugging & Profiling
Anil Kumar Pugalia
 
gcc and friends
gcc and friendsgcc and friends
gcc and friends
Anil Kumar Pugalia
 

What's hot (20)

POSIX Threads
POSIX ThreadsPOSIX Threads
POSIX Threads
 
SPI Drivers
SPI DriversSPI Drivers
SPI Drivers
 
Introduction to Linux
Introduction to LinuxIntroduction to Linux
Introduction to Linux
 
Linux Network Management
Linux Network ManagementLinux Network Management
Linux Network Management
 
Processes
ProcessesProcesses
Processes
 
Bootloaders
BootloadersBootloaders
Bootloaders
 
Kernel Programming
Kernel ProgrammingKernel Programming
Kernel Programming
 
Kernel Debugging & Profiling
Kernel Debugging & ProfilingKernel Debugging & Profiling
Kernel Debugging & Profiling
 
"make" system
"make" system"make" system
"make" system
 
Character Drivers
Character DriversCharacter Drivers
Character Drivers
 
Functional Programming with LISP
Functional Programming with LISPFunctional Programming with LISP
Functional Programming with LISP
 
Linux File System
Linux File SystemLinux File System
Linux File System
 
Real Time Systems
Real Time SystemsReal Time Systems
Real Time Systems
 
Kernel Debugging & Profiling
Kernel Debugging & ProfilingKernel Debugging & Profiling
Kernel Debugging & Profiling
 
Architecture Porting
Architecture PortingArchitecture Porting
Architecture Porting
 
RPM Building
RPM BuildingRPM Building
RPM Building
 
Signals
SignalsSignals
Signals
 
gcc and friends
gcc and friendsgcc and friends
gcc and friends
 
Video Drivers
Video DriversVideo Drivers
Video Drivers
 
Linux Kernel Overview
Linux Kernel OverviewLinux Kernel Overview
Linux Kernel Overview
 

Similar to Linux Internals Part - 2

ColdFusion 11 Overview - CFSummit 2013
ColdFusion 11 Overview - CFSummit 2013ColdFusion 11 Overview - CFSummit 2013
ColdFusion 11 Overview - CFSummit 2013
Rupesh Kumar
 
Application Development | Delphi Review 2009
Application Development | Delphi Review 2009Application Development | Delphi Review 2009
Application Development | Delphi Review 2009
Michael Findling
 
C Programming UNIT 1.pptx
C Programming  UNIT 1.pptxC Programming  UNIT 1.pptx
C Programming UNIT 1.pptx
Mugilvannan11
 
Unit 2 ppt
Unit 2 pptUnit 2 ppt
Unit 2 ppt
Mitali Chugh
 
Ab initio training Ab-initio Architecture
Ab initio training Ab-initio ArchitectureAb initio training Ab-initio Architecture
Ab initio training Ab-initio Architecture
Sandeep Sharma IIMK Smart City,IoT,Bigdata,Cloud,BI,DW
 
INTRODUCTION TO C LANGUAGE.pptx
INTRODUCTION TO C LANGUAGE.pptxINTRODUCTION TO C LANGUAGE.pptx
INTRODUCTION TO C LANGUAGE.pptx
MohammedtajuddinTaju
 
Makefile
MakefileMakefile
Makefile
Ionela
 
CS8251_QB_answers.pdf
CS8251_QB_answers.pdfCS8251_QB_answers.pdf
CS8251_QB_answers.pdf
vino108206
 
Safetty systems intro_embedded_c
Safetty systems intro_embedded_cSafetty systems intro_embedded_c
Safetty systems intro_embedded_c
Maria Cida Rosa
 
Unit 2 l1
Unit 2 l1Unit 2 l1
Unit 2 l1
Mitali Chugh
 
Chap 2 structure of c programming dti2143
Chap 2  structure of c programming dti2143Chap 2  structure of c programming dti2143
Chap 2 structure of c programming dti2143alish sha
 
LOSS_C11- Programming Linux 20221006.pdf
LOSS_C11- Programming Linux 20221006.pdfLOSS_C11- Programming Linux 20221006.pdf
LOSS_C11- Programming Linux 20221006.pdf
Thninh2
 
A quick start on Zend Framework 2
A quick start on Zend Framework 2A quick start on Zend Framework 2
A quick start on Zend Framework 2
Enrico Zimuel
 
Android Deep Dive
Android Deep DiveAndroid Deep Dive
Android Deep Dive
Marko Gargenta
 
Better Code: Concurrency
Better Code: ConcurrencyBetter Code: Concurrency
Better Code: Concurrency
Platonov Sergey
 
Android For Java Developers
Android For Java DevelopersAndroid For Java Developers
Android For Java Developers
Mike Wolfson
 
Why Drupal is Rockstar?
Why Drupal is Rockstar?Why Drupal is Rockstar?
Why Drupal is Rockstar?
Gerald Villorente
 
Zend Products and PHP for IBMi
Zend Products and PHP for IBMi  Zend Products and PHP for IBMi
Zend Products and PHP for IBMi
Shlomo Vanunu
 

Similar to Linux Internals Part - 2 (20)

ColdFusion 11 Overview - CFSummit 2013
ColdFusion 11 Overview - CFSummit 2013ColdFusion 11 Overview - CFSummit 2013
ColdFusion 11 Overview - CFSummit 2013
 
Application Development | Delphi Review 2009
Application Development | Delphi Review 2009Application Development | Delphi Review 2009
Application Development | Delphi Review 2009
 
C Programming UNIT 1.pptx
C Programming  UNIT 1.pptxC Programming  UNIT 1.pptx
C Programming UNIT 1.pptx
 
Unit 2 ppt
Unit 2 pptUnit 2 ppt
Unit 2 ppt
 
Ab initio training Ab-initio Architecture
Ab initio training Ab-initio ArchitectureAb initio training Ab-initio Architecture
Ab initio training Ab-initio Architecture
 
INTRODUCTION TO C LANGUAGE.pptx
INTRODUCTION TO C LANGUAGE.pptxINTRODUCTION TO C LANGUAGE.pptx
INTRODUCTION TO C LANGUAGE.pptx
 
Ide
IdeIde
Ide
 
Makefile
MakefileMakefile
Makefile
 
CS8251_QB_answers.pdf
CS8251_QB_answers.pdfCS8251_QB_answers.pdf
CS8251_QB_answers.pdf
 
Safetty systems intro_embedded_c
Safetty systems intro_embedded_cSafetty systems intro_embedded_c
Safetty systems intro_embedded_c
 
Unit 2 l1
Unit 2 l1Unit 2 l1
Unit 2 l1
 
Dotnet basics
Dotnet basicsDotnet basics
Dotnet basics
 
Chap 2 structure of c programming dti2143
Chap 2  structure of c programming dti2143Chap 2  structure of c programming dti2143
Chap 2 structure of c programming dti2143
 
LOSS_C11- Programming Linux 20221006.pdf
LOSS_C11- Programming Linux 20221006.pdfLOSS_C11- Programming Linux 20221006.pdf
LOSS_C11- Programming Linux 20221006.pdf
 
A quick start on Zend Framework 2
A quick start on Zend Framework 2A quick start on Zend Framework 2
A quick start on Zend Framework 2
 
Android Deep Dive
Android Deep DiveAndroid Deep Dive
Android Deep Dive
 
Better Code: Concurrency
Better Code: ConcurrencyBetter Code: Concurrency
Better Code: Concurrency
 
Android For Java Developers
Android For Java DevelopersAndroid For Java Developers
Android For Java Developers
 
Why Drupal is Rockstar?
Why Drupal is Rockstar?Why Drupal is Rockstar?
Why Drupal is Rockstar?
 
Zend Products and PHP for IBMi
Zend Products and PHP for IBMi  Zend Products and PHP for IBMi
Zend Products and PHP for IBMi
 

More from SysPlay eLearning Academy for You

Kernel Timing Management
Kernel Timing ManagementKernel Timing Management
Kernel Timing Management
SysPlay eLearning Academy for You
 
Understanding the BBB
Understanding the BBBUnderstanding the BBB
Understanding the BBB
SysPlay eLearning Academy for You
 
Linux DMA Engine
Linux DMA EngineLinux DMA Engine
Cache Management
Cache ManagementCache Management
BeagleBone Black Bootloaders
BeagleBone Black BootloadersBeagleBone Black Bootloaders
BeagleBone Black Bootloaders
SysPlay eLearning Academy for You
 
Introduction to BeagleBone Black
Introduction to BeagleBone BlackIntroduction to BeagleBone Black
Introduction to BeagleBone Black
SysPlay eLearning Academy for You
 
Introduction to BeagleBoard-xM
Introduction to BeagleBoard-xMIntroduction to BeagleBoard-xM
Introduction to BeagleBoard-xM
SysPlay eLearning Academy for You
 
Platform Drivers
Platform DriversPlatform Drivers
BeagleBone Black Bootloaders
BeagleBone Black BootloadersBeagleBone Black Bootloaders
BeagleBone Black Bootloaders
SysPlay eLearning Academy for You
 
BeagleBone Black Booting Process
BeagleBone Black Booting ProcessBeagleBone Black Booting Process
BeagleBone Black Booting Process
SysPlay eLearning Academy for You
 
BeagleBoard-xM Bootloaders
BeagleBoard-xM BootloadersBeagleBoard-xM Bootloaders
BeagleBoard-xM Bootloaders
SysPlay eLearning Academy for You
 
BeagleBoard-xM Booting Process
BeagleBoard-xM Booting ProcessBeagleBoard-xM Booting Process
BeagleBoard-xM Booting Process
SysPlay eLearning Academy for You
 
SPI Drivers
SPI DriversSPI Drivers
I2C Drivers
I2C DriversI2C Drivers

More from SysPlay eLearning Academy for You (15)

Kernel Timing Management
Kernel Timing ManagementKernel Timing Management
Kernel Timing Management
 
Understanding the BBB
Understanding the BBBUnderstanding the BBB
Understanding the BBB
 
Linux DMA Engine
Linux DMA EngineLinux DMA Engine
Linux DMA Engine
 
Cache Management
Cache ManagementCache Management
Cache Management
 
BeagleBone Black Bootloaders
BeagleBone Black BootloadersBeagleBone Black Bootloaders
BeagleBone Black Bootloaders
 
Introduction to BeagleBone Black
Introduction to BeagleBone BlackIntroduction to BeagleBone Black
Introduction to BeagleBone Black
 
Introduction to BeagleBoard-xM
Introduction to BeagleBoard-xMIntroduction to BeagleBoard-xM
Introduction to BeagleBoard-xM
 
Platform Drivers
Platform DriversPlatform Drivers
Platform Drivers
 
BeagleBone Black Bootloaders
BeagleBone Black BootloadersBeagleBone Black Bootloaders
BeagleBone Black Bootloaders
 
BeagleBone Black Booting Process
BeagleBone Black Booting ProcessBeagleBone Black Booting Process
BeagleBone Black Booting Process
 
BeagleBoard-xM Bootloaders
BeagleBoard-xM BootloadersBeagleBoard-xM Bootloaders
BeagleBoard-xM Bootloaders
 
BeagleBoard-xM Booting Process
BeagleBoard-xM Booting ProcessBeagleBoard-xM Booting Process
BeagleBoard-xM Booting Process
 
Serial Drivers
Serial DriversSerial Drivers
Serial Drivers
 
SPI Drivers
SPI DriversSPI Drivers
SPI Drivers
 
I2C Drivers
I2C DriversI2C Drivers
I2C Drivers
 

Recently uploaded

GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
Guy Korland
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdfSAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
Peter Spielvogel
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
sonjaschweigert1
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
Pierluigi Pugliese
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
nkrafacyberclub
 
Quantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIsQuantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIs
Vlad Stirbu
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
Elena Simperl
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
Alison B. Lowndes
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
Product School
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Paige Cruz
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Product School
 
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
UiPathCommunity
 

Recently uploaded (20)

GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdfSAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
 
Quantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIsQuantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIs
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilot™
 

Linux Internals Part - 2

  • 1. © 2012 SysPlay eLearning Academy for You <https://sysplay.in> All Rights Reserved. Linux Internals (Day 2)
  • 2. 2 © 2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. What to Expect? GNU OS & GNU Development Tools Compiler, Assembler and linker Writing Large Programs Writing Makefiles Linux Process & Process Control Programs on Process control
  • 3. © 2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. GNU Operating System A Unix-like operating system that is free software – respects your freedom GNU is a recursive acronym for “GNU's Not Unix” because GNU's design is Unix like, but differs by being free software and containing no Unix Code. A software collection of application, libraries and developer tools. And a program to allocate resources and talk to hardware, known as Kernel Used with the Kernel called Linux Together known as GNU/Linux Operating System
  • 4. © 2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. GNU Tools Contains a rich set of software packages ranging from Archiving, Audio, Business Productivity to Health, Hobbies and so on Archiving – cpio, gzip, tar... Audio – EMMS, Gmediaserver Database – Ferret, Gdbm, Guil dbi.. Games – Acm, Aetherspace And the list continues...
  • 5. © 2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. GNU Development Tools The GNU development tools are collection of Compiler, Assembler, Linker, Libraries, Makefile, debugger and so on. GCC – GNU Compiler Collection Binutils – Linker(ld), Assembler (gas) and so on GNU C Library – libc Debugger – GDB, DDD
  • 6. © 2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. GNU Compiler Collection (GCC) Most widely used compiler A full featured ANSI C compiler with support for C, C++, java and Fortran Contains the libraries for these languages as well. Historically, known as 'GNU C Compiler'
  • 7. 7 © 2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. W's of Building a Program A Process to translate a program from a Higher Level Language to an executable Four stages for a C Program to become an executable Pre-processing Compiling Assembling Linking
  • 8. 8 © 2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Stages for Building a Program
  • 9. 9 © 2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Pre-Processor Accepts Source Code as input and is responsible for Removing Comments Interpreting special pre-processor directives denoted by # #include – includes contents of named file #include <stdio.h> #define – defines symbolic name or constants #define MAX_SIZE 100 gcc -E hello_world.c -o hello_world.i gives hello_world.i
  • 10. 10 © 2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Compiler, Assembler and Linker Compiler Translates a pre-Processed source code to assembly code gcc -S hello_world.i -o hello_world.s Gives hello_world.s Assembler Translate assembly code to machine code with unresolved symbols gcc -c hello_world.s -o hello_world.o Gives hello_world.o
  • 11. 11 © 2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Assembler, Compiler & Linker... Linker Translates the object code into the executable All the called functions named would be replaced by address of these function. gcc hello_world.o -o hello_world Gives hello_world
  • 12. 12 © 2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Useful gcc Compiler Options -c Suppress the linking process and produce a .o file for each source file listed gcc -c file1.c file2.c ... -llibrary: Link with object libraries which are archived. Example : math library gcc sqrt.c -o sqrt -lm -L<path> - Look into the non standard path for libraries gcc -o bar bar.o -L/home/user/ -lfoo -I<path> - Look into the non standard path for headers Gcc -c -I/home/user foo.c -g : invoke debugging option -D: define symbols either as identifies or as values
  • 13. © 2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. GNU Binutils The GNU binutils are a collection of binary tools. Most important as – GNU assembler ld – GNU linker Also, includes Addr2line, ar, gprof, nlmconv, nm, objcopy, objdump, ranlib, readelf, size, strings, strip
  • 14. © 2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Getting the Listing with Assembler Use the '-Wa' option in gcc to pass arguments to the assembler The arguments must be separated from each other by commas. Eg: gcc -c -g -Wa,-alh,-L file.c -alh – emit the listing to standard output with high level and assembly source -L - Retain local symbols in symbol table
  • 15. © 2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Exploring GCC Linking Process gcc -c main.c func.c Give main.o and func.o gcc func.o main.o -o main ldd main Prints the shared libraries needed by program Eg for printf, Code Relocations Are the entries within a binary that are left to be filled at the link time or run time. Use objdump and readelf
  • 16. © 2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. nm command Provides the information on the symbols being used in the an object file or executable file Provides Virtual address of the Symbol A character to depict the symbol type Lower case – Symbol is local Upper case – Symbol is global Name of the symbol nm <object file or executable name> nm -A ./*.o - display the symbol with function which contains it nm -u <executable> – displays all the unresolved symbols in a executable file
  • 17. © 2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Strings Command Finds and displays the printable strings in given executable, binary or object file strings <filename>
  • 18. © 2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Building Libraries Collection of object files can be used to form a library Use ar command ar cru libfoo.a foo1.o foo2.o foo3.o Will create a file libfoo.a from foo1.o foo2.o foo3.o Ranlib libfoo.a Generates and adds the symbol table to .a file Gcc main.o libfoo.a
  • 19. © 2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Writing a Larger Programs Divide the programs into modules Separate source file. Main() in main.c and others will contain the functions Can Create our own library of functions. Can be shared amongst many programs Advantages The modules will divide into common group of functions We can link each module separately and link in compiled modules
  • 20. © 2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Header Files Each module needs to have a variable definition, function definitions etc with itself. Several modules need to share such some declarations. Centralize the PreProcessor commands function prototypes in a file called header with an extension .h. #include <stdio.h> - Standard Header file #inlcude “my_head.h”
  • 21. © 2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Sharing the Variables Pass the variable as Paramters Passing long variable list to many functions can be laborious. Large arrays and structures are difficult to store locally - Memory problems with stack. External Variable and functions Defined outside of functions Potentially available to the whole program We have global variable – AnotherString declared in main.c and shared with WriteMyString.
  • 22. © 2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Advantages of Using Several Files Each programmer works on different file. An Object orientation style can be used. Files can contain all functions from a related group. Code re-usability for well implemented objects or functions. When changes are made to a file, only that file needs to be recompiled to rebuild the program
  • 23. © 2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Makefile & make Utility make is a utility for automatically building large applications Files specifying the instructions for make are Makefile in the directory which it was invoked Make is an expert system that tracks which files have changed since the last time the project was build and invokes the compiler on only those files and their dependency
  • 24. © 2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Why make? A software project which consists of many source codes, can have complex and long compiler commands. Using make, it can be reduced. Programming project sometimes need specialized compiler options that are so rarely used they are hard to remember; with make this can be reduced. Maintaining a consistent development environment. Automating the build process, because make can be called easily from a shell script.
  • 25. © 2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Makefile Components Makefile is a set of rules. Each rule has the form TARGET: DEPENDENCIES <TAB> COMMAND <TAB> COMMAND The <TABS> are Mandatory Target Either a name of the file that is generated by the program or the name of the action to carry out Object files and executable files are example of files that are generated by other programs. Cleaning up the object files is an example of an action that we might need to carry
  • 26. © 2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Make File Components... Dependency A name of the file that is used to create the target More than one dependencies must be separated out by spaces Commands Must be prepended with <TAB> If the target is the file, commands explains how to create that file If the target is an action, then the commands describe the action
  • 27. © 2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. How it works? Works on the Timestamps Dependency is up-to-date, if and only if the target's latest updates happened after the dependency's latest update. Dependency is changed, if and only if the target's latest update happened before the dependency' s latest update Makefile takes the action only if Dependency is changed. Starts from the first target & chain proceeds and all the other targets are ignored. Type make <target> for specific target Let's try a simple Makefile
  • 28. © 2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Building a C Program a.o : a.c gcc -c a.c -o a.o clean: rm -rf *.o
  • 29. 29 © 2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. What is Process? Running instance of program Program in execution. Executable Program Loaded -> Process Two instances of same program -> ? Processes
  • 30. 30 © 2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Program Vs Process
  • 31. 31 © 2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Why we need a process? Improve the system performance Multitasking In turn needs Time sharing (on same processor) Scheduling Priority And for all these: Process Identifier (PID)
  • 32. 32 © 2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Let's view Shell Local process: ps Console attached System processes: ps a All system processes: ps ax List Many other details: Add l Observe uid, ppid, priority, nice, status, tty, time Dynamic process status : top Try pid.c
  • 33. 33 © 2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Linux Process Scheduling Based on the time-sharing technique Several processes are allowed to run concurrently CPU time is divided into “slices”, one for each runnable process. Preemption Based on various task priorities Specified by its scheduling policies
  • 34. 34 © 2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Types of Processes IO Bound Spend much of the time waiting on I/O Runs for only short duration CPU Bound Spend much of the time executing code Tend to run until they are preempted. Linux Implicitly favours I/O bound processes over CPU bound processes
  • 35. 35 © 2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Process Context Switch
  • 36. 36 © 2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Generic Process State Diagram
  • 37. 37 © 2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Linux Process States The possible states are: TASK_RUNNING (R) – ready or running TASK_INTERRUPTIBLE (S) – blocked (waiting for an event) TASK_UNINTERRUPTIBLE (D) – blocked (usually for IO) TASK_ZOMBIE (Z) – terminated, but not cleaned by its parent TASK_STOPPED (T) : execution stopped Mutually exclusive Additional Information: Foreground (+), Threaded (I)
  • 38. 38 © 2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Process Management in Linux Information associated with each process Address Space (Code, Variables, stack, heap) Processor state (PC, Registers,..) CPU registers Scheduling info, priority Memory Management information Stored in a structure of type 'task_struct' Maintained by Linux Kernel for each process Also, called the Process Descriptor/ Process Control Block
  • 39. 39 © 2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Basic Process Management bg – Starts a suspended process in the background fg = Starts a suspended process in the foreground jobs – Lists the jobs running pidof – Find the Process ID of a running process top – Display the processes that are using the most cpu resources
  • 40. 40 © 2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Process Creation From Shell By running a command / Program / Script By 'exec' ing a Command / Program By Programming Using System() Simple, but inefficient Using fork and exec family function Comparatively complex Greater flexibility, speed and security
  • 41. 41 © 2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Using the System Function Execution Flow Creates a sub-process running the standard shell Hands the command to that shell for execution Used to execute the command from within a program Subjected to the features and limitations of the system shell Try this example (system.c)
  • 42. 42 © 2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Using fork() Creates a child process that is an copy of its parent
  • 43. 43 © 2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Distinguishing Parent & Child Child has a distinct PID from that of parent The fork provides a different return values to parent and child. Return Values PID of the child in Parent process 0 in the Child process
  • 44. 44 © 2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Using Exec function An exec family of functions replace the current program by new one Before exec(), PID program code Program a.c program counter r e g i s t e r stack data section
  • 45. 45 © 2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Using exec function... program code ProgramA.c PID program counter stack data section r e g i s t e r heap Preserved Reset Overwritten by the new program Change program code
  • 46. 46 © 2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Exec Family of Functions execve() execv() execvp() execlp() execle() execl()
  • 47. 47 © 2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Cons of fork & exec Fork Forked child process typically executes the copy of parent process' program Can't execute the program external to current executable Exec Doesn't creates a new process Calling program ceases to execute
  • 48. 48 © 2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. fork complementing exec New Copy of Parent Initial process Fork Original process Continues new_Program (replacement) execv(new_program) fork() returns pid=0 and runs as a cloned parent until execv is called Returns a new PID
  • 49. 49 © 2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Copy On Write (COW) Parent and Child shares the Address space (ro) Data & other resources are marked COW If written to, a duplicate is make and each process receives a unique copy Consequently, the duplication of resources occurs only when they are written to Avoids copy in case of immediate exec fork()'s only overheads Duplication of parent's page table Creation of unique PCB for child
  • 50. 50 © 2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Process Termination Parent and Child process terminate as usual Success Exit – Normal Success Error Exit – Normal failure Fatal Exit – Signaled from Kernel space for a bug Kill Exit – Signaled by process But which one of them terminates first? If it matters, parent can wait for their children using wait family of system calls.
  • 51. 51 © 2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Wait family of system calls Four different system calls in the wait family wait() : Block until one of the child process terminates waitpid() : Wait for a specific child to exit/stop/resume. wait3() : Along with, return the resource usage info about exiting/stopping/resuming child process. Wait4() : wait3() for specific child or parent. All of these fill a status code In an integer pointer argument About how the child process exited Which can decoded using...
  • 52. 52 © 2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. wait status macros WEXITSTATUS : Extracts the child process exit code WIFEXITED: Determine whether child process exited normally or died from unhandled signal WIFSIGNALED : Determines if the child process was signaled WTERMSIG : Extracts from the process' exit status, the signal by which it died Other WIFSTOPPED, WCOREDUMP, WSTOPSIG, WIFCONTINUED
  • 53. 53 © 2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Zombie Process A process that is terminated but the resources are not yet cleaned up is called zombie process Reason The child process exited before the parent The parent didn't call the wait Even if a parent does a wait later, it remains a Zombie till then.
  • 54. 54 © 2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Who cleans up a Zombie? Typically, again a Parent By doing a wait on it What if the parent exits without “wait”? Does it stay around in the system? Not really. It gets inherited by init which then cleans it up right there
  • 55. 55 © 2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Orphan Process If the parent dies before the children Child become orphan And are adopted by init which then does the clean up on their exit
  • 56. 56 © 2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. What all we have learnt? GNU OS & GNU Development Tools Compiler, Assembler and linker Writing Large Programs Writing Makefiles Linux Process & Process Control Programs on Process control
  • 57. 57 © 2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Any Queries?