0% found this document useful (0 votes)
104 views

Linux Internals & Networking: System Programming Using Kernel Interfaces

Here are the steps for static linking: 1. Compile source files to create object files: gcc -c file1.c file2.c 2. This creates file1.o and file2.o 3. Create a static library from the object files: ar rcs libstatic.a file1.o file2.o 4. This creates a static library file called libstatic.a 5. Link the main file along with the static library: gcc main.c libstatic.a -o executable 6. This will statically link the object files in the library into the executable. So in summary, static linking involves compiling to object files,

Uploaded by

Siddharth Sidhu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
104 views

Linux Internals & Networking: System Programming Using Kernel Interfaces

Here are the steps for static linking: 1. Compile source files to create object files: gcc -c file1.c file2.c 2. This creates file1.o and file2.o 3. Create a static library from the object files: ar rcs libstatic.a file1.o file2.o 4. This creates a static library file called libstatic.a 5. Link the main file along with the static library: gcc main.c libstatic.a -o executable 6. This will statically link the object files in the library into the executable. So in summary, static linking involves compiling to object files,

Uploaded by

Siddharth Sidhu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 305

Linux Internals & Networking

System programming using Kernel interfaces

Team Emertxe
Contents
Linux Internals & Networking
Contents

● Introduction
● Transition to OS programmer
● System Calls
● Process
● IPC

Signals
● Networking
● Threads
● Synchronization
● Process Management
● Memory Management
Introduction
Introduction
Let us ponder ...

● What exactly is an Operating System (OS)?


● Why do we need OS?
● How would the OS would look like?
● Is it possible for a team of us (in the room) to create an
OS of our own?
● Is it necessary to have an OS running in a Embedded
System?
● Will the OS ever stop at all?
What is an OS
OS is an interface between application and hardware
Applications Which abstracts H/W layer from user

OS

Hardware

Applications

Is it possible to make an embedded without OS.? Hardware


Why we need OS
● Multitasking
● Multi user
● Scheduling
● Memory management
etc ...
Linux Booting
Sequence

System Start-up BIOS / Boot Monitor

Stage 1 Boot Loader Master Boot Record

Stage 2 Boot Loader LILO, GRUB, etc

Kernel Linux

Init User Application


Control flow in OS

Interrupts System Call Exceptions

Operating System
main() Modules
Boot Initialization
Supervisor Mode

RTI

User Mode
History

1940 - 1955 ENIAC, Mechanical switches, Mainframe


computers

1955 - 1970 Concept OS, FORTRAN, IBM OS/360,


Multiprogramming, Minicomputers.
Vertical
Horizontal
1970 - 1980 UNIX, Microprocessors(intel), Personal
computers age

1980 - 1990 First computer IBM 5150, DOS, Apple


& Windows with GUI

1990 - NOW Linux, ios, Android.....


Vertical vs Horizontal
● Vertical
– Hardware & Software made by same company
– OS was integrated part of the Hardware
– Applications were propitiatory
● Horizontal
– Hardware & Software made by different company
– OS is an independent software, that can run on diversified
set of hardware
– Applications are developed by everybody (propitiatory or
open source)
Quiz
● How would the OS look like ?
a) H/W only
b) S/W only
c) S/W + H/W
● How big is OS ?
Introduction
Operating System

Humans

Program
Interface

User
Programs
Compiler Assembler Text Editor Database
OS
System and Application Programs Interface

Operating
System Operating System
HW
Interface/
Privileged
Instr
Hardware
Introduction
Kernel Architecture

● Most older operating systems are monolithic, that is, the


whole operating system is a single executable file that runs in
'kernel mode'
● This binary contains the process management, memory
management, file system and the rest (Ex: UNIX)
● The alternative is a microkernel-based system, in which most
of the OS runs as separate processes, mostly outside the
kernel
● They communicate by message passing. The kernel's job is to
handle the message passing, interrupt handling, low-level
process management, and possibly the I/O (Ex: Mach)
Introduction
Kernel Architecture

Mionolithic Kernel Micro Kernel


Based Operating System Based Operating System

Application System Call


User
Mode
VFS

IPC, File Systems


Unix Device File
Application
Server Driver Server
Scheduler, Virtual Memory
Kernel
Device
Device Drivers,
Drivers, Dispatcher...
Dispatcher... Mode
Basic IPC, Virtual Memory, Scheduling

Hardware Hardware
Introduction
Mono vs Micro

Monolithic kernel Microlithic kernel


● Kernel size increases because ● Kernel size is small because
kernel + kernel subsystems kernel subsystems run as
compiled as single binary separate binaries.
● Difficult to extension or bug ● Easily extensible and bug
fixing, fixing.
● Need to compile entire source ● Easily recover from crash
code. ● Slower due to complex message
● Bad maintainability passing between services
● Faster, run as single binary ● Process management is
● Communication between complex
services is faster. ● Communication is slow
● No crash recovery. ● Easily recoverable from
● More secure crashing
● Eg: Windows, Linux etc ● Eg: MacOS, WinNT
RTOS
● Real time means fast..?
● RTOS is an operating system that guarantees a certain
capabilities within a specified time constraint.
● RTOS must also must able to respond predictably to
unpredictable events
● We use RTOS in Aircraft, Nuclear reactor control systems
where time is crucial.
● Eg: LynxOS, OSE, RTLinux, VxWorks, Windows CE
Transition to OS programming
Course & module view

0101100
C LS

010110
01001
DS

Resorce / Service Applications


Provider LI
System Call
Interface

Kernel
EOS Abstracts HW
from users
Device Drivers

LDD Hardware
MC
Application vs OS
C Algorithms, Syntax, Logic ● Preprocessor
● Compiler
● Assembler
● Linker
● Executable file
(a.out)
OS Memory segments, process, ● Executing a program
Threads ●
Loader
Signals, IPC, Networking
Application Programming
Compilation Stages


Preprocessor
– Expands header files
– Substitute all macros
– Remove all comments
– Expands and all # directives

Compilation
– Convert to assembly level instructions

Assembly
– Convert to machine level instructions
– Commonly called as object files
– Create logical address

Linking
– Linking with libraries and other object files
Application Programming
Compilation Stages

Preprocessor .c Source Code

Expanded Source
Compiler .i Code
gcc -E file.c

Assembly Source
Assembler .s Code
gcc -S file.c

Object Code gcc -c file.c


Linker .o

Executable
Loader .out

gcc -save-temps file.c would generate all intermediate files


Application Programming
Linking - Static

● Static linking is the process of copying all library modules


used in the program into the final executable image.
● This is performed by the linker and it is done as the last
step of the compilation process.
● Its both faster and more portable, since it does not
require the presence of the library on the system where
it is run.
● Compiling two .o files also a type of static linking.
Application Programming
Linking – Static

● Create two files file1.c & file2.c with appropriate functions in them
● Compile them to create object files ("gcc -c fun1.c fun2.c"). It will
create fun1.o and fun2.o files as output
● Create a static library by using ("ar rcs fun1.o fun2.o -o libstatic.a),
with this you have completed creating your static library
● Create your main.c file by calling these two functions
● Perform a static linking (gcc main.c libstatic.a)
● This will generate your a.out, execute the program and observe the
output

Note: ar - Archive command


rcs option - r(replace), c(create), s(write an object file into archive)
Application Programming
Linking - Dynamic

● It performs the linking process when programs are


executed in the system.
● During dynamic linking the name of the shared library is
placed in the final executable file.
● Actual linking takes place at run time when both
executable file and library are placed in the memory.
● The main advantage to using dynamically linked libraries
is that the size of executable programs is reduced
● To create a dynamic library (shared object file)
Application Programming
Linking - Dynamic

Create two files file1.c & file2.c with appropraite functions in them

Create a dynamic library (shared object) ("gcc -fPIC -shared fun1.c fun2.c -o
libdynamic.so)

Set the environmental variable LD_LIBRARY_PATH to the directory where this
new *.so is located (ex:
LD_LIBRARY_PATH="/home/LinuxInternals/DynamicLinking")

Perform echo $LD_LIBRARY_PATH and ensure this variable is set properly

Execute export LD_LIBRARY_PATH to ensure this gets into the shell's
environmental variables ecosystem

Create your main.c file by calling these two functions

Perform a dynamic linking ("gcc main.c -L . -ldynamic)

This will generate your a.out, execute the program and observe the output
Note: fPIC option - Generating Position Independent Code (as it is dynamically linked)
Application Programming
Linking - Static vs Dynamic

Linking
Static Dynamic
Parameter

Executable Size

Loding Time

Memory Usage

No of Sys Calls
Executing a process
RAM Memory Layout Stack
Frame
P1
P1 Command Line
Arguments Local Variables

P2 Stack Return Address

Parameter List
Hole
P3
Heap

. .BSS

Segment
(Uninitialized)
. Data

. Initialized

Pn-1
Code Segment

Pn
Quiz
● How a user defined function works?
● How a library function works?
Storage Classes
Storage Class Scope Lifetime Memory Allocation
Within the
Till the end of the block /
auto block / Stack
function
Function
Within the block / Till the end of the block /
register Register
Function function
Within the block /
static local Till the end of the program Data Segment
Function
static global File Till the end of the program Data segment
extern Program Till the end of the program Data segment
Hands-on
● Access a static variable from outside file.
● Access a global variable from outside file.
● Combination of both static and local.
Common errors
with various memory segments

Command Line Stack Overflow / Stack Smashing


Arguments ● When ever process stack limit is over

Eg: Call a recursive function inifinite times.


Stack ● When you trying to access array beyond limits.

Hole
Eg int arr[5]; arr[100];

Memory Leak
Heap
● When you never free memory after allocating.

.BSS Eventually process heap memory will run-out


Segment

(Uninitialized)
Data

Initialized

Segmentation
Fault
● When you try to change text segment, which is a
Text Segment
read-only memory or try trying to access a memory
beyond process memory limit (like NULL pointer)
Introduction
What is Linux?

● Linux is a free and open source operating system that is


causing a revolution in the computer world
● Originally created by Linus Torvalds with the assistance of
developers called community
● This operating system in only a few short years is
beginning to dominate markets worldwide
Introduction
Why use Linux?

● Free & Open Source –GPL license, no cost


● Reliability –Build systems with 99.999% upstream
● Secure –Monolithic kernel offering high security
● Scalability –From mobile phone to stock market servers
Introduction
Linux Components

● Hardware Controllers: This subsystem is


comprised of all the possible physical devices
User in a Linux installation - CPU, memory

User Space
Application
hardware, hard disks
● Linux Kernel: The kernel abstracts and
GNU mediates access to the hardware resources,
C
Library
including the CPU. A kernel is the core of the
operating system
Linux

System Call Interface


● O/S Services: These are services that are
typically considered part of the operating
Kernel Space

system (e.g. windowing system, command


Kernel
shell)

Architecture Dependent
● User Applications: The set of applications in
Kernel Code
use on a particular Linux system (e.g. web
Hardware Platform browser)
Introduction
Linux Kernel Subsystem

Process Scheduler (SCHED):
– To provide control, fair access
of CPU to process, while
interacting with HW on time

Memory Manager (MM):
– To access system memory
securely and efficiently by
multiple processes. Supports
Virtual Memory in case of
huge memory requirement

Virtual File System (VFS):
– Abstracts the details of the
variety of hardware devices
by presenting a common file
interface to all devices
Introduction
Linux Kernel Subsystem
● Network Interface (NET):
– provides access to several
networking standards and a
variety of network hardware
● Inter Process
Communications (IPC):
– supports several
mechanisms for process-to-
process communication on a
single Linux system
Introduction
Virtual File System

User Processes
User space
System Calls

System Call Interface

Virtual File System – dentry & inode cache

Kernel space
FIFO
ext2 ext3 /proc sockets
Pipes

Drivers and Buffer Cache


Introduction
Virtual File System

● Presents the user with a unified interface, via the file-


related system calls.
● The VFS interacts with file-systems which interact with
the buffer cache, page-cache and block devices.
● Finally, the VFS supplies data structures such as the
dcache, inodes cache and open files tables.
– Allocate a free file descriptor.
– Try to open the file.
– On success, put the new 'struct file' in the fd table of the
process.On error, free the allocated file descriptor.
NOTE: VFS makes “Everythig is file” in Linux
Summary

Linux Device Drivers

Linux Internals

Compilation Storage Program &


Stages Classes Process
System Calls
Synchronous & Asynchronous
● Communications are two types
Synchronous Asynchronous
See in next chapters

Polling Interrupts
Interrupts
Interrupts

Hardware Software

Generates whenever a Generates by instruction


Hardware changes happens from code (eg: INT 0x80)

● Interrupt controller signals CPU that interrupt has occurred, passes


interrupt number
● Basic program state saved
● Uses interrupt number to determine which handler to start
● CPU jumps to interrupt handler
● When interrupt done, program state reloaded and program resumes
System calls
● A set of interfaces to interact with hardware devices such
as the CPU, disks, and printers.
● Advantages:
– Freeing users from studying low-level programming
– It greatly increases system security
– These interfaces make programs more portable

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
System Call Interface
Kernel Mode

● open()
● Implementation
of open() system
call
● ●



return
System Call
Calling Sequence

user task
user mode
user task executing calls system call return from system call (mode bit = 1)

kernel trap return


mode bit = 0 mode bit = 1 kernel mode
execute system call (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

● A library function is an ordinary function that resides in a


library external to your program. A call to a library function is
just like any other function call
● A system call is implemented in the Linux kernel and a special
procedure is required in to transfer the control to the kernel
● Usually, each system call has a corresponding wrapper routine,
which defines the API that application programs should employ

 Understand the differences between:


• Functions
• Library functions
• System calls
 From the programming perspective they all are nothing but simple C functions
System Call
Implementation

User Mode Kernel Mode

system_call:
xyz() { … sys_xyz() {
…. …. sys_xyz()
xyz() int 0x80 … …
…. …. ret_from_sys_call:
} … }
iret

System Call Wrapper System call System call


Invocation in routine in libc handler service
application standard routine
program library
System Call
Example: gettimeofday()

● Gets the system’s wall-clock time.


● It takes a pointer to a struct timeval variable. This
structure represents a time, in seconds, split into two
fields.
– tv_sec field - integral number of seconds
– tv_usec field - additional number of usecs
System Call
Example: nanosleep()

● A high-precision version of the standard UNIX sleep call


● Instead of sleeping an integral number of seconds,
nanosleep takes as its argument a pointer to a struct
timespec object, which can express time to nanosecond
precision.
– tv_sec field - integral number of seconds
– tv_nsec field - additional number of nsecs
System Call
Example: Others

● open
● read

write

exit
● close

wait
● waitpid

getpid
● sync

nice

kill etc..
Process
Process
● Running instance of a program is called a PROCESS
● If you have two terminal windows showing on your screen,
then you are probably running the same terminal program
twice-you have two terminal processes
● Each terminal window is probably running a shell; each
running shell is another process
● When you invoke a command from a shell, the corresponding
program is executed in a new process
● The shell process resumes when that process complete
Process
vs Program

● A program is a passive entity, such as file containing a list of


instructions stored on a disk
● Process is a active entity, with a program counter specifying
the next instruction to execute and a set of associated
resources.
● A program becomes a process when an executable file is
loaded into main memory

Factor Process Program


Storage Dynamic Memory Secondary Memory
State Active Passive
Process
vs Program
Program Task

int global_1 = 0;
int global_2 = 0; local_1
stack
local_2 5
void do_somthing()
{
int local_2 = 5;
local_2 = local_2 + 1;

CPU Registers
}
heap
int main()
{ global_1
data
char *local_1 = malloc(100); global_2
.start main
do_somthing(); code
.call do_somthing
…..
}
…..
Process
More processes in memory!

P0
Stack
Free Space

P1
Heap
P2
Data
Free Space
Code
OS

Each Process will have its own Code, Data, Heap and Stack
Process
State Transition Diagram

new admitted exit terminated


interrupted

ready running

scheduler dispatch

I/O or event completion I/O or event wait

waiting
Process
State Transition Diagram

Priority
Round Robin
FCFS
Preemptive

interrupted

ready running

scheduler dispatch

I/O or event completion I/O or event wait

waiting
I/O: Keyboard
Even: Signal
Process
States

● A process goes through multiple states ever since it is


created by the OS

State Description
New The process is being created
Running Instructions are being executed
Waiting The process is waiting for some event to occur
Ready The process is waiting to be assigned to processor
Terminated The process has finished execution
Process
Descriptor

● To manage tasks:
– OS kernel must have a clear picture of what each task is
doing.
– Task's priority
– Whether it is running on the CPU or blocked on some event
– What address space has been assigned to it
– Which files it is allowed to address, and so on.
● Usually the OS maintains a structure whose fields contain
all the information related to a single task
Process
Descriptor

Pointer Process State ● Information associated with


Process ID
each process.
Program Counter ● Process state
Registers ● Program counter
Memory Limits
● CPU registers
List of Open Files

● CPU scheduling information


● Memory-management


information

● I/O status information
Process
Descriptor – State Field

● State field of the process descriptor describes the state of


process.

The possible states are:

State Description
TASK_RUNNING Task running or runnable
TASK_INTERRUPTIBLE process can be interrupted while sleeping

TASK_UNINTERRUPTIBLE process can't be interrupted while sleeping

TASK_STOPPED process execution stopped


TASK_ZOMBIE parent is not issuing wait()
Process
Descriptor - ID

● Each process in a Linux system is identified by its unique


process ID, sometimes referred to as PID
● Process IDs are numbers that are assigned sequentially by
Linux as new processes are created
● Every process also has a parent process except the
special init process
● Processes in a Linux system can be thought of as arranged
in a tree, with the init process at its root
● The parent process ID or PPID, is simply the process ID of
the process’s parent
Process
Schedule

P1 P2 P3 P4
Stack Stack Stack Stack
User space

Heap Heap Heap Heap


Data Data Data Data
Code Code Code Code
Kernel space

Addr PS Addr PS Addr PS Addr PS


PID PID PID PID
PC PC PC PC
REG REG REG REG
Memory Memory Memory Memory
Files Files Files Files
Process
Active Processes

● The ps command displays the processes that are running on your


system
● By default, invoking ps displays the processes controlled by the
terminal or terminal window in which ps is invoked

For example (Executed as “ps –aef”):

user@user:~] ps -aef
UID PID PPID C STIME TTY TIME CMD
Process
root 1 0 0 12:17 ? 00:00:01 /sbin/init
ID root 2 0 0 12:17 ? 00:00:00 [kthreadd]
root 3 2 0 12:17 ? 00:00:02 [ksoftirqd/0]
Parent
root 4 2 0 12:17 ? 00:00:00 [kworker/0:0]
Process root 5 2 0 12:17 ? 00:00:00 [kworker/0:0H]
ID root 7 2 0 12:17 ? 00:00:00 [rcu_sched]
Process
Context Switching

● Switching the CPU to another task requires saving the state of


the old task and loading the saved state for the new task
● The time wasted to switch from one task to another without
any disturbance is called context switch or scheduling jitter
● After scheduling the new process gets hold of the processor
for its execution
Context Switching

process P0 operating system process P1

Interrupt or system call


executing

save state into PCB0



idle

reload state from PCB1

idle Interrupt or system call executing

save state into PCB1



idle

reload state from PCB0

executing
Process
Creation

● Two common methods are used for creating new process


● Using system(): Relatively simple but should be used
sparingly because it is inefficient and has considerably
security risks
● Using fork() and exec(): More complex but provides
greater flexibility, speed, and security
Process
Creation - system()

● It creates a sub-process running the standard shell


● Hands the command to that shell for execution
● Because the system function uses a shell to invoke your
command, it's subject to the features and limitations of
the system shell
● The system function in the standard C library is used to
execute a command from within a program
● Much as if the command has been typed into a shell
Process
Creation - fork()


fork makes a child process that is an exact copy of its
parent process

When a program calls fork, a duplicate process, called the
child process, is created

The parent process continues executing the program from
the point that fork was called

The child process, too, executes the same program from
the same place

All the statements after the call to fork will be executed
twice, once, by the parent process and once by the child
process
Process
Creation - fork()

● The execution context for the child process is a copy of


parent's context at the time of the call
int child_pid;
int child_status;

int main() fork()


{
int ret;
Stack Stack
ret = fork();
switch (ret)
{
Heap ret = 0 Heap
case -1:
perror(“fork”);
exit(1); Data Data
case 0:
<code for child process> Code ret = xx Code
exit(0);
default:
<code for parent process>
wait(&child_status);
}
}
Process
fork() - The Flow

PID = 25

Data
Text
Stack
Process Status

Linux
Kernel
Process
fork() - The Flow

PID = 25
Files
Data
Text
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
fork() - The Flow

PID = 25
Files
Data
Text
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
fork() - The Flow

PID = 25 PID = 26
Files
Data Data
Text Text
Stack Resources Stack
Process Status 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
fork() - The Flow

PID = 25 PID = 26
Files
Data Data
Text Text
Stack Resources Stack
Process Status Process Status

ret = fork(); ret = 26 ret = fork();


switch (ret) switch (ret)
{ {
case -1: case -1:
perror(“fork”); perror(“fork”);
exit(1); exit(1);
case 0: case 0:
<code for child> <code for child>
exit(0); exit(0);
default: default:
<code for parent> <code for parent>
wait(&child_status); Linux wait(&child_status);
} }
Kernel
Process
fork() - The Flow

PID = 25 PID = 26
Files
Data Data
Text Text
Stack Resources Stack
Process Status Process Status

ret = fork(); ret = 26 ret = fork(); ret = 0


switch (ret) switch (ret)
{ {
case -1: case -1:
perror(“fork”); perror(“fork”);
exit(1); exit(1);
case 0: case 0:
<code for child> <code for child>
exit(0); exit(0);
default: default:
<code for parent> <code for parent>
wait(&child_status); Linux wait(&child_status);
} }
Kernel
Process
fork() - The Flow

PID = 25 PID = 26
Files
Data Data
Text Text
Stack Resources Stack
Process Status Process Status

ret = fork(); ret = 26 ret = fork(); ret = 0


switch (ret) switch (ret)
{ {
case -1: case -1:
perror(“fork”); perror(“fork”);
exit(1); exit(1);
case 0: case 0:
<code for child> <code for child>
exit(0); exit(0);
default: default:
<code for parent> <code for parent>
wait(&child_status); Linux wait(&child_status);
} }
Kernel
Process
fork() - The Flow

PID = 25 PID = 26
Files
Data Data
Text Text
Stack Resources Stack
Process Status Process Status

ret = fork(); ret = 26 ret = fork(); ret = 0


switch (ret) switch (ret)
{ {
case -1: case -1:
perror(“fork”); perror(“fork”);
exit(1); exit(1);
case 0: case 0:
<code for child> <code for child>
exit(0); exit(0);
default: default:
<code for parent> <code for parent>
wait(&child_status); Linux wait(&child_status);
} }
Kernel
Process
fork() - The Flow

PID = 25
Files
Data
Text
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
fork() - How to Distinguish?


First, the child process is a new process and therefore has a
new process ID, distinct from its parent’s process ID

One way for a program to distinguish whether it’s in the
parent process or the child process is to call getpid

The fork function provides different return values to the
parent and child processes

One process “goes in” to the fork call, and two processes
“come out,” with different return values

The return value in the parent process is the process ID of the
child

The return value in the child process is zero
Process
fork() - Example

● What would be output of the following program?


int main()
{
fork();
fork();
fork();

printf(“Hello World\n”);

return 0;
}
Process
fork() - Example

P
int main()
{
fork();
fork();
fork();

printf(“Hello World\n”);

return 0;
}
Process
fork() - Example

P
int main()
{
fork(); C1
fork();
fork();

printf(“Hello World\n”);

return 0;
}
Process
fork() - Example

P
int main()
{
fork(); C1 C2
fork();
fork();

printf(“Hello World\n”);

return 0; C3
}

Note: The actual order of execution based on scheduling


Process
fork() - Example

P
int main()
{
fork(); C1 C2 C4
fork();
fork();

printf(“Hello World\n”);
C3 C5 C6
return 0;
}

C7

Note: The actual order of execution based on scheduling


Process
Zombie


Zombie process is a process that has terminated but has not
been cleaned up yet

It is the responsibility of the parent process to clean up its
zombie children

If the parent does not clean up its children, they stay
around in the system, as zombie

When a program exits, its children are inherited by a special
process, the init program, which always runs with process ID
of 1 (it’s the first process started when Linux boots)

The init process automatically cleans up any zombie child
processes that it inherits.
Process
Orphan

● An orphan process is a computer process whose parent process


has finished or terminated, though it remains running itself.
● Orphaned children are immediately "adopted" by init .
● An orphan is just a process. It will use whatever resources it
uses. It is reasonable to say that it is not an "orphan" at all
since it has a parent but "adopted".
● Init automatically reaps its children (adopted or otherwise).
● So if you exit without cleaning up your children, then they will
not become zombies.
Process
Overlay - exec()


The exec functions replace the program running in a process
with another program

When a program calls an exec function, that process
immediately ceases executing and begins executing a new
program from the beginning
● Because exec replaces the calling program with another one, it
never returns unless an error occurs
● This new process has the same PID as the original process, not
only the PID but also the parent process ID, current directory,
and file descriptor tables (if any are open) also remain the same

Unlike fork, exec results in still having a single process
Process
Overlay - exec()

● Let us consider an example of execlp (variant of exec()


function) shown below
PID

Program
Counter

/* Program: my_ls.c */ Registers

int main()
{ Stack
print(“Executing my ls :)\n”);
execlp(“/bin/ls”, “ls”, NULL); 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 */ Registers Reset

int main()
{ Stack Overwritten by New Code
print(“Executing my ls :)\n”);
execlp(“/bin/ls”, “ls”, NULL); Heap Overwritten by New Code
}
Data Overwritten by New Code

Code Overwritten with New Code


Process
exec() - Variants

● The exec has a family of system calls with variations among


them
● They are differentiated by small changes in their names
● The exec family looks as follows:

System call Meaning


execl(const char *path, const char *arg, ...); Full path of executable, variable number of
arguments
execlp(const char *file, const char *arg, ...); Relative path of executable, variable number
of arguments
execv(const char *path, char *const argv[]); Full path of executable, arguments as pointer
of strings
execvp(const char *file, char *const argv[]); Relative path of executable, arguments as
pointer of strings
Process
Blending fork() and exec()

● Practically calling program never returns after exec()


● If we want a calling program to continue execution after
exec, then we should first fork() a program and then exec
the subprogram in the child process
● This allows the calling program to continue execution as
a parent, while child program uses exec() and proceeds
to completion
● This way both fork() and exec() can be used together
Process
COW – Copy on Write


Copy-on-write (called COW) is an optimization strategy

When multiple separate process use same copy of the same
information it is not necessary to re-create it

Instead they can all be given pointers to the same resource,
thereby effectively using the resources

However, when a local copy has been modified (i.e. write) ,
the COW has to replicate the copy, has no other option

For example if exec() is called immediately after fork()
they never need to be copied the parent memory can be
shared with the child, only when a write is performed it
can be re-created
Process
Termination

● When a parent forks a child, the two process can take


any turn to finish themselves and in some cases the
parent may die before the child
● In some situations, though, it is desirable for the parent
process to wait until one or more child processes have
completed
● This can be done with the wait() family of system calls.
● These functions allow you to wait for a process to finish
executing, enable parent process to retrieve information
about its child’s termination
Synchronous & Asynchronous
● Wait for child to finish
Synchronous Asynchronous

Polling Interrupts

sleep wait
Process
Wait

● fork() in combination with wait() can be used for child monitoring


● Appropriate clean-up (if any) can be done by the parent for ensuring
better resource utilization
● Otherwise it will result in a ZOMBIE process
● There are four different system calls in the wait family
System call Meaning
wait(int *status) Blocks & waits the calling process until
one of its child processes exits. Return
status via simple integer argument

waitpid (pid_t pid, int* status, int options) Similar to wait, but only blocks on a child
with specific PID
wait3(int *status, int options, struct rusage Returns resource usage information
*rusage) about the exiting child process.
wait4 (pid_t pid, int *status, int options, struct Similar to wait3, but on a specific child
rusage *rusage)
Process
Resource Structure

struct rusage {
struct timeval ru_utime; /* user CPU time used */
struct timeval ru_stime; /* system CPU time used */
long ru_maxrss; /* maximum resident set size */
long ru_ixrss; /* integral shared memory size */
long ru_idrss; /* integral unshared data size */
long ru_isrss; /* integral unshared stack size */
long ru_minflt; /* page reclaims (soft page faults) */
long ru_majflt; /* page faults (hard page faults) */
long ru_nswap; /* swaps */
long ru_inblock; /* block input operations */
long ru_oublock; /* block output operations */
long ru_msgsnd; /* IPC messages sent */
long ru_msgrcv; /* IPC messages received */
long ru_nsignals; /* signals received */
long ru_nvcsw; /* voluntary context switches */
long ru_nivcsw; /* involuntary context switches */
};
Inter Process Communications (IPC)
Communication
in real world
● Face to face
● Fixed phone
● Mobile phone
● Skype
● SMS
Inter Process Communications
Introduction

● Inter process communication (IPC) is the mechanism


whereby one process can communicate, that is exchange
data with another processes
● There are two flavors of IPC exist: System V and POSIX
● Former is derivative of UNIX family, later is when
standardization across various OS (Linux, BSD etc..) came
into picture
● Some are due to “UNIX war” reasons also
● In the implementation levels there are some differences
between the two, larger extent remains the same
● Helps in portability as well
Inter Process Communications
Introduction

● IPC can be categorized broadly into two areas:

Data exchange Resource usage/access/control

Communication Synchronization
● Pipes ● Semaphores
● FIFO
● Shared memory
● Signals
● Sockets

● Even in case of Synchronization also two processes are talking.

Each IPC mechanism offers some advantages & disadvantages. Depending on the
program design, appropriate mechanism needs to be chosen.
Application and Tasks

A T Example: Read from a file


$ cat file.txt

T1

T2

A Example: Paper jam handling


in printer
T3

T4
Inter Process Communications
User vs Kernel Space

● Protection domains - (virtual address space)

User
Kernel
process 1

process 2

process n

How can processes communicate with each other and the kernel? The
answer is nothing but IPC mechanisms
Inter Process Communications
Pipes

● A pipe is a communication device that permits unidirectional


communication
● Data written to the “write end” of the pipe is read back from
the “read end”
● Pipes are serial devices; the data is always read from the pipe
in the same order it was written
Data
in
Out

Water Water Data


In Out In
End End End
Inter Process Communications
Pipes - Creation

● To create a pipe, invoke the pipe system call


● Supply an integer array of size 2
● The call to pipe stores the reading file descriptor in array
position 0
● Writing file descriptor in position 1
Function Meaning
int pipe(  Pipe gets created
int pipe_fd[2])  READ and WRITE pipe descriptors are populated
 RETURN: Success (0)/Failure (Non-zero)

Pipe read and write can be done simultaneously between two processes by
creating a child process using fork() system call.
Inter Process Communications
Pipes – Direction of communication

● Let's say a Parent wants to communicate with a Child


● Generally the communication is possible both the way!

Parent Child
Inter Process Communications
Pipes – Direction of communication

● So it necessary to close one of the end form both sides

Parent Child
Inter Process Communications
Pipes – Working

Process Kernel

fd[1]

Pipe
Buffer

fd[0]
Inter Process Communications
Pipes - Pros & Cons

PROS CONS

● Naturally synchronized ● Less memory size (4K)


● Simple to use and create ● Only related process can
● No extra system calls communicate.
required to communicate ● Only two process can
(read/write)
communicate
● One directional
communication
● Kernel is involved
Inter Process Communications
Summary

● We have covered

Data exchange Resource usage/access/control

Communication Synchronization
● Pipes ● Semaphores
● FIFO
● Shared memory
● Signals
● Sockets
Inter Process Communications
FIFO - Properties

● A first-in, first-out (FIFO) file is a pipe that has a name in


the file-system
● FIFO file is a pipe that has a name in the file-system
● FIFOs are also called Named Pipes
● FIFOs is designed to let them get around one of the
shortcomings of normal pipes
Inter Process Communications
FIFO – Working

User Kernel

P1

Pipe
file Buffer

P2
Inter Process Communications
FIFO - Creation

● FIFO can also be created similar to directory/file creation with


special parameters & permissions
● After creating FIFO, read & write can be performed into it just
like any other normal file
● Finally, a device number is passed. This is ignored when
creating a FIFO, so you can put anything you want in there
● Subsequently FIFO can be closed like a file

Function Meaning
int mknod(  path: Where the FIFO needs to be created (Ex:
const char *path, “/tmp/Emertxe”)
mode_t mode,  mode: Permission, similar to files (Ex: 0666)
dev_t dev)  dev: can be zero for FIFO
Inter Process Communications
FIFO - Access

● Access a FIFO just like an ordinary file


● To communicate through a FIFO, one program must open it
for writing, and another program must open it for reading
● Either low-level I/O functions (open, write, read, close
and so on) or C library I/O functions (fopen, fprintf,
fscanf, fclose, and so on) may be used.

user@user:~] ls -l my_fifo
prw-rw-r-- 1 biju biju 0 Mar 8 17:36 my_fifo

prw-
Inter Process Communications
FIFO vs Pipes

● Unlike pipes, FIFOs are not temporary objects, they are


entities in the file-system
● Any process can open or close the FIFO
● The processes on either end of the pipe need not be related to
each other
● When all I/O is done by sharing processes, the named pipe
remains in the file system for later use
Inter Process Communications
FIFO - Example

● Unrelated process can communicate with FIFO

Shell 1

user@user:~] cat > /tmp/my_fifo


Hai hello

Shell 2
user@user:~] cat /tmp/my_fifo
Hai hello
Inter Process Communications
FIFO - Pros & Cons

PROS CONS

● Naturally synchronized ● Less memory size (4K)


● Simple to use and create ● Only two process can
● Unrelated process can communicate

communicate.
● One directional
communication
● No extra system calls
required to communicate
● Kernel is involved
(read/write)
● Work like normal file
Inter Process Communications
Summary

● We have covered

Data exchange Resource usage/access/control

Communication Synchronization
● Pipes ● Semaphores
● FIFO
● Shared memory
● Signals
● Sockets
Inter Process Communications
Shared Memories - Properties


Shared memory allows two or more processes to access the same
memory

When one process changes the memory, all the other processes see
the modification

Shared memory is the fastest form of Inter process communication
because all processes share the same piece of memory

It also avoids copying data unnecessarily

Note:
• Each shared memory segment should be explicitly de-allocated
• System has limited number of shared memory segments
• Cleaning up of IPC is system program’s responsibility 
Inter Process Communications
Shared vs Local Memory

Process 1 Process 2 Process 3 Process n

Local Local Local Local


User Memory Memory Memory Memory
Space

Kernel
Space Shared Memory
Inter Process Communications
Shared Memories - Procedure

● Create
● Attach
● Read/Write 95%
● Detach
● Remove
Inter Process Communications
Shared Memories - Procedure

● To start with one process must allocate the segment


● Each process desiring to access the segment must attach to it
● Reading or Writing with shared memory can be done only after
attaching into it
● After use each process detaches the segment
● At some point, one process must de-allocate the segment

While shared memory is fastest IPC, it will create synchronization issues as


more processes are accessing same piece of memory. Hence it has to be
handled separately.
Inter Process Communications
Shared Memories – Function calls

Function Meaning
int shmget(  Create a shared memory segment
key_t key,  key: Seed input
size_t size,  size: Size of the shared memory
int shmflag)  shmflag: Permission (similar to file)
 RETURN: Shared memory ID / Failure
void *shmat(  Attach to a particular shared memory location
int shmid,  shmid: Shared memory ID to get attached
void *shmaddr,  shmaddr: Exact address (if you know or leave it
int shmflag) 0)
 shmflag: Leave it as 0
 RETURN: Shared memory address / Failure
int shmdt(void *shmaddr)  Detach from a shared memory location
 shmaddr: Location from where it needs to get
detached
 RETURN: SUCCESS / FAILURE (-1)
shmctl(shmid, IPC_RMID, NULL)  shmid: Shared memory ID
 Remove and NULL
Inter Process Communications
Synchronization - Debugging

● The ipcs command provides information on inter-process


communication facilities, including shared segments.
● Use the -m flag to obtain information about shared memory.
● For example, this image illustrates that one shared memory
segment, numbered 392316, is in use:

user@user:~] ipcs -s

Semaphores ------ Semaphore Arrays --------


In the key semid owner perms nsems
system
user@user:~] ipcs -m | more
Shared
Memory ------ Shared Memory Segments --------
in the key shmid owner perms bytes nattch status
system 0x00000000 393216 user 600 524288 2 dest
0x00000000 557057 user 700 2116 2 dest
0x00000000 589826 user 700 5152 2 dest
Inter Process Communications
Summary

● We have covered

Data exchange Resource usage/access/control

Communication Synchronization
● Pipes ● Semaphores
● FIFO
● Shared memory
● Signals
● Sockets
Signals
Signals
● Signals are used to notify a process of a particular event
● Signals make the process aware that something has
happened in the system
● Target process should perform some pre-defined actions
to handle signals
● This is called ‘signal handling’
● Actions may range from 'self termination' to 'clean-up'
Get Basics Right
Function pointers
● What is function pointer?
● Datatype *ptr ; normal pointer
● Datatype (*ptr)(datatype,..); Function pointer
● How it differs from normal data pointer?
Function Pointer Data Pointer

● Holds address of function ● Holds address of an object



Pointing to a address from code ●
Pointing to a address from
segment. stack/heap/data

Dereference to execute the ●
Dereference to get value from
function address

Pointer arithmetic not valid ●
Pointer arithmetic is valid
Get Basics Right
Call back functions

Registering an event for later use


Get Basics Right
Call back functions

● In computer programming, a callback is a reference to


executable code, or a piece of executable code, that is
passed as an argument to other code. This allows a lower-
level software layer to call a subroutine (or function)
defined in a higher-level layer.
Signals
Names

● Signals are standard, which are pre-defined


● Each one of them have a name and number
● Examples are follows:

Signal name Number Description


SIGINT 2 Interrupt character typed
SIGQUIT 3 Quit character typed (^\)
SIGKILL 9 Kill -9 was executed
SIGSEGV 11 Invalid memory reference
SIGUSR1 10 User defined signal
SIGUSR2 12 User defined signal

To get complete signals list, open /usr/include/bits/signum.h in your system.


Signals
Origins

● The kernel
● A Process may also send a Signal to another Process
● A Process may also send a Signal to itself
● User can generate signals from command prompt:
‘kill’ command:
$ kill <signal_number> <target_pid>
$ kill –KILL 4481
Sends kill signal to PID 4481
$ kill –USR1 4481
Sends user signal to PID 4481
Signals
Handling

● When a process receives a signal, it processes


● Immediate handling
● For all possible signals, the system defines a default
disposition or action to take when a signal occurs
● There are four possible default dispositions:
– Exit: Forces process to exit
– Core: Forces process to exit and create a core file
– Stop: Stops the process
– Ignore: Ignores the signal
● Handling can be done, called ‘signal handling’
Signals
Handling

● The signal() function can be called by the user for


capturing signals and handling them accordingly
● First the program should register for interested signal(s)
● Upon catching signals corresponding handling can be done

Function Meaning
signal (int signal_number, void *(fptr) (int)) signal_number : Interested signal
fptr: Function to call when signal handles
Signals
Handling

P1

Registering handler
User Signal
signal / sigaction
Space handler

Signal handler
Kernel
Space executed

Process
Pointer
State
Signal generated
Process ID

Signals

Registers

Memory Limits

List of Open Files


Signals
Handler

● A signal handler should perform the minimum work


necessary to respond to the signal
● The control will return to the main program (or terminate
the program)
● In most cases, this consists simply of recording the fact
that a signal occurred or some minimal handling
● The main program then checks periodically whether a
signal has occurred and reacts accordingly
● Its called as asynchronous handling
Signals
vs Interrupt

● Signals can be described as soft-interrupts


● The concept of 'signals' and 'signals handling' is
analogous to that of the 'interrupt' handling done by a
microprocessor
● When a signal is sent to a process or thread, a signal
handler may be entered
● This is similar to the system entering an interrupt handler

• System calls are also soft-interrupts. They are initiated by applications.


• Signals are also soft-interrupts. Primarily initiated by the Kernel itself.
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)
● In order to alter/change actions, sigaction() function to be used
● Any signal except SIGKILL and SIGSTOP can be handled using this

Function Meaning
sigaction( signum : Signal number that needs to be handled
int signum,
const struct sigaction *act, act: Action on signal
struct sigaction *oldact)
oldact: Older action on signal
Signals
Advanced Handling – sigaction structure

struct sigaction
{
void (*sa_handler)(int);
void (*sa_sigaction)(int, siginfo_t *, void *);
sigset_t sa_mask;
int sa_flags;
void (*sa_restorer)(void);
}

• sa_handler: SIG_DFL (default handling) or SIG_IGN (Ignore) or Signal handler


function for handling
• Masking and flags are slightly advanced fields
• Try out sa_sigaction during assignments/hands-on session along with Masking &
Flags
Signals
vs system calls

Sending Signal
P2 P1

System call
User S/w interrupt from
Space U/S to K/S

Kernel Signals
Space S/w interrupt from
K/S to U/S
Synchronous & Asynchronous
● Wait for child to finish
Synchronous Asynchronous

Polling Interrupts

sleep wait

sleep Pause
Signals
Self Signaling

● A process can send or detect signals to itself


● This is another method of sending signals
● There are three functions available for this purpose
● This is another method, apart from ‘kill’
Function Meaning
raise (int sig) Raise a signal to currently executing process. Takes signal number
as input
alarm (int sec) Sends an alarm signal (SIGALRM) to currently executing process
after specified number of seconds
pause() Suspends the current process until expected signal is received. This
is much better way to handle signals than sleep, which is a crude
approach
Inter Process Communications
Summary

● We have covered

Data exchange Resource usage/access/control

Communication Synchronization
● Pipes ● Semaphores
● FIFO
● Shared memory
● Signals
● Sockets
Networking Fundamentals
Networking Fundamentals
Introduction


Networking technology is key behind today’s success of Internet

Different type of devices, networks, services work together

Transmit data, voice, video to provide best in class
communication

Client-server approach in a scaled manner towards in Internet

Started with military remote communication

Evolved as standards and protocols

Organizations like IEEE, IETF, ITU etc…work together in creating


global standards for interoperability and compliance
Networking Fundamentals
TCP / IP Model

7 Application Telnet DNS

6 Presentation Application FTP RIP

5 Session SMTP SNMP

4 Transport Transport TCP UDP

3 Network Inter-network IP ARP ICMP IGMP

2 Data Link Ethernet Token Ring


Link
1 Physical ATM Wireless

OSI Model TCP / IP Protocol Internet Protocol


Layers Suite
Networking Fundamentals
TCP / IP Model – Implementation in Linux

Application
7 Application details
User
6 Presentation Application process

5 Session
socket
XTI
4 Transport TCP UDP

3 Network IPv4, IPv6 kernel

2 Data Link Device Drivers Communications


And details
1 Physical Hardware

OSI Model Internet Protocol


Suite
Networking Fundamentals
Protocols

Web application protocol Web Application


User
process client server Layer

TCP protocol Transport


TCP TCP
Layer
Protocol
stack
within
kernel IP protocol Network
IP IP
Layer

Ethernet Ethernet protocol Ethernet Data Link


driver driver Layer
actual flow between client and server

Ethernet
Networking Fundamentals
ATM Protocols stack

Higher
ATM Network Higher
User
process Layer Layer

AAL AAL
Data
Link
Layer
ATM Layer ATM Layer
ATM switch ATM switch

ATM ATM
Physical Physical
PHY PHY
layer layer
Networking Fundamentals
x.25 Protocols stack

X.25 Network
User Higher Higher
process Layer Layer

Network
PLP PLP
Layer

X.25 Data
Protocol Link LAPB LAPB
suite Layer

EIA/TIA-232, EIA/TIA-232,
Physical EIA/TIA-449
EIA/TIA-449
Layer EIA-530
EIA-530
Networking Fundamentals
Addressing

● IP layer: IP address
– Dotted decimal notation (“192.168.1.10”)
– 32 bit integer is used for actual storage
– IP address must be unique in a network
– Two modes IPv4 (32 bits) and IPv6 (128 bits)
– Total bits divided into two parts
● Network
● Host
– Host part obtained using subnet mask
Networking Fundamentals
IPv4

An IPv4 address (dotted-decimal notation)

172 . 16 . 254 . 1

10101100.00010000.11111110.00000001

One byte = Eight bits

Thirty-two bits (4 x 8), or 4 bytes


Networking Fundamentals
IPv4 classes
8 Bits

A 0 1.0.0.0 to 127.255.255.255
Network Host

16 Bits

B 1 0 128.0.0.0 to 191.255.255.255
Network Host

24 Bits

C 1 1 0 192.0.0.0 to 223.255.255.255
Network Host

D 1 11 0 224.0.0.0 to 239.255.255.255
Multi-cast Address

E 1 11 1 240.0.0.0 to 255.255.255.255
Reserved for future use
Networking Fundamentals
Ipv6

An IPv6 Address (in Hexadecimal)

2001:0DB8:AC10:FE01:0000:0000:0000:0000

2001:0DB8:AC10:FE01:: Zero can be omitted

0010000000000001:0000110110111000:0000010000010000:1111111000000001:
0000000000000000:0000000000000000:0000000000000000:0000000000000000
Networking Fundamentals
ip address and domain name

● Commands related to networking


– Ifconfig (/sbin/ifconfig) command to find the ip-address of
system
– Ping – To check the connectivity using ICMP protocol
– Host – To convert domain name to ip-address
Eg: host emertxe.com
Networking Fundamentals
Ports

ENT Pedia FTP TELNET

General Ortho TFTP SSH


Hospital Transport
Reception Layer

Visitor Packet
Networking Fundamentals
Ports

● TCP/UDP layer: Port numbers


– Well known ports [ex: HTTP (80), Telnet (23)]
– System Ports (0-1023)
– User Ports (1024-49151)
– Dynamic and/or Private Ports (49152-65535)
● Port number helps in multiplexing and de-multiplexing
the messages
● To see all port numbers used in system by opening a
file /etc/services
Networking Fundamentals
Socket as a IPC
Clients Server

Socket
Process Process
Networking Fundamentals
TCP/IP three way handshake connection

Server
Client

Syn req

K
Syn req + AC

ACK

Connection established
Socket
Sockets
● Sockets is another IPC mechanism, different from other
mechanisms as they are used in networking
● Apart from creating sockets, one need to attach them
with network parameter (IP address & port) to enable it
communicate it over network
● Both client and server side socket needs to be created &
connected before communication
● Once the communication is established, sockets provide
‘read’ and ‘write’ options similar to other IPC
mechanisms
Get Basics Right
Between big endian & little endian

Example ● Let us consider the following


#include <stdio.h>
example and how it would be
int main()
{ stored in both machine types
int num = 0x12345678;

return 0;
}

1000 1001 1002 1003


1000 num
12 34 56 78 12 34 56 78
Big Endian 1004 →

1000 1001 1002 1003


1000 num
78 56 34 12 78 56 34 12
Little Endian 1004 →
Sockets
Help Functions

Host Byte Order


16 Bit 32 Bit

htons ntohs htonl ntohl

Network Byte Order


16 Bit 32 Bit

uint16_t htons(uint16_t host_short);


uint16_t ntohs(uint16_t network_short);
uint32_t htonl(uint32_t host_long);
uint32_t ntohl(uint32_t network_long);
Sockets
Help Functions


Since machines will have different type of byte orders (little
endian v/s big endian), it will create undesired issues in the
network

In order to ensure consistency network (big endian) byte
order to be used as a standard

Any time, any integers are used (IP address, Port number
etc..) network byte order to be ensured

There are multiple help functions (for conversion) available
which can be used for this purpose

Along with that there are some utility functions (ex:
converting dotted decimal to hex format) are also available
Sockets
Address

● In order to attach (called as “bind”) a socket to network address (IP


address & Port number), a structure is provided
● This (nested) structure needs to be appropriately populated
● Incorrect addressing will result in connection failure

struct sockaddr_in
{
short int sin_family; /* Address family */
unsigned short int sin_port; /* Port number */
struct in_addr sin_addr; /* IP address structure */
unsigned char sin_zero[8]; /* Zero value, historical purpose */
};

/* IP address structure for historical reasons */


struct in_addr
{
unsigned long s_addr; /* 32 bit IP address */
};
Sockets
Calls - socket

Function Meaning
int socket(  Create a socket
int domain,  domain: Address family (AF_INET, AF_UNIX etc..)
int type,  type: TCP (SOCK_STREAM) or UDP (SOCK_DGRAM)
int protocol)  protocol: Leave it as 0
 RETURN: Socket ID or Error (-1)

Example usage:
sockfd = socket(AF_INET, SOCK_STREAM, 0); /* Create a TCP socket */
Sockets
Calls - bind
Function Meaning
int bind(  Bind a socket to network address
int sockfd,  sockfd: Socket descriptor
struct sockaddr *my_addr,  my_addr: Network address (IP address & port number)
int addrlen)  addrlen: Length of socket structure
 RETURN: Success or Failure (-1)

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; /* 192.168.1.10 */
memset(&(my_addr.sin_zero), ’\0’, 8);

bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr));


Sockets
Calls - connect
Function Meaning
int connect(  Create to a particular server
int sockfd,  sockfd: Client socket descriptor
struct sockaddr *serv_addr,  serv_addr: Server network address
int addrlen)  addrlen: Length of socket structure
 RETURN: Socket ID or Error (-1)

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; /* Server port */
serv_addr.sin_addr.s_addr = 0xC0A8010B; /* Server IP = 192.168.1.11 */
Sockets
Calls - listen

Function Meaning
int listen(  Prepares socket to accept connection
int sockfd,  MUST be used only in the server side
int backlog)  sockfd: Socket descriptor
 Backlog: Length of the queue

Example usage:
listen (sockfd, 5);
Sockets
Calls - accept

Function Meaning
int accept(  Accepting a new connection from client
int sockfd,  sockfd: Server socket ID
struct sockaddr *addr,  addr: Incoming (client) address
socklen_t *addrlen)  addrlen: Length of socket structure
 RETURN: New socket ID or Error (-1)

Example usage:
new_sockfd = accept(sockfd,&client_address, &client_address_length);

• The accept() returns a new socket ID, mainly to


separate control and data sockets
• By having this servers become concurrent
• Further concurrency is achieved by fork() system call
Sockets
Calls – recv

Function Meaning
int recv  Receive data through a socket
(int sockfd,  sockfd: Socket ID
void *buf,  msg: Message buffer pointer
int len,  len: Length of the buffer
int flags)  flags: Mark it as 0
 RETURN: Number of bytes actually sent or Error(-1)
Sockets
Calls – send

Function Meaning
int send(  Send data through a socket
int sockfd,  sockfd: Socket ID
const void *msg,  msg: Message buffer pointer
int len,  len: Length of the buffer
int flags)  flags: Mark it as 0
 RETURN: Number of bytes actually sent or Error(-1)
Sockets
Calls – close

Function Meaning
close (int sockfd)  Close socket data connection
 sockfd: Socket ID
Sockets
TCP - Summary
Server Client
socket socket

bind

listen

accept
Connection establishment
connect
Data (request)
recv send

Data (reply)
send recv

close close

NOTE: Bind() – call is optional from client side


Sockets
TCP vs UDP

TCP socket (SOCK_STREAM) UDP socket (SOCK_DGRAM)


● Connection oriented TCP ● Connectionless UDP

Reliable delivery ●
Unreliable delivery
● In-order guaranteed ● No-order guarantees

Three way handshake ●
No notion of “connection”
● More network BW ● Less network BW

D1
App App
TCP UDP
1
2 Socket D 1
D2
3 2
3 Socket

D3
Sockets
UDP

Server
socket
Client
socket bind

sendto recvfrom

recvfrom sendto

close

Each UDP data packet need to be addressed separately. sendto()


and recvfrom() calls are used
Sockets
UDP – Functions calls

Function Meaning
int sendto(  Send data through a UDP socket
int sockfd,  sockfd: Socket ID
const void *msg,  msg: Message buffer pointer
int len,  len: Length of the buffer
unsigned int flags,  flags: Mark it as 0
const struct sockaddr *to,  to: Target address populated
socklen_t length);  length: Length of the socket structure
 RETURN: Number of bytes actually sent or Error(-1)

int recvfrom(  Receive data through a UDP socket


int sockfd,  sockfd: Socket ID
void *buf,  buf: Message buffer pointer
int len,  len: Length of the buffer
unsigned int flags,  flags: Mark it as 0
struct sockaddr *from,  to: Receiver address populated
int *length);  length: Length of the socket structure
 RETURN: Number of bytes actually received or Error(-1)
Client – Server Models
Client – Server Models

● Iterative Model
– The Listener and Server portion coexist in the same task
– So no other client can access the service until the current
running client finishes its task.
● Concurrent Model
– The Listener and Server portion run under control of different
tasks
– The Listener task is to accept the connection and invoke the
server task
– Allows higher degree of concurrency
Client – Server Models
Iterative Model – The Flow

● Create a socket
● Bind it to a local address
● Listen (make TCP/IP aware that the socket is
available)
● Accept the connection request
● Do data transaction
● Close
Client – Server Models
Iterative Model – The Flow

Iterative
Server Listen
Client – Server Models
Iterative Model – The Flow

Client A
Iterative
Connect Listen
Server
Client – Server Models
Iterative Model – The Flow

Client A
Iterative
Connect Accept and Process
Server
Client – Server Models
Iterative Model – The Flow

Client A
Iterative Processing
Connected
Server Client A

Client B

Connect
Client – Server Models
Iterative Model – The Flow

Client A
Iterative Processing
Connected
Server Client A

Client B

Wait
Client – Server Models
Iterative Model – The Flow

Client A
Iterative Processing
Close Client A
Server
Done
Client B

Wait
Client – Server Models
Iterative Model – The Flow

Iterative
Server Listen

Client B

Connect
Client – Server Models
Iterative Model – The Flow

Iterative Accept and Process


Server Client B

Client B

Connect
Client – Server Models
Iterative Model – The Flow

Iterative Processing
Server Client B
Done
Client B

close
Client – Server Models
Iterative Model – The Flow

Iterative
Server Listen
Client – Server Models
Iterative Model – Pros and Cons

● Pros:
– Simple
– Reduced network overhead
– Less CPU intensive
– Higher single-threaded transaction throughput
● Cons
– Severely limits concurrent access
– Server is locked while dealing with one client
Client – Server Models
Concurrent Model – The Flow

● Create a Listening socket


● Bind it to a local address
● Listen (make TCP/IP aware that the socket is
available)
● Accept the connection request in loop
● Create a new process and passing the new sockfd
● Do data transaction
● Close (Both process depending on the
implementation)
Client – Server Models
Concurrent Model – The Flow

Concurrent
Server Listen
Client – Server Models
Concurrent Model – The Flow

Client A

Connect Concurrent
Server Listen
Client – Server Models
Concurrent Model – The Flow

Client A

Connect Concurrent
Server Accept and fork
Client – Server Models
Concurrent Model – The Flow

Server
Child 1

Client A

Connect Concurrent
Server Listen
Client – Server Models
Concurrent Model – The Flow

Server Process
Child 1 Client A

Client A

Connected Concurrent
Server Listen
Client – Server Models
Concurrent Model – The Flow

Server Process
Child 1 Client A

Client A

Connected Concurrent
Server Accept and fork

Client B

Connect
Client – Server Models
Concurrent Model – The Flow

Server Process
Child 1 Client A

Client A

Connected Concurrent
Server Accept and fork

Client B

Connect Server
Child 2
Client – Server Models
Concurrent Model – The Flow

Server Process
Child 1 Client A

Client A

Connected Concurrent
Server Listen

Client B

Connected Server Process


Child 2 Client B
Client – Server Models
Concurrent Model – The Flow

Process
Server Client A
Child 1 Done

Client A

close Concurrent
Server Listen

Client B

Connected Server Process


Child 2 Client B
Client – Server Models
Concurrent Model – The Flow

Concurrent
Server Listen

Client B

Connected Server Process


Child 2 Client B
Client – Server Models
Concurrent Model – The Flow

Concurrent
Server Listen

Client B

Close Server Process


Child 2 Client B
Done
Client – Server Models
Concurrent Model – The Flow

Concurrent
Server Listen
Client – Server Models
Concurrent Model – Pros and Cons

● Pros:
– Concurrent access
– Can run longer since no one is waiting for
completion
– Only one listener for many clients
● Cons
– Increased network overhead
– More CPU and resource intensive
Inter Process Communications
Summary

● We have covered

Data exchange Resource usage/access/control

Communication Synchronization
● Pipes ● Semaphores
● FIFO
● Shared memory
● Signals
● Sockets
Threads
Threads
● Threads, like processes, are a mechanism to allow a program
to do more than one thing at a time
● As with processes, threads appear to run concurrently
● The Linux kernel schedules them asynchronously, interrupting
each thread from time to time to give others a chance to
execute
● Threads are a finer-grained unit of execution than processes
● That thread can create additional threads; all these threads
run the same program in the same process
● But each thread may be executing a different part of the
program at any given time
Threads
Single and Multi threaded Process

code data files code data files

registers registers registers registers

stack stack stack stack

thread thread thread thread

Single Threaded Process Multi Threaded Process

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

● Takes less time to create a new thread in an existing


process than to create a brand new process
● Switching between threads is faster than a normal
context switch
● Threads enhance efficiency in communication between
different executing programs
● No kernel involved
Threads
pthread API's

● GNU/Linux implements the POSIX standard thread API


(known as pthreads)
● All thread functions and data types are declared in the
header file <pthread.h>
● The pthread functions are not included in the standard C
library
● Instead, they are in libpthread, so you should add
-lpthread to the command line when you link your
program

Using libpthread is a very good example to understand differences between


functions, library functions and system calls
Threads
Compilation

● Use the following command to compile the programs


using thread libraries
$ gcc -o <output_file> <input_file.c> -lpthread
Threads
Creation

● The pthread_create function creates a new thread

Function Meaning
int pthread_create(  A pointer to a pthread_t variable, in which the
pthread_t *thread, thread ID of the new thread is stored
const pthread_attr_t *attr,  A pointer to a thread attribute object. If you
void *(*start_routine) (void *), pass NULL as the thread attribute, a thread will
void *arg) be created with the default thread attributes
 A pointer to the thread function. This is an
ordinary function pointer, of this type: void* (*)
(void*)
 A thread argument value of type void *.
Whatever you pass is simply passed as the
argument to the thread function when thread
begins executing
Threads
Creation

● A call to pthread_create returns immediately, and the


original thread continues executing the instructions
following the call
● Meanwhile, the new thread begins executing the thread
function
● Linux schedules both threads asynchronously
● Programs must not rely on the relative order in which
instructions are executed in the two threads
Threads
Joining

● It is quite possible that output created by a thread needs to be


integrated for creating final result
● So the main program may need to wait for threads to
complete actions
● The pthread_join() function helps to achieve this purpose

Function Meaning
int pthread_join(  Thread ID of the thread to wait
pthread_t thread,  Pointer to a void* variable that will receive
void **value_ptr) thread finished value
 If you don’t care about the thread return
value, pass NULL as the second argument.
Threads
Passing Data

● The thread argument provides a convenient method of


passing data to threads
● Because the type of the argument is void*, though, you
can’t pass a lot of data directly via the argument
● Instead, use the thread argument to pass a pointer to
some structure or array of data
● Define a structure for each thread function, which
contains the “parameters” that the thread function
expects
● Using the thread argument, it’s easy to reuse the same
thread function for many threads. All these threads
execute the same code, but on different data
Threads
Return Values

● If the second argument you pass to pthread_join is non-


null, the thread’s 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

● Thread attributes provide a mechanism for fine-tuning


the behaviour of individual threads
● Recall that pthread_create accepts an argument that is a
pointer to a thread attribute object
● If you pass a null pointer, the default thread attributes
are used to configure the new thread
● However, you may create and customize a thread
attribute object to specify other values for the attributes
Threads
Attributes

● There are multiple attributes related to a


● particular thread, that can be set during creation
● Some of the attributes are mentioned as follows:
– Detach state
– Priority
– Stack size
– Name
– Thread group
– Scheduling policy
– Inherit scheduling
Threads
Joinable and Detached

● A thread may be created as a joinable thread (the default)


or as a detached thread
● A joinable thread, like a process, is not automatically cleaned
up by GNU/Linux when it terminates
● Thread’s exit state hangs around in the system (kind of like a
zombie process) until another thread calls pthread_join to
obtain its return value. Only then are its resources released
● A detached thread, in contrast, is cleaned up automatically
when it terminates

Because a detached thread is immediately cleaned up,
another thread may not synchronize on its completion by
using pthread_join or obtain its return value
Threads
Creating a Detached Thread

● In order to create a detached thread, the thread


attribute needs to be set during creation
● Two functions help to achieve this
Function Meaning
int pthread_attr_init(  Initializing thread attribute
pthread_attr_t *attr)  Pass pointer to pthread_attr_t type
 Reurns integer as pass or fail

int pthread_attr_setdetachstate  Pass the attribute variable


(pthread_attr_t *attr,   Pass detach state, which can take
int detachstate); ●
PTHREAD_CREATE_JOINABLE
● PTHREAD_CREATE_DETACHED
Threads
ID


Occasionally, it is useful for a sequence of code to determine
which thread is executing it.

Also sometimes we may need to compare one thread with
another thread using their IDs
● Some of the utility functions help us to do that

Function Meaning
pthread_t pthread_self()  Get self ID

int pthread_equal(  Compare threadID1 with threadID2


pthread_t threadID1,   If equal return non-zero value, otherwise
pthread_t threadID2); return zero
Threads
Cancellation


It is possible to cancel a particular thread

Under normal circumstances, a thread terminates normally
or by calling pthread_exit.

However, it is possible for a thread to request that another
thread terminate. This is called cancelling a thread
Function Meaning
int pthread_cancel(pthread_t  Cancel a particular thread, given the
thread) thread ID

Thread cancellation needs to be done carefully, left-over resources will create


issue. In order to clean-up properly, let us first understand what is a “critical
section”?
Synchronization - Concepts
Synchronization
why?

● In a multi-tasking system the most critical resource is CPU. This is shared


between multiple tasks / processes with the help of ‘scheduling’ algorithm
● When multiple tasks are running simultaneously:
– Either on a single processor, or on
– A set of multiple processors
● They give an appearance that:
– For each process, it is the only task in the system.
– At a higher level, all these processes are executing efficiently.
– Process sometimes exchange information:
– They are sometimes blocked for input or output (I/O).
● Whereas multiple processes run concurrently in a system by
communicating, exchanging information with others all the time. They also
have very close dependency with various I/O devices and peripherals.
Synchronization
why?
● Considering resources are lesser
and processes are more, there is
a contention going between
multiple processes
● Hence resource needs to be P2
shared between multiple
processes. This is called as P1 P3
‘Critical section’
● Access / Entry to critical section Shared
is determined by scheduling, Resource
(Critical Section)
however exit from critical
section needs to be done when
activity is completed properly
● Otherwise it will lead to a
situation called ‘Race condition’
Synchronization
why?


Synchronization is defined as a mechanism which ensures that two
or more concurrent processes do not simultaneously execute some
particular program segment known as critical section

When one process starts executing the critical section (serialized
segment of the program) the other process should wait until the
first process finishes

If not handled properly, it may cause a race condition where, the
values of variables may be unpredictable and vary depending on the
timings of context switches of the processes

If any critical decision to be made based on variable values (ex: real
time actions – like medical system), synchronization problem will
create a disaster as it might trigger totally opposite action than
what was expected
Synchronization
Race Condition in Embedded Systems


Embedded systems are typically lesser in terms of resources,
but having multiple processes running. Hence they are more
prone to synchronization issues, thereby creating race
conditions

Most of the challenges are due to shared data condition. Same
pathway to access common resources creates issues

Debugging race condition and solving them is a very difficult
activity because you cannot always easily re-create the
problem as they occur only in a particular timing sequence

Asynchronous nature of tasks makes race condition simulation
and debugging as a challenging task, often spend weeks to
debug and fix them
Synchronization
Critical Section

● The way to solve race condition is to have the critical section access in
such a way that only one process can execute at a time
● If multiple process try to enter a critical section, only one can run and the
others will sleep (means getting into blocked / waiting state)

Critical Section
Synchronization
Critical Section

● Only one process can enter the critical section; the other two have to
sleep. When a process sleeps, its execution is paused and the OS will run
some other task

Critical Section
Synchronization
Critical Section

● Once the process in the critical section exits, another process is woken up
and allowed to enter the critical section. This is done based on the existing
scheduling algorithm
● It is important to keep the code / instructions inside a critical section as
small as possible (say similar to ISR) to handle race conditions effectively

Critical Section
Synchronization
Priority Inversion


One of the most important aspect of critical section is to ensure
whichever process is inside it, has to complete the activities at one
go. They should not be done across multiple context switches. This
is called Atomicity

Assume a scenario where a lower priority process is inside the
critical section and higher priority process tries to enter

Considering atomicity the higher priority process will be pushed into
blocking state. This creates some issue with regular priority
algorithm

In this juncture if a medium priority tasks gets scheduled, it will
enter into the critical section with higher priority task is made to
wait. This scenario is further creating a change in priority algorithm

This is called as ‘Priority Inversion’ which alters the priority schema
Synchronization
Priority Inversion
Synchronization
Priority Inversion
Quick refresher
● Before moving onto exploring various solutions for critical
section problem, ensure we understand these
terminologies / definitions really well.
– Difference between scheduling & Synchronization
– Shared data problem
– Critical section
– Race condition
– Atomicity
– Priority inversion
Critical section - Solutions
Critical Section
Solutions

Solution to critical section should have following three aspects into


it:

Mutual Exclusion: If process P is executing in its critical section,
then no other processes can be executing in their critical sections

Progress: If no process is executing in its critical section and there
exist some processes that wish to enter their critical section, then
the selection of the processes that will enter the critical section
next cannot be postponed indefinitely

Bounded Waiting: A bound must exist on the number of times that
other processes are allowed to enter their critical sections after a
process has made a request to enter its critical section and before
that request is granted
Critical Section
Solutions


There are multiple algorithms (ex: Dekker’s algorithm) to implement
solutions that is satisfying all three conditions. From a programmers point
of view they are offered as multiple solutions as follows:
– Locks / Mutex
– Readers–writer locks
– Recursive locks
– Semaphores
– Monitors
– Message passing
– Tuple space
● Each of them are quite detailed in nature, in our course two varieties of
solutions are covered they are Mutex and Semaphores
● Let us look into them in detail!
Critical Section
Solutions - Mutual Exclusion

● A Mutex works in a critical section while granting access


● You can think of a Mutex as a token that must be grabbed
before execution can continue. open
Mutex

Protected Resource
Critical Section
Solutions - Mutual Exclusion

● During the time that a task holds the mutex, all other tasks waiting on the
mutex sleep.

Mutex

Protected Resource

lock
Critical Section
Solutions - 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
release
Protected Resource
Critical Section
Solutions - Mutual Exclusion – Locking / Blocking

● A process may attempt to get a Mutex by calling a lock


method. If the Mutex was unlocked (means already available),
it becomes locked (unavailable) and the function returns
immediately
● If the Mutex was locked by another process, the locking
function blocks execution and returns only eventually when
the Mutex is unlocked by the other process
● More than one process may be blocked on a locked Mutex at
one time
● When the Mutex is unlocked, only one of the blocked process
is unblocked and allowed to lock the Mutex. Other tasks stay
blocked.
Critical Section
Semaphores


A semaphore is a counter that can be used to synchronize
multiple processes. Typically semaphores are used where
multiple units of a particular resources are available

Each semaphore has a counter value, which is a non-negative
integer. It can take any value depending on number of
resources available

The ‘lock’ and ‘unlock’ mechanism is implemented via ‘wait’
and ‘post’ functionality in semaphore. Where the wait will
decrement the counter and post will increment the counter

When the counter value becomes zero that means the
resources are no longer available hence remaining processes
will get into blocked state
Critical Section
Semaphores - Sleeping barber problem

A lazy barber who sleeps and gets up on his own will :)


Critical Section
Semaphores – 2 basic operations

● Wait operation:
– Decrements the value of the semaphore by 1
– If the value is already zero, the operation blocks until the value of the
semaphore becomes positive
– When the semaphore’s value becomes positive, it is decremented by 1
and the wait operation returns
● Post operation:
– Increments the value of the semaphore by 1
– If the semaphore was previously zero and other threads are blocked in
a wait operation on that semaphore
– One of those threads is unblocked and its wait operation completes
(which brings the semaphore’s value back to zero)
Critical Section
Mutex & Semaphores


Semaphores which allow an arbitrary resource count (say 25) are
called counting semaphores

Semaphores which are restricted to the values 0 and 1 (or
locked/unlocked, unavailable/available) are called binary
semaphores

A Mutex is essentially the same thing as a binary semaphore,
however the differences between them are in how they are used

While a binary semaphore may be used as a Mutex, a Mutex is a
more specific use-case, in that only the process that locked the
Mutex is supposed to unlock it

This constraint makes it possible to implement some additional
features in Mutexes
Critical Section
Practical Implementation


The problem is critical section / Global Variable
race condition is common in multi- Head Space
threading and multi-processing Environmental Strings
environment. Since both of them
offer concurrency & common Thread Thread Thread
stack stack stack
resource facility, it will raise to
race conditions
thread thre`ad thread
● However the common resource can
be different. In case of multiple
threads a common resource can be
a data segment / global variable
P1 P2 P3 Pn
which is a shared resource
between multiple threads

In case of multiple processes a Shared Memory
common resource can be a shared
memory
Synchronization
Treads - Mutex

● pthread library offers multiple Mutex related library functions


● These functions help to synchronize between multiple threads
Function Meaning
int pthread_mutex_init(  Initialize mutex variable
pthread_mutex_t *mutex  mutex: Actual mutex variable
const pthread_mutexattr_t  attribute: Mutex attributes
*attribute)  RETUR: Success (0)/Failure (Non zero)

int pthread_mutex_lock(  Lock the mutex


pthread_mutex_t *mutex)  mutex: Mutex variable
 RETURN: Success (0)/Failure (Non-zero)

int pthread_mutex_unlock(  Unlock the mutex


pthread_mutex_t *mutex)  Mutex: Mutex variable
 RETURN: Success (0)/Failure (Non-zero)
int pthread_mutex_destroy(  Destroy the mutex variable
pthread_mutex_t *mutex)  Mutex: Mutex variable
 RETURN: Success (0)/Failure (Non-zero)
Synchronization
Treads – Semaphores – 2 basic operations

● Wait operation:
– Decrements the value of the semaphore by 1
– If the value is already zero, the operation blocks until the value of the
semaphore becomes positive
– When the semaphore’s value becomes positive, it is decremented by 1
and the wait operation returns
● Post operation:
– Increments the value of the semaphore by 1
– If the semaphore was previously zero and other threads are blocked in
a wait operation on that semaphore
– One of those threads is unblocked and its wait operation completes
(which brings the semaphore’s value back to zero)
Synchronization
Treads - Semaphores
● pthread library offers multiple Semaphore related library functions
● These functions help to synchronize between multiple threads
Function Meaning
int sem_init (  sem: Points to a semaphore object
sem_t *sem,  pshared: Flag, make it zero for threads
int pshared,  value: Initial value to set the semaphore
unsigned int value)  RETURN: Success (0)/Failure (Non zero)
int sem_wait(sem_t *sem)  Wait on the semaphore (Decrements count)
 sem: Semaphore variable
 RETURN: Success (0)/Failure (Non-zero)

int sem_post(sem_t *sem)  Post on the semaphore (Increments count)


 sem: Semaphore variable
 RETURN: Success (0)/Failure (Non-zero)

int sem_destroy(sem_t *sem)  Destroy the semaphore


 No thread should be waiting on this semaphore
 RETURN: Success (0)/Failure (Non-zero)
Inter Process Communications
Synchronization - Semaphores


Semaphores are similar to counters

Process semaphores synchronize between multiple processes, similar
to thread semaphores

The idea of creating, initializing and modifying semaphore values
remain same in between processes also

However there are different set of system calls to do the same
semaphore operations
Inter Process Communications
Synchronization – Semaphore Functions

Function Meaning
int semget(  Create a process semaphore
key_t key,  key: Seed input
int nsems,  nsems: Number of semaphores in a set
int flag)  flag: Permission (similar to file)
 RETURN: Semaphore ID / Failure
int semop(  Wait and Post operations
int semid,  semid: Semaphore ID
struct sembuf *sops,  sops: Operation to be performed
unsigned int nsops)  nsops: Length of the array
 RETURN: Operation Success / Failure
semctl(semid, 0, IPC_RMID)  Semaphores need to be explicitly removed
 semid: Semaphore ID
 Remove and NULL
Inter Process Communications
Summary

● We have covered

Data exchange Resource usage/access/control

Communication Synchronization
● Pipes ● Semaphores
● FIFO
● Shared memory
● Signals
● Sockets
Process Management - Concepts
Process Management - Concepts
Scheduling

● It is a mechanism used to achieve the desired goal of


multitasking
● This is achieved by SCHEDULER which is the heart and
soul of operating System
● Long-term scheduler (or job scheduler) – selects which
processes should be brought into the ready queue
● Short-term scheduler (or CPU scheduler) – selects which
process should be executed next and allocates CPU

When a scheduler schedules tasks and gives a predictable response, they are
called as “Real Time Systems”
Process Management - Concepts
CPU Scheduling
:
● Maximum CPU utilization load store
add store CPU Burst
obtained with multi
read from file
programming
wait for I/O I/O Burst
● CPU–I/O Burst Cycle – Process
execution consists of a cycle store increment
Index CPU Burst
of CPU execution and I/O write to file
wait
wait for I/O I/O Burst

load store
add store CPU Burst
read from file

wait for I/O I/O Burst

:
Process Management - Concepts
States

new admitted exit terminated


interrupted

ready running

scheduler dispatch

I/O or event completion I/O or event wait

waiting
Process Management - Concepts
States

● A process goes through multiple states ever since it is


created by the OS

State Description
New The process is being created
Running Instructions are being executed
Waiting The process is waiting for some event to occur
Ready The process is waiting to be assigned to processor
Terminated The process has finished execution
Process Management - Concepts
Schedulers


Selects from among the processes in memory that are ready
to execute

Allocates the CPU to one of them

CPU scheduling decisions may take place when a process:
– Switches from running to waiting state
– Switches from running to ready state
– Switches from waiting to ready
– Terminates

Scheduling under 1 and 4 is non-preemptive

All other scheduling is preemptive
Process Management - Concepts
Scheduling - Types

Round Robin:

First Come First Time-slice (TS) based


Scheduling

Co-operative
Serve (FCFS)
Round Robin:
Priority based

Static: Rate
Dynamic: Earliest Deadline First (EDF)
Monotonic (RM)
Pre-emptive Priority Based
Dynamic: Earliest
Deadline First (EDF)
Process Management - Concepts
Scheduling – Types – Co-operative vs Pre-emptive

● In Co-operative scheduling, process co-operate in terms


of sharing processor timing. The process voluntarily gives
the kernel a chance to perform a process switch
● In Preemptive scheduling, process are preempted a
higher priority process, thereby the existing process will
need to relinquish CPU
Process Management - Concepts
Scheduling – Types – FCFS

● First Come First Served (FCFS) is a Non-Preemptive


scheduling algorithm. FIFO (First In First Out) strategy
assigns priority to process in the order in which they
request the processor. The process that requests the CPU
first is allocated the CPU first. This is easily implemented
with a FIFO queue for managing the tasks. As the process
come in, they are put at the end of the queue. As the
CPU finishes each task, it removes it from the start of the
queue and heads on to the next task.
Process Management - Concepts
Scheduling – Types – FCFS

Process Burst time


P1 20
P2 5
P3 3

● Suppose processes arrive in the order: P1, P2, P3 , The Gantt Chart
for the schedule is

P1 P2 P3

0 20 25 28
Process Management - Concepts
Scheduling – Types – RR: Time Sliced

● Processes are scheduled based on time-slice, but they are


time-bound
● This time slicing is similar to FCFS except that the
scheduler forces process to give up the processor based
on the timer interrupt
● It does so by preempting the current process (i.e. the
process actually running) at the end of each time slice
● The process is moved to the end of the priority level
Process Management - Concepts
Scheduling – Types – RR: Time Sliced

Process Burst time


P1 20
P2 5
P3 3

● Suppose processes arrive in the order: P1, P2, P3 , The Gantt Chart
for the schedule is

P1 P2 P3 P1 P1 P1

0 4 9 12 17 22
Process Management - Concepts
Scheduling – Types – RR: Priority

● Processes are scheduled based on RR, but priority


attached to it
● While processes are allocated based on RR (with specified
time), when higher priority task comes in the queue, it
gets pre-empted
● The time slice remain the same
Process Management - Concepts
Scheduling – Types – RR: Time Sliced

Process Burst time


P1 24
P2 10
P3 15

● Suppose processes arrive in the order: P1, P2, P3 and assume P2


have high priority, The Gantt Chart for the schedule is

P1 P2 P3 P1 P1 P1

0 4 11 16 21 24
Process Management - Concepts
Scheduling – Types – Pre-emptive


Pre-emption means while a lower priority process is executing on the
processor another process higher in priority than comes up in the ready
queue, it preempts the lower priority process.

Rate Monotonic (RM) scheduling:
– The highest Priority is assigned to the Task with the Shortest Period
– All Tasks in the task set are periodic
– The relative deadline of the task is equal to the period of the Task
– Smaller the period, higher the priority

Earliest Deadline First (EDF) scheduling:
– 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 on-
the-fly as they approach their individual deadlines
Process Management - Concepts
Scheduling – Types – Rate Monotonic (RM)

● T1 preempts T2 and T3.


● T2 and T3 do not preempt each other.
Process Management - Concepts
Scheduling – Types – Earliest Deadline First (EDF)
Introduction to RTOS
Real Time Systems
● Characteristics:
– Capable of guaranteeing timing requirements of the processes under its control
– Fast – low latency
– Predictable – able to determine task’s completion time with certainty
– Both time-critical and non time-critical tasks to coexist
● Types:
– Hard real time system
● Guarantees that real-time tasks be completed within their required deadlines.
● Requires formal verification/guarantees of being to always meet its hard deadlines
(except for fatal errors).
● Examples: air traffic control , vehicle subsystems control, medical systems.
– Soft real time system
● Provides priority of real-time tasks over non real-time tasks.
● Also known as “best effort” systems. Example – multimedia streaming, computer games
Real Time OS
● Operating system is a program that runs on a super loop
● Consist of Scheduler, Task, Memory, System call interface, File
systems etc.
● All of these components are very much part of Embedded and
Real-time systems
● Some of the parameters need to be tuned/changed in order to
meet the needs of these systems
● Real time & Embedded systems – Coupling v/s De-coupling
Real Time OS
Characteristics
● Real-time systems are typically single-purpose (Missing: Support for variety
of peripherals)
● Real-time systems often do not require interfacing with a user (Missing:
Sophisticated user modes & permissions)
● High overhead required for protected memory and for switching modes
(Missing: User v/s Kernel mode)
● Memory paging increases context switch time (Missing: Memory address
translation between User v/s Kernel)
● User control over scheduler policy & configuration
Real Time OS
Properties
● Reliability
● Predictability
● Performance
● Compactness
● Scalability
● User control over OS Policies
● Responsiveness
– Fast task switch
– Fast interrupt response
Real Time OS
Examples

● LynxOS
● OSE
● QNX
● VxWorks
● Windows CE
● RT Linux
Memory Management - Concepts
Memory Management - Concepts
Introduction

● Overall memory sub-division:


– OS
– Application
● Uni-programming vs. Multi-programming
● Memory Management is task of OS, called MMU
● May involve movement between:
– Primary (Hard disk / Flash)
– Secondary (RAM)
Memory Management - Concepts
Requirements

● Relocation
● Protection
● Sharing
● Logical Organization
● Physical Organization
Memory Management - Concepts
Requirements - Relocation

Programmer does not know where the program will be placed in
memory when it is executed

Before the program is loaded, address references are usually
relative addresses to the entry point of program

These are called logical addresses, part of logical address space
● All references must be translated to actual addresses
● It can be done at compile time, load time or execution

Mapping between logical to physical address mechanism is
implemented as “Virtual memory”

Paging is one of the memory management schemes where the
program retrieves data from the secondary storage for use in main
memory
Memory Management - Concepts
Virtual memory – Why?


If programs access physical memory we will face three
problems
– Don't have enough physical memory.
– Holes in address space (fragmentation).
– No security (All program can access same memory)

These problems can be solved using virtual memory.
– Each program will have their own virtual memory.
– They separately maps virtual memory space to physical
memory.
– We can even move to disk if we run out of memory
(Swapping)
Memory Management - Concepts
Virtual memory – Paging & page table

● Virtual memory 0
divided into 1
small chunks 00000 frame number valid-invalid bit 2 page 0
page 0
called pages. 3 page 1
2 v
page 1

Similarly 3 v 4 page 2
4 v
physical memory page 2
7 v
5
divided into page 3 8 v
6
frames. 9 v
page 4 0 i 7 page 3

Virtual memory 10,468
0 i
8 page 4
page 5 page table
and physical 12,287 9 page 5
memory mapped :
:
using page
page n
table.
Memory Management - Concepts
Virtual memory – TLB
logical
● For faster access page address
table will be stored in
CPU p d
CPU cache memory.
page frame
● But limited entries number number
only possible.

If page entry available
TLB hit
in TLB (Hit), control physical
goes to physical address
address directly
physical
(Within one cycle). f d
memory

If page entry not TLB
available in TLB (Miss), ____
it use page table from p
____
main memory and TLB miss
maps to physical f
address(Takes more ____
cycles compared to ____
TLB).
page table
Memory Management - Concepts
Page fault

● When a process
try access a
frame using a
page table and
that frame
moved to swap
memory,
generates an
interrupt called
page fault.
Memory Management - Concepts
Page fault – Handling

1.Check an internal table for this process, to determine whether the


reference was a valid or it was an invalid memory access.
2.If the reference was invalid, terminate the process. If it was valid,
but page have not yet brought in, page in the latter.
3.Find a free frame.
4.Schedule a disk operation to read the desired page into the newly
allocated frame.
5.When the disk read is complete, modify the internal table kept with
the process and the page table to indicate that the page is now in
memory.
6.Restart the instruction that was interrupted by the illegal address
trap. The process can now access the page as though it had always
been in memory.
Memory Management - Concepts
Page fault – Handling
Memory Management - Concepts
MMU
● MMU is responsible for all aspects of memory management. It is
usually integrated into the processor, although in some systems it
occupies a separate IC (integrated circuit) chip.
● The work of the MMU can be divided into three major categories:
● Hardware memory management, which oversees and regulates
the processor's use of RAM (random access memory) and cache
memory.
● OS (operating system) memory management, which ensures the
availability of adequate memory resources for the objects and
data structures of each running program at all times.
● Application memory management, which allocates each
individual program's required memory, and then recycles freed-
up memory space when the operation concludes.
Memory Management - Concepts
MMU - Relocation

● The logical address of a memory allocated to a process is the


combination of base register and limit register. When this
logical address is added to the relocation register, it gives the
physical address.
relocation
register
14000
logical physical
address address
CPU 346
+ 14346
Memory

Memory Management
Unit (MMU)
Memory Management - Concepts
Requirements - Protection
● Processes should 0
not be able to 1
reference memory 00000 frame number valid-invalid bit 2 page 0
locations in another page 0
process without 2 v
3 page 1
page 1
permission 3 v 4 page 2
page 2 4 v
5
● Impossible to check 7 v
page 3 8 v
absolute addresses 9 v
6

in programs since page 4 0 i 7 page 3


0 i
the program could 10,468 8 page 4
page 5 page table
be relocated 9 page 5
12,287
:
● Must be checked :
during execution page n
Memory Management - Concepts
Requirements - Sharing
Ed1 0
● Allow several 2

processes to Ed2 4 1 Data1


6
access the Ed3 1 2 Data3
same portion Data1 page table p1 3 Ed1
of memory P1 4 Ed2
Ed1 3
● For example, Ed2 4
5

when using 6 6
Ed3 7
shared 7 Ed3
Data2 page table p2
memory IPC, 8 Data2
P2
we need two Ed1 3 9
processes to Ed2 4
share the Ed3
6
10
2
same memory Data3 page table p3
segment
P3
Memory Management - Concepts
Requirements – Logical Organization

● Logical Organization Memory is organized linearly


(usually) In contrast, programs are organized into
modules.
● Modules can be written and compiled independently
● Different degrees of protection can be given to different
modules (read-only, execute-only)
● Modules can be shared among processes Segmentation
helps here
● In Linux, Code Segment has a read-only attribute
Memory Management - Concepts
Requirements – Physical Organization

● Processes in the user space will be leaving & getting in


● Each process needs the memory to execute
● So, the memory needs to be partitioned between
processes
– Fixed Partitioning
– Dynamic Partitioning
Networking - Concepts
Networking – concept
Types of networks

There are several different types of computer networks. Computer


networks can be characterized by their size as well as their purpose.
The size of a network can be expressed by the geographic area they
occupy and the number of computers that are part of the network.
Networks can cover anything from a handful of devices within a single
room to millions of devices spread across the entire globe.
Some of the different networks based on size are:

Personal area network, or PAN


Local area network, or LAN
Metropolitan area network, or MAN
Wide area network, or WAN
Networking – concept
Types of networks

LAN

Mostly same physical technology

No switching elements

Channel contention problem

Range: 10 m to 1 km

WAN

Different physical layer technologies

Switching elements

Hosts and routers

Internet
Networking – concept
Types of networks

● WPN
➔ Consumer home networking
➔ Connected home ‘experience’
➔ Connect everything
➔ Low range, high power
➔ Office on the move
Networking – concept
Protocols

A protocol is a set of rules and standards that basically define a


language that devices can use to communicate. There are a great
number of protocols in use extensively in networking, and they are
often implemented in different layers.
Networking – concept
Protocols
Networking – concept
Protocols- Examples

Media Access Control
Media access control is a communications protocol that is
used to distinguish specific devices. Each device is
supposed to get a unique MAC address during the
manufacturing process that differentiates it from every
other device on the internet.
● ICMP
ICMP stands for internet control message protocol. It is
used to send messages between devices to indicate the
availability or error conditions. These packets are used in
a variety of network diagnostic tools, such as ping and
traceroute.
Networking – concept
Protocols- Examples
● HTTP
HTTP stands for hypertext transfer protocol. It is a protocol defined
in the application layer that forms the basis for communication on
the web.
● FTP
FTP stands for file transfer protocol. It is also in the application layer
and provides a way of transferring complete files from one host to
another.
● DNS
DNS stands for domain name system. It is an application layer
protocol used to provide a human-friendly naming mechanism for
internet resources. It is what ties a domain name to an IP address
and allows you to access sites by name in your browser.
Networking – concept
Protocols- Examples
● SSH
SSH stands for secure shell. It is an encrypted protocol implemented in the
application layer that can be used to communicate with a remote server in
a secure way. Many additional technologies are built around this protocol
because of its end-to-end encryption and ubiquity.
● DHCP
Dynamic Host Configuration Protocol (DHCP) is a client/server protocol
that automatically provides an Internet Protocol (IP) host with its IP
address and other related configuration information such as the subnet
mask and default gateway.
● ARP
A host wishing to obtain a physical address broadcastsan ARP request onto
the TCP/IP network. The host on the network that has the IP address in the
request then replies with its physical hardware address.
Networking – concept
Some questions from protocols

1)What type of cable or transmission media is used to


connect hosts on the network?
2)How is data transmitted on the transmission media?
3)How do the hosts on the network know when to
transmit data?
4)How does each host know how much data can be
transmitted at a time?
5)How can hosts using different operating systems
communicate?
6)How can a host check the data received for
transmissions?
Stay Connected
About us: Emertxe is India’s 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

Emertxe Information Technologies,


No-1, 9th Cross, 5th Main,
Jayamahal Extension,
Bangalore, Karnataka 560046
T: +91 80 6562 9666
E: [email protected]

https://www.facebook.com/Emertxe https://twitter.com/EmertxeTweet https://www.slideshare.net/EmertxeSlides


Thank You

You might also like