SlideShare una empresa de Scribd logo
1 de 52
© 2012 SysPlay eLearning Academy for You <https://sysplay.in>
All Rights Reserved.
Linux Internals (Day 3)
2
©2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
What to Expect?
Debugging with Gdb
Building Statically Linked Modules
Compiling and Building Complex application
using make
Inter Process Communication Mechanisms
©2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Debugging With GDB
©2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Ways of Debugging
Printing
The most basic type of debugging
Using GUI
Various IDEs provide this facility
ddd in Linux, VDD
Text mode debugger
gdb
©2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Debugger: gdb
Text mode debugger
Repetition of previous command
Very powerful
All kind of options: Breakpoint, Watch, ...
Extensive Help
GUI interfaces: ddd, ...
©2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Gdb: Debugging Strategies
Oh no!!! Another tool to learn
Worth learning a new tool
Segmentation faults & Core dumps
Execution intercepted by gdb
Allows examining the state, backtrace, etc
Debugging is Narrowing Down
Reaching close & then stepping
7
©2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
'gdb' Usage
Compile with -g option
gcc -g file.c -o file
Run gdb (with desired options)
gdb [options] ./file [core file]
Pass command line arguments by setting args
set args <cmd_line_args>
Run the program by
Typing c, or
run <cmd_line_args>
For debugging: break, watch, backtrace, …
For help: help <command>
8
©2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
gdb: Other ways of Running
step [ count ] – step into sub-routines
next – run over sub-routines in a go
finish – run till current function returns
return – make selected stack frame return to its
caller
jump <address> – continue program at specified
line or address
9
©2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
gdb: Breakpoints
info break – Show list & status of breakpoints
Setting breakpoints (returns its number)
[t]break <function> [ if <expr> ]
[t]break [<file>:]<line>
Breakpoint Operations
disable <break_no>
enable <break_no>
delete <break_no>
commands <break_no>
Execute gdb commands on reaching <break_no>
10
©2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
gdb: Examining Data
print[/xduotcf] <expr>
Evaluate the valid C <expr> within current frame
set <var> = <expr>
Assign <expr> to <var> in the current scope
Variables starting with $ are local to gdb
display <expr>
Print <expr> whenever program stops
undisplay
Cancels all previous display requests
11
©2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
gdb: Shortcuts
Just enough of command
To make it unique
Added with tab completion
First letter only for
break, delete, run, continue, step, next, print
Repeat last by <Enter>
Executing the same set of initial commands
Put them in .gdbinit
-x <gdb command file>
12
©2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
gdb: Miscellaneous
Set editmode for gdb command line
editmode [ emacs | vi | dumb ]
Execute a shell command
shell <cmd>
Print command history
history
Set logging
set logging <on | off>
set logging file <log_file> (Default: gdb.txt)
13
©2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Statically Linked Modules
Static Modules are simply a collection of ordinary object files
Linked with and becomes the part of the application
Conventionally ends with “.a” suffix
Advantages
Simplifies the reusability
Allows the application vendors a way to simply release an API
14
©2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Building Statically Linked Modules
Compile and get the object codes
gcc -c <file1.o> <file2.o> <file3.o> ...
Create the static library using ar utility
ar rcu libmylib <file1.o> <file2.o> <file3.o> ...
Compile a C program to be linked with Libraries
gcc <filename.c> -L<path> -lmylib -o <executable>
15
©2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Makefile Revisited
Special Variables
$@ - Target
$^ - Dependency
%.o – All the .o files in the directory
%.c – All the .c files in the Directory
%.o: %.c
gcc -c $^ -o $@
Normal Variables
Conventionally declared in Capitals
Functions
Written in Brackets
SRCS:= $(wildcard *.c)
16
©2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Makefile Revisited...
Standard Variables
CC - c
RM – rm -rf
Flags
CFLAGS, LDFLAGS, ASF, ARF
Implicit Commands
%.o: %.c
cc -c $^ -o $@
make looks for Makefiles in this sequence
GNUMakefile, makefile, Makefile
Non Standard Makefile using -f option
©2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Inter Process Communication
18
©2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
W's of IPC
Mechanism by which one process communicate
i.e. exchange the data with another process.
Require Special IPC mechanism because
Processes are non-sharing entities
They need to coordinate
They need to exchange data
Separate mechanisms are answers to this
19
©2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Example of IPC
Print the filenames in a directory
'ls' outputs the filenames
'lpr' prints its input
But ls & lpr are separate processes
What are the solutions?
Put into a file -> ls > file; lpr < file
Set up an IPC, say a pipe (I) -> ls | lpr
20
©2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Types of IPC mechanisms
Pipe: Allows the flow of data in one direction only
Name Pipe: Pipe with a specific name
Shared Memory & Semaphore
Allows a interchange of data through a defined area of Memory
Semaphore is use for synchronization
Mapped Memory: Similar to shared memory, but use file
instead of memory
Why so many?
Based on requirements, flexibility & benefits
©2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Pipes
22
©2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
W's of Pipe
Two ends – Read & Write to allow the transfer
Unidirectional and serial communication
Limited Capacity
Provides automatic synchronization
Read blocks on no data
Write blocks on full data
23
©2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Pipe Usage
On shell
Using |
In Programming / C
Creation
Using system call pipe()
Communicate as with other fd
Using the system calls read(), write(), ...
24
©2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Creating a Pipe
Invoke the pipe() system call
Supply an integer array of 2
The call to a pipe stores the reading file
descriptor at array index 0 and writing file
descriptor at index 1.
Let's try pipe.c
25
©2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Limitations of Pipe
pipe() call must precede a call to fork()
Pipes are only for related processes
That is processes with related ancestory
What about unrelated processes?
There are special types of pipes called...
©2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
FIFO
27
©2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
W's of FIFO
A Pipe with a name in the Filesystem
Also called the Named Pipe
Properties identical to those of pipe
including the serial communication
which is now termed as First-In-First-Out
Except that now the processes can be unrelated
28
©2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
FIFO Usage
On Shell
Creation: mknod, or mkfifo
Communication: cat, echo..
In programming / C
Creation
System Call: mknod(), open(), close()
Library function: mkfifo(), fopen(), fopen(), fclose()
Communication
System calls : read, write,...
Library Functions: fread(), fwrite(), fputs()
29
©2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Experiments on Shell
Mkfifo /tmp/fifo Open another shell
ls -l /tmp/fifo cat > /tmp/fifo
cat < /temp/fifo type some text
<See the text> rm /tmp/fifo
30
©2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Using System call to create FIFO
mknod(const char *filename, mode_t mode, dev_t dev)
filename – name for the FIFO
Mode
Permission to use and type of node to be created
The node type must be S_IFIFO for FIFO
dev_t
specifies the major and minor number of device special file
Not used for FIFO, so make it 0.
Let's try an example
©2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Shared Memory
32
©2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
W's of Shared Memory
Rawest communication mechanism
Piece of Memory which is shared
Between two or more processes
Fastest form of IPC
As no unnecessary copying of data
33
©2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Linux Memory Model
All accesses in Linux are through virtual addresses
Each Process has its “own” virtual address space
which is split into Virtual Pages
Each Process maintains a mapping table
From its Physical pages to these virtual pages
For access to its actual data
And mappings of multiple processes can point to the same
physical page
Enabling the sharing of “actual” memory
34
©2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Shared Memory Usage
Shell Way
Create: ipcmk -M <size> [-p <mode> ]
Get the information: ipcs [-m [ -i -id]]
Remove: ipcrm [-M <shmkey> | -m <shmid>]
Programming way in Linux
Physical Memory Allocation: shmget()
Mapping / Attaching to the Shared Memory: shmat()
Access through usual (Virtual) Address Dereferencing
Unmapping / Detaching from Virtual Memory – shmdt()
Physical Memory Deallocation & other shared memory Control Operations:
Shmctl()
35
©2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Shared Memory Allocation
Done by only one of the sharing process
int shmget(key_t key, size_t size, int shm_flg);
Returns
Shared Memory Id on success
-1 on error
key: System-wide unique id for a shared Memory
Process gets a particular shared memory id using creation key
Size: Number of bytes to allocate or get for the shared memory
shm_flg: operation | permissions
IPC_CREAT, IPC_EXCL
36
©2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Shared Memory Attachment
Done by all the sharing Processes to access the Shared Memory
void *shmat(int shm_id, const void *shm_addr, int shm_flg);
Returns
Virtual start address of the attached shared memory on success
MAP_FAILED i.e (void *)(-1) on error (and sets errno)
shm_id: Shared Memory Id returned by shmget()
shm_addr
Requested process vitual start address
NULL enables the Kernel to choose one
shm_flg
SHM_RND: round down shm_addr to a multiple of the page size
SHM_RDONLY: shared memory will only be read, not written
Children created by fork() inherit attached shared segments
37
©2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Shared Memory Detachment
Done by all the process which did attach or inherit
int shmdt(const void *shm_addr)
Returns : 0 on success; -1 on error (and sets errno)
shm_addr: Process Virtual address map returned by shmat
Call to _exit() or exec*() automatically detaches
Detachment may deallocates
If done by last process
And already marked to deallocate
38
©2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Shared Memory Control Ops
Control Operations on any Shared memory like
Physical Memory Deallocation for Shared Memory
Any other
int shmctl(int shm_id, int cmd, struct shmid_ds *buf)
Retuns 0 on success, -1 on error (and set errno)
shm_id: Shared Memory id returned by shmget()
Cmd
IPC_STAT - Get the shared memory information into buf
IPC_SET - Set the various shared memory info specified by buf
IPC_RMID – (Mark) Deallocate the physical memory (buf is ignored)
39
©2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Need for Synchronization
Shared memory has no Access Coordination
It has to be handled by us – the User
Moreover, for communication among the
unrelated processes, you need a synchronization
mechanism
So, now its a time to look into resolving it
That's where we go to the next topic....
©2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Process Semaphores
41
©2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
W's of Process Semaphores
First, it isn't any form of IPC
But a mechanism for synchronization used by IPCs
Counter used to provide the synchronized access
To a shared data object for multiple processes
Variable protected by kernel
No direct access by the user
Counter
> 0 -> Resource is available
== 0 -> Resource is busy / in use
< 0 -> Should never happen
42
©2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Semaphore Types
Counting Semaphores
Semaphores controlling a multiple resources availability
Possible Values
>= 1 -> Available
0 -> Unavailable
Binary Semaphores
Semaphores controlling a single resource availability
Possible Values
1 -> Available
0 – Unavailable
Typically initialized to 0
43
©2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Process Semaphore Usage
Shell Way
Create : ipcmk -S <nsems> [-p <mode>]
Get information: ipcs [-s [-i id]]
Remove: ipcrm [-S <semkey> | -s <semid>]
Programming way in C (using sys calls)
Semaphore Allocation/Access: semget()
Semaphore Deallocation & other Semaphore control operations:
semctl()
Semaphore usage Operations: semop()
44
©2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Process Semaphore Allocation
Done by only one of the sharing process
int semget(key_t key, int nsems, int sem_flg);
Returns
Semaphore array Id on success
-1 on error (and sets errno)
key: System-wide unique id for a semaphore array
Process gets a particular semaphore array id using creation key
nsems: Number of semaphores to create or access in the Array
sem_flg: operation | permissions
IPC_CREAT, IPC_EXCL
45
©2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Semaphore Control Ops
Control operations on semaphore like
Deleting a Semaphore Array
And many more
int semctl(int sem_id, int sem_num, int cmd, ...)
Returns 0 on success, -1 on error (set errno)
sem_id: Semaphore array id returned by semget
sem_num: Index of the semaphore in an arry
Cmd
IPC_STAT - Get the semaphore information array into optional arg (sem_num is ignored)
IPC_SET – Set the various semaphore array info specified by optional arg (sem_num is ignored)
IPC_RMID – Remove the semaphore array (sem_num is ignored)
SETVAL – Set sem_num'th semaphore value as specified by optional arg
...
46
©2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
The Optional Arg
Union semun
{
int val; //Value for set val
struct semid_ds *buf; //Buffer for IPC_STAT & IPC_SET
unsigned short *array; //Array for GETALL, SETALL
....
}
struct semid_ds
{
struct ipc_perm sem_perm; //Ownership & Permissions
time_t sem_otime; // Time of last semop
time_t sem_ctime; //Time of last change
unsigned short sem_nsems; //No. Of semaphores in the array
}
47
©2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Caution On
Deleting Process Semaphores
Invoking _exit() or exec* doesn't do it
Needs explicit removal
Using IPC_RMID cmd of semctl
Otherwise, may violate the system limit
Of total number of process semaphores
48
©2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Initializing the Process Semaphores
Two ways though both using semctl
Initialize all in the array
cmd = SETALL
arg.array = Array of values to be set
Initializing one at a time
sem_num = Semaphore number to be initialized
cmd = SETVAL
arg.val = Value to be set
49
©2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Semaphore Usage Ops
Usage operations on the Semaphore like
Increment, Decrement, ..
Int semop(sem_id, struct sembuf *sops, unsigned int
nsops)
Returns: 0 on success; -1 on error (and sets errno)
semid: Semaphore array id returned by sem_get
sops: Array of operations to be performed
Nsops: Number of elements n sops
50
©2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Operation Structure Decoded
struct sembuf
{
unsigned short sem_num; // Index of semaphore to be operated on
short sem_op; //Value to added to the current semaphore
short sem_flg; //Operation flags
}
Operation Specifies
sem_op > 0 -> value added & Processes waiting on it are signalled
sem_op < 0 and current semaphore value >= |sem_op| -> value added
sem_op < 0 and current semaphore value < | sem_op|
It would block/wait until the semaphore value becomes >= |sem_op|
Operation Flags
IPC_NOWAIT – Prevents the operation from blocking
Return -1 with EAGAIN in the above blocking case
Kernel automatically undos the semaphore operations on the process exit
51
©2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
What all have we learnt?
Debugging with Gdb
Building Statically Linked Modules
Compiling and Building Complex application
using make
Inter Process Communication Mechanisms
52
©2012 Pradeep Tewani <pradeep@sysplay.in>
All Rights Reserved.
Any Queries?

Más contenido relacionado

La actualidad más candente (20)

Video Drivers
Video DriversVideo Drivers
Video Drivers
 
Kernel Debugging & Profiling
Kernel Debugging & ProfilingKernel Debugging & Profiling
Kernel Debugging & Profiling
 
Linux Network Management
Linux Network ManagementLinux Network Management
Linux Network Management
 
Toolchain
ToolchainToolchain
Toolchain
 
Shell Scripting
Shell ScriptingShell Scripting
Shell Scripting
 
Linux Porting
Linux PortingLinux Porting
Linux Porting
 
BeagleBone Black Bootloaders
BeagleBone Black BootloadersBeagleBone Black Bootloaders
BeagleBone Black Bootloaders
 
BeagleBoard-xM Booting Process
BeagleBoard-xM Booting ProcessBeagleBoard-xM Booting Process
BeagleBoard-xM Booting Process
 
Processes
ProcessesProcesses
Processes
 
BeagleBone Black Booting Process
BeagleBone Black Booting ProcessBeagleBone Black Booting Process
BeagleBone Black Booting Process
 
I2C Drivers
I2C DriversI2C Drivers
I2C Drivers
 
Introduction to Linux Drivers
Introduction to Linux DriversIntroduction to Linux Drivers
Introduction to Linux Drivers
 
Synchronization
SynchronizationSynchronization
Synchronization
 
Bootloaders
BootloadersBootloaders
Bootloaders
 
Introduction to Linux
Introduction to LinuxIntroduction to Linux
Introduction to Linux
 
USB Drivers
USB DriversUSB Drivers
USB Drivers
 
BeagleBone Black Bootloaders
BeagleBone Black BootloadersBeagleBone Black Bootloaders
BeagleBone Black Bootloaders
 
Block Drivers
Block DriversBlock Drivers
Block Drivers
 
Character Drivers
Character DriversCharacter Drivers
Character Drivers
 
Linux Kernel Overview
Linux Kernel OverviewLinux Kernel Overview
Linux Kernel Overview
 

Similar a Linux Internals Part - 3

Better Code: Concurrency
Better Code: ConcurrencyBetter Code: Concurrency
Better Code: ConcurrencyPlatonov Sergey
 
How to hack Citrix (So, You Just Inherited Someone Else's Citrix Environment....
How to hack Citrix (So, You Just Inherited Someone Else's Citrix Environment....How to hack Citrix (So, You Just Inherited Someone Else's Citrix Environment....
How to hack Citrix (So, You Just Inherited Someone Else's Citrix Environment....Denis Gundarev
 
ColdFusion 11 Overview - CFSummit 2013
ColdFusion 11 Overview - CFSummit 2013ColdFusion 11 Overview - CFSummit 2013
ColdFusion 11 Overview - CFSummit 2013Rupesh Kumar
 
Windows Server 2012 R2 Jump Start - Intro
Windows Server 2012 R2 Jump Start - IntroWindows Server 2012 R2 Jump Start - Intro
Windows Server 2012 R2 Jump Start - IntroPaulo Freitas
 
DDD (Debugger Driven Development)
DDD (Debugger Driven Development)DDD (Debugger Driven Development)
DDD (Debugger Driven Development)Carlos Granados
 
Pursue the Attackers – Identify and Investigate Lateral Movement Based on Beh...
Pursue the Attackers – Identify and Investigate Lateral Movement Based on Beh...Pursue the Attackers – Identify and Investigate Lateral Movement Based on Beh...
Pursue the Attackers – Identify and Investigate Lateral Movement Based on Beh...CODE BLUE
 
Automating Active Directory mgmt in PowerShell
Automating Active Directory mgmt in PowerShellAutomating Active Directory mgmt in PowerShell
Automating Active Directory mgmt in PowerShellConcentrated Technology
 
LiveDB Introduction at devsum 2011
LiveDB Introduction at devsum 2011LiveDB Introduction at devsum 2011
LiveDB Introduction at devsum 2011Robert Friberg
 
ZFConf 2012: Zend Framework 2, a quick start (Enrico Zimuel)
ZFConf 2012: Zend Framework 2, a quick start (Enrico Zimuel)ZFConf 2012: Zend Framework 2, a quick start (Enrico Zimuel)
ZFConf 2012: Zend Framework 2, a quick start (Enrico Zimuel)ZFConf Conference
 
Zend Framework 2 quick start
Zend Framework 2 quick startZend Framework 2 quick start
Zend Framework 2 quick startEnrico Zimuel
 
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 2Enrico Zimuel
 
Securing Containers - Sathyajit Bhat - Adobe
Securing Containers - Sathyajit Bhat - AdobeSecuring Containers - Sathyajit Bhat - Adobe
Securing Containers - Sathyajit Bhat - AdobeCodeOps Technologies LLP
 
EMC Documentum - xCP 2.x Troubleshooting
EMC Documentum - xCP 2.x TroubleshootingEMC Documentum - xCP 2.x Troubleshooting
EMC Documentum - xCP 2.x TroubleshootingHaytham Ghandour
 

Similar a Linux Internals Part - 3 (20)

Better Code: Concurrency
Better Code: ConcurrencyBetter Code: Concurrency
Better Code: Concurrency
 
Processes
ProcessesProcesses
Processes
 
Inter Process Communication
Inter Process CommunicationInter Process Communication
Inter Process Communication
 
How to hack Citrix (So, You Just Inherited Someone Else's Citrix Environment....
How to hack Citrix (So, You Just Inherited Someone Else's Citrix Environment....How to hack Citrix (So, You Just Inherited Someone Else's Citrix Environment....
How to hack Citrix (So, You Just Inherited Someone Else's Citrix Environment....
 
ColdFusion 11 Overview - CFSummit 2013
ColdFusion 11 Overview - CFSummit 2013ColdFusion 11 Overview - CFSummit 2013
ColdFusion 11 Overview - CFSummit 2013
 
Windows Server 2012 R2 Jump Start - Intro
Windows Server 2012 R2 Jump Start - IntroWindows Server 2012 R2 Jump Start - Intro
Windows Server 2012 R2 Jump Start - Intro
 
AdminKit
AdminKitAdminKit
AdminKit
 
DDD (Debugger Driven Development)
DDD (Debugger Driven Development)DDD (Debugger Driven Development)
DDD (Debugger Driven Development)
 
Pursue the Attackers – Identify and Investigate Lateral Movement Based on Beh...
Pursue the Attackers – Identify and Investigate Lateral Movement Based on Beh...Pursue the Attackers – Identify and Investigate Lateral Movement Based on Beh...
Pursue the Attackers – Identify and Investigate Lateral Movement Based on Beh...
 
Automating Active Directory mgmt in PowerShell
Automating Active Directory mgmt in PowerShellAutomating Active Directory mgmt in PowerShell
Automating Active Directory mgmt in PowerShell
 
LiveDB Introduction at devsum 2011
LiveDB Introduction at devsum 2011LiveDB Introduction at devsum 2011
LiveDB Introduction at devsum 2011
 
ZFConf 2012: Zend Framework 2, a quick start (Enrico Zimuel)
ZFConf 2012: Zend Framework 2, a quick start (Enrico Zimuel)ZFConf 2012: Zend Framework 2, a quick start (Enrico Zimuel)
ZFConf 2012: Zend Framework 2, a quick start (Enrico Zimuel)
 
Zend Framework 2 quick start
Zend Framework 2 quick startZend Framework 2 quick start
Zend Framework 2 quick start
 
Linux on System z debugging with Valgrind
Linux on System z debugging with ValgrindLinux on System z debugging with Valgrind
Linux on System z debugging with Valgrind
 
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
 
Flink internals web
Flink internals web Flink internals web
Flink internals web
 
Fall of a domain | From local admin to Domain user hashes
Fall of a domain | From local admin to Domain user hashesFall of a domain | From local admin to Domain user hashes
Fall of a domain | From local admin to Domain user hashes
 
Linux scheduler
Linux schedulerLinux scheduler
Linux scheduler
 
Securing Containers - Sathyajit Bhat - Adobe
Securing Containers - Sathyajit Bhat - AdobeSecuring Containers - Sathyajit Bhat - Adobe
Securing Containers - Sathyajit Bhat - Adobe
 
EMC Documentum - xCP 2.x Troubleshooting
EMC Documentum - xCP 2.x TroubleshootingEMC Documentum - xCP 2.x Troubleshooting
EMC Documentum - xCP 2.x Troubleshooting
 

Más de SysPlay eLearning Academy for You (10)

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
 
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
 
BeagleBoard-xM Bootloaders
BeagleBoard-xM BootloadersBeagleBoard-xM Bootloaders
BeagleBoard-xM Bootloaders
 
Serial Drivers
Serial DriversSerial Drivers
Serial Drivers
 
SPI Drivers
SPI DriversSPI Drivers
SPI Drivers
 

Último

Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 

Último (20)

Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 

Linux Internals Part - 3

  • 1. © 2012 SysPlay eLearning Academy for You <https://sysplay.in> All Rights Reserved. Linux Internals (Day 3)
  • 2. 2 ©2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. What to Expect? Debugging with Gdb Building Statically Linked Modules Compiling and Building Complex application using make Inter Process Communication Mechanisms
  • 3. ©2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Debugging With GDB
  • 4. ©2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Ways of Debugging Printing The most basic type of debugging Using GUI Various IDEs provide this facility ddd in Linux, VDD Text mode debugger gdb
  • 5. ©2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Debugger: gdb Text mode debugger Repetition of previous command Very powerful All kind of options: Breakpoint, Watch, ... Extensive Help GUI interfaces: ddd, ...
  • 6. ©2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Gdb: Debugging Strategies Oh no!!! Another tool to learn Worth learning a new tool Segmentation faults & Core dumps Execution intercepted by gdb Allows examining the state, backtrace, etc Debugging is Narrowing Down Reaching close & then stepping
  • 7. 7 ©2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. 'gdb' Usage Compile with -g option gcc -g file.c -o file Run gdb (with desired options) gdb [options] ./file [core file] Pass command line arguments by setting args set args <cmd_line_args> Run the program by Typing c, or run <cmd_line_args> For debugging: break, watch, backtrace, … For help: help <command>
  • 8. 8 ©2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. gdb: Other ways of Running step [ count ] – step into sub-routines next – run over sub-routines in a go finish – run till current function returns return – make selected stack frame return to its caller jump <address> – continue program at specified line or address
  • 9. 9 ©2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. gdb: Breakpoints info break – Show list & status of breakpoints Setting breakpoints (returns its number) [t]break <function> [ if <expr> ] [t]break [<file>:]<line> Breakpoint Operations disable <break_no> enable <break_no> delete <break_no> commands <break_no> Execute gdb commands on reaching <break_no>
  • 10. 10 ©2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. gdb: Examining Data print[/xduotcf] <expr> Evaluate the valid C <expr> within current frame set <var> = <expr> Assign <expr> to <var> in the current scope Variables starting with $ are local to gdb display <expr> Print <expr> whenever program stops undisplay Cancels all previous display requests
  • 11. 11 ©2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. gdb: Shortcuts Just enough of command To make it unique Added with tab completion First letter only for break, delete, run, continue, step, next, print Repeat last by <Enter> Executing the same set of initial commands Put them in .gdbinit -x <gdb command file>
  • 12. 12 ©2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. gdb: Miscellaneous Set editmode for gdb command line editmode [ emacs | vi | dumb ] Execute a shell command shell <cmd> Print command history history Set logging set logging <on | off> set logging file <log_file> (Default: gdb.txt)
  • 13. 13 ©2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Statically Linked Modules Static Modules are simply a collection of ordinary object files Linked with and becomes the part of the application Conventionally ends with “.a” suffix Advantages Simplifies the reusability Allows the application vendors a way to simply release an API
  • 14. 14 ©2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Building Statically Linked Modules Compile and get the object codes gcc -c <file1.o> <file2.o> <file3.o> ... Create the static library using ar utility ar rcu libmylib <file1.o> <file2.o> <file3.o> ... Compile a C program to be linked with Libraries gcc <filename.c> -L<path> -lmylib -o <executable>
  • 15. 15 ©2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Makefile Revisited Special Variables $@ - Target $^ - Dependency %.o – All the .o files in the directory %.c – All the .c files in the Directory %.o: %.c gcc -c $^ -o $@ Normal Variables Conventionally declared in Capitals Functions Written in Brackets SRCS:= $(wildcard *.c)
  • 16. 16 ©2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Makefile Revisited... Standard Variables CC - c RM – rm -rf Flags CFLAGS, LDFLAGS, ASF, ARF Implicit Commands %.o: %.c cc -c $^ -o $@ make looks for Makefiles in this sequence GNUMakefile, makefile, Makefile Non Standard Makefile using -f option
  • 17. ©2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Inter Process Communication
  • 18. 18 ©2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. W's of IPC Mechanism by which one process communicate i.e. exchange the data with another process. Require Special IPC mechanism because Processes are non-sharing entities They need to coordinate They need to exchange data Separate mechanisms are answers to this
  • 19. 19 ©2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Example of IPC Print the filenames in a directory 'ls' outputs the filenames 'lpr' prints its input But ls & lpr are separate processes What are the solutions? Put into a file -> ls > file; lpr < file Set up an IPC, say a pipe (I) -> ls | lpr
  • 20. 20 ©2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Types of IPC mechanisms Pipe: Allows the flow of data in one direction only Name Pipe: Pipe with a specific name Shared Memory & Semaphore Allows a interchange of data through a defined area of Memory Semaphore is use for synchronization Mapped Memory: Similar to shared memory, but use file instead of memory Why so many? Based on requirements, flexibility & benefits
  • 21. ©2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Pipes
  • 22. 22 ©2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. W's of Pipe Two ends – Read & Write to allow the transfer Unidirectional and serial communication Limited Capacity Provides automatic synchronization Read blocks on no data Write blocks on full data
  • 23. 23 ©2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Pipe Usage On shell Using | In Programming / C Creation Using system call pipe() Communicate as with other fd Using the system calls read(), write(), ...
  • 24. 24 ©2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Creating a Pipe Invoke the pipe() system call Supply an integer array of 2 The call to a pipe stores the reading file descriptor at array index 0 and writing file descriptor at index 1. Let's try pipe.c
  • 25. 25 ©2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Limitations of Pipe pipe() call must precede a call to fork() Pipes are only for related processes That is processes with related ancestory What about unrelated processes? There are special types of pipes called...
  • 26. ©2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. FIFO
  • 27. 27 ©2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. W's of FIFO A Pipe with a name in the Filesystem Also called the Named Pipe Properties identical to those of pipe including the serial communication which is now termed as First-In-First-Out Except that now the processes can be unrelated
  • 28. 28 ©2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. FIFO Usage On Shell Creation: mknod, or mkfifo Communication: cat, echo.. In programming / C Creation System Call: mknod(), open(), close() Library function: mkfifo(), fopen(), fopen(), fclose() Communication System calls : read, write,... Library Functions: fread(), fwrite(), fputs()
  • 29. 29 ©2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Experiments on Shell Mkfifo /tmp/fifo Open another shell ls -l /tmp/fifo cat > /tmp/fifo cat < /temp/fifo type some text <See the text> rm /tmp/fifo
  • 30. 30 ©2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Using System call to create FIFO mknod(const char *filename, mode_t mode, dev_t dev) filename – name for the FIFO Mode Permission to use and type of node to be created The node type must be S_IFIFO for FIFO dev_t specifies the major and minor number of device special file Not used for FIFO, so make it 0. Let's try an example
  • 31. ©2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Shared Memory
  • 32. 32 ©2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. W's of Shared Memory Rawest communication mechanism Piece of Memory which is shared Between two or more processes Fastest form of IPC As no unnecessary copying of data
  • 33. 33 ©2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Linux Memory Model All accesses in Linux are through virtual addresses Each Process has its “own” virtual address space which is split into Virtual Pages Each Process maintains a mapping table From its Physical pages to these virtual pages For access to its actual data And mappings of multiple processes can point to the same physical page Enabling the sharing of “actual” memory
  • 34. 34 ©2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Shared Memory Usage Shell Way Create: ipcmk -M <size> [-p <mode> ] Get the information: ipcs [-m [ -i -id]] Remove: ipcrm [-M <shmkey> | -m <shmid>] Programming way in Linux Physical Memory Allocation: shmget() Mapping / Attaching to the Shared Memory: shmat() Access through usual (Virtual) Address Dereferencing Unmapping / Detaching from Virtual Memory – shmdt() Physical Memory Deallocation & other shared memory Control Operations: Shmctl()
  • 35. 35 ©2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Shared Memory Allocation Done by only one of the sharing process int shmget(key_t key, size_t size, int shm_flg); Returns Shared Memory Id on success -1 on error key: System-wide unique id for a shared Memory Process gets a particular shared memory id using creation key Size: Number of bytes to allocate or get for the shared memory shm_flg: operation | permissions IPC_CREAT, IPC_EXCL
  • 36. 36 ©2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Shared Memory Attachment Done by all the sharing Processes to access the Shared Memory void *shmat(int shm_id, const void *shm_addr, int shm_flg); Returns Virtual start address of the attached shared memory on success MAP_FAILED i.e (void *)(-1) on error (and sets errno) shm_id: Shared Memory Id returned by shmget() shm_addr Requested process vitual start address NULL enables the Kernel to choose one shm_flg SHM_RND: round down shm_addr to a multiple of the page size SHM_RDONLY: shared memory will only be read, not written Children created by fork() inherit attached shared segments
  • 37. 37 ©2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Shared Memory Detachment Done by all the process which did attach or inherit int shmdt(const void *shm_addr) Returns : 0 on success; -1 on error (and sets errno) shm_addr: Process Virtual address map returned by shmat Call to _exit() or exec*() automatically detaches Detachment may deallocates If done by last process And already marked to deallocate
  • 38. 38 ©2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Shared Memory Control Ops Control Operations on any Shared memory like Physical Memory Deallocation for Shared Memory Any other int shmctl(int shm_id, int cmd, struct shmid_ds *buf) Retuns 0 on success, -1 on error (and set errno) shm_id: Shared Memory id returned by shmget() Cmd IPC_STAT - Get the shared memory information into buf IPC_SET - Set the various shared memory info specified by buf IPC_RMID – (Mark) Deallocate the physical memory (buf is ignored)
  • 39. 39 ©2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Need for Synchronization Shared memory has no Access Coordination It has to be handled by us – the User Moreover, for communication among the unrelated processes, you need a synchronization mechanism So, now its a time to look into resolving it That's where we go to the next topic....
  • 40. ©2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Process Semaphores
  • 41. 41 ©2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. W's of Process Semaphores First, it isn't any form of IPC But a mechanism for synchronization used by IPCs Counter used to provide the synchronized access To a shared data object for multiple processes Variable protected by kernel No direct access by the user Counter > 0 -> Resource is available == 0 -> Resource is busy / in use < 0 -> Should never happen
  • 42. 42 ©2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Semaphore Types Counting Semaphores Semaphores controlling a multiple resources availability Possible Values >= 1 -> Available 0 -> Unavailable Binary Semaphores Semaphores controlling a single resource availability Possible Values 1 -> Available 0 – Unavailable Typically initialized to 0
  • 43. 43 ©2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Process Semaphore Usage Shell Way Create : ipcmk -S <nsems> [-p <mode>] Get information: ipcs [-s [-i id]] Remove: ipcrm [-S <semkey> | -s <semid>] Programming way in C (using sys calls) Semaphore Allocation/Access: semget() Semaphore Deallocation & other Semaphore control operations: semctl() Semaphore usage Operations: semop()
  • 44. 44 ©2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Process Semaphore Allocation Done by only one of the sharing process int semget(key_t key, int nsems, int sem_flg); Returns Semaphore array Id on success -1 on error (and sets errno) key: System-wide unique id for a semaphore array Process gets a particular semaphore array id using creation key nsems: Number of semaphores to create or access in the Array sem_flg: operation | permissions IPC_CREAT, IPC_EXCL
  • 45. 45 ©2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Semaphore Control Ops Control operations on semaphore like Deleting a Semaphore Array And many more int semctl(int sem_id, int sem_num, int cmd, ...) Returns 0 on success, -1 on error (set errno) sem_id: Semaphore array id returned by semget sem_num: Index of the semaphore in an arry Cmd IPC_STAT - Get the semaphore information array into optional arg (sem_num is ignored) IPC_SET – Set the various semaphore array info specified by optional arg (sem_num is ignored) IPC_RMID – Remove the semaphore array (sem_num is ignored) SETVAL – Set sem_num'th semaphore value as specified by optional arg ...
  • 46. 46 ©2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. The Optional Arg Union semun { int val; //Value for set val struct semid_ds *buf; //Buffer for IPC_STAT & IPC_SET unsigned short *array; //Array for GETALL, SETALL .... } struct semid_ds { struct ipc_perm sem_perm; //Ownership & Permissions time_t sem_otime; // Time of last semop time_t sem_ctime; //Time of last change unsigned short sem_nsems; //No. Of semaphores in the array }
  • 47. 47 ©2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Caution On Deleting Process Semaphores Invoking _exit() or exec* doesn't do it Needs explicit removal Using IPC_RMID cmd of semctl Otherwise, may violate the system limit Of total number of process semaphores
  • 48. 48 ©2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Initializing the Process Semaphores Two ways though both using semctl Initialize all in the array cmd = SETALL arg.array = Array of values to be set Initializing one at a time sem_num = Semaphore number to be initialized cmd = SETVAL arg.val = Value to be set
  • 49. 49 ©2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Semaphore Usage Ops Usage operations on the Semaphore like Increment, Decrement, .. Int semop(sem_id, struct sembuf *sops, unsigned int nsops) Returns: 0 on success; -1 on error (and sets errno) semid: Semaphore array id returned by sem_get sops: Array of operations to be performed Nsops: Number of elements n sops
  • 50. 50 ©2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Operation Structure Decoded struct sembuf { unsigned short sem_num; // Index of semaphore to be operated on short sem_op; //Value to added to the current semaphore short sem_flg; //Operation flags } Operation Specifies sem_op > 0 -> value added & Processes waiting on it are signalled sem_op < 0 and current semaphore value >= |sem_op| -> value added sem_op < 0 and current semaphore value < | sem_op| It would block/wait until the semaphore value becomes >= |sem_op| Operation Flags IPC_NOWAIT – Prevents the operation from blocking Return -1 with EAGAIN in the above blocking case Kernel automatically undos the semaphore operations on the process exit
  • 51. 51 ©2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. What all have we learnt? Debugging with Gdb Building Statically Linked Modules Compiling and Building Complex application using make Inter Process Communication Mechanisms
  • 52. 52 ©2012 Pradeep Tewani <pradeep@sysplay.in> All Rights Reserved. Any Queries?