Hot answers tagged

50

I think this part of the clone(2) man page may clear up the difference re. the PID: CLONE_THREAD (since Linux 2.4.0-test8) If CLONE_THREAD is set, the child is placed in the same thread group as the calling process. Thread groups were a feature added in Linux 2.4 to support the POSIX threads notion of a ...


47

The problem is caused by the TasksMax systemd attribute. It was introduced in systemd 228 and makes use of the cgroups pid subsystem, which was introduced in the linux kernel 4.3. A task limit of 512 is thus enabled in systemd if kernel 4.3 or newer is running. The feature is announced here and was introduced in this pull request and the default values were ...


38

You are right, indeed "something must have changed between 2001 and now". The book you are reading describes the world according to the first historical implementation of POSIX threads on Linux, called LinuxThreads (see also Wikipedia article for some). LinuxThreads had some compatibility issues with POSIX standard - for example threads not sharing PIDs - ...


28

From a task_struct perspective, a process’s threads have the same thread group leader (group_leader in task_struct), whereas child processes have a different thread group leader (each individual child process). This information is exposed to user space via the /proc file system. You can trace parents and children by looking at the ppid field in /proc/${pid}/...


23

There is absolutely no difference between a thread and a process on Linux. If you look at clone(2) you will see a set of flags that determine what is shared, and what is not shared, between the threads. Classic processes are just threads that share nothing; you can share what components you want under Linux. This is not the case on other OS ...


22

The documentation can be pretty confusing, so here is the "real" Linux model: inside the Linux kernel, something that can be run (& scheduled) is called a "process", each process has a system-unique Process ID (PID), and a Thread Group ID (TGID), a "normal" process has PID=TGID and no other process shares this TGID value, a "threaded" process is a ...


22

(Userspace) threads are not implemented as processes as such on Linux, in that that they do not have their own private address space, they still share the address space of the parent process. However, these threads are implemented to use the kernel process accounting system, so are allocated their own Thread ID (TID), but are given the same PID and 'thread ...


19

You can use /proc/$PID/task to find all threads of a given process, therefore you can use $ ls /proc/$PID/task | xargs renice $PRIO to renice all threads belonging to a given process. Same way /proc/$PID/task/$PID/children can be used to find all child processes (or /proc/$PID/task/*/children if you want all child processes of all threads of a given ...


17

Threads are processes under Linux. They are created with the clone system call, which returns a process ID that can be sent a signal via the kill system call, just like a process. Thread processes are visible in ps output. The clone call is passed flags which determine how much of the parent process's environment is shared with the thread process.


17

It generally is pretty dangerous to kill an individual thread from a larger process. That thread might: Be modifying some shared state with other threads that could become corrupted Be holding some lock which never gets freed, causing the lock to become indefinitely unavailable ...or any number of other things which might cause other threads to go wrong. ...


15

Let us understand the difference between a process and a thread. As per this link, The typical difference is that threads (of the same process) run in a shared memory space, while processes run in separate memory spaces. Now, we have the pid_max parameter which can be determined as below. cat /proc/sys/kernel/pid_max So the above command returns 32,...


13

The idea behind threads and processes is about the same: You fork the execution path. Otherwise threads and processes differ in things like memory. I.e. processes have different VM space while threads share whatever existed before the split. Underlying both threading and forking work by using the clone() call (man 2 clone): Unlike fork(2), clone() ...


12

You might use tgkill(2) or tkill in your C program (you'll need to use syscall(2)) but you don't want to. From inside your program you can use pthread_kill(3) - which is rarely useful. (I don't exactly know what effect would have tgkill or tkill - e.g. with SIGKILL or SIGTERM - on a thread) The pthreads(7) library uses low-level stuff (including some ...


11

Sorry, the accepted answer is bad information on several fronts. /proc/sys/kernel/pid_max has nothing to do with the maximum number of processes that can be run at any given time. It is, in fact, the maximum numerical PROCESS IDENTIFIER than can be assigned by the kernel. In the Linux kernel, a process and a thread are one an the same. They're handled ...


9

Nice value or CPU shares ? Please note that nowadays, nice values may not be so relevant "system-wide", because of automatic task grouping, espacially when using systemd. Please see this answer for more details. Difference between threads and processes Important question on Linux, because documentation perpetuates doubts (about threads not having their ...


8

Most non-Unix multiprocessing operating systems (OSes) use a "spawn()" call or something similar to generate a new OS process or control flow. Spawn() tends to be a very complex call, with lots of options and lots of overhead. One of Unix's innovations was to provide a much lower overhead way of creating processes - fork(). Unix took care of the many ...


8

The kondemand process helps conserve power by reducing the CPU-Speed if the CPU isn't needed to run at maximum speed. Reduced Clock Speed == Reduced power requirements. Personally, I find that very useful on portable devices (smartphones, netbooks) but I'm not sure about that feature when it comes to Servers.


8

The mistake was to presume those numbers were PIDS, when in fact they are TIDS (thread IDs). See Linux function gettid(2). Reading up on clone(2) gives a lot of extra (and interesting) details.


8

Basically, the information in your book is historically accurate, because of a shamefully bad implementation history of threads on Linux. This answer by me to a related question on SO also serves as an answer to your question: https://stackoverflow.com/questions/9154671/distinction-between-processes-and-threads-in-linux/9154725#9154725 These confusions ...


8

Internally, there is no such thing as processes or threads in the linux kernel. Processes and threads are a mostly userland concept, the kernel itself only sees "tasks", which are a schedulable object that may share none, some, or all of its resources with other tasks. Threads are tasks that have been configured to share most of its resources (address space, ...


7

You can always do: ps -eLo pid= -o tid= | awk '$2 == 792 {print $1}' On Linux: $ readlink -f /proc/*/task/792/../.. /proc/300 Or with zsh: $ echo /proc/*/task/792(:h:h:t) 300


6

For your concrete example, there is a function cd_builtin, which is defined in builtins/cd.def (in the bash source code). It normally does a cd by calling that function. But it may fork first if you use it in a pipeline—for example, cd / | echo forks and calls cd_builtin in the child. You can also notice this by how the directory doesn't actually change: ...


6

We should not confuse the process PID and the thread id sometime written TID or in the ps command LPW. The scommand has options to display threads, and under top or htop you switch between threads and process by the H letter. As previously told by @Totor, with NPTL, which is the current implementation with kernel > 2.6, all threads have the same pid, but ...


5

Interrupts are handled by the operating system, threads (or processes, for that matter) aren't even aware of them. In the scenario you paint: Your thread issues a read() system call; the kernel gets the request, realizes that the thread won't do anything until data arrives (blocking call), so the thread is blocked. Kernel allocates space for buffers (if ...


5

Are lock, mutex, and semaphore for between threads or between processes? You'll find examples of locking primitives for both situations. For example, pthread mutexes are used for mutual exclusion between threads of the same process. On the other hand, System V IPC (man svipc) semaphores can be used across processes. Filesystem-level locks (on files or parts ...


5

The definition of sleep(3) allows for the call to return before, at, or after the time specified: DESCRIPTION sleep() makes the calling thread sleep until seconds seconds have elapsed or a signal arrives which is not ignored. So we have these possible scenarios The call is interrupted with an uncaught signal. sleep() returns immediately and before ...


5

Linus Torvalds stated in a kernel mailing list post in 1996 that “both threads and processes are treated as a 'context of execution'", which is "just a conglomerate of all of the state of that CoE.... includes things like CPU state, MMU state, permissions, and various communication states (open files, signal handlers, etc)". // simple program to create ...


5

This runs the top command with some extra options: top -H -b -n 1 The -H argument instructs top to display each individual thread. Normally top summarizes all threads under their parent process. The -b argument makes top run in batch mode – the information is gathered, displayed, and then dumped to stdout as opposed to running in an interactive mode and ...


4

Judging by the question you pose you probably haven't seen problems where Threads provide an advantage over the standard processes. There are problems like High-Frequency Trading for example where system becomes sensitive to the number of context switches in the system as well as switching from user to kernel mode and back. In this case ability to work ...


Only top voted, non community-wiki answers of a minimum length are eligible