Linux Internals and Networking
Linux Internals and Networking
Team Emertxe
Contents
Introduction
System Calls
Process
Signals
Threads
Synchronization
IPC
Networking
Process Management
Memory Management
Introduction
Introduction
Let us ponder ...
Introduction
Operating System
Humans
Program
Interface
User
Programs
Compiler
Assembler
Text Editor
Database
Hardware
OS
Interface
Operating System
HW
Interface/
Privileged
Instr
Introduction
What is Linux?
Introduction
Why use Linux?
Introduction
What is Open Source?
Freedom of
software
Freedom of
use
Freedom of
modify
Freedom of
redistribute
Freedom of
copy
Introduction
Open Source - How it all started?
Introduction
Open Source - How it evolved?
Kernel
Applications
Customization
Introduction
Open Source - Where it stands now?
OS
Databases
Server/Cloud
Enterprise
Consumer
Education
CMS
eCommerce
Introduction
GPL
Introduction
GPL - Issues
Silent on sub-licensing
Introduction
Linux Components
GNU
C
Library
Kernel
Architecture Dependent
Kernel Code
Hardware Platform
Kernel Space
Linux
User
Application
User Space
Introduction
Linux Kernel Subsystem
Introduction
Linux Kernel Subsystem
Inter Process
Communications (IPC):
supports several
mechanisms for process-toprocess communication on a
single Linux system
Introduction
Linux Kernel Architecture
Introduction
Linux Kernel Architecture
System Calls
System calls
For a OS programmer, calling a system call is no different from a normal function call.
But the way system call is executed is way different.
System calls
User
Application
open()
User Mode
Kernel Mode
open()
Implementation
of open() system
call
return
System Call
Calling Sequence
user task
user task executing
kernel
trap
mode bit = 0
user mode
return from system call (mode bit = 1)
return
mode bit = 1
execute system call
kernel mode
(mode bit = 0)
Logically the system call and regular interrupt follow the same flow of steps. The
source (I/O device v/s user program) is very different for both of them. Since system
call is generated by user program they are called as Soft interrupts or Traps
System Call
vs Library Function
System Call
Implementation
User Mode
.
xyz()
.
System Call
Invocation in
application
program
xyz() {
.
int 0x80
.
}
Wrapper
routine in libc
standard
library
Kernel Mode
system_call:
sys_xyz()
ret_from_sys_call:
iret
System call
handler
sys_xyz() {
}
System call
service
routine
System Call
Example: gettimeofday()
System Call
Example: nanosleep()
System Call
Example: Others
open
read
write
exit
close
wait
waitpid
getpid
sync
nice
kill etc..
Process
Process
Process
vs Program
Process
Program
Storage
Dynamic Memory
Secondary Memory
State
Active
Passive
Process
vs Program
int global_1 = 0;
int global_2 = 0;
void do_somthing()
{
int local_2 = 5;
local_2 = local_2 + 1;
}
int main()
{
char *local_1 = malloc(100);
do_somthing();
..
}
Task
local_1
local_2
stack
heap
global_1
global_2
data
.start main
.call do_somthing
..
code
CPU Registers
Program
Process
P0
Free Space
Stack
P1
Heap
P2
Free Space
OS
Data
Code
Each Process will have its own Code, Data, Heap and Stack
Process
new
admitted
terminated
exit
interrupted
ready
running
scheduler dispatch
waiting
Process
States
State
Description
New
Running
Waiting
Ready
Terminated
Process
Descriptor
To manage tasks:
Task's priority
Process
Descriptor
Pointer
Process State
Process ID
Program Counter
Registers
Process state
Program counter
CPU registers
Memory Limits
List of Open Files
Memory-management
information
Process
Description
TASK_RUNNING
TASK_INTERRUPTIBLE
TASK_UNINTERRUPTIBLE
TASK_STOPPED
TASK_ZOMBIE
Process
Descriptor - ID
Process
Active Processes
Process
ID
Parent
Process
ID
user@user:~] ps -aef
UID
PID PPID C STIME TTY
root
1
0 0 12:17 ?
root
2
0 0 12:17 ?
root
3
2 0 12:17 ?
root
4
2 0 12:17 ?
root
5
2 0 12:17 ?
root
7
2 0 12:17 ?
TIME CMD
00:00:01 /sbin/init
00:00:00 [kthreadd]
00:00:02 [ksoftirqd/0]
00:00:00 [kworker/0:0]
00:00:00 [kworker/0:0H]
00:00:00 [rcu_sched]
Process
Context Switching
Process
Context Switching
process P0
operating system
process P1
executing
idle
idle
executing
idle
Process
Creation
Process
Creation - system()
Process
Creation - fork()
Process
Creation - fork()
fork()
Stack
Heap
Stack
ret = 0
Data
Code
Heap
Data
ret = xx
Code
Process
Text
Data
Stack
Process Status
Linux
Kernel
Process
Text
Data
Files
Stack
Resources
Process Status
ret = fork();
switch (ret)
{
case -1:
perror(fork);
exit(1);
case 0:
<code for child>
exit(0);
default:
<code for parent>
wait(&child_status);
}
Linux
Kernel
Process
Text
Data
Files
Stack
Resources
Process Status
ret = fork();
switch (ret)
{
case -1:
perror(fork);
exit(1);
case 0:
<code for child>
exit(0);
default:
<code for parent>
wait(&child_status);
}
Linux
Kernel
Process
Text
PID = 26
Data
Files
Stack
Resources
Process Status
ret = fork();
ret = 26
switch (ret)
{
case -1:
perror(fork);
exit(1);
case 0:
<code for child>
exit(0);
default:
<code for parent>
wait(&child_status);
}
Data
Stack
Text
Process Status
Linux
Kernel
Process
Text
PID = 26
Data
Files
Stack
Resources
Process Status
ret = fork();
ret = 26
switch (ret)
{
case -1:
perror(fork);
exit(1);
case 0:
<code for child>
exit(0);
default:
<code for parent>
wait(&child_status);
}
Data
Stack
Text
Process Status
Linux
Kernel
ret = fork();
switch (ret)
{
case -1:
perror(fork);
exit(1);
case 0:
<code for child>
exit(0);
default:
<code for parent>
wait(&child_status);
}
Process
Text
PID = 26
Data
Files
Stack
Resources
Process Status
ret = fork();
ret = 26
switch (ret)
{
case -1:
perror(fork);
exit(1);
case 0:
<code for child>
exit(0);
default:
<code for parent>
wait(&child_status);
}
Data
Stack
Text
Process Status
Linux
Kernel
ret = fork();
ret = 0
switch (ret)
{
case -1:
perror(fork);
exit(1);
case 0:
<code for child>
exit(0);
default:
<code for parent>
wait(&child_status);
}
Process
Text
PID = 26
Data
Files
Stack
Resources
Process Status
ret = fork();
ret = 26
switch (ret)
{
case -1:
perror(fork);
exit(1);
case 0:
<code for child>
exit(0);
default:
<code for parent>
wait(&child_status);
}
Data
Stack
Text
Process Status
Linux
Kernel
ret = fork();
ret = 0
switch (ret)
{
case -1:
perror(fork);
exit(1);
case 0:
<code for child>
exit(0);
default:
<code for parent>
wait(&child_status);
}
Process
Text
PID = 26
Data
Files
Stack
Resources
Process Status
ret = fork();
ret = 26
switch (ret)
{
case -1:
perror(fork);
exit(1);
case 0:
<code for child>
exit(0);
default:
<code for parent>
wait(&child_status);
}
Data
Stack
Text
Process Status
Linux
Kernel
ret = fork();
ret = 0
switch (ret)
{
case -1:
perror(fork);
exit(1);
case 0:
<code for child>
exit(0);
default:
<code for parent>
wait(&child_status);
}
Process
Text
Data
Files
Stack
Resources
Process Status
ret = fork();
ret = 26
switch (ret)
{
case -1:
perror(fork);
exit(1);
case 0:
<code for child>
exit(0);
default:
<code for parent>
wait(&child_status);
}
Linux
Kernel
Process
Process
Overlay - exec()
Process
Overlay - exec()
Registers
Stack
Heap
Data
Code
Process
Overlay - exec()
After executing the exec function, you will note the following
changes
PID
Preserved
Program
Counter
/* Program: my_ls.c */
int main()
{
print(Executing my ls :)\n);
execlp(/bin/ls, ls, NULL);
}
Registers
Reset
Stack
Heap
Data
Code
Process
exec() - Variants
Meaning
Process
Process
Process
Termination
Process
Wait
Meaning
wait(int *status)
Process
Resource Structure
struct rusage {
struct timeval ru_utime; /* user CPU time used */
struct timeval ru_stime; /* system CPU time used */
};
long ru_maxrss;
long ru_ixrss;
long ru_idrss;
long ru_isrss;
long ru_minflt;
long ru_majflt;
long ru_nswap;
/* swaps */
long ru_inblock;
long ru_oublock;
long ru_msgsnd;
long ru_msgrcv;
long ru_nsignals;
/* signals received */
long ru_nvcsw;
long ru_nivcsw;
Process
Zombie
Signals
Signals
Signals
Names
Number
Description
SIGINT
SIGQUIT
SIGKILL
SIGSEGV
11
SIGUSR1
10
SIGUSR2
12
Signals
Origins
The kernel
Signals
Handling
Immediate handling
Signals
Handling
Meaning
Signals
Handler
Signals
vs Interrupt
Signals
Advanced Handling
The signal() function can be called by the user for capturing signals
and handling them accordingly
It mainly handles user generated signals (ex: SIGUSR1), will not alter
default behavior of other signals (ex: SIGINT)
Any signal except SIGKILL and SIGSTOP can be handled using this
Function
Meaning
sigaction(
int signum,
const struct sigaction *act,
struct sigaction *oldact)
Signals
Signals
Self Signaling
Meaning
pause()
Threads
Threads
Threads
code
code
data
files
registers
registers
registers
registers
stack
stack
stack
stack
thread
thread
thread
data
files
thread
Threads are similar to handling multiple functions in parallel. Since they share
same code & data segments, care to be taken by programmer to avoid issues.
Threads
Advantages
Threads
pthread API's
Threads
Compilation
Threads
Creation
Function
Meaning
int pthread_create(
pthread_t *thread,
const pthread_attr_t *attr,
void *(*start_routine) (void *),
void *arg)
Threads
Creation
Threads
Joining
Meaning
int pthread_join(
pthread_t thread,
void **value_ptr)
Threads
Passing Data
Threads
Return Values
If the second argument you pass to pthread_join is nonnull, the threads return value will be placed in the
location pointed to by that argument
The thread return value, like the thread argument, is of
type void*
If you want to pass back a single int or other small
number, you can do this easily by casting the value to
void* and then casting back to the appropriate type after
calling pthread_join
Threads
Attributes
Threads
Attributes
Detach state
Priority
Stack size
Name
Thread group
Scheduling policy
Inherit scheduling
Threads
Threads
Meaning
int pthread_attr_init(
pthread_attr_t *attr)
intpthread_attr_setdetachstate
(pthread_attr_t*attr,
intdetachstate);
PTHREAD_CREATE_JOINABLE
PTHREAD_CREATE_DETACHED
Threads
ID
Meaning
pthread_t pthread_self()
Get self ID
intpthread_equal(
pthread_tthreadID1,
pthread_t threadID2);
Threads
Cancellation
Meaning
int pthread_cancel(pthread_t
thread)
Synchronization
Synchronization
why?
Synchronization
Race Condition
Synchronization
Critical Section
Synchronization
Critical Section
Only one task can enter the critical section; the other two have to sleep.
When a task sleeps, its execution is paused and the OS will run some other
task.
Critical Section
Synchronization
Critical Section
Once the thread in the critical section exits, another thread is woken up
and allowed to enter the critical section.
It is important to keep the code inside a critical section as small as
possible
Critical Section
Synchronization
Mutual Exclusion
Synchronization
Mutual Exclusion
During the time that a task holds the mutex, all other tasks waiting on the
mutex sleep.
Mutex
Protected Resource
lock
Synchronization
Mutual Exclusion
Once a task has finished using the shared resource, it releases the mutex.
Another task can then wake up and grab the mutex.
Mutex
Protected Resource
release
Synchronization
Practical Implementation
Synchronization
Treads
The same issue of synchronization exists in multiprocessing environment also, which is solved by process
level Mutex and Semaphores
The lock and unlock concept remain the same
Synchronization
Treads - Mutex
Meaning
int pthread_mutex_init(
pthread_mutex_t *mutex
const pthread_mutexattr_t
*attribute)
int pthread_mutex_lock(
pthread_mutex_t *mutex)
int pthread_mutex_unlock(
pthread_mutex_t *mutex)
int pthread_mutex_destroy(
pthread_mutex_t *mutex)
Synchronization
Treads - Semaphores
Synchronization
Wait operation:
If the value is already zero, the operation blocks until the value of the
semaphore becomes positive
Post operation:
If the semaphore was previously zero and other threads are blocked in
a wait operation on that semaphore
Synchronization
Treads - Semaphores
Meaning
int sem_init (
sem_t *sem,
int pshared,
unsigned int value)
Communication
Synchronization
Pipes
Message Queues
Shared memory
Sockets
Each IPC mechanism offers some advantages & disadvantages. Depending on the
program design, appropriate mechanism needs to be chosen.
User
process n
process 2
process 1
Kernel
How can processes communicate with each other and the kernel? The
answer is nothing but IPC mechanisms
Water
In
End
Water
Out
End
Data
In
End
Meaning
int pipe(
int pipe_fd[2])
Pipe read and write can be done simultaneously between two processes by
creating a child process using fork() system call.
Meaning
int mknod(
const char *path,
mode_t mode,
dev_t dev)
Note:
Each shared memory segment should be explicitly de-allocated
System has limited number of shared memory segments
Cleaning up of IPC is system programs responsibility
Process 1
Process 2
Process 3
Process n
Local
Memory
Local
Memory
Local
Memory
Local
Memory
Shared Memory
Meaning
int shmget(
key_t key,
size_t size,
int shmflag)
void *shmat(
int shmid,
void *shmaddr,
int shmflag)
Meaning
int semget(
key_t key,
int nsems,
int flag)
int semop(
int semid,
struct sembuf *sops,
unsigned int nsops)
semctl(semid, 0, IPC_RMID)
nsems
bytes
524288
2116
5152
nattch
2
2
2
status
dest
dest
dest
Networking Fundamentals
Networking Fundamentals
Introduction
Transmit data,
communication
voice,
video
to
provide
best
in
class
Networking Fundamentals
TCP / IP Model
Application
Presentation
Session
Transport
Transport
Network
Inter-network
Data Link
Telnet
DNS
FTP
RIP
SMTP
SNMP
TCP
UDP
Application
IP
Ethernet
Token Ring
ATM
Wireless
Link
1
Physical
OSI Model
TCP / IP Protocol
Layers
Internet Protocol
Suite
Networking Fundamentals
Application
details
Application
Application
Presentation
Session
Transport
Network
IPv4, IPv6
Data Link
Physical
Device Drivers
And
Hardware
OSI Model
User
process
TCP
UDP
Internet Protocol
Suite
socket
XTI
kernel
Communications
details
Networking Fundamentals
Protocols
User
process
Protocol
stack
within
kernel
Web
client
TCP
IP
Ethernet
driver
application protocol
TCP protocol
IP protocol
Ethernet protocol
actual flow between client and server
Ethernet
Web
server
Application
Layer
TCP
Transport
Layer
IP
Network
Layer
Ethernet
driver
Data Link
Layer
Networking Fundamentals
Addressing
IP layer: IP address
Source IP, Port and Destination IP, Port are essential for
creating a communication channel
IP address must be unique in a network
Port number helps in multiplexing and de-multiplexing the
messages
Socket
Sockets
Sockets
Address
struct sockaddr_in
{
short int sin_family;
unsigned short int sin_port;
struct in_addr sin_addr;
unsigned char sin_zero[8];
};
/*
/*
/*
/*
Address family */
Port number */
IP address structure */
Zero value, historical purpose */
Sockets
Calls - socket
Function
Meaning
int socket(
int domain,
int type,
int protocol)
Create a socket
domain: Address family (AF_INET, AF_UNIX etc..)
type: TCP (SOCK_STREAM) or UDP (SOCK_DGRAM)
protocol: Leave it as 0
RETURN: Socket ID or Error (-1)
Example usage:
sockfd = socket(AF_INET, SOCK_STREAM, 0);
Sockets
Calls - bind
Function
Meaning
int bind(
int sockfd,
struct sockaddr *my_addr,
int addrlen)
Example usage:
int sockfd;
struct sockaddr_in my_addr;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
my_addr.sin_family = AF_INET;
my_addr.sin_port = 3500;
my_addr.sin_addr.s_addr = 0xC0A8010A;
memset(&(my_addr.sin_zero), \0, 8);
/* 192.168.1.10 */
Sockets
Calls - connect
Function
Meaning
int connect(
int sockfd,
struct sockaddr *serv_addr,
int addrlen)
Example usage:
struct sockaddr_in my_addr, serv_addr;
/* Create a TCP socket & Bind */
sockfd = socket(AF_INET, SOCK_STREAM, 0);
bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = 4500;
serv_addr.sin_addr.s_addr = 0xC0A8010B;
/* Server port */
/* Server IP = 192.168.1.11 */
Sockets
Calls - listen
Function
Meaning
int listen(
int sockfd,
int backlog)
Example usage:
listen (sockfd, 5);
Sockets
Calls - accept
Function
Meaning
int accept(
int sockfd,
struct sockaddr *addr,
socklen_t *addrlen)
Example usage:
new_sockfd = accept(sockfd,&client_address, &client_address_length);
Sockets
Calls recv
Function
Meaning
int recv
(int sockfd,
void *buf,
int len,
int flags)
Sockets
Calls send
Function
Meaning
int send(
int sockfd,
const void *msg,
int len,
int flags)
Sockets
Calls close
Function
Meaning
Sockets
TCP - Summary
Server
Client
socket
socket
bind
listen
accept
Connection establishment
recv
send
close
Data (request)
Data (reply)
connect
send
recv
close
Sockets
TCP vs UDP
Connectionless UDP
Unreliable delivery
No-order guarantees
No notion of connection
Less network BW
D1
App
1
TCP
Socket
App
D
UDP
Socket
D2
D3
Sockets
UDP
Server
socket
Client
socket
bind
sendto
recvfrom
recvfrom
sendto
close
Sockets
Meaning
int sendto(
int sockfd,
const void *msg,
int len,
unsigned int flags,
const struct sockaddr *to,
socklen_t length);
int recvfrom(
int sockfd,
void *buf,
int len,
unsigned int flags,
struct sockaddr *from,
int *length);
Sockets
Help Functions
Sockets
Help Functions
Host Byte Order
16 Bit
htons
32 Bit
ntohs
htonl
ntohl
uint16_t
uint16_t
uint32_t
uint32_t
htons(uint16_t
ntohs(uint16_t
htonl(uint32_t
ntohl(uint32_t
32 Bit
host_short);
network_short);
host_long);
network_long);
:
load store
add store
read from file
CPU Burst
I/O Burst
store increment
Index
write to file
CPU Burst
I/O Burst
load store
add store
read from file
CPU Burst
I/O Burst
new
admitted
terminated
exit
interrupted
ready
running
scheduler dispatch
waiting
State
Description
New
Running
Waiting
Ready
Terminated
Terminates
Scheduling
Round Robin:
Co-operative
Serve (FCFS)
Round Robin:
Priority based
Static: Rate
Monotonic (RM)
Priority Based
Dynamic: Earliest
Deadline First (EDF)
The highest Priority is assigned to the Task with the Shortest Period
The relative deadline of the task is equal to the period of the Task
This kind of scheduler tries to give execution time to the task that is
most quickly approaching its deadline
This is typically done by the scheduler changing priorities of tasks onthe-fly as they approach their individual deadlines
Introduction to RTOS
Characteristics:
Types:
Also known as best effort systems. Example multimedia streaming, computer games
Real Time OS
Real Time OS
Characteristics
Real Time OS
Properties
Reliability
Predictability
Performance
Compactness
Scalability
Responsiveness
Real Time OS
Examples
LynxOS
OSE
QNX
VxWorks
Windows CE
RT Linux
OS
Application
Secondary (RAM)
Relocation
Protection
Sharing
Logical Organization
Physical Organization
CPU
logical
address
346
14000
Memory Management
Unit (MMU)
physical
address
14346
Memory
Fixed Partitioning
Dynamic Partitioning
Stay Connected
About us: Emertxe is Indias one of the top IT finishing schools & self learning kits provider. Our primary
focus is on Embedded with diversification focus on Java, Oracle and Android areas
https://www.facebook.com/Emertxe
https://twitter.com/EmertxeTweet
https://www.slideshare.net/EmertxeSlides
Thank You