Intro Iphone
Intro Iphone
iOS is the operating system developed by Apple, used on its smartphones (iPhones). It was first
introduced in 2007 alongside the first iPhone. Every year, Apple releases a new version of iOS, with
the latest being iOS 18, released in September 2024.
While iOS was once used on the iPod Touch (which has been discontinued), it continues to be one
of the most popular mobile operating systems in the world, second only to Android. As of
December 2023, there were over 3.8 million apps available for iOS users to download from
Apple’s App Store.
Versions of iOS
Each year, Apple releases a new major version of iOS with improved features. Below is a list of
significant iOS versions:
1. iPhone OS 1.0 (2007): The very first version, launched with the original iPhone. It had basic
apps like Phone, Mail, Safari, and iPod.
2. iPhone OS 2.0 (2008): Introduced the App Store, allowing users to download third-party
apps.
3. iPhone OS 3.0 (2009): Added features like copy-paste, MMS, and Spotlight search.
4. iOS 4 (2010): The name changed from "iPhone OS" to iOS. Introduced multitasking,
folders, and FaceTime.
5. iOS 5 (2011): Brought features like iMessage, Notification Center, and iCloud.
6. iOS 6 (2012): Introduced Apple Maps and improved Siri.
7. iOS 7 (2013): Came with a new design and features like Control Center.
8. iOS 8 (2014): Introduced HealthKit and HomeKit for health and home automation.
9. iOS 9 (2015): Focused on performance improvements and battery life.
10.iOS 10 (2016): Added features like redesigned iMessage and Siri improvements.
11.iOS 11 (2017): Added augmented reality (AR) features and introduced Files for managing
documents.
12.iOS 12 (2018): Improved performance and introduced Screen Time to monitor device
usage.
13.iOS 13 (2019): Introduced Dark Mode and enhanced privacy features.
14.iOS 14 (2020): Added home screen widgets and App Library.
15.iOS 15 (2021): Brought new FaceTime features and Focus modes for managing
notifications.
16.iOS 16 (2022): Improved Lock Screen customization and iCloud Shared Photo Library.
17.iOS 17 (2023): Focused on communication enhancements like live voicemail and StandBy
mode.
18.iOS 18 (2024): Latest version, with new improvements in AI-powered personalization and
health tracking.
Rebranding (2010)
In June 2010, Apple officially renamed its mobile operating system from iPhone OS to iOS.
User interface
The user interface of the iPhone Operating System is characterized by direct manipulation of the
interface items and a multi-touch approach which involves the use of screen synaptic to control the
components of the iPhone. The interface controls used in iOS include switches, buttons, and sliders
which are used to either select, scroll or toggle between different applications that are currently in
use. An important feature of iOS is the response time to user input is fast. The basic system
interaction operations that are deployed in iOS include tapping, swiping, and other gestures such as
pinching . Recent developments in iOS include automatic switching depending on the orientation of
the handheld device, for instance, portrait and landscape mode. The iOS is derived from the
Macintosh OS X, implying that it draws its features from Unix Operating Systems.
• Processes—Individual threads and associated data that perform tasks, such as system
maintenance, switching packets, and implementing routing protocols.
• Kernel—Provides basic system services to the rest of IOS, such as memory
management and process scheduling. It provides hardware (CPU and memory)
resource management to processes.
• Packet Buffers—Global memory buffers and their associated management functions
used to hold packets being switched.
• Device Drivers—Functions that control network interface hardware and peripherals
(such as a flash card). Device drivers interface between the IOS processes, the IOS
kernel, and the hardware. They also interface to the fast switching software.
• Fast Switching Software—Highly optimized packet switching functions.
IOS Processes
IOS processes are essentially equivalent to a single thread in other operating systems—IOS
processes have one and only one thread each. Each process has its own stack space, its own
CPU context, and can control such resources as memory and a console device (more about
that later). To minimize overhead, IOS does not employ virtual memory protection between
processes. No memory management is performed during context switches. As a result,
although each process receives its own memory allocation, other processes can freely
access that same memory.
• Low overhead—Cooperative multitasking generally results in fewer context
switches between threads, reducing the total CPU overhead contributed by
scheduling.
• Less complexity for the programmer—Because the programmer can control where
a process is suspended, it’s easy to limit context switches to places where shared data
isn’t being changed, reducing the possibility for side effects and deadlocks between
threads.
During their lifetime, processes transition through various stages, including creation, execution,
waiting, and termination. These stages define the process life cycle in iOS.
Creation Stage
When a new process is created, it receives its own stack area and enters the new state. The
process can then move to the modification stage. If no modification is necessary, the process
moves to the execution stage.
Modification Stage
Unlike most operating systems, IOS doesn’t automatically pass startup parameters or
assign a console to a new process when it is created, because it’s assumed most processes
don’t need these resources. If a process does need either of these resources, the thread that
created it can modify it to add them.
Execution Stage
After a new process is successfully created and modified, it transitions to the ready state
and enters the execution stage. During this stage, a process can gain access to the CPU
and run.
During the execution stage, a process can be in one of three states: ready, run, or idle. A
process in the ready state is waiting its turn to access the CPU and to begin executing
instructions. A process in the run state is in control of the CPU and is actively executing
instructions. An idle process is asleep, waiting on external events to occur before it can be
eligible to run.
A process transitions from the ready state to the run state when it’s scheduled to run. With
non-preemptive multitasking, a scheduled process continues to run on the CPU until it
either suspends or terminates. A process can suspend in one of two ways. It can explicitly
suspend itself by telling the kernel it wants to relinquish the CPU, transition to the ready
state, and wait its next turn to run. A process can also suspend by waiting for an external
event to occur. When a process begins waiting on an event, the kernel implicitly suspends
it by transitioning it to the idle state, where it remains until the event occurs. After the event
occurs, the kernel transitions the process back to the ready state to await its next turn to run.
Termination Stage
The final stage in the process life cycle is the termination stage. A process enters the
termination stage when it completes its function and shuts down (called self termination)
or when another process kills it. When a process is killed or self terminates, the process
transitions to the dead state. A terminated process remains in the dead state, inactive, until
the kernel reclaims all of its resources. The kernel might also record statistics about the
process’ stack when it terminates. After its resources are reclaimed, the terminated process
transitions out of the dead state and is totally removed from the system.
Process scheduler
Process scheduling is a major concept of iOS that facilitates the multitasking and multiprocessing
abilities of iOS. The scheduling concept is used to determine the way processes are assigned to the
available CPU time. The task of the process scheduler/dispatcher is to select a task from spooled
tasks and sequentially load it into the memory. For fast user response, iOS uses the short-term
process scheduler. The short-term scheduler is primarily responsible for choosing which of the
processes that have been already loaded into the memory are to be executed next depending on a
CPU clock interrupt, an IO device interrupt, or a system call.
The short-term scheduler is ideal for iOS due to its ability to execute processes more frequently than
long-term and medium-term schedulers. The process scheduler used in iOS is non preemptive/
cooperative, implying that the scheduler can not force a process out of the CPU.
Dispatcher
The main aim of the dispatcher in iOS is to control the processes to be executed by the CPU. The
dispatcher in the iOS has to be ultrafast to facilitate fast process switching.
PCB in iOS
• A Process Control Block (PCB) in iOS holds process-specific information such as:
• Process ID
• Parent process ID
• Priority
• Register states
• Memory pointers
• I/O status information
• Thread information (Mach threads)
Process Examples
You can use the show process command to see a list of all the processes in a system along
with some run-time data about each one, as demonstrated in Example 1-4.
The following list describes each of the show process command output fields found in
Example 1-4.
• PID—Process identifier. Each process has a unique process identifier number to
distinguish it from other processes.
• Qty—Process priority and process state. The first character represents the process’
priority as follows:
— K—No priority, process has been killed.
— D—No priority, process has crashed.
— X—No priority, process is corrupted.
— C—Critical priority.
— H—High priority.
— M—Medium priority.
— L—Low priority.
The remaining two characters in this field represent the current state of the
process as follows:
— *—Process is currently running on the CPU.
— E—Process is waiting for an event (event dismiss).
— S—Process is suspended.
— rd—Process is ready to run.
— we—Process is idle, waiting on an event.
— sa—Process is idle, waiting until a specific absolute time occurs.
— si—Process is idle, waiting for a specific time interval to elapse.
— sp—Process is idle, waiting for a specific time interval to elapse (periodic).
— st—Process is idle, waiting for a timer to expire.
— hg—Process is hung.
— xx—Process is dead.
• PC—Contents of the CPU program counter register when the process last
relinquished the CPU. This field is a memory address that indicates where the process
begins executing the next time it gets the CPU. A value of zero means the process is
currently running.
• Runtime—Cumulative amount of time (in milliseconds) the process has used the
CPU.
• Invoked—Total number of times the process has run on the CPU since it was created.
uSecs—Average amount of CPU time (in microseconds) used each time the process
is invoked.
• Stacks—Stack space usage statistic. The number on the right of the slash (/) shows
the total size of the stack space. The number on the left indicates the low water mark
for the amount of free stack space available.
• TTY—Console device associated with this process. Zero indicates the process does
not own a console or communicates with the main system console.
• Process—Name of the process. Process names need not be unique (multiple copies
of a process can be active simultaneously). However, process IDs are always unique.
If you issue the show process command on several different IOS systems, you’ll notice
some processes appear on every one. Most of these are processes that perform
housekeeping or provide services to other processes. Table 1-2 describes the most common
of these processes and the tasks they perform.
All the processes in Table 1-2, except EXEC, are created by the kernel during system
initialization and normally persist until IOS is shut down.
Concurrent processes
Concurrent processes in an operating system are two processes running at the same time that can
function independently without impairing the overall performance of the operating system.
IOS Kernel
When used within the context of operating systems, the word kernel usually conjures
pictures of an operating system core that runs in a special protected CPU mode and
manages system resources. Although the IOS kernel does help manage system resources,
its architecture differs from those in other operating systems. The IOS kernel is not a single
unit but rather a loose collection of components and functions linked with the rest of IOS
as a peer rather than a supervisor. There’s no special kernel mode. Everything, including
the kernel, runs in user mode on the CPU and has full access to system resources.
The kernel schedules processes, manages memory, provides service routines to trap and
handle hardware interrupts, maintains timers, and traps software exceptions. The major
functions of the kernel are described in more detail in the following sections.
The Scheduler
The actual task of scheduling processes is performed by the scheduler. The IOS scheduler
manages all the processes in the system using a series of process queues that represent each
process state. The queues hold context information for processes in that state. Processes
transition from one state to another as the scheduler moves their context from one process
queue to another. In all, there are six process queues:
• Idle queue—Contains processes that are still active but are waiting on an event to
occur before they can run.
• Dead queue—Contains processes that have terminated but need to have their
resources reclaimed before they can be totally removed from the system.
• Ready queues—Contain processes that are eligible to run. There are four ready
queues, one for each process priority:
— Critical
— High
— Medium
— Low
When a running process suspends, the scheduler regains control of the CPU and uses an
algorithm to select the next process from one of its four ready queues. The steps for this
algorithm are as follows:
Step 1 The scheduler first checks for processes waiting in the critical priority
ready queue. It runs each critical process, one by one, until all have had
a chance to run.
Step 2 After all critical processes have had a chance to run, the scheduler checks
the high priority queue. If there are no high priority processes ready, the
scheduler skips to Step 3 and checks the medium priority queue.
Otherwise, the scheduler removes each high priority process from the
queue and allows it to run. Between each high priority process, the
scheduler checks for any critical processes that are ready and runs all of
them before proceeding to the next high priority process. After all high
priority processes have had their chance, the scheduler skips the medium
and low process queues and starts over at Step 1.
Step 3 After there are no high priority processes waiting to run, the scheduler
checks the medium priority queue. If there are no medium priority
processes ready, the scheduler skips to Step 4 and checks the low priority
queue. Otherwise, the scheduler removes each medium priority process
from the queue and allows it to run. Between each medium priority
process, the scheduler checks for any high priority processes that are
ready and runs all of them (interleaved with any critical priority
processes) before proceeding to the next medium priority process. After
all medium priority processes have had their chance, the scheduler skips
over the low priority queue and starts over at Step 1 again. The scheduler
skips the low priority queue a maximum of 15 times before proceeding
to Step 4. This threshold is a failsafe mechanism to prevent the low
priority processes from being starved.
Step 4 After there are no medium or high priority processes waiting to run (or
the low priority queue has been skipped 15 times) the scheduler checks
the low priority queue. The scheduler removes each low priority process
from the queue and allows it to run. Between each low priority process,
the scheduler checks for any ready medium priority processes and runs
them (interleaved with any high and critical priority processes) before
proceeding to the next low priority process.
Step 5 The scheduler finally returns to Step 1 and starts over.
The show process cpu command provides CPU utilization statistics averaged over three
intervals: 5 seconds, 1 minute, and 5 minutes. The 5-second statistic is shown as two values
separated by a slash, where the left value represents total CPU usage, and the right value indicates
CPU usage for interrupt processing. For the 1-minute and 5-minute intervals, usage is presented as
exponentially decaying averages. Additionally, the CPU usage statistics for individual processes are
calculated every 5 seconds, enabling identification of the most active processes and their respective
CPU consumption. For example, if total usage is 90% and interrupt processing accounts for 82%,
the remaining 8% is used by scheduled processes.
Interrupt processing often dominates CPU time when fast packet switching occurs, as seen in
scenarios with high interrupt usage. However, there is no detailed account of interrupt processing,
with most of the time being used for packet switching. The remaining idle CPU time, such as 10%
in the example, is actually used by the scheduler to manage processes, highlighting its critical role
in maintaining system stability. As CPU utilization approaches 100%, insufficient idle time for the
scheduler may lead to system failure.
To mitigate the risk of runaway processes, IOS employs a watchdog timer. This mechanism allows
the scheduler to monitor processes and regain control if a process runs too long. By default, the
watchdog timer expires after 2 seconds of continuous process execution. A warning message is
displayed upon the first expiration, and the process is terminated if it exceeds the limit again. These
safeguards, combined with interrupt throttling in some systems, prevent unresponsive behavior and
ensure system reliability.
Inter-Process Communication in iOS
iOS uses the following IPC mechanisms:
• Mach Ports: Used for communication between processes.
• Shared Memory: Allows processes to share data efficiently.
• Notification Center: Broadcasts notifications between apps.
Piping in iOS
• iOS supports piping using UNIX-style pipe() system calls.
• Pipes allow data to be passed between processes in a unidirectional manner.
Advantages:
• Simple to use.
• Ensures thread safety by blocking other threads from accessing the critical section until the
lock is released.
Disadvantages:
• If a thread holding the lock crashes or forgets to unlock, it can cause a deadlock.
• Slightly slower compared to lower-level locks like os_unfair_lock.
2. NSRecursiveLock
Overview: NSRecursiveLock is a variation of NSLock that allows the same thread to acquire
the lock multiple times without causing a deadlock. This is useful for recursive functions.
Code Example:
let recursiveLock = NSRecursiveLock()
recursiveLock.lock()
// Code inside the first lock
recursiveLock.lock()
// Code inside the nested lock
recursiveLock.unlock()
recursiveLock.unlock()
Advantages:
• Prevents deadlocks in recursive functions by allowing the same thread to lock multiple
times.
Disadvantages:
• Slightly more overhead compared to NSLock.
Advantages:
• Lightweight and efficient.
• No need to explicitly lock and unlock; the queue manages execution order.
Disadvantages:
• Not suitable for tasks that need to be canceled or interrupted easily.
4. os_unfair_lock
Overview: os_unfair_lock is a low-level lock introduced in iOS 10, optimized for
performance. It prevents priority inversion by giving priority to higher-priority threads.
Code Example:
var unfairLock = os_unfair_lock_s()
os_unfair_lock_lock(&unfairLock)
// Critical Section
os_unfair_lock_unlock(&unfairLock)
Advantages:
• Highly efficient and fast.
• Avoids priority inversion issues.
Disadvantages:
• Cannot be used with recursive locking.
• Only works in modern iOS versions.
5. pthread_mutex
Overview: pthread_mutex is a POSIX-compliant lock available in C. It provides a flexible and
low-level mechanism for mutual exclusion.
Code Example:
var mutex = pthread_mutex_t()
pthread_mutex_init(&mutex, nil)
pthread_mutex_lock(&mutex)
// Critical Section
pthread_mutex_unlock(&mutex)
pthread_mutex_destroy(&mutex)
Advantages:
• Highly flexible and robust.
• Suitable for cross-platform applications.
Disadvantages:
• Requires manual setup and teardown.
6. @synchronized
Overview: @synchronized is an Objective-C feature for automatic locking and unlocking of a
block of code.
Code Example:
@synchronized(self) {
// Critical Section
}
Advantages:
• Easy to use.
• No need to explicitly unlock; the block handles it automatically.
Disadvantages:
• Deprecated and not recommended in Swift projects.
7. DispatchSemaphore
Overview: A semaphore controls access to a resource by maintaining a count, which determines
how many threads can access it simultaneously.
Code Example:
let semaphore = DispatchSemaphore(value: 1)
semaphore.wait()
// Critical Section
semaphore.signal()
Advantages:
• Flexible for controlling access to limited resources.
Disadvantages:
• Slightly more complex than simple locks.
8. NSCondition
Overview: NSCondition allows threads to wait for a condition to be signaled before proceeding.
Code Example:
let condition = NSCondition()
condition.lock()
// Wait for a condition
condition.signal()
condition.unlock()
Advantages:
• Supports conditional locking and signaling.
Disadvantages:
• More complex to implement.
9. Atomic Properties
Overview: Atomic properties ensure that read and write operations on a variable are thread-safe.
Code Example:
@Atomic var value: Int = 0
Advantages:
• Simplifies thread-safe variable access.
Disadvantages:
• Limited to single variable access; does not provide full mutual exclusion.
Advantages:
• Ideal for file-based resource access.
Disadvantages:
• Only applicable to file resources.