what is an
um
API ? AppliancesTamming Interface.
API is !
Before getting into Process Apl, let us try to understand what an
It is a set of protocols to let different pieces of software communicate
with each other and share data
.
Developers use APls to connect
chunks of code from different places in order to create the web and
mobile applications we use
every .
day
For instance , consider the apps like uber, ola, zomato , etcc.
swiggy
Along with placing a food order/a ride
all these
apps provides an option to track
order ?
your
How do they provide it ?
Do
they build their own
map systems ? No ! -
to provide
then how ? >
-
they use
Google maps APls
location-based services.
In particular, inorder to show the restaurant location
and delivery route to the customer
,
-
swiggy sends a
request Ge
Mapsto
with source (restaurant
and destination (your home)
-
Google maps respond with
* best y
route distance - estimated
delivery time etc...
Benefit
-
:
swiggy can focus our food delivery ,
not on
building complex
map softwaree.
like
sunsilarly these apps use Payment Apl providers
,
Google Pay/Paytm
to integrate online payments without
building a
payment system from
.
scratch
Advantagesof using an
Don't referent the wheel
A re-usability -
>
> saves
-
tirse and no need to code complex features from scratch
.
A focus on core business
A enable rich features
*
security
The that discussed
Apls we above are
generally termed as web APIs .
sinjilarly ,
there are other types of API like
System API ,
library Apl etc..
In this course, we focus on APIs > the APIs provided by the
system -
to let programs interact with hardware and Os services.
operating system
eg. -
Process AP1 >
-
create/manage process
File APl >
-
read/write files
Memory API > manage memory
-
thread API >
-
work with multiple threads etc...
Gal In thischepter : Process API
Question : should a
process have a direct access to hardwarea critical
os resources ? No !
they must request permission through "system calls" use
- function call into os code that runs at a higher privilege level
of the CPU .
- sensitive operations (eg :- access to hardware) are allowed
higher privilege leve.
l
only at a
(will learn more about systems calls in the next chapter
Recall that in the last lecture ,
we talked about the conceptual steps
of process creation
.
&
user
double click on an
Process API
command
icon/type a
to viitiate a
process ·
how a user
programs tells what
os does to create
the Os to create a
process ?
a
process
user : customer who
wants to order food
os implements the steps
nitchnig a
system call :
menu and kitchen starts
reading the
food.
placing as order with preparing
waiter
&
Process related sym calls
i UNIX)
* fork() -
creates a new child process.
-all process are created by forking from a parent
who is the ancestor all the - the mit
of process ? process
exec1)-makes a process execute executable
* a
given
A wait()-causes a parent to block until child terminates
variants above also .
exists
many of
Parent child
Thefor system
call
A a new process is by making a copy of
created
fork()
>
-
fork()
parent's memory image.
I .,
after calling fork 2) ,You have two processes.
-the parent process (original)
-
the child process (newly created
to
* the child process gets a
unique process ID and gets added
the list and scheduled (i ready
Os
process gets to
.,
run
* Note that the child has the same code
data,
- a
copy of the
memory (stack, heap ,
etc.)
-but a
separate independent address space.
(so changes in child do not affect the parent and vice-versal
* parent and child will execute the code after the fork() call
but with different return values
In particular , fork() returns
A 0 to the child process
A childs PID (the enteger) to the parent
-1 facted.
*
if fork()
Depending on
which among the
and the
parent
child processes gets
scheduled first !
Relationw cl
parent and childprocesses : thewaisystem
3
exit()
Different types of process > by calling
-
terminiated
terminiation scenarios
killed the
process exists as
- os process zombies
but still entry in the
Why Zombie ? - it is dead,
occupies an
process table -
How to zombies ? parent wait()
chanup > calls
- .
or reap
What
*
if the parent temiiates before child ?
the ancester of all process mit adopts orphans and
reap them .
>
-In addition to the
code, here
previous
the
Watt
↑
parent is
calling
()
Do
you expect any
difference in the output
compared to before ?
YES !
call to the code above makes the output
Adding a wait()
deterministic.
In particular, the child will print first
seem
i
the parent wait for the child to
even if runs first, it politely
returns , and then the
finish running ,
then wait)) parent resumes.
all
theexecsystem
important piece of the process creation API
an
after fork() > parent and child are running the same
-
code.
> which is not
-
very useful !
* a process run execs >
-
to load another executable to its
a .
child run a different program from parent
memory image >
-
of you only use exec1) >
- the process doesn't change its PID
,
but new code, stack and heap.
·
Thus , if you don't fork1) before execl) ,
be overwritten
your current process wil
a variant of exec()
-
execu + fork() >
-
Very powerful.
In the fell, example, the child process calls execupl)
-
worder to run the
words limes,
program we (word court program, telling us how many ,
etc. are the
bytes ,
file which
input ,
here is the source
file P3 .
c
itself)
Basically,
execcs (If executed
succesfully) ,
transforms
it
the
currently
running program
(formerly P3)
into different
a
running program
CWoj .
Summanfork
, W exec))
(1 ait
,
Read : Chapter 5
OSTEP