Difference between processes and threads

00:10:30
https://www.youtube.com/watch?v=IKG1P4rgm54

摘要

TLDRThe video dives into the differences between threads and processes, explaining through code how each functions. Threads and processes may seem similar as both run code concurrently, but they differ significantly. Processes, created with 'fork', run in their own memory space with unique process IDs, duplicating variables for independent modification. Threads, created with 'pthread_create', share memory and resources within a single process, leading to same process IDs and the ability for all threads to access and alter the same variables. While this minimizes memory usage, it risks conflicts from simultaneous modifications. The video outlines how this shared environment impacts execution and highlights the advantages and challenges of threads versus processes. The differences in memory, process IDs, and resource sharing are key, revealing trade-offs in using threads or processes.

心得

  • 🔄 Threads share memory within a process, leading to shared variables.
  • 🔀 Processes run independently with distinct process IDs and memory spaces.
  • 📂 Threads utilize shared file handlers and system resources.
  • 🔍 Processes duplicate variables upon forking, allowing separate modifications.
  • 🚦 Synchronization is critical in threads to manage shared data access.
  • 🧩 Threads and processes exhibit different execution and resource attributes.
  • ⚠️ Shared memory in threads can lead to conflicts without proper handling.
  • 🛠️ Code examples illustrate the operational differences between threads and processes.
  • 🧵 Thread creation requires routine definition and management via pthread functions.
  • 📈 Understanding these differences helps in choosing the right concurrency model.

时间轴

  • 00:00:00 - 00:05:00

    The speaker begins by introducing the idea of comparing threads and processes, noting that they may appear similar at first glance since both allow concurrent code execution. The speaker recommends watching their previous playlist on processes to understand this video better. They proceed with a demonstration using two files to illustrate the difference: one implementing threads and the other using processes. For processes, they use the fork() system call to create two processes, checking for errors and ensuring child processes finish execution before printing messages, showing a straightforward process creation.

  • 00:05:00 - 00:10:30

    Continuing the exploration, the speaker explains that threads have a shared memory space, unlike processes. In the process example, variables are duplicated in separate address spaces, demonstrated by modifying a variable in child and parent processes showing different results. For threads, however, any change in one thread is visible to others, as they share the same address space. They further compare threads and processes by mentioning that threads share resources like file handlers and security contexts, whereas these are duplicated in processes. The speaker concludes warning about issues arising from shared memory when multiple threads modify the same variable, which will be addressed in future discussions.

思维导图

Mind Map

常见问题

  • What is the main difference between threads and processes in terms of execution?

    Threads run within the same process and share memory space, while processes run independently and do not share memory space.

  • Why do processes have different process IDs while threads do not?

    Each process runs in its own independent memory space, thus having unique IDs, whereas threads share the same memory space within a process, so they share process IDs.

  • What do threads share that processes do not?

    Threads share memory, file handlers, and system resources within a process, while processes do not share these resources, making them more isolated.

  • How do processes handle variables during execution compared to threads?

    Processes duplicate variables when forking, allowing modification independently, while threads alter shared variables in the same memory space.

  • What potential issues arise from threads sharing memory?

    Sharing memory can lead to conflicts when multiple threads try to modify the same variable simultaneously, requiring careful synchronization.

  • What should viewers do if they haven't seen the previous playlist about processes?

    Viewers should watch the previous playlist on processes for a better understanding, as this video is a continuation of that content.

  • What is necessary to create and manipulate threads in code?

    To create threads, a routine function is defined and called using the pthread_create and pthread_join functions.

  • How are child processes synchronized to complete before the parent proceeds?

    Child processes are synchronized using the wait function to ensure they finish execution before the parent continues.

查看更多视频摘要

即时访问由人工智能支持的免费 YouTube 视频摘要!
字幕
en
自动滚动:
  • 00:00:00
    now that we looked how to create threads
  • 00:00:02
    i want to take a look
  • 00:00:03
    briefly at the difference between
  • 00:00:05
    threads and the processes
  • 00:00:07
    because at first class they look much
  • 00:00:09
    the same thing right we're just
  • 00:00:11
    executing uh just some code
  • 00:00:16
    at the same time right with threads we
  • 00:00:18
    have a function we execute with
  • 00:00:20
    with processes we just execute the the
  • 00:00:22
    code from
  • 00:00:23
    fork onwards by the way if you haven't
  • 00:00:26
    watched the
  • 00:00:27
    uh playlist about processes i do suggest
  • 00:00:30
    you first
  • 00:00:31
    go ahead and watch that i have a link up
  • 00:00:33
    top and
  • 00:00:34
    uh because this is gonna be sort of a
  • 00:00:36
    continuation to this
  • 00:00:38
    to that uh playlist anyway
  • 00:00:41
    moving forward um so what is
  • 00:00:44
    actually the difference now to
  • 00:00:47
    illustrate the difference between the
  • 00:00:48
    two
  • 00:00:49
    uh concepts threads and processes i'm
  • 00:00:51
    gonna have here two
  • 00:00:52
    uh main files the left one is gonna be
  • 00:00:56
    implemented using threads and right one
  • 00:00:58
    we're gonna
  • 00:00:59
    implement everything using processes and
  • 00:01:01
    we're gonna see the difference between
  • 00:01:03
    the two
  • 00:01:04
    in here so first things first let's
  • 00:01:06
    create two processes and two threads
  • 00:01:09
    uh on each of the source files so that
  • 00:01:11
    we have something to work on
  • 00:01:12
    so to create two processes we can simply
  • 00:01:15
    call here
  • 00:01:16
    uh paint uh speed equals fork
  • 00:01:20
    of course if p is negative one then we
  • 00:01:23
    return an error
  • 00:01:25
    that's simple as that and
  • 00:01:28
    well if speed is not zero then we should
  • 00:01:32
    wait of no
  • 00:01:35
    for the child process to finish its
  • 00:01:37
    execution so that's simple enough and if
  • 00:01:40
    we print here something like
  • 00:01:42
    i know hello uh from
  • 00:01:46
    processes let's say and i launch this
  • 00:01:49
    we should get two lines of text
  • 00:01:53
    down here so that's straightforward i
  • 00:01:55
    think that's pretty
  • 00:01:57
    self-explanatory now uh let's do the
  • 00:02:00
    same thing with threads so with threads
  • 00:02:02
    we have to create a function we have to
  • 00:02:03
    say void
  • 00:02:04
    pointer this has to be avoid pointer and
  • 00:02:06
    call it again routine
  • 00:02:08
    that's simple enough and then
  • 00:02:11
    i'm gonna i guess just say printf hello
  • 00:02:15
    from threads this time and as we've seen
  • 00:02:18
    in the previous video
  • 00:02:20
    just say p thread d
  • 00:02:23
    t one then we say p thread create
  • 00:02:26
    uh the address of t one null
  • 00:02:30
    and routine then note here as well as
  • 00:02:33
    the arguments we don't need any
  • 00:02:34
    arguments
  • 00:02:35
    and p thread join
  • 00:02:38
    p1 and no we don't care about the
  • 00:02:41
    return values and of course i'm going to
  • 00:02:43
    check for errors so i'm going to do
  • 00:02:45
    something like
  • 00:02:46
    if uh if
  • 00:02:50
    this then return one
  • 00:02:53
    and if this then return
  • 00:02:56
    to let's say and if we try to launch
  • 00:02:59
    this we only get
  • 00:03:00
    uh one line of text on the terminal so
  • 00:03:03
    we're going to have to call this
  • 00:03:04
    one more time so i'm going to create
  • 00:03:06
    another thread
  • 00:03:08
    here so p thread create t2 and of course
  • 00:03:10
    to
  • 00:03:11
    define it here
  • 00:03:14
    i'm gonna change these error code so
  • 00:03:16
    that they are different
  • 00:03:18
    copy the join t2
  • 00:03:22
    and that's about it so we're creating
  • 00:03:23
    again two threads
  • 00:03:25
    they both call routine and we should see
  • 00:03:28
    this message printed twice on the
  • 00:03:29
    terminal so now if i try to launch this
  • 00:03:31
    you will notice we get this twice fair
  • 00:03:34
    enough so that works
  • 00:03:36
    now let's say i want to print first the
  • 00:03:39
    process ids
  • 00:03:41
    right what happens if i print the
  • 00:03:44
    process ids
  • 00:03:45
    in here let's say
  • 00:03:48
    process id percent d and to get a
  • 00:03:51
    process id
  • 00:03:52
    you just call get feed
  • 00:03:56
    and if i try to launch this we should
  • 00:03:59
    get
  • 00:04:00
    two different numbers right we got
  • 00:04:02
    twenty five thousand nine hundred thirty
  • 00:04:04
    two and twenty five thousand hundred
  • 00:04:05
    thirty eight so we got two different
  • 00:04:07
    process id so that's expected what
  • 00:04:09
    happens if we do the same with threads
  • 00:04:11
    so if instead say this we're gonna call
  • 00:04:13
    say
  • 00:04:14
    process id process id
  • 00:04:17
    percent d and then say get b
  • 00:04:20
    alright and launch this what's gonna
  • 00:04:22
    happen well
  • 00:04:24
    you will notice that you actually get
  • 00:04:27
    two of the same
  • 00:04:28
    ideas so what's going on here why are
  • 00:04:32
    they not different
  • 00:04:33
    that is because processes can contain
  • 00:04:35
    multiple
  • 00:04:37
    threads right so you can have multiple
  • 00:04:40
    threads inside
  • 00:04:42
    a single process but of course you
  • 00:04:45
    cannot have multiple processes inside a
  • 00:04:47
    single thread
  • 00:04:48
    so sort of processes encompass
  • 00:04:51
    the environment of threads
  • 00:04:54
    and you can have multiple of them that
  • 00:04:58
    is
  • 00:04:58
    one difference now the second difference
  • 00:05:01
    is regarding the address
  • 00:05:04
    space remember whenever we work with
  • 00:05:07
    processes i said okay when
  • 00:05:09
    we're forking we are duplicating all the
  • 00:05:13
    variables
  • 00:05:14
    into the fork into a child process so we
  • 00:05:17
    get two different
  • 00:05:18
    variables basically one on each process
  • 00:05:21
    and we can modify them
  • 00:05:22
    individually but with threads
  • 00:05:26
    there's a big difference where we have
  • 00:05:27
    all the variables in the same
  • 00:05:29
    place so all threads can access all
  • 00:05:32
    variables and they all have a common
  • 00:05:35
    very
  • 00:05:36
    common set of variables common address
  • 00:05:38
    space
  • 00:05:39
    what we can do to illustrate this is
  • 00:05:41
    first we can do
  • 00:05:43
    a simple addition we can have here a
  • 00:05:45
    variable in
  • 00:05:46
    x equals 2 inside the processes
  • 00:05:50
    source code and what we can say is
  • 00:05:53
    if b is zero so if we are in the child
  • 00:05:56
    process
  • 00:05:58
    increment this x okay and i want to
  • 00:06:01
    print this
  • 00:06:02
    x value on the terminal but uh to make
  • 00:06:05
    sure that
  • 00:06:06
    we are actually incrementing it and then
  • 00:06:09
    we are printing it on both processes i'm
  • 00:06:11
    gonna first
  • 00:06:12
    sleep for let's say two seconds and it's
  • 00:06:14
    gonna that sleep is gonna occur for both
  • 00:06:16
    processes
  • 00:06:17
    and then we're gonna printf value of x
  • 00:06:21
    by equation actually something like that
  • 00:06:25
    and just x here and we no longer need
  • 00:06:28
    this printf with the process id
  • 00:06:30
    now if i try to execute it
  • 00:06:34
    a an interesting thing will happen
  • 00:06:37
    and it is well for one process value of
  • 00:06:40
    x was 2
  • 00:06:42
    and for another value of x was 3 and
  • 00:06:44
    since we waited
  • 00:06:45
    we are sure that this operation occurred
  • 00:06:48
    but as you can see
  • 00:06:49
    uh only in one of the process
  • 00:06:53
    the x variable got incremented on the
  • 00:06:56
    other one it stayed the same
  • 00:06:58
    uh as it was initialized
  • 00:07:01
    now if we do the same with threads how
  • 00:07:04
    can we
  • 00:07:05
    do that now to check that we've tried is
  • 00:07:07
    a bit more tricky than that so i'm going
  • 00:07:09
    to create another
  • 00:07:10
    function
  • 00:07:13
    call it routine oops routine two and i'm
  • 00:07:16
    gonna change
  • 00:07:17
    the second thread to actually call this
  • 00:07:20
    function so
  • 00:07:21
    uh thread one is gonna call this and
  • 00:07:23
    thread two is gonna call this
  • 00:07:24
    and thread one what it's gonna do is
  • 00:07:27
    well we have here a
  • 00:07:28
    variable x equals to two as we said
  • 00:07:31
    i'm gonna do x plus plus
  • 00:07:35
    and i'm gonna sleep for two seconds and
  • 00:07:39
    after it i'm just gonna print f well
  • 00:07:41
    whatever i printed in here so i'm gonna
  • 00:07:43
    write it again
  • 00:07:46
    just that and i'm gonna do the same
  • 00:07:48
    thing on the second thread
  • 00:07:51
    except i'm not gonna increment it so
  • 00:07:54
    we're just going to
  • 00:07:55
    uh increment it in the first thread now
  • 00:07:58
    let's see what
  • 00:07:59
    actually happens if i try to launch this
  • 00:08:06
    you will notice that the value of x is
  • 00:08:08
    actually
  • 00:08:09
    3 on both
  • 00:08:12
    trends right so even though we have only
  • 00:08:15
    incremented it here in the thread one
  • 00:08:17
    let's say
  • 00:08:18
    thread two went along and slept for two
  • 00:08:22
    seconds
  • 00:08:22
    like this one so we made sure that this
  • 00:08:25
    printf was executed after the x plus
  • 00:08:27
    plus
  • 00:08:28
    and we have in fact printed
  • 00:08:31
    on the screen the exact value that was
  • 00:08:34
    incremented by tread one
  • 00:08:36
    so all in all threads are sharing
  • 00:08:39
    memories
  • 00:08:40
    this x if you add a number to it
  • 00:08:44
    even if i add here x uh plus equals five
  • 00:08:48
    anything if i do anything in one thread
  • 00:08:50
    and execute it of course it's gonna be
  • 00:08:53
    seen
  • 00:08:53
    in the other thread as you can see
  • 00:08:57
    here and that is the
  • 00:09:00
    one one of the biggest differences
  • 00:09:02
    between threads
  • 00:09:03
    and processes so aside from those two
  • 00:09:06
    uh differences are that well with
  • 00:09:09
    threads all of the threads actually
  • 00:09:11
    share the file handlers
  • 00:09:12
    whereas with processes all the file
  • 00:09:14
    handlers are
  • 00:09:16
    duplicated uh all the really all the
  • 00:09:19
    system resources are shared between all
  • 00:09:21
    the threads
  • 00:09:22
    on a process uh the security context is
  • 00:09:25
    shared
  • 00:09:26
    and so on and so forth but these are
  • 00:09:28
    just
  • 00:09:29
    details that we're gonna get into at
  • 00:09:31
    some
  • 00:09:32
    point the biggest difference is that
  • 00:09:34
    they share memory but
  • 00:09:36
    there comes a trade-off because since
  • 00:09:39
    they share memory
  • 00:09:41
    that means that there could be certain
  • 00:09:44
    issues
  • 00:09:44
    when it comes to certain threads
  • 00:09:46
    modifying the same
  • 00:09:48
    variable and that's what we're gonna get
  • 00:09:50
    into at a later
  • 00:09:52
    point but this is what i wanted to keep
  • 00:09:56
    in mind
  • 00:09:56
    for this video thank you so much for
  • 00:09:58
    watching if you do have any questions
  • 00:10:00
    please leave them down the comments
  • 00:10:01
    below or on our discord server the
  • 00:10:03
    source codes for both of these
  • 00:10:05
    programs are gonna be on our website and
  • 00:10:08
    leave
  • 00:10:08
    a link straight to them on
  • 00:10:11
    in the description below you can check
  • 00:10:13
    it down there download it execute it
  • 00:10:15
    whatever you want to do with it
  • 00:10:16
    alright take care bye
标签
  • threads
  • processes
  • concurrency
  • memory sharing
  • multithreading
  • fork
  • pthread
  • code execution
  • process IDs
  • shared resources