fork() and exec() System Calls

Neso Academy
25 Jul 201920:34

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

00:00

🔄 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.

05:03

🛠️ 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.

10:04

🔄 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.

15:06

🔄 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.

20:06

📚 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

Multi-threading refers to a programming model where multiple threads of execution run concurrently within a single process. It is a way to achieve parallelism and is central to the video's theme of understanding concurrency in operating systems. In the script, multi-threading is mentioned as a pre-studied topic, setting the stage for the discussion on threading issues.

💡Hyper-threading

Hyper-threading is a technology that allows a single physical CPU to handle multiple threads simultaneously, effectively increasing the CPU's throughput. It is a concept related to multi-threading and is mentioned in the script as a part of the pre-lecture content, indicating the progression from basic concepts to more advanced topics like threading issues.

💡Fork System Call

The fork system call is a fundamental operation in Unix and Unix-like operating systems used to create a new process by duplicating the calling process. In the script, the fork system call is described as creating an exact replica of the parent process with a different process ID, which is crucial for understanding process creation and duplication in Linux-based systems.

💡Exec System Call

The exec system call is used to replace the current process image with a new process image. It is significant in the script as it demonstrates how a process can be replaced by another, maintaining the same process ID but with different program content, which is a key concept in process management.

💡Process ID (PID)

A Process ID (PID) is a unique identifier assigned to each process by the operating system. In the script, the concept of PIDs is used to distinguish between parent and child processes created by the fork system call and to show that the exec system call maintains the same PID when replacing a process.

💡Parent Process

A parent process is the original process from which a new process is created using the fork system call. The script explains that the parent process is the basis for the child process, which is an exact replica except for having a different PID.

💡Child Process

A child process is a process created by the parent process using the fork system call. The script illustrates how a child process is an exact replica of the parent, with the only difference being its unique PID, which is essential for understanding process hierarchy and relationships.

💡System Calls

System calls are the interface between user space and the kernel, allowing programs to request services from the operating system. In the script, system calls are introduced as a prerequisite for understanding the specific fork and exec system calls, which are central to the video's content.

💡Linux-based Systems

Linux-based systems are operating systems that use the Linux kernel. The script specifies that the fork and exec system calls are specific to Linux-based systems, which is important for understanding the context in which these system calls are used and their relevance to the video's discussion.

💡Open Source

Open source refers to a type of software whose source code is available to the public for viewing, modification, and enhancement. In the script, the presenter suggests Linux as a good choice for in-depth learning due to its open-source nature, allowing users to see and modify the system's code.

💡Process Replacement

Process replacement is the concept of replacing the current process with a new one, as demonstrated by the exec system call in the script. It is a key operation in process management, where the script shows that the process ID remains the same after replacement, but the program content changes.

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

play00:00

in the previous lecture we have studied

play00:01

about multi-threading models and hyper

play00:04

threading now the next topic that we

play00:06

need to discuss is about issues in

play00:08

threading but before we go to the issues

play00:10

that we face in threading there are two

play00:12

important things that we need to

play00:13

understand which are the fork and exe C

play00:16

system calls now after understanding the

play00:19

fork and exe C system calls will be in a

play00:21

position to understand the issues that

play00:23

we face in 3d now I'll not be explaining

play00:26

the meaning of system calls again

play00:27

because we have already done a lecture

play00:29

on system calls and if you have not

play00:31

watched it I request you to watch it so

play00:33

that you understand what system calls

play00:34

mean now before we go into fork and exe

play00:37

C system calls let me also tell you that

play00:39

this fork and exe C system calls are

play00:42

mainly specific to Linux based systems

play00:45

and you may not be able to use it

play00:47

exactly like how I am going to show you

play00:49

in a windows-based system but here since

play00:51

we are discussing about operating system

play00:53

as a whole and since we are not focusing

play00:56

on a specific operating system it is

play00:58

good to know how different things work

play01:00

on different operating systems and also

play01:02

if you are interested in learning in

play01:04

depth of our operating systems Linux

play01:06

would be a very good choice because it

play01:08

is an open source system and hence you

play01:11

can see how it is written and you can

play01:12

also modify it yourself and learn a lot

play01:14

from that all right now let us see what

play01:17

are this Fork and exe C system calls and

play01:20

what do they do so coming to the first

play01:22

one which is the fork system call the

play01:24

fork system call is used to create a

play01:26

separate duplicate process so whenever a

play01:29

fork system call is used a separate

play01:32

duplicate process of that same process

play01:35

from which it is called will be created

play01:37

so suppose you are having a process

play01:39

running and you want to create a

play01:40

duplicate or an exact replica of that

play01:43

process then we use the fork system call

play01:46

so if we are using a fork system call a

play01:48

separate duplicate process which will be

play01:51

exactly the same like the process from

play01:53

which it was called will be created but

play01:55

the only difference is that the new

play01:57

process that is created will have a

play01:59

different process ID so the process from

play02:02

which where we call the fork is known as

play02:04

the parent process and then the

play02:06

duplicate process that is created is

play02:08

known as the child process so as I said

play02:11

this is an exact replica of the

play02:13

in process with the only exception that

play02:15

it will have a different process ID all

play02:18

right now let's go to the next one which

play02:20

is the exe C system core in this exe C

play02:23

system call what happens is when an exe

play02:26

C system call is invoked the program

play02:28

specified in the parameter to the exe C

play02:31

will replace the entire process

play02:34

including all the threats

play02:36

so unlike the fork system call when you

play02:39

call an exe C system call or when an exe

play02:41

C system call is invoked then we have to

play02:44

pass into this exe C system called a

play02:47

parameter which will be another program

play02:49

so when we pass a program into this exe

play02:53

C as a parameter then that program will

play02:57

replace the entire process from which

play03:00

this exe C was cold we can understand in

play03:03

simple terms that exe C system call is

play03:06

used to replace a process with another

play03:09

process and then they will have the same

play03:12

process ID because here we are not

play03:14

creating a new process but we are

play03:15

replacing an existing one so the process

play03:18

ID will remain the same

play03:20

but the contents will be different so

play03:22

that is what this exe system call is

play03:25

used for now we don't want to simply

play03:27

learn this definition but let us try to

play03:29

see and understand how this fork and exe

play03:32

system call actually work so we will be

play03:35

writing programs for this in C language

play03:37

and then we will run it and we will

play03:39

analyze the output and we will see how

play03:41

this fork an ax is a system calls work

play03:43

as I told you in the beginning these are

play03:46

specific to linux-based systems so I

play03:47

will not be able to show you this in

play03:49

Windows so let me just switch over to my

play03:51

Linux system and there will be writing

play03:53

the program and we will see how this two

play03:55

system calls work so here I am on my

play03:58

Linux based system and I am using Linux

play04:01

Mint and here I have created two folders

play04:03

on my desktop called for an exe see and

play04:05

inside these folders I have written the

play04:08

programs where we will be able to

play04:11

understand how fork and exe C system

play04:13

calls work

play04:15

so coming to the first folder here I

play04:17

have a file called F II x1 dot C this is

play04:21

a file in which I have written a program

play04:23

in C language so let us open that up

play04:28

all right so here this is a simple

play04:31

program where we have declared a main

play04:33

function and then we are just printing

play04:36

one a line over here so the line here

play04:38

says hello Nassau Academy and then it

play04:41

says PID equal to and here I've used a

play04:44

function to get the PID which stands for

play04:46

the process ID so what this program will

play04:48

do it will just print hello NASA Academy

play04:50

and we'll print the process ID of this

play04:53

particular process that we'll be running

play04:55

so now in order to run this program let

play04:57

me go to the folder where this file was

play04:59

created which is this four folder so let

play05:03

us open that up so now here we need to

play05:05

open the terminal of our dinosaur that

play05:07

we just right click and click open in

play05:10

terminal and then the terminal will open

play05:12

for that particular folder which is in

play05:14

desktop and the folder is named Fork now

play05:17

in order to run this we type GCC space

play05:19

the name of the file so GCC is the

play05:23

compiler that is used in Linux in order

play05:25

to compile C programs so we type GCC

play05:28

followed by the name of the file that we

play05:31

created which is F x1 dot C and then you

play05:35

press Enter now when you press ENTER

play05:38

this file is compiled and the output or

play05:40

the executable file would be generated

play05:43

now if you look here you see that there

play05:45

is a file called a dot out so in the

play05:48

beginning we only had F x1 dot C now

play05:51

when we compile it here this air dot out

play05:54

file is created which is the executable

play05:56

or the output file now we need to run

play05:58

this output file in order to get the

play06:01

output

play06:01

so let's come back to the terminal and

play06:03

do that so in order to run it what we

play06:05

have to do is we have to type dot slash

play06:08

a dot out so that is the command that we

play06:11

use and when we run it here we see the

play06:14

output hello Ness Academy and the

play06:16

process ID is printed so that is the

play06:19

output of our program so that's all we

play06:21

did we just printed hello NASA Academy

play06:23

and the process ID is printed which is

play06:25

five eight two four now what we'll do is

play06:28

we'll go back to the file and let us

play06:30

edit that program and put the fork

play06:33

system call into it and see how it works

play06:35

now we come back here and I will type a

play06:38

fork system call here alright and then

play06:40

let us save this probe

play06:42

now after saving it we have to go back

play06:44

to the terminal and we have to run this

play06:47

program so we come back to the terminal

play06:48

and in order to run the program we type

play06:51

the same command GCC file name and we

play06:53

press ENTER and then the output file

play06:56

would have been created now if you look

play06:58

at this in the folder there is a dot out

play07:01

file was already there in the beginning

play07:03

now this a dot out file which is here

play07:06

now this is the new edit out that was

play07:09

created and replaced now we are going to

play07:11

run this error out in our terminal so we

play07:14

come back to the terminal and type the

play07:16

same thing dot slash I wrote out and

play07:18

here you see the output it is printed

play07:21

hello naso Academy with PID 583 seven

play07:24

and again it is printed hello NASA

play07:26

Academy with PID five eight three eight

play07:28

now you see that the program was

play07:31

executed two times and why is that that

play07:34

is because of the fork system call so if

play07:36

you look here we type fork over here and

play07:39

when we type this forth what happened

play07:40

was this program created its own child

play07:43

process that means this process was

play07:45

executed two times once by the parent

play07:48

and another time by the child so when

play07:51

this fork was used

play07:52

hello NASA Academy with the first PID

play07:54

which was five a three seven is printed

play07:56

and then the child process was created

play07:59

because of this fork and that also got

play08:01

executed printing the same thing hello

play08:03

NASA Academy with a different process ID

play08:06

which is five eight three eight so you

play08:08

see that the process IDs are different

play08:10

so that is what I told you in the

play08:11

beginning that when the fork system call

play08:13

is invoked it will create a separate

play08:16

process with a different process ID but

play08:18

the contents will be exactly the same so

play08:21

that is why the same thing is printed

play08:22

but just not to show you the process

play08:24

idea I've put this PID

play08:25

to make sure that they are different now

play08:28

let us do some modifications and try to

play08:30

find out if I come back to this program

play08:33

and insert a few more folks let's try to

play08:35

see what would be the output so here I

play08:38

am coming and I am typing two more folks

play08:40

so here now we have a total of three

play08:44

fork system cores and I have saved the

play08:46

program now if I have this three fork

play08:48

system calls what will be the output of

play08:51

my program let's try to find that out so

play08:53

we come back to the terminal and we

play08:55

while the program GCC Fe x1 dot C now

play08:58

before I run this take a moment of pause

play09:01

this video and think for yourself how

play09:03

many times will the print statement be

play09:05

executed when I have given this for

play09:08

three times and then we'll see if you

play09:11

got it right all right now let's try to

play09:13

run the program again so we type dot

play09:16

slash a dot out and let's see how many

play09:19

times is it printed okay so here we have

play09:21

our output from here if we check this is

play09:24

where we ran it and it is printed one

play09:27

two three four five six seven eight

play09:31

times so when we put three fork system

play09:34

calls it was executed eight times and in

play09:38

all of the eight times it is having a

play09:40

different process ID here it is five

play09:42

eight five zero here it's five eight

play09:43

five to here is five five five one five

play09:46

three five seven five six five four so

play09:48

it is having a different process ID at

play09:50

each of the times now can we understand

play09:53

why is it printed eight times so first

play09:56

of all what happened is that we have the

play09:58

first parent process which is d1 so let

play10:01

me call it p1 which is the first parent

play10:04

process that is there so it will print

play10:06

hello Nasir Academy with its PID just

play10:09

one time so that is what it will do so

play10:11

that is the first process that is there

play10:12

now when this first fork was executed

play10:16

what will happen is this process p1 will

play10:19

create its own child process and let me

play10:22

call it P 2 so B 2 got created and now

play10:25

there are two processes now what happens

play10:27

is when the second fork system call is

play10:30

executed then we see that there are two

play10:33

processes now in our system there will

play10:35

be child process created for each of

play10:38

these two processes so for p1 it will

play10:41

create another child process let's call

play10:43

it P 3 and for P 2 it will create

play10:46

another child process P 4 so we have a

play10:50

total of 4 processes now and now when

play10:53

the last system call or fork is executed

play10:56

what will happen is here we see there

play10:58

are four processes now and for each of

play11:01

these four processes there will be one

play11:03

child each created

play11:05

so let's say for this p1 it

play11:08

have another child process called p5 and

play11:12

for p2 it will have another child

play11:14

process let's call it P six and then for

play11:18

this p4 it will have another chunk

play11:20

processed let us call it P seven and for

play11:25

this P three over here also it will

play11:27

create one child process let's call it P

play11:30

8 so these are the number of processes

play11:34

that will be created now there are no

play11:35

more fork system calls and how many are

play11:38

created eight of them were created and

play11:40

that is why hello Nassau Academy was

play11:42

printed eight times in this output so

play11:46

here we see for each of the times it is

play11:48

having a different process ID so the

play11:51

thing is they are all new processes but

play11:54

the contents are the same with different

play11:56

process ID so that is what happened when

play11:59

we use the fork system call so I hope

play12:01

with that it is clear to you how the

play12:03

fork system call works now let us go to

play12:06

the next one which is the exe C system

play12:08

call now I will come back to the next

play12:11

folder that I have created which is the

play12:12

exe C folder where I have written two

play12:15

programs in order to make us understand

play12:16

how the exe C system calls work so let's

play12:19

go into that and see what does it do

play12:23

so as we enter this folder we see that

play12:26

I've created two C programs e^x one dot

play12:29

C and E X two dot C so these are two C

play12:32

programs so let us see what is inside

play12:33

these programs so inside the first file

play12:38

which is e x 1 dot C here I have written

play12:41

a small program where there is a main

play12:43

function with some arguments and then

play12:45

here inside main I have printed the PID

play12:49

of e^x 1 dot C that means the process ID

play12:52

of this program will be printed

play12:54

so here the ID of the X 1 dot C is equal

play12:57

to and I am using the function get PID

play12:59

to get the process ID from the system

play13:02

and here I have declared a character

play13:04

array where I have put in the words

play13:07

hello NASA Academy and the last one is

play13:10

null and after that we are using the exe

play13:13

C system call so here it is written exe

play13:16

C V so there are different type of exe C

play13:19

system calls within the family of exe C

play13:21

so this is one among them and I will not

play13:23

be explaining each of them separately

play13:25

but just now that this is an exe C

play13:27

system call and here what we are doing

play13:29

is we are passing as an argument the

play13:32

output of ex2 program that we have

play13:35

written so ax 2 was the next program

play13:38

that I have written so this is e x2 so

play13:42

I'll be showing it to you later

play13:44

here I have written the output of e^x 2

play13:46

and that is passed as an argument to the

play13:49

exe C system core and then it is written

play13:52

here another statement printf back to e

play13:54

x1 dot C and return 0 now let's go and

play13:57

look at the next file which is e x2 dot

play13:59

C and C what I have written there so

play14:01

coming to this ex2 dot C here again

play14:04

similarly we have a main function with

play14:06

some arguments and here we have just

play14:08

written to printf statements the first

play14:10

printf we are just displaying we are in

play14:12

a x2 dot C we are writing this so that

play14:15

when we enter into this ex2 this will be

play14:18

printed and we know that we are in a x2

play14:20

now and then the PID of x2 dot C will be

play14:24

printed that means the process ID of

play14:26

this particular program or process will

play14:28

be printed here using the get PID

play14:30

function now let us try to compile these

play14:33

two programs and run just one of them

play14:35

and see what

play14:37

so in order to compile these two

play14:39

programs what we have to do is we come

play14:41

to the folder where they are present and

play14:43

then we right-click and open the

play14:44

terminal and then we have to type the

play14:47

same thing that is GCC space the name of

play14:51

the file so the first one that we are

play14:53

going to compile is ex1 dot C one thing

play14:56

that you have to keep in mind here is

play14:57

that when I showed you the previous

play14:58

program of fork we saw that when we

play15:01

compile the programs in the GCC compiler

play15:03

of kleenex what it does is it will

play15:06

create a default output file with the

play15:08

name a dot out so a dot out is a default

play15:11

name of the output file that is going to

play15:13

be created but here in this case we are

play15:15

going to compile to programs x1 dot C

play15:18

any X 2 dot C and since they are

play15:20

residing in the same folder so here what

play15:22

will happen is that if I don't specify a

play15:24

different name for the output files of

play15:26

these two programs and I compile this

play15:28

e^x 1 dot C it will create a default a

play15:31

dot out file after that when I compile

play15:33

the next program which is e x 2 dot C

play15:36

then the output file that is generated

play15:38

for that is also named air dot out and

play15:41

that will replace the first arrow door

play15:43

which is created by this e x 1 dot C but

play15:46

we don't want that to happen because we

play15:47

need these two programs so in order to

play15:50

do that we have to specify the name of

play15:52

the output file specifically we have to

play15:54

type - O and we have to just specify a

play15:57

name like I have done here this e x1 is

play16:00

the name of the output file that will be

play16:02

generated for ex1 dot C now similarly we

play16:06

can do for the next one so x2 dot C and

play16:10

- or e^x to hear what happened I

play16:13

compiled ex2 dot C and the output file I

play16:16

have specified the name as e^x - now

play16:19

before we run this let us come and take

play16:21

a look at this first file X 1 dot C

play16:24

again

play16:24

so here what should be printed PID of

play16:27

e^x 1 should be printed then here we

play16:29

have declared hello NASA Academy and

play16:31

here we are invoking the exe C system

play16:33

call and as a parameter or argument we

play16:36

have passed the output file ex2 that is

play16:39

output file of the second C program that

play16:42

we have created and then it prints back

play16:44

to the X 1 dot C now let's see what will

play16:46

happen when we run it so we type dot

play16:48

slash

play16:49

and the name of the output file is ex1

play16:52

we are just going to run the first one

play16:54

now see what is the output so let me

play16:56

move it here and let us closely examine

play16:58

what happened when we executed ex1 so

play17:01

ex1 is the output of this ex1 dot C so

play17:05

here what happened it came here and it

play17:07

printed the PID of x1 dot C which is

play17:10

equal to 5 9 6 2 so it printed this line

play17:13

over here then here we just declared

play17:16

this character array and then here we

play17:19

invoke the exe C system call and what

play17:22

did we pass as an argument e x2 which is

play17:24

the output of the second program which

play17:27

is e x2 dot C and then let's see what

play17:29

happened here it is written we are in

play17:31

e^x 2 dot C now now why did that happen

play17:33

check this X 2 dot C here we are in X 2

play17:37

dot C it is written we are in X 2 dot C

play17:40

we are in X 2 dot C so we see that from

play17:43

the first program when we invoke the exe

play17:47

C system call it jumped to this program

play17:49

right and then it printed we are in X 2

play17:53

dot C and then the PID of X 2 dot C is

play17:57

printed what is it PID okay X 2 is 5 9 6

play18:00

2 so basically what happened it it did

play18:02

not jump

play18:03

what exactly happened was that when we

play18:05

call the exe C system call the content

play18:09

of the first process was replaced by the

play18:11

second process so that is why after the

play18:14

first line these things were printed

play18:16

which is the content of this second

play18:18

program X 2 dot C and if you observe it

play18:21

we see that the PID or the process ID

play18:24

remains the same so when we were in the

play18:26

first program the process ID was 5 9 6 2

play18:29

and when we are in the second program

play18:31

the process ID is still 5 9 6 2 so we

play18:34

see that the process IDs remains the

play18:37

same exe C system core will replace the

play18:40

contents of one process with another

play18:42

process so it is not creating a new one

play18:44

but the first one is replaced by another

play18:47

one that is why the process ID is the

play18:49

same but the contents are now replaced

play18:51

we only executed one of them and from

play18:54

that we came to this so that is how exe

play18:57

C system call works now if we come back

play18:59

and look at this first program X 1 dot C

play19:01

we see that they

play19:03

one more line to be printed back to ex1

play19:06

dot C which is not printed in our output

play19:08

why is that that is because when we

play19:11

entered this e^x to dot C in this second

play19:14

program we did not specify anything for

play19:17

it to go back to the first program again

play19:19

so it never had the control to come back

play19:21

to this first program again so that is

play19:23

why this portion is not printed so that

play19:26

is how Exe C system call works it

play19:28

replaced the contents of the process

play19:31

with another process so the processes

play19:33

have the same process ID admit is the

play19:35

same process but the contents are

play19:37

replaced so with that I hope it was

play19:40

clear to you about how fork and exe C

play19:42

system call works so fork will create a

play19:45

separate duplicate process which is a

play19:47

child process with a different process

play19:49

idea but the contents remain the same

play19:51

but an exe C system call it will replace

play19:54

the entire process but the process ID

play19:57

remains the same so that is how fork an

play19:59

exe C system call works so if you

play20:01

understood fork and X is a system course

play20:03

we can now move into the issues that we

play20:06

face in threading from the next lecture

play20:08

so I believe that by writing the program

play20:10

and running it and seeing the output we

play20:13

were visually able to understand and

play20:15

visualize how the fork and exe C system

play20:17

called works so I hope this was helpful

play20:19

to you thank you for watching and see

play20:21

you in the next one

play20:22

[Music]

play20:23

[Applause]

play20:25

[Music]

Rate This

5.0 / 5 (0 votes)

Связанные теги
Multi-threadingSystem CallsForkExecLinuxC ProgrammingProcess ManagementOpen SourceEducationalTechnical Tutorial
Вам нужно краткое изложение на английском?