linux - How does the kernel separate threads from processes -


suppose have browser process firefox, has pid = 123. firefox has 5 opened tabs each running in separate thread, in total has 5 threads.

  1. so want know in depth, how kernel separate process thread execute in struct task_struct or in thread_info.

  2. like struct task_struct task descriptor of task list. struct task_struct contain reference or link these 5 threads.

  3. does struct thread_struct of process firefox contain reference 5 thread

    or

    each thread treated process inside linux kernel.

unlike windows, linux not have implementation of "threads" in kernel. kernel gives called "lightweight processes", generalization of concepts of "processes" , "threads", , can used implement either.

it may confusing when read kernel code , see things thread_struct on 1 hand, , pid (process id) on other. in reality, both 1 , same. don't confused terminology.

each lightweight process has different thread_info , task_struct (with embedded thread_struct). seem think task_struct of 1 lightweight process should have pointers task_structs of other (userspace) "threads" in same (userspace) "process". not case. inside kernel, each "thread" separate process, , scheduler deals each 1 separately.

linux has system call called clone used create new lightweight processes. when call clone, must provide various flags indicate shared between new process , existing process. can share address space, or can each have different address space. can share open files, or can each have own list of open files. can share signal handlers, or can each have own signal handlers. can in same "thread group", or can in different thread groups. , on...

although "threads" , "processes" same thing in linux, can implement think of "processes" using clone create processes do not share address space, open files, signal handlers, etc.

you can implement think of "threads" using clone create processes do share address space, open files, signal handlers, etc.

if @ definition of task_struct, find has pointers other structs such mm_struct (address space), files_struct (open files), sighand_struct (signal handlers), , on. when clone new "process", of these structs copied. when clone new "thread", these structs shared between new , old task_structs -- both point same mm_struct, same files_struct, , on. either way, providing different flags clone tell copy, , share.

i mentioned "thread groups" above, might wonder that. in short, each "thread" in "process" has own pid, share same tgid (thread group id). tgids equal pid of first program thread. userspace "pids", shown in ps, or in /proc, "tgids" in kernel. naturally, clone has flag determine whether new lightweight process have new tgid (thus putting in new "thread group") or not.

unix processes have "parents" , "children". there pointers in linux task_struct implement parent-child relationships. and, might have guessed, clone has flag determine parent of new lightweight process be. can either process called clone, or parent of process called clone. can figure out used when creating "process", , used when creating "thread"?

look @ manpage clone; educational. try strace on program uses pthreads see clone in use.

(a lot of written memory; others should feel free edit in corrections necessary)


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -