fork() and exec() System Calls
Summary
TLDRThis lecture delves into the intricacies of multi-threading and hyper-threading, focusing on Linux-specific system calls: 'fork' and 'exec'. The 'fork' system call is highlighted for creating a duplicate process with a unique PID, while 'exec' is demonstrated to replace a process's content with another program, retaining the original PID. The presenter illustrates these concepts with C programs, showcasing the practical application and output of these system calls, providing a clear understanding of their functionality and implications in process management.
Takeaways
- π The lecture discusses multi-threading models, hyper-threading, and issues in threading, with a focus on the fork and exec system calls specific to Linux-based systems.
- π The fork system call is used to create a duplicate process that is an exact replica of the parent process, with the only difference being the new process ID.
- π¨βπ§ The parent process is the one from which the fork is called, and the child process is the duplicate process created by the fork system call.
- π€ The exec system call replaces the entire process with another program specified as a parameter, retaining the same process ID but with different contents.
- π The lecture emphasizes the importance of understanding system calls and encourages watching previous lectures for a deeper understanding.
- π‘ Linux is recommended for in-depth learning of operating systems due to its open-source nature, allowing users to view and modify the system's code.
- π Demonstrations of fork and exec system calls are conducted through C language programs, showcasing how they work through compilation and execution.
- π When multiple fork system calls are used in a program, the number of times the program executes is exponential, with each fork creating a new process.
- π The exec system call is shown to replace the process content with another program, as demonstrated by the transition from the first to the second C program in the example.
- π The script provides a step-by-step guide on compiling and running C programs to understand the practical application of fork and exec system calls.
- π The lecture concludes with a clear explanation of how fork creates a new process with a different ID but same content, while exec replaces the process content but keeps the same ID.
Q & A
What are the two important system calls discussed in the script related to threading?
-The two important system calls discussed are 'fork' and 'exec'. The 'fork' system call is used to create a duplicate process, while the 'exec' system call is used to replace the current process image with a new process image.
What is the primary difference between the 'fork' and 'exec' system calls?
-The primary difference is that 'fork' creates a new process that is an exact replica of the parent process, with a different process ID, whereas 'exec' replaces the current process with a new process, retaining the same process ID but changing the process content.
Why are 'fork' and 'exec' system calls mainly specific to Linux-based systems?
-They are mainly specific to Linux-based systems because they are part of the POSIX standard which is widely used in Unix-like operating systems, including Linux. They may not function in the same way in Windows-based systems.
What does the 'fork' system call return in the parent process and in the child process?
-The 'fork' system call returns the process ID of the child process in the parent process and returns 0 in the child process.
How does the 'exec' system call work in the context of the provided script?
-In the script, the 'exec' system call is used to replace the current process (e.g., ex1) with another process (e.g., ex2), and it retains the same process ID, but the program's content changes to that of the new process.
What is the significance of the process ID (PID) in the context of the 'fork' system call?
-The process ID (PID) is significant because it uniquely identifies a process in the system. After a 'fork', the child process has a different PID from the parent, allowing the operating system to differentiate between the two processes.
Why is it recommended to watch the lecture on system calls before this one?
-It is recommended because the lecture on system calls provides foundational knowledge about what system calls are and how they function in operating systems, which is essential for understanding the specific 'fork' and 'exec' system calls discussed in this script.
What is the role of the GCC compiler in the script?
-The GCC compiler is used to compile the C programs written to demonstrate the 'fork' and 'exec' system calls. It translates the C code into an executable file that can be run on a Linux system.
How does the script illustrate the concept of process replacement using 'exec'?
-The script illustrates this by showing a program (ex1) that uses the 'exec' system call to replace its own process image with another program (ex2), demonstrating that the process ID remains the same but the program content changes.
What is the purpose of the 'getpid()' function used in the script?
-The 'getpid()' function is used to retrieve the process ID of the current process. It is used in the script to demonstrate that the process ID changes after a 'fork' and remains the same after an 'exec'.
Why is Linux recommended for in-depth learning of operating systems in the script?
-Linux is recommended for in-depth learning because it is an open-source system, allowing learners to view and modify the system's code, providing a deeper understanding of how operating systems work.
Outlines
π Understanding Fork and Exec System Calls
This paragraph introduces the concepts of fork and exec system calls, which are crucial for understanding threading issues. The fork system call is used to create a duplicate process, resulting in a parent and a child process with the same content but different process IDs. The exec system call, on the other hand, replaces the current process with a new one specified by the parameter, maintaining the same process ID but changing the process content. The explanation emphasizes the specificity of these system calls to Linux-based systems and encourages learning on Linux due to its open-source nature, allowing for deeper understanding and modification of the system.
π οΈ Demonstrating Fork System Call in C Programming
The speaker demonstrates the fork system call through a C program that prints a greeting and the process ID. After compiling and running the program, the output shows the program's execution and the process ID. The demonstration then modifies the program to include the fork system call, which, when executed, results in the program running twiceβonce by the parent process and once by the child process, each with a unique process ID. Further modifications with additional fork calls illustrate the exponential creation of child processes and their respective outputs.
π Breakdown of Fork System Call Execution
This section provides a detailed explanation of how the fork system call works by examining the process IDs and the number of times the program executes with multiple fork calls. It clarifies that each fork call creates a new process with a unique ID, and the content of the process remains the same. The explanation uses a step-by-step approach to show how multiple fork calls lead to multiple process executions, each with a distinct process ID.
π Exploring the Exec System Call with C Programs
The paragraph explains the exec system call by showing two C programs that demonstrate its functionality. The first program prints its own process ID and then uses an exec system call to replace its content with the second program. The second program prints a message indicating its execution and its process ID. When the first program is run, it is replaced by the second program during execution, maintaining the same process ID but changing the content, thus illustrating the exec system call's behavior.
π Conclusion on Fork and Exec System Calls
The final paragraph wraps up the explanation of fork and exec system calls, summarizing their functions and differences. It reiterates that the fork system call creates a new process with a different process ID but the same content, while the exec system call replaces the entire process with another, keeping the process ID the same but changing the content. The speaker expresses hope that the visual demonstration through programming and output analysis has been helpful for understanding these concepts.
Mindmap
Keywords
π‘Multi-threading
π‘Hyper-threading
π‘Fork System Call
π‘Exec System Call
π‘Process ID (PID)
π‘Parent Process
π‘Child Process
π‘System Calls
π‘Linux-based Systems
π‘Open Source
π‘Process Replacement
Highlights
Introduction to the fork and exec system calls, essential for understanding threading issues.
Fork and exec system calls are specific to Linux-based systems and may not be directly applicable to Windows.
Linux is recommended for in-depth operating system learning due to its open-source nature, allowing for visibility and modification of its code.
The fork system call creates a separate duplicate process with a different process ID but identical content.
The exec system call replaces the entire process with another program, maintaining the same process ID but with different content.
Demonstration of compiling and running a C program to show the process ID and the effect of the fork system call.
The fork system call, when used multiple times, results in multiple processes with unique process IDs executing the same code.
Explanation of how the fork system call works with a single, double, and triple fork, leading to multiple process executions.
The exec system call is used to replace the content of a process with another program, as demonstrated with two C programs.
Process ID remains the same after an exec system call, indicating that the process itself is replaced, not created anew.
Compilation of two C programs with specific output file names to prevent overwriting and demonstrate exec system call functionality.
Running the first C program shows how the exec system call jumps to the second program, printing its content while keeping the same process ID.
The exec system call does not provide a mechanism to return control to the original process once executed.
Summary of the fork and exec system calls, emphasizing their differences in process creation and replacement.
Transition to the next lecture topic, which will cover issues faced in threading, building on the understanding of fork and exec system calls.
The practical demonstration through C programs helps visualize the working of fork and exec system calls in a Linux environment.
Transcripts
in the previous lecture we have studied
about multi-threading models and hyper
threading now the next topic that we
need to discuss is about issues in
threading but before we go to the issues
that we face in threading there are two
important things that we need to
understand which are the fork and exe C
system calls now after understanding the
fork and exe C system calls will be in a
position to understand the issues that
we face in 3d now I'll not be explaining
the meaning of system calls again
because we have already done a lecture
on system calls and if you have not
watched it I request you to watch it so
that you understand what system calls
mean now before we go into fork and exe
C system calls let me also tell you that
this fork and exe C system calls are
mainly specific to Linux based systems
and you may not be able to use it
exactly like how I am going to show you
in a windows-based system but here since
we are discussing about operating system
as a whole and since we are not focusing
on a specific operating system it is
good to know how different things work
on different operating systems and also
if you are interested in learning in
depth of our operating systems Linux
would be a very good choice because it
is an open source system and hence you
can see how it is written and you can
also modify it yourself and learn a lot
from that all right now let us see what
are this Fork and exe C system calls and
what do they do so coming to the first
one which is the fork system call the
fork system call is used to create a
separate duplicate process so whenever a
fork system call is used a separate
duplicate process of that same process
from which it is called will be created
so suppose you are having a process
running and you want to create a
duplicate or an exact replica of that
process then we use the fork system call
so if we are using a fork system call a
separate duplicate process which will be
exactly the same like the process from
which it was called will be created but
the only difference is that the new
process that is created will have a
different process ID so the process from
which where we call the fork is known as
the parent process and then the
duplicate process that is created is
known as the child process so as I said
this is an exact replica of the
in process with the only exception that
it will have a different process ID all
right now let's go to the next one which
is the exe C system core in this exe C
system call what happens is when an exe
C system call is invoked the program
specified in the parameter to the exe C
will replace the entire process
including all the threats
so unlike the fork system call when you
call an exe C system call or when an exe
C system call is invoked then we have to
pass into this exe C system called a
parameter which will be another program
so when we pass a program into this exe
C as a parameter then that program will
replace the entire process from which
this exe C was cold we can understand in
simple terms that exe C system call is
used to replace a process with another
process and then they will have the same
process ID because here we are not
creating a new process but we are
replacing an existing one so the process
ID will remain the same
but the contents will be different so
that is what this exe system call is
used for now we don't want to simply
learn this definition but let us try to
see and understand how this fork and exe
system call actually work so we will be
writing programs for this in C language
and then we will run it and we will
analyze the output and we will see how
this fork an ax is a system calls work
as I told you in the beginning these are
specific to linux-based systems so I
will not be able to show you this in
Windows so let me just switch over to my
Linux system and there will be writing
the program and we will see how this two
system calls work so here I am on my
Linux based system and I am using Linux
Mint and here I have created two folders
on my desktop called for an exe see and
inside these folders I have written the
programs where we will be able to
understand how fork and exe C system
calls work
so coming to the first folder here I
have a file called F II x1 dot C this is
a file in which I have written a program
in C language so let us open that up
all right so here this is a simple
program where we have declared a main
function and then we are just printing
one a line over here so the line here
says hello Nassau Academy and then it
says PID equal to and here I've used a
function to get the PID which stands for
the process ID so what this program will
do it will just print hello NASA Academy
and we'll print the process ID of this
particular process that we'll be running
so now in order to run this program let
me go to the folder where this file was
created which is this four folder so let
us open that up so now here we need to
open the terminal of our dinosaur that
we just right click and click open in
terminal and then the terminal will open
for that particular folder which is in
desktop and the folder is named Fork now
in order to run this we type GCC space
the name of the file so GCC is the
compiler that is used in Linux in order
to compile C programs so we type GCC
followed by the name of the file that we
created which is F x1 dot C and then you
press Enter now when you press ENTER
this file is compiled and the output or
the executable file would be generated
now if you look here you see that there
is a file called a dot out so in the
beginning we only had F x1 dot C now
when we compile it here this air dot out
file is created which is the executable
or the output file now we need to run
this output file in order to get the
output
so let's come back to the terminal and
do that so in order to run it what we
have to do is we have to type dot slash
a dot out so that is the command that we
use and when we run it here we see the
output hello Ness Academy and the
process ID is printed so that is the
output of our program so that's all we
did we just printed hello NASA Academy
and the process ID is printed which is
five eight two four now what we'll do is
we'll go back to the file and let us
edit that program and put the fork
system call into it and see how it works
now we come back here and I will type a
fork system call here alright and then
let us save this probe
now after saving it we have to go back
to the terminal and we have to run this
program so we come back to the terminal
and in order to run the program we type
the same command GCC file name and we
press ENTER and then the output file
would have been created now if you look
at this in the folder there is a dot out
file was already there in the beginning
now this a dot out file which is here
now this is the new edit out that was
created and replaced now we are going to
run this error out in our terminal so we
come back to the terminal and type the
same thing dot slash I wrote out and
here you see the output it is printed
hello naso Academy with PID 583 seven
and again it is printed hello NASA
Academy with PID five eight three eight
now you see that the program was
executed two times and why is that that
is because of the fork system call so if
you look here we type fork over here and
when we type this forth what happened
was this program created its own child
process that means this process was
executed two times once by the parent
and another time by the child so when
this fork was used
hello NASA Academy with the first PID
which was five a three seven is printed
and then the child process was created
because of this fork and that also got
executed printing the same thing hello
NASA Academy with a different process ID
which is five eight three eight so you
see that the process IDs are different
so that is what I told you in the
beginning that when the fork system call
is invoked it will create a separate
process with a different process ID but
the contents will be exactly the same so
that is why the same thing is printed
but just not to show you the process
idea I've put this PID
to make sure that they are different now
let us do some modifications and try to
find out if I come back to this program
and insert a few more folks let's try to
see what would be the output so here I
am coming and I am typing two more folks
so here now we have a total of three
fork system cores and I have saved the
program now if I have this three fork
system calls what will be the output of
my program let's try to find that out so
we come back to the terminal and we
while the program GCC Fe x1 dot C now
before I run this take a moment of pause
this video and think for yourself how
many times will the print statement be
executed when I have given this for
three times and then we'll see if you
got it right all right now let's try to
run the program again so we type dot
slash a dot out and let's see how many
times is it printed okay so here we have
our output from here if we check this is
where we ran it and it is printed one
two three four five six seven eight
times so when we put three fork system
calls it was executed eight times and in
all of the eight times it is having a
different process ID here it is five
eight five zero here it's five eight
five to here is five five five one five
three five seven five six five four so
it is having a different process ID at
each of the times now can we understand
why is it printed eight times so first
of all what happened is that we have the
first parent process which is d1 so let
me call it p1 which is the first parent
process that is there so it will print
hello Nasir Academy with its PID just
one time so that is what it will do so
that is the first process that is there
now when this first fork was executed
what will happen is this process p1 will
create its own child process and let me
call it P 2 so B 2 got created and now
there are two processes now what happens
is when the second fork system call is
executed then we see that there are two
processes now in our system there will
be child process created for each of
these two processes so for p1 it will
create another child process let's call
it P 3 and for P 2 it will create
another child process P 4 so we have a
total of 4 processes now and now when
the last system call or fork is executed
what will happen is here we see there
are four processes now and for each of
these four processes there will be one
child each created
so let's say for this p1 it
have another child process called p5 and
for p2 it will have another child
process let's call it P six and then for
this p4 it will have another chunk
processed let us call it P seven and for
this P three over here also it will
create one child process let's call it P
8 so these are the number of processes
that will be created now there are no
more fork system calls and how many are
created eight of them were created and
that is why hello Nassau Academy was
printed eight times in this output so
here we see for each of the times it is
having a different process ID so the
thing is they are all new processes but
the contents are the same with different
process ID so that is what happened when
we use the fork system call so I hope
with that it is clear to you how the
fork system call works now let us go to
the next one which is the exe C system
call now I will come back to the next
folder that I have created which is the
exe C folder where I have written two
programs in order to make us understand
how the exe C system calls work so let's
go into that and see what does it do
so as we enter this folder we see that
I've created two C programs e^x one dot
C and E X two dot C so these are two C
programs so let us see what is inside
these programs so inside the first file
which is e x 1 dot C here I have written
a small program where there is a main
function with some arguments and then
here inside main I have printed the PID
of e^x 1 dot C that means the process ID
of this program will be printed
so here the ID of the X 1 dot C is equal
to and I am using the function get PID
to get the process ID from the system
and here I have declared a character
array where I have put in the words
hello NASA Academy and the last one is
null and after that we are using the exe
C system call so here it is written exe
C V so there are different type of exe C
system calls within the family of exe C
so this is one among them and I will not
be explaining each of them separately
but just now that this is an exe C
system call and here what we are doing
is we are passing as an argument the
output of ex2 program that we have
written so ax 2 was the next program
that I have written so this is e x2 so
I'll be showing it to you later
here I have written the output of e^x 2
and that is passed as an argument to the
exe C system core and then it is written
here another statement printf back to e
x1 dot C and return 0 now let's go and
look at the next file which is e x2 dot
C and C what I have written there so
coming to this ex2 dot C here again
similarly we have a main function with
some arguments and here we have just
written to printf statements the first
printf we are just displaying we are in
a x2 dot C we are writing this so that
when we enter into this ex2 this will be
printed and we know that we are in a x2
now and then the PID of x2 dot C will be
printed that means the process ID of
this particular program or process will
be printed here using the get PID
function now let us try to compile these
two programs and run just one of them
and see what
so in order to compile these two
programs what we have to do is we come
to the folder where they are present and
then we right-click and open the
terminal and then we have to type the
same thing that is GCC space the name of
the file so the first one that we are
going to compile is ex1 dot C one thing
that you have to keep in mind here is
that when I showed you the previous
program of fork we saw that when we
compile the programs in the GCC compiler
of kleenex what it does is it will
create a default output file with the
name a dot out so a dot out is a default
name of the output file that is going to
be created but here in this case we are
going to compile to programs x1 dot C
any X 2 dot C and since they are
residing in the same folder so here what
will happen is that if I don't specify a
different name for the output files of
these two programs and I compile this
e^x 1 dot C it will create a default a
dot out file after that when I compile
the next program which is e x 2 dot C
then the output file that is generated
for that is also named air dot out and
that will replace the first arrow door
which is created by this e x 1 dot C but
we don't want that to happen because we
need these two programs so in order to
do that we have to specify the name of
the output file specifically we have to
type - O and we have to just specify a
name like I have done here this e x1 is
the name of the output file that will be
generated for ex1 dot C now similarly we
can do for the next one so x2 dot C and
- or e^x to hear what happened I
compiled ex2 dot C and the output file I
have specified the name as e^x - now
before we run this let us come and take
a look at this first file X 1 dot C
again
so here what should be printed PID of
e^x 1 should be printed then here we
have declared hello NASA Academy and
here we are invoking the exe C system
call and as a parameter or argument we
have passed the output file ex2 that is
output file of the second C program that
we have created and then it prints back
to the X 1 dot C now let's see what will
happen when we run it so we type dot
slash
and the name of the output file is ex1
we are just going to run the first one
now see what is the output so let me
move it here and let us closely examine
what happened when we executed ex1 so
ex1 is the output of this ex1 dot C so
here what happened it came here and it
printed the PID of x1 dot C which is
equal to 5 9 6 2 so it printed this line
over here then here we just declared
this character array and then here we
invoke the exe C system call and what
did we pass as an argument e x2 which is
the output of the second program which
is e x2 dot C and then let's see what
happened here it is written we are in
e^x 2 dot C now now why did that happen
check this X 2 dot C here we are in X 2
dot C it is written we are in X 2 dot C
we are in X 2 dot C so we see that from
the first program when we invoke the exe
C system call it jumped to this program
right and then it printed we are in X 2
dot C and then the PID of X 2 dot C is
printed what is it PID okay X 2 is 5 9 6
2 so basically what happened it it did
not jump
what exactly happened was that when we
call the exe C system call the content
of the first process was replaced by the
second process so that is why after the
first line these things were printed
which is the content of this second
program X 2 dot C and if you observe it
we see that the PID or the process ID
remains the same so when we were in the
first program the process ID was 5 9 6 2
and when we are in the second program
the process ID is still 5 9 6 2 so we
see that the process IDs remains the
same exe C system core will replace the
contents of one process with another
process so it is not creating a new one
but the first one is replaced by another
one that is why the process ID is the
same but the contents are now replaced
we only executed one of them and from
that we came to this so that is how exe
C system call works now if we come back
and look at this first program X 1 dot C
we see that they
one more line to be printed back to ex1
dot C which is not printed in our output
why is that that is because when we
entered this e^x to dot C in this second
program we did not specify anything for
it to go back to the first program again
so it never had the control to come back
to this first program again so that is
why this portion is not printed so that
is how Exe C system call works it
replaced the contents of the process
with another process so the processes
have the same process ID admit is the
same process but the contents are
replaced so with that I hope it was
clear to you about how fork and exe C
system call works so fork will create a
separate duplicate process which is a
child process with a different process
idea but the contents remain the same
but an exe C system call it will replace
the entire process but the process ID
remains the same so that is how fork an
exe C system call works so if you
understood fork and X is a system course
we can now move into the issues that we
face in threading from the next lecture
so I believe that by writing the program
and running it and seeing the output we
were visually able to understand and
visualize how the fork and exe C system
called works so I hope this was helpful
to you thank you for watching and see
you in the next one
[Music]
[Applause]
[Music]
5.0 / 5 (0 votes)