Recent

Author Topic: Program creating many threads  (Read 2354 times)

Чебурашка

  • Hero Member
  • *****
  • Posts: 569
  • СЛАВА УКРАЇНІ! / Slava Ukraïni!
Re: Program creating many threads
« Reply #15 on: May 18, 2023, 08:31:24 am »
Hi
@Чебурашка: Thank you for doing the research, I just learned a new thing about threads/threadprogramming, and pretty important too  ;)
Regards Benny

Countelss times I been helped by other users of forums.

In italian we say "E' una ruota che gira" (literally "It's a spinning wheel")
FPC 3.2.0/Lazarus 2.0.10+dfsg-4+b2 on Debian 11.5
FPC 3.2.2/Lazarus 2.2.0 on Windows 10 Pro 21H2

alpine

  • Hero Member
  • *****
  • Posts: 1064
Re: Program creating many threads
« Reply #16 on: May 18, 2023, 08:34:31 am »
@Alpine et al... I was wondering about (lack of) synchronisation on termination after I posted last, but have been away from my desk. That sort of thing /should/ show up as a ballooning number of entries in /proc, since my understanding is that a Linux thread is a cloned (as distinct from forked) process.

Please explain: cloned vs forked.

AFAIK, forking (of a process) is when it starts like a single one, but eventually:
Code: C  [Select][+][-]
  1.   ...
  2.   if (fork() == 0)
  3.   {
  4.     // do the child work
  5.   } else {
  6.     // parent continued ...
  7.   }
  8.  
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

Чебурашка

  • Hero Member
  • *****
  • Posts: 569
  • СЛАВА УКРАЇНІ! / Slava Ukraïni!
Re: Program creating many threads
« Reply #17 on: May 18, 2023, 08:47:14 am »
@Alpine et al... I was wondering about (lack of) synchronisation on termination after I posted last, but have been away from my desk. That sort of thing /should/ show up as a ballooning number of entries in /proc, since my understanding is that a Linux thread is a cloned (as distinct from forked) process.

Please explain: cloned vs forked.

AFAIK, forking (of a process) is when it starts like a single one, but eventually:
Code: C  [Select][+][-]
  1.   ...
  2.   if (fork() == 0)
  3.   {
  4.     // do the child work
  5.   } else {
  6.     // parent continued ...
  7.   }
  8.  

I think MarkMLI is saying that in linux when you call fork, a new process is created by cloning the original = an exact copy of the process descriptor is made and assigned to the forked process (except the pid and ppid)
FPC 3.2.0/Lazarus 2.0.10+dfsg-4+b2 on Debian 11.5
FPC 3.2.2/Lazarus 2.2.0 on Windows 10 Pro 21H2

MarkMLl

  • Hero Member
  • *****
  • Posts: 6686
Re: Program creating many threads
« Reply #18 on: May 18, 2023, 08:56:25 am »
I think MarkMLI is saying that in linux when you call fork, a new process is created by cloning the original = an exact copy of the process descriptor is made and assigned to the forked process (except the pid and ppid)

No, I am not. These are different system calls, and the distinction is critical to the way pthreads are implemented.

fork creates a new process with a distinct memory context, subject to COW. clone creates a new process sharing the came memory context, i.e. a thread.

The PID situation appears... messy in the TThread context, with the owning process's PID becoming the thread group and the subsidiary threads' PID becoming the thread identifiers. It might be that the BeginThread() return value is significant here.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

alpine

  • Hero Member
  • *****
  • Posts: 1064
Re: Program creating many threads
« Reply #19 on: May 18, 2023, 09:18:27 am »
fork creates a new process with a distinct memory context, subject to COW. clone creates a new process sharing the came memory context, i.e. a thread.
I see.

But isn't it that the main difference between processes and threads - the former gets a copy of the parent data and the latter - just a private stack space (and CPU/FPU context)?
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

MarkMLl

  • Hero Member
  • *****
  • Posts: 6686
Re: Program creating many threads
« Reply #20 on: May 18, 2023, 09:38:41 am »
But isn't it that the main difference between processes and threads - the former gets a copy of the parent data and the latter - just a private stack space (and CPU/FPU context)?

Yes, but the point is that "fork" and "clone" are "terms of art" here and shouldn't be (ab)used lightly.

I admit that from the POV of implementing an OS I far prefer the progression coroutine (AKA fiber etc.) -> thread -> process. But the fact of the matter is that in the context of Linux things are enormously constrained by POSIX and (using 1980s terminology) GLX, and changing subsystem architecture is incredibly and increasingly difficult: you've only got to look at the problems caused by the attempted transition from X11 to Wayland.

And I'd add at this point that I don't think that things are helped by Lennart Poettering's attempts to introduce complex subsystems like systemd and PulseAudio which operate at multiple levels in parallel with the Linux kernel, rather at distinct layers as is traditional.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

 

TinyPortal © 2005-2018