FANG Interview Question | Process vs Thread

Поделиться
HTML-код
  • Опубликовано: 22 дек 2024

Комментарии • 152

  • @TannerBarcelos
    @TannerBarcelos Год назад +123

    Building a house is a process, but the people doing the work to install the pipes, paint the walls and do the electrical work are all the working threads within the process of building the house.
    I like to use threads synonymously with “unit of work” or “thing that executes a specific piece of work / action for the process to complete successfully.
    So if I run chrome, and have 10 tabs open, I will have 10 processes running. Within each process are 1 or many units of work (called threads) responsible for memory management, access, logic, etc.
    I believe the house analogy works well in a lot of cases
    Also, I was asked this question in an interview during a Comp. Sci. Fundamentals round during the systems design portion of my interview process at a large, global company we all interact with daily. (Keeping vague for obvious reason)

    • @ayubbrow3880
      @ayubbrow3880 7 месяцев назад

      brov why don't you make a video the house example is very brilliant

    • @pablakes
      @pablakes 6 месяцев назад +2

      Thank you! You just helped a compsci freshman immensely.

    • @Taddy_Mason
      @Taddy_Mason 5 месяцев назад

      Excellent metaphor, actually helped me understand.

    • @vikram5010
      @vikram5010 Месяц назад

      Man, I understood better from your explanation than this video.

  • @fieryscorpion
    @fieryscorpion 2 года назад +260

    This is how video lessons should be done; plenty of diagrams and animations instead of someone just talking.
    Thanks!

  • @vikram5010
    @vikram5010 Месяц назад +17

    I can try to explain using a definition and a real-life example:
    1. Program
    Definition: A program is a set of instructions written in a specific programming language that the computer can execute. It is essentially a blueprint, and it does not perform any action until it is run.
    Analogy: Think of a program as a recipe for cooking. The recipe outlines all the steps and ingredients needed to make a dish, but until you start cooking, the recipe is just an inactive set of instructions.
    2. Process
    Definition: A process is an instance of a program in execution. When you run a program, it becomes a process. A process has its own memory space, system resources, and an operating system environment.
    Analogy: Continuing with the recipe analogy, a process is like someone actually cooking according to the recipe. Each person (process) has their own kitchen space (memory) and resources, like ingredients and utensils, which they use while cooking.
    Example: When you open a web browser (like Chrome or Firefox), a process is created for it. Each tab within the browser might even have its own process, depending on the browser design.
    3. Thread
    Definition: A thread is the smallest unit of execution within a process. A process can have multiple threads, all sharing the same memory but running independently. Threads within a process can perform different tasks simultaneously, improving efficiency.
    Analogy: In the cooking example, if multiple people are working together on the same dish, each person could be considered a thread. They share the same kitchen (memory) and ingredients (resources) but may be responsible for different parts of the task, like chopping vegetables or stirring the pot.
    Example: In your web browser, multiple threads might handle different tasks: one for rendering the page, another for handling user input, and yet another for network requests. These threads all work within the same browser process.
    4. Task
    Definition: In many modern programming frameworks (like .NET), a task is an abstraction that represents an asynchronous operation, which can run on a thread. Tasks are typically used for parallelism and concurrency, making it easier to manage multiple operations without blocking the main thread.
    Analogy: If cooking is the process and each cook is a thread, then a task might represent specific cooking jobs, like “boil water” or “slice onions.” These tasks can be assigned to cooks (threads) as they become available, optimizing workflow without everyone waiting for one task to complete before starting another.
    Example: In a web application, if you request data from a database, this request can be handled as a task. The application doesn't have to wait for the database to respond (which can be slow); instead, it can continue other work until the data is ready.

    • @palomacardozo-willigs2314
      @palomacardozo-willigs2314 Месяц назад +1

      Best description thanks. I got a test on this tomorrow!

    • @vikram5010
      @vikram5010 Месяц назад

      ​@palomacardozo-willigs2314 This description is taken from ChatGpt.

    • @vikram5010
      @vikram5010 Месяц назад

      @@palomacardozo-willigs2314 How was the test?

    • @tayyab.sheikh
      @tayyab.sheikh Месяц назад +1

      Did you generated this through ChatGPT?

    • @vikram5010
      @vikram5010 Месяц назад

      @@tayyab.sheikh Yes

  • @furiUfc
    @furiUfc 10 месяцев назад +11

    I have to admit... I have never seen such good explaination videos merged with animations and diagrams in my life. GREAT CONTENT SIR! Keep on doing what you do.

  • @futurexjam2
    @futurexjam2 2 года назад +269

    Thread is the mechanism of sending instructions from a program(process) to CPU. Threads are OS based mechanisms, also processes are too but a process includes at least one thread. You can imagine that a process is encapsulation of a thread or threads. The OS looks at the program(when you click an executable), creates an executing process for the execution of the program and in the process the thread or threads are the carrier of instructions which are sent to the CPU. This is the execution mechanism of a program in an OS. When you create a thread rather than the main thread, you say to the OS that in this thread those instructions should run in a different context, that will create a context switch(that is execution of instruction(s) given by a thread to the CPU in a determined duration), and by that, you will see imaginary parallel execution of different instructions in different threads. Maybe one second long instruction set will be executed in a one short lived thread while another long running thread (especially main thread) keeps running and by that short living thread will not wait the long running thread to be finished. That is the magical part of multi threaded executions.

    • @TheZhouh12
      @TheZhouh12 2 года назад +10

      @@mils3318 Yes, it is possible to run multiple threads in parallel within a single process in most modern operating systems. A process can have multiple threads of execution, which are also known as lightweight processes. Each thread runs concurrently and shares the process's resources, including memory and open files.

    • @thecatleo
      @thecatleo 2 года назад +26

      bro, we hate to read so that's why watching video but bro is writing essay here🤣🤣

    • @mils3318
      @mils3318 2 года назад

      ​@@TheZhouh12 so it is possible that, for example, if we have a 8-core CPU on it, in one currently active process, multiple threads can be executed in parallel... not multitasking.
      Okay, thank you very much.

    • @TheZhouh12
      @TheZhouh12 2 года назад +7

      @@mils3318 When a CPU has multiple cores, it can execute multiple threads concurrently, as long as the operating system supports it. This is known as parallel processing.

    • @TheZhouh12
      @TheZhouh12 2 года назад +5

      @@mils3318 On a system with a single-core CPU, the operating system can still create multiple threads and schedule them to be executed one after the other, but they will not be able to run concurrently. This is known as multitasking.

  • @muzammilnxs
    @muzammilnxs Год назад +3

    Analogy for understanding process and thread
    • In essence a program/process/thread is executing a job(a set of instructions). So, in that context, it’s no different than physical labour work where one executes a bunch of instructions from his/her brain and uses the required tools to complete a job
    • Imagine you hire a fitter to mount your newly bought TV and you have already bought a TV mount. Here, the fitter represents a program. He has skills required to mount the TV(Set of instructions) and required tools(Data, library, plugins)
    • Hiring a fitter is equivalent to installing a program. You have got commitment from him/her to work on your job. And he brings himself and loads the tools in your space ready to do the job. But, You have yet to extract the job from him. For this, you need to prepare some physical space near your TV and also give him the authorization to work at your home. This is the equivalent of creating a process where you allocate CPU and ram memory for the program to execute its job
    • Just as a program can spin up multiple process, a fitter can bring multiple fitters to execute the same job. All of them work independently in their own physical space and with required authorization
    • So, what's the analogy for threads? A fitter usually breaks the job into sub tasks. Here, mounting a TV can be broken in tasks like punching holes in the wall, preparing the Mount frame, adjusting the tilt etc. These subtasks represent individual threads. These threads are executed in the same memory/heap space just as the fitter performs his subtasks on his original physical space.
    •Threads can be executed in parallel saving time. You can imagine this to be fitter performing tasks at the same time using his two hands. For example, one hand used to punch holes in the wall and other hand used to prepare the mount frame. This might sound like a stretch but there are some crazy people who could multitask with both hands

  • @rickywj
    @rickywj 2 года назад +23

    Fantastic video - concise and clear explanations accompanied by extremely helpful visuals. Couldn't ask for a better description within 4 minutes of time. Subscribed!

  • @gabshu22
    @gabshu22 2 месяца назад +1

    This video perfectly summed up 100 lecture slides provided by my teacher without missing any key points... well done 👏👏

  • @orterves
    @orterves Год назад +3

    All information that I knew but would be hard-pressed to scoop out of my brain on demand - it's great to have clear and succinct refreshers like this to keep the neurons fresh

  • @Nerdimo
    @Nerdimo Год назад +2

    I keep getting confused between the two (threads and processes) but this depiction has helped me a ton. Thank you!

  • @PH4RX
    @PH4RX 2 года назад +18

    1:00 "one process cannot corrupt the memory space of another process" - well, should not be able to.

  • @jennwng
    @jennwng Год назад +4

    Also, would love to see videos on "heap" and "serialization / encoding" (some visuals on why in-memory representation is different from byte sequence would be super super helpful ). Thanks again for these great videos!!!!

  • @johnvandenberg1448
    @johnvandenberg1448 2 года назад +10

    Recently came across your channel. Love the content. Very informative yet short enough to digest unlike most other tech videos

  • @Vinod_Kumar827
    @Vinod_Kumar827 2 года назад +8

    Good refresher for me. Thanks once again for the awesome and meaningful video.

  • @hugohultqvist6132
    @hugohultqvist6132 11 месяцев назад

    Learnt more in 4 min than from 4 hrs of classes, thank you!

  • @StephenBeale
    @StephenBeale 10 месяцев назад

    The standard here is exceptional. Thank you so much for this - I wish my lecturers had taken a leaf out of your book!

  • @milad8436
    @milad8436 Год назад +1

    Great channel. Simple, short and straight-forward

  • @stylecine
    @stylecine Год назад

    Holy cow... so clear. So coherent. So neat. The graphics go perfectly with your explanation. Thank you for this.

  • @martingallauner
    @martingallauner 2 года назад +2

    The graphics and animations are so good!
    (And the content of course 😄)
    Thanks for sharing!

  • @sungjuyea4627
    @sungjuyea4627 2 года назад +3

    Thank you for this video! I got this question in an interview this week, and I wish I could have watched this video before the interview! So concise but detailed explanation! And now I get to know what is the purpose of "yield" in Python. None of the resources about it explain in context of OS and context switching...!

  • @rida_brahim
    @rida_brahim 5 месяцев назад

    This channel is a gem

  • @MM-ts9jy
    @MM-ts9jy Год назад

    Thanks for those visuals, I can tell a lot of love went into them

  • @NBopus
    @NBopus 2 года назад +3

    YOu have a great way of explaining things. Keep the good work! Can't wait you get into subjects like Docker and Kubernetes.

  • @ravikumarsuvvari2177
    @ravikumarsuvvari2177 Год назад +2

    Multi tasking and multi threading needs to be well known and especially where to use under what circumstances 🙏

  • @jennwng
    @jennwng Год назад +1

    I really love these videos!!! The visuals are very helpful to build mental models and understand these complex concepts - thank you so much!!!

  • @yashshukla1637
    @yashshukla1637 2 месяца назад +1

    - **Definition Overview**
    - **Process:** Created when a program (executable file) is loaded into memory; includes resources managed by the OS (e.g., registers, memory pages).
    - **Key Property:** Each process has **its own memory address space**, preventing interference between processes; isolation ensures other processes continue running if one fails (e.g., Chrome tabs).
    - **Thread:** Unit of execution within a process; every process has at least one thread, with threads sharing the same memory space.
    - **Thread vs. Process Context**
    - Threads share memory, enabling easy communication but risking the whole process if one thread fails.
    - Processes have **separate memory spaces**, enhancing isolation but making inter-process communication more complex.
    - **Context Switching:** The OS switches between processes/threads, saving/restoring states. Switching between threads is faster than between processes due to shared memory.
    - **Context Switching Details**
    - Context switching is **expensive**, requiring saving/loading registers, memory pages, and updating kernel data structures.
    - **Thread switching** is faster since threads share memory, avoiding virtual memory page switching, which is the most costly part.
    - Mechanisms like **fibers** and **coroutines** are designed to minimize context-switching costs by being cooperatively scheduled, with tasks yielding control periodically.
    - **Conclusion**
    - Threads enable efficient execution within processes but can compromise stability.
    - Processes ensure strong isolation at the cost of slower context switching.
    - The discussion emphasizes trade-offs between complexity and performance.

  • @spattanaik75
    @spattanaik75 Год назад

    powerful concepts within just 4 minutes. Thanks

  • @DK-ox7ze
    @DK-ox7ze 2 года назад +2

    Great video. But I didn't understand how auto yielding reduces the cost of context switching? Because after yielding, context switching will have to be done anyway for the CPU to execute other processes.

  • @vikassangwan9523
    @vikassangwan9523 Год назад +1

    Code in a program, loaded into memory and executed by the processor, it becomes a process❤

  • @syedahmad5655
    @syedahmad5655 9 месяцев назад

    clear concise explanation together with great graphics. Would love to know how you guys do your graphics.... Thank you...

  • @michaelhernandez2075
    @michaelhernandez2075 Год назад

    Best video on this subject by far

  • @MikeTon
    @MikeTon 2 года назад +2

    Nice and insightful capture of concepts that I were fuzzy on : thanks for putting this together

  • @ДауленЖангабылов
    @ДауленЖангабылов 7 месяцев назад

    The best explanation I watched!

  • @ReflectionOcean
    @ReflectionOcean Год назад

    A process is a program executing by a processor. A process doesn't share memory with another process. It has a collection of threads: the main thread and the other threads if any.
    A thread is an execution unit inside a process. It shares the same heap memory with the other threads in the same process. But it has its own stack memory.

    • @jonniuss
      @jonniuss 11 месяцев назад

      Captain 😅 it was precisely explained in the video

  • @chetan_bommu
    @chetan_bommu 2 года назад

    Sir Alex, Please do in-depth videos. The content you post is so amazing but they are covered on a high level. Please go deep and explain things.

  • @haneulkim4902
    @haneulkim4902 Год назад

    Thanks for great video. Two questions,
    1. So to run K programs embarassingly parallel via multi-processing you have to have K CPU? If number of CPU < K then it would have to switch context, correct?
    2. when we are talking about multi-processing python basically two of same script is copied to each process memory?

  • @ramazanustun6755
    @ramazanustun6755 Год назад

    This 4-minute video explains what my professor could not do in an entire semester.

  • @aniketbisht763
    @aniketbisht763 2 года назад +4

    Hi, can the upcoming videos be on -> http polling (short and long), streaming, websockets etc. ? Thanks your teaching style is just awesome

  • @tanyacharanpahadi158
    @tanyacharanpahadi158 Год назад

    This 4 minute video sums up your years of coding experience.

  • @mnmLCoder
    @mnmLCoder 10 месяцев назад

    love it, short and on point. Also i like your style! directly subbed :)

  • @tens0r884
    @tens0r884 Год назад

    These animations are beautiful holy. Do you mind sharing just how they were accomplished?

  • @dharmikshah2251
    @dharmikshah2251 Год назад

    For the process having multiple threads, OS would provide time quantum of execution per thread under a process? or each thread would get (1 quantum time / no. of threads in process) time for execution?

  • @wargreymon2024
    @wargreymon2024 7 месяцев назад

    gosh, this is so clear and informative

  • @arun2438
    @arun2438 2 года назад +2

    You can add subtitles also for a larger audience

  • @justmasdd
    @justmasdd 2 года назад +1

    Thank you! Very clear and useful video content!

  • @caseyspaulding
    @caseyspaulding 2 года назад +1

    very good. makes more sense now.

  • @joedroider7879
    @joedroider7879 2 года назад +3

    Love your channel. Subscribed to it.
    If you don't mind, what tool do you use to author the content? I find this to be more engaging vs powerpoint based sessions.
    Thanks

    • @thieltube390
      @thieltube390 2 года назад

      I'd be interested in hearing this too

    • @patchfox
      @patchfox 2 года назад

      @@thieltube390 Looks like Adobe After Effect

  • @chrismenui7344
    @chrismenui7344 Год назад +1

    program - executable instructions of code
    Program disc + ram + cpu = process
    process control block(PCB)
    thread - unit of execution within process
    stack
    pointers
    counters
    thread vs process?
    threads (of the same process) run in a shared memory space,
    processes run in separate memory spaces
    shared memory
    virtual memory pages switching ⚠
    fibers
    coroutines

  • @sogolyazdanmadad9483
    @sogolyazdanmadad9483 Год назад

    Thanks for the great video!🎉

  • @wwhill8033
    @wwhill8033 2 года назад +1

    Excellent Stuff!

  • @snapman218
    @snapman218 Год назад

    This is high quality content, but I'm also curious if you could do a video on how you make your videos? :)

  • @rickythebird9584
    @rickythebird9584 2 года назад

    Many many thanks! This is a great video instruction!

  • @akshat_tamrakar
    @akshat_tamrakar 9 месяцев назад

    We need more of these videos

  • @barisballi70
    @barisballi70 Год назад

    This video helped me, thank you 😊

  • @rammehar5531
    @rammehar5531 Год назад

    Love you Sir, please keep it up. and Thank you very much

  • @Spider-Man_67
    @Spider-Man_67 Год назад

    Can you please explain about fibres & co-routines?

  • @samael-o8u
    @samael-o8u Год назад

    thank you for the information, sr. Subscribed!!!!

  • @pierregeopardi1481
    @pierregeopardi1481 8 месяцев назад

    Nice video! What about doing one about coroutines.

  • @tyeinstem
    @tyeinstem Год назад

    This is a brilliant video.

  • @pranaypallavtripathi2460
    @pranaypallavtripathi2460 2 года назад

    Please make more such videos on common interview questions explained so greatly

  • @mils3318
    @mils3318 2 года назад

    And what if we have processors with more cores. Does this mean that we can run more processes at the same time? In that case, do we potentially have higher performance because more processors mean less context switching between processes?
    Can we run two threads in parallel in one process? Or is only multitasking threads allowed per process (execution one by one)?

    • @MortyrSC2
      @MortyrSC2 2 года назад +2

      _And what if we have processors with more cores. Does this mean that we can run more processes at the same time?_
      Yes, every logical core can run one thread.
      _In that case, do we potentially have higher performance because more processors mean less context switching between processes?_
      Hypothetically, if the number of active threads were lower than the number of available logical cores, no context switching would occur. However in reality, even freshly installed system like Windows 10 will have ~100-150 processes running by default, which means context switching is necessary.
      Higher performance comes from being able to process multiple threads concurrently, not from reduced cost of context switching.
      _Can we run two threads in parallel in one process?_
      Yes, if code was properly written to allow multithreading.

  • @wahyukoco4562
    @wahyukoco4562 Год назад

    great explanation!

  • @sergeikharchikov1157
    @sergeikharchikov1157 2 года назад

    Brilliant channel

  • @amber4131
    @amber4131 2 года назад

    If adding coroutine comparison, that would be perfect!

  • @akdisrael
    @akdisrael 11 месяцев назад

    deserve a subscribe

  • @hoangchu4036
    @hoangchu4036 2 года назад

    Best explanation!!!

  • @aroon8325
    @aroon8325 2 года назад +2

    Does anyone know what animation software is used to create this video?

    • @dgeo27
      @dgeo27 2 года назад

      I was thinking of this for a long time. I'd also like to know. Not interested in the animation part. But the diagrams are beautiful

    • @patchfox
      @patchfox 2 года назад

      @@dgeo27 Looks like Adobe After Effect

  • @TheCause41
    @TheCause41 10 месяцев назад

    Well explained. Thank you.

  • @devictoribero
    @devictoribero 2 года назад +1

    As always, incredible video! Real great work. I'm so impressed by the animations. How do you create them?

  • @arch_285
    @arch_285 6 месяцев назад

    Great Video

  • @nouchance
    @nouchance 2 года назад +1

    Amazing SIR!

  • @Spider-Man_67
    @Spider-Man_67 Год назад

    Man you're dope, thanks a lot

  • @nasamind
    @nasamind Год назад

    simply awesome

  • @deghta
    @deghta Год назад

    What program is used for visualization?

  • @MrAtomUniverse
    @MrAtomUniverse 2 года назад +2

    What software was this video made with?

    • @kovolexiy
      @kovolexiy 2 года назад +1

      I'm also interested in this question. Did you find it out?

    • @MrAtomUniverse
      @MrAtomUniverse 2 года назад

      @@kovolexiy nope a lot of channel not willing to share knowledge they scared of competition. What’s your telegram handler let’s research this up

    • @patchfox
      @patchfox 2 года назад

      @@kovolexiy Looks like Adobe After Effect

  • @multivalfran
    @multivalfran Год назад

    Great video. Thxs

  • @hotlineoperator
    @hotlineoperator 2 года назад

    What app did you use to create that animated presentation graphics?

  • @araara967
    @araara967 Год назад

    pls upload more videos on operating systems concepts

  • @NewHorizon-v9p
    @NewHorizon-v9p 7 месяцев назад

    Well done

  • @cool_guy_Vaibhav
    @cool_guy_Vaibhav 2 года назад

    animation is amazing

  • @sayedaweshrahman4608
    @sayedaweshrahman4608 6 месяцев назад

    fan of ByteByteGo 👏

  • @sampathmethuku7428
    @sampathmethuku7428 Год назад

    what is the software used for animation

  • @gokulraj8302
    @gokulraj8302 5 месяцев назад

    What is registers ?

  • @allezvenga7617
    @allezvenga7617 2 года назад

    Thanks for your sharing

  • @Itsameemario
    @Itsameemario 6 месяцев назад

    someone explain this to university level at the electron levels. like use logic gates, an electricity because in the end it is just 1's and 0's 1 instruction at a time

  • @neuroillusion
    @neuroillusion Год назад +1

    It would be much more interesting to watch a video "fibers vs coroutines" :)

  • @tilli7500
    @tilli7500 2 года назад

    can you or anybody from the comments provide a systematic overview of the relationship(technical,,,,not abstract concepts ///take x86, unix for example)bw mode bits(when exactly they are changed,,,,kindly try to be precise),kernel user mode(relating kernel stack ,,,ig both implementations ie per process,,,one for all)and virtual address spaces?

  • @大盗江南
    @大盗江南 6 месяцев назад

    我的娘这么复杂🥲 amazing content tho! Thank you!

  • @joshuabrard1061
    @joshuabrard1061 9 месяцев назад

    Good video

  • @mohokhachai
    @mohokhachai Год назад

    So how many process can 4 ram handle

  • @athatien2553
    @athatien2553 2 года назад +2

    I am about to have an interview and this topic has given me a headache for days :(

    • @8starsAND
      @8starsAND 5 месяцев назад

      So your not suitable there

  • @morenoh149
    @morenoh149 2 года назад

    Describe fibers in react

  • @skrmnghrd4520
    @skrmnghrd4520 3 месяца назад

    whole semester of a damn college course in 5 mins here

  • @saadmansakib6612
    @saadmansakib6612 Год назад

    I like how he’s a fan of witcher 3

  • @DhilipKumar-i5p
    @DhilipKumar-i5p 4 месяца назад

    You explanation is good but need voice to be more clear. I think

  • @Ruth___597a
    @Ruth___597a 5 месяцев назад

    Notification of successful receipt of BTC transaction

  • @algu1984
    @algu1984 Год назад

    The simplest explanation I would say that the Process is virtualization of memory and Thread is virtualization of CPU the rest is following cons

  • @jonniuss
    @jonniuss 11 месяцев назад

    Read the Windows internals book by Mark Russinovich and you guys will discover the new universe

    • @AKA-077
      @AKA-077 Месяц назад

      Will it help me too understand Linux too?(Or it is entirely different)

  • @mohokhachai
    @mohokhachai Год назад

    Teaching them plz

  • @allentsai1982
    @allentsai1982 Год назад

    超讚得