Program vs Process (low level concepts)

NeetCodeIO · Intermediate ·⚡ Algorithms & Data Structures ·1y ago

Key Takeaways

The video explains the difference between a program and a process, discussing how a program is a sequence of instructions and data stored on disk, while a process is an instance of a program stored in memory and executed by the CPU, utilizing tools such as the Java Virtual Machine (JVM) and Python interpreter.

Full Transcript

program is not a process by core dumped probably my new favorite YouTube channel because it takes me back to the days when I was just getting a computer science degree and I was actually learning about things that were interesting most people learn to code so they can actually build things that are genuinely useful not me I just like understanding how things work and this channel is going to let us do that a question that arises in computer science is what to call all the activities performed by the CPU I hate to pause it right now but I think so just based on the title The first thing that I think of is that a program versus a process what's the difference um I guess a process is just an instance of a program for example like for one program you can have multiple processes and a program I believe is just going to be stored on your dis so it's like persisted there versus a process is going to be stored in memory and it's going to be shortlived or temporary at the very least early computers were batch systems that executed work units called jobs as I discussed in my video about concurrency when time sharing operating systems emerged they were designed to share the CPU among multiple users so it was said that those systems ran user programs interesting I actually don't know very much at all about very early computers eventually the home computer Market popularized the use of concurrency to allow a single user to run multiple programs at once that's very interesting actually let me replay that did he say concurrency eventually the home computer Market popularized the use of concurrency the use of concurr currency what he means by that I think is not like this is like the invention of a multi-threaded or like multi-core CPU it's more that just you have multiple processes running at the same time and then the CPU is going to contact switch between them uh that's very interesting so that was probably even with a single core CPU to allow a single user to run multiple programs at once but in computer science we don't refer to these entities as programs but rather as processes so today we are going to learn the difference that's very interesting to this shows me how much I actually don't know because I thought Linux is just I I believe from its birth was using like processes so that's interesting it's kind of hard to believe that there was a point you literally couldn't run multiple processes at the same time just imagine living like that today I don't even think it's possible Concepts hi friends my name is George and this is core dumped for the vast majority the answer to this question seems obvious programs after all that's what CPU does right it runs programs but as I mentioned before in computer science the correct term is process that's technically correct I wonder how mad people would get though if you just said program kind of reminds me of the other day how I said pointer in technically the wrong context informally a process is usually defined as a program in execution even though this definition is technically correct what I find frustrating is that it is not enough to help a casual reader understand what a process is so let's start by establishing what a process is not think about this interesting I think he's right CU like the devil is in the detail to say that a process is an instance of a program isn't really getting into the hardware and like like I said kind of at the beginning the hardware matters like the dis versus like the memory where each is stored and how it's run and all that different programs are obviously intended for different purposes but in many respects they are similar when executing as they all reduce to the fundamental actions performed by the CPU and here's where we can start making distinctions a program is a sequence of instructions and the data that the CPU needs to accomplish a specific task now if you carefully Analyze This description you'll notice that it can also describe an executable file and that's because that's pretty much what a program is let's say like you have a c program you compile it it turns into like CPU instructions that the CPU is going to load into memory and execute that makes sense so what is this data at the top of my head I actually don't know cuz the executable file it has data itself what does that mean cuz I know when a process is running it's going to have some memory allocated like the stack space Etc but the executable file itself has data I'm curious about that so I wanted to at least have like a little bit of understanding of what that meant so I just went to chat gbt and asked it so can you give me a simple example of a c program after compilation what parts of it would be the CPU instructions and which parts of it would be the data I think that the CPU instructions are like Rel relatively straightforward in terms of like if you had conditional statements loops and things like that like the control flow that makes sense that those would turn into CPU instructions but I was surprised to see that so if you have like Global variables and for example string literals those are actually saved in the data region after the program is compiled and so local variables like this are going to be as we would kind of expect pushed onto like the stack space and I would assume like the same thing with recursive calls of course but it's interesting to me that a string literal like this would be saved not as like a CPU instruction but rather in the data region so what does that mean like if I had multiple print statements would that same string be copied multiple times I'm assuming not CU that sort of optimization could be made but actually now that I think of it on second thought if there are distinct strings and you want to update one of the strings you don't necessarily want both strings to be updated I think if we were to dig deeper into this we would probably be getting really into the realm of like operating system so I guess I won't do that right now but it does make sense that a lot of variables and just large amounts of data are going to be stored in the data region of an executable file separate from the code instructions and so all of that is going to be loaded into memory when it's actually run but there's going to be separate memory regions like the stack and probably the Heap that can complement the data section a program is that passive entity that resides in your storage waiting for you to click on it to tell the the computer to start running it as I've mentioned many times in the past to run a program it first needs to be loaded into memory as that's where the CPU can start fetching instructions and data when loaded into memory the section containing the executable code is known as the text section while data such as Global variables and constant values is loaded into the data section okay I mean he literally just answered the question sorry about that but running a program requires more than these two sections it also need extra space in memory to store all the data generated at runtime like user input and temporary results or variables yeah so he said at runtime like there's going to be separate memory so I don't think he's probably going to have enough time to like go into the details of every little thing in this video but I'll try to add like my commentary which is that so conceptually think about it for a second why would we not have the memory up front for something like a recursive function or even different variables that may or may not necessarily be allocated why do we need like a variable amount of memory because you don't necessarily know you could have a recursive function depending on the input to the program it might stack up 10 times before hitting the base case or depending on a second input it might stack up 20 times if you pre-allocate the memory you don't necessarily know how much memory it's going to take now even the stack is technically Limited in memory that's why like stack Overflow occurs like this region called the stack which recursive functions use it will hit a limit like there is a limit to this I think you can sometimes update that limit many languages will have like a default limit size but anyways we already know a lot about the stack and the Heap such as the fact that they are growing and shrinking in size all the time the tech section is the only one that never change neither in size nor content interesting that would make sense based on like the CPU instructions text is not going to change stack is obviously growing for reasons I talked about not just for recursive functions but also like one function calls another it's going to go on the stack when that function finished it's going to return back to the previous function the Heap I actually don't remember off the top of my head but I'm pretty sure one program if it's using Heap memory the amount of memory that that program is actually using from the Heap could change but theoretically you should be able to use as much memory as your computer has uh for the Heap let me actually just double check that just from our cursory Google search I guess this part is actually AI generated but it does look like a program can use unlimited amounts of Heap memory what's the default stack memory limit in C for example um it looks like it's 1 Megabyte I don't know if that depends on the compiler but again like you can see that this is like relatively small so it's reasonable that you could hit a stack Overflow even if there's not infinite recursion like that's why recursion is actually not good and you're hearing this from me the data structures guy on YouTube that generally speaking you don't want to do recursion even for valid recursion you could hit the memory limit anyways but we can't say the same about the data section because even though it doesn't change in size its content may varies depending on what the running program is doing as you can see so the data region can vary in size I mean if this is part of the executable I wonder if that means that at runtime that that is like the amount of memory that's being used by that data region is going to change but like all that memory should be allocated for the programmer maybe I'm misunderstanding let me just replay it just in case can't say the same about the data section because even though it doesn't change in size its content may varies depending on what the running program is doing as you can see even though initially we loaded the program into memory when running this layout can no longer be considered as a program of course not it has now become a process and please note that this is the memory layout of the process not the process itself the reason this definition is often con that's interesting so that's the memory layout of the process but not the process itself so how do you define a process in my head it's an operating system level unit right like the operating system is managing the process it gives it an ID if there are multiple threads that are a part of that process they will possibly share memory I guess we're getting overly technical let's just continue considered informal is that the notion of what a process is is far too complex to be captured in just a few words we're not going to go into technical details in this video because there's a video about processes already scheduled but for now let me help you get an idea of what a process is consider that little program that comes with your operating system to open text files now imagine we have two files to read if we open both files the operating system will launch the text editor app twice this results in two separate Windows each displaying a different text file both are the same program but two completely different processes right each process has its own memory space of course while the content of the text section in both processes memory is identical since they are the same program the data they are working with is different right so that obviously makes sense and it also makes sense that in a text editor all of the data would be going to the Heap you again wouldn't want to run out of like stack space for that imagine just declaring a massive variable with megabytes like a megabyte long string or something like that probably want to avoid something like that in this example the first process is working with a much larger text file than the second so it naturally requires more memory to manage its data and if you're a casual viewer hopefully that's everything you needed to understand the difference between a program and a process I are we casual viewers I don't feel like I'm a casual viewer I always want to know like the exact details and by the way definitely subscribe to this guy if you're not convinced by now definitely subscribe to this guy Cor dumped I don't want to just call him this guy I actually know him very little uh he's very nice guy he's actually perfectly okay with me reacting to this video I probably should have mentioned that towards the beginning but his channel is just fantastic I could not get enough of it I want to send a special thanks to all of you because we finally surpassed 100,000 subscribers I enjoy learning new things as much as you do in the same way I encourage you to check out brilliant if you're going to get brilliant definitely go to cump's channel and use the link in his description and make sure to support him I emphasize that a program by itself is not a process a program is a passive entity whereas in contrast a process is an active entity although two processes may be associated with the same program they are nevertheless considered two separate execution sequences I know that having a notion of what a process is is not the same as being able to Define it in technical terms but as I mentioned earlier a dedicated video on processes is coming soon this episode is not over though everything I've discussed so far makes sense for compil languages when using languages like C or rust the result of the compilation is an executable file that is the program itself but what about interpreted languages like python or JavaScript where there is no compilation process in Python for example every time we want to run code we must first instruct the computer to run The Interpreter and then one thing I actually want to go back to is he put Java in the same realm as interpreted and so that to me actually does make sense given that like when you're running a piece of java code is it the code that's being executed because for people who don't know Java turns into like bik code and then there's a separate program called the Java virtual machine which then runs that Java code so in a sense I think it is interpreted it really is because what's running is the Java virtual machine at least an instance of it I'm actually not an expert on Java if somebody wants to comment maybe we can answer this ourselves let's use Claude this time and so I asked when a get away when a Java process is running does it get its own instance of the Java virtual machine yes when a Java process is running it typically does so this may or may not be correct I'm going to assume it is because for something basic as this I would think Claude is correct we could also Google this but so that's very interesting so in that sense yeah I mean Java definitely does feel like a interpreted language I don't want to start a debate or anything and in the same realm I actually recently learned this that python is also compiled like there is a compil step like I used to think like I had a very naive understanding of how the python interpreter worked I thought it literally just read the python text files line by line but there is a compilation step so what is the compilation step with python code and how does it differ from java compilation okay so there is a compilation step in Python it's compiled into bite code but it happens at runtime I believe there's not an actual separate compilation step so that honestly makes me feel like you could not know that you could not know that there's a compilation step in Java just like you could ignore like the just in time compilation with JavaScript and you'd probably be fine I'm assuming this is mainly done for optimization reasons was there a point where python did not have a compilation step why does it exist or performance so it sounds like it always was there and mainly does EX for performance reasons or JavaScript where there is no compilation process in Python for example every time we want to run code we must first instruct the computer to run The Interpreter and then tell The Interpreter to execute our code right so every time we run a python file or Python program we're creating a process and that process encapsulates the python interpreter and it's going to specifically be using the file itself self as like the executable as the instruction set not the instruction set as the instructions in other words there are now two passive entities involved the python interpreter which is a program okay right so there's a separate unit running for that process not just the CPU reading like the lowle instructions there are higher level instructions involved file which is practically a text file I would like to highlight here the fact that the python source file is not a program remember that source code is just text that computers cannot understand and therefore cannot execute I guess in this case and maybe I'm getting overly technical but even let's say you took that python file and it turns into bite code the distinction between This and like that c program example we were looking at towards the beginning is I wonder if this python when it's compiled into the bite code in the bite code I wonder if there's a separate region for the data versus like the instructions I mean I wonder if that matters at all but that's just something that came to my mind so in this kind of situations the program we're actually asking the computer to run is The Interpreter not the python code we wrote once this new process is created it has its own memory region with text and data sections right I mean of course every process has a text and data section but I wonder how that relates to like the bik code or like how like the Java virtual machine does it this is from the operating systems perspective but from the python interpretor perspective it's going to be reading like the bik code where's the bik code probably within the text I think I'm just confusing myself maybe they are distinct as well as a stack and a heap just like any other process the key Point here is that what's loaded into the text section is not our python source code but the executable code of The Interpreter our source code is loaded by The Interpreter process into one of its data sections most likely the Heap because it serves as the data The Interpreter will work right that's very interesting so this is the program that's running for it to know how to execute it's going to keep fetching the data from the Heap that's very interesting because still when a program is running it's not just running from top to bottom like there's control flow for example like you might have some instructions but if the if statement does not evaluate to true that if statement is going to be skipped that must mean when you have a python interpreter it's going to go through the memory it's sure it can read through the memory but like that itself needs to be coded to skip certain parts of the code so it's almost like when you're creating an interpreter in a sense you're kind of creating like an abstraction for like a CPU work with to read and interpret and let's wrap it up for now compilers and interpreters deserve their own videos since I'm committed to continue releasing quality content once again I encourage you to subscribe I encourage you guys to subscribe as well honestly his channel is fantastic

Original Description

Full video by @CoreDumpped - https://www.youtube.com/watch?v=7ge7u5VUSbE 🚀 https://neetcode.io/ - A better way to prepare for Coding Interviews 🧑‍💼 LinkedIn: https://www.linkedin.com/in/navdeep-singh-3aaa14161/ 🐦 Twitter: https://twitter.com/neetcode1 ⭐ BLIND-75 PLAYLIST: https://www.youtube.com/watch?v=KLlXCFG5TnA&list=PLot-Xpze53ldVwtstag2TL4HQhAnC8ATf #neetcode #leetcode #python
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from NeetCodeIO · NeetCodeIO · 0 of 60

← Previous Next →
1 Leetcode 149 - Maximum Points on a Line - Python
Leetcode 149 - Maximum Points on a Line - Python
NeetCodeIO
2 Design Linked List - Leetcode 707 - Python
Design Linked List - Leetcode 707 - Python
NeetCodeIO
3 Minimum Time to Collect All Apples in a Tree - Leetcode 1443 - Python
Minimum Time to Collect All Apples in a Tree - Leetcode 1443 - Python
NeetCodeIO
4 Design Browser History - Leetcode 1472 - Python
Design Browser History - Leetcode 1472 - Python
NeetCodeIO
5 Number of Good Paths - Leetcode 2421 - Python
Number of Good Paths - Leetcode 2421 - Python
NeetCodeIO
6 Flip String to Monotone Increasing - Leetcode 926 - Python
Flip String to Monotone Increasing - Leetcode 926 - Python
NeetCodeIO
7 Maximum Sum Circular Subarray - Leetcode 918 - Python
Maximum Sum Circular Subarray - Leetcode 918 - Python
NeetCodeIO
8 Find Closest Node to Given Two Nodes - Leetcode 2359 - Python
Find Closest Node to Given Two Nodes - Leetcode 2359 - Python
NeetCodeIO
9 Concatenated Words - Leetcode 472 - Python
Concatenated Words - Leetcode 472 - Python
NeetCodeIO
10 Data Stream as Disjoint Intervals - Leetcode 352 - Python
Data Stream as Disjoint Intervals - Leetcode 352 - Python
NeetCodeIO
11 LFU Cache - Leetcode 460 - Python
LFU Cache - Leetcode 460 - Python
NeetCodeIO
12 N-th Tribonacci Number - Leetcode 1137
N-th Tribonacci Number - Leetcode 1137
NeetCodeIO
13 Best Team with no Conflicts - Leetcode 1626 - Python
Best Team with no Conflicts - Leetcode 1626 - Python
NeetCodeIO
14 Greatest Common Divisor of Strings - Leetcode 1071 - Python
Greatest Common Divisor of Strings - Leetcode 1071 - Python
NeetCodeIO
15 Shortest Path in a Binary Matrix - Leetcode 1091 - Python
Shortest Path in a Binary Matrix - Leetcode 1091 - Python
NeetCodeIO
16 Insert into a Binary Search Tree - Leetcode 701 - Python
Insert into a Binary Search Tree - Leetcode 701 - Python
NeetCodeIO
17 Delete Node in a BST - Leetcode 450 - Python
Delete Node in a BST - Leetcode 450 - Python
NeetCodeIO
18 Shuffle the Array (Constant Space) - Leetcode 1470 - Python
Shuffle the Array (Constant Space) - Leetcode 1470 - Python
NeetCodeIO
19 Fruits into Basket - Leetcode 904 - Python
Fruits into Basket - Leetcode 904 - Python
NeetCodeIO
20 Number of Subarrays of size K and Average Greater than or Equal to Threshold - Leetcode 1343 Python
Number of Subarrays of size K and Average Greater than or Equal to Threshold - Leetcode 1343 Python
NeetCodeIO
21 Naming a Company - Leetcode 2306 - Python
Naming a Company - Leetcode 2306 - Python
NeetCodeIO
22 As Far from Land as Possible - Leetcode 1162 - Python
As Far from Land as Possible - Leetcode 1162 - Python
NeetCodeIO
23 Shortest Path with Alternating Colors - Leetcode 1129 - Python
Shortest Path with Alternating Colors - Leetcode 1129 - Python
NeetCodeIO
24 Minimum Fuel Cost to Report to the Capital - Leetcode 2477 - Python
Minimum Fuel Cost to Report to the Capital - Leetcode 2477 - Python
NeetCodeIO
25 Count Odd Numbers in an Interval Range - Leetcode 1523 - Python
Count Odd Numbers in an Interval Range - Leetcode 1523 - Python
NeetCodeIO
26 Contains Duplicate II - Leetcode 219 - Python
Contains Duplicate II - Leetcode 219 - Python
NeetCodeIO
27 Path with Maximum Probability - Leetcode 1514 - Python
Path with Maximum Probability - Leetcode 1514 - Python
NeetCodeIO
28 Add to Array-Form of Integer - Leetcode 989 - Python
Add to Array-Form of Integer - Leetcode 989 - Python
NeetCodeIO
29 Unique Paths II - Leetcode 63 - Python
Unique Paths II - Leetcode 63 - Python
NeetCodeIO
30 Minimum Distance between BST Nodes - Leetcode 783 - Python
Minimum Distance between BST Nodes - Leetcode 783 - Python
NeetCodeIO
31 Design Hashmap - Leetcode 706 - Python
Design Hashmap - Leetcode 706 - Python
NeetCodeIO
32 Range Sum Query Immutable - Leetcode 303 - Python
Range Sum Query Immutable - Leetcode 303 - Python
NeetCodeIO
33 Binary Tree Zigzag Level Order Traversal - Leetcode 103 - Python
Binary Tree Zigzag Level Order Traversal - Leetcode 103 - Python
NeetCodeIO
34 Middle of the Linked List - Leetcode 876 - Python
Middle of the Linked List - Leetcode 876 - Python
NeetCodeIO
35 Course Schedule IV - Leetcode 1462 - Python
Course Schedule IV - Leetcode 1462 - Python
NeetCodeIO
36 Single Element in a Sorted Array - Leetcode 540 - Python
Single Element in a Sorted Array - Leetcode 540 - Python
NeetCodeIO
37 Capacity to Ship Packages - Leetcode 1011 - Python
Capacity to Ship Packages - Leetcode 1011 - Python
NeetCodeIO
38 IPO - Leetcode 502 - Python
IPO - Leetcode 502 - Python
NeetCodeIO
39 Minimize Deviation in Array - Leetcode 1675 - Python
Minimize Deviation in Array - Leetcode 1675 - Python
NeetCodeIO
40 Longest Turbulent Array - Leetcode 978 - Python
Longest Turbulent Array - Leetcode 978 - Python
NeetCodeIO
41 Last Stone Weight II - Leetcode 1049 - Python
Last Stone Weight II - Leetcode 1049 - Python
NeetCodeIO
42 Construct Quad Tree - Leetcode 427 - Python
Construct Quad Tree - Leetcode 427 - Python
NeetCodeIO
43 Find Duplicate Subtrees - Leetcode 652 - Python
Find Duplicate Subtrees - Leetcode 652 - Python
NeetCodeIO
44 Sort an Array - Leetcode 912 - Python
Sort an Array - Leetcode 912 - Python
NeetCodeIO
45 Ones and Zeroes - Leetcode 474 - Python
Ones and Zeroes - Leetcode 474 - Python
NeetCodeIO
46 Remove Duplicates from Sorted Array II - Leetcode 80 - Python
Remove Duplicates from Sorted Array II - Leetcode 80 - Python
NeetCodeIO
47 Maximum Twin Sum of a Linked List - Leetcode 2130 - Python
Maximum Twin Sum of a Linked List - Leetcode 2130 - Python
NeetCodeIO
48 Concatenation of Array - Leetcode 1929 - Python
Concatenation of Array - Leetcode 1929 - Python
NeetCodeIO
49 Symmetric Tree - Leetcode 101 - Python
Symmetric Tree - Leetcode 101 - Python
NeetCodeIO
50 Check Completeness of a Binary Tree - Leetcode 958 - Python
Check Completeness of a Binary Tree - Leetcode 958 - Python
NeetCodeIO
51 Construct Binary Tree from Inorder and Postorder Traversal - Leetcode 106 - Python
Construct Binary Tree from Inorder and Postorder Traversal - Leetcode 106 - Python
NeetCodeIO
52 Find Peak Element - Leetcode 162 - Python
Find Peak Element - Leetcode 162 - Python
NeetCodeIO
53 Accounts Merge - Leetcode 721 - Python
Accounts Merge - Leetcode 721 - Python
NeetCodeIO
54 Binary Tree Preorder Traversal (Iterative) - Leetcode 144 - Python
Binary Tree Preorder Traversal (Iterative) - Leetcode 144 - Python
NeetCodeIO
55 Binary Tree Postorder Traversal (Iterative) - Leetcode 145 - Python
Binary Tree Postorder Traversal (Iterative) - Leetcode 145 - Python
NeetCodeIO
56 Number of Zero-Filled Subarrays - Leetcode 2348 - Python
Number of Zero-Filled Subarrays - Leetcode 2348 - Python
NeetCodeIO
57 Minimum Score of a Path Between Two Cities - Leetcode 2492 - Python
Minimum Score of a Path Between Two Cities - Leetcode 2492 - Python
NeetCodeIO
58 Sqrt(x) - Leetcode 69 - Python
Sqrt(x) - Leetcode 69 - Python
NeetCodeIO
59 Successful Pairs of Spells and Potions - Leetcode 2300 - Python
Successful Pairs of Spells and Potions - Leetcode 2300 - Python
NeetCodeIO
60 Optimal Partition of String - Leetcode 2405 - Python
Optimal Partition of String - Leetcode 2405 - Python
NeetCodeIO

This video teaches the difference between a program and a process, and how they relate to memory management, compilation, and interpretation, with a focus on systems design and computer architecture. It provides a foundational understanding of low-level system concepts and operating system level units. By watching this video, viewers will gain a deeper understanding of how programs are executed and how processes are managed.

Key Takeaways
  1. Define what a process is not
  2. Establish the difference between a program and a process
  3. Analyze the description of a program to understand its limitations
  4. Run a program by loading it into memory
  5. Fetch instructions and data from the loaded program
  6. Try to add commentary to a video
  7. Double check information on Google
  8. Replay a video
💡 The distinction between a program and a process is often blurred, and the operating system perspective is important in understanding this distinction

Related Reads

📰
Building a Power Grid Inside Minecraft with BFS Algorithms
Learn to build a power grid inside Minecraft using BFS algorithms and understand its relevance to cloud services
Dev.to · Carlos Cortez 🇵🇪 [AWS Hero]
📰
The Run-Length Encoding Trick: How Simple Strings Get Compressed
Learn how Run-Length Encoding (RLE) compresses simple strings, a fundamental technique in programming and data compression, and apply it to optimize storage and transmission of data
Medium · Programming
📰
75 Days of Leetcode — Day 4: #238 — Product of Array Except Self
Solve the Product of Array Except Self problem on LeetCode to improve coding skills and learn array manipulation techniques
Medium · AI
📰
I implemented the algorithm that broke the sorting barrier. Dijkstra still wins.
Learn how the author implemented an algorithm that broke the sorting barrier and compare its performance with Dijkstra's algorithm
Medium · Programming
Up next
Stump Grinder Carbide Wheel Grinds Hardwood To Chips
Innoforge Studio
Watch →