Misplaced Pages

C10k problem

Article snapshot taken from Wikipedia with creative commons attribution-sharealike license. Give it a read and then ask your questions in the chat. We can research this topic together.

The C10k problem is the problem of optimizing network sockets to handle a large number of clients at the same time. The name C10k is a numeronym for concurrently handling ten thousand connections. Handling many concurrent connections is a different problem from handling many requests per second : the latter requires high throughput (processing them quickly), while the former does not have to be fast, but requires efficient scheduling of connections.

#696303

39-427: The problem of socket server optimisation has been studied because a number of factors must be considered to allow a web server to support many clients. This can involve a combination of operating system constraints and web server software limitations. According to the scope of services to be made available and the capabilities of the operating system as well as hardware considerations such as multi-processing capabilities,

78-483: A 1:1 model. FreeBSD 5 implemented M:N model. FreeBSD 6 supported both 1:1 and M:N, users could choose which one should be used with a given program using /etc/libmap.conf. Starting with FreeBSD 7, the 1:1 became the default. FreeBSD 8 no longer supports the M:N model. In computer programming , single-threading is the processing of one command at a time. In the formal analysis of the variables' semantics and process state,

117-452: A context switch. On multi-processor systems, the thread may instead poll the mutex in a spinlock . Both of these may sap performance and force processors in symmetric multiprocessing (SMP) systems to contend for the memory bus, especially if the granularity of the locking is too fine. Other synchronization APIs include condition variables , critical sections , semaphores , and monitors . A popular programming pattern involving threads

156-508: A cooperatively multitasked thread blocks by waiting on a resource or if it starves other threads by not yielding control of execution during intensive computation. Until the early 2000s, most desktop computers had only one single-core CPU, with no support for hardware threads , although threads were still used on such computers because switching between threads was generally still quicker than full-process context switches . In 2002, Intel added support for simultaneous multithreading to

195-484: A given process may be executed concurrently (via multithreading capabilities), sharing resources such as memory , while different processes do not share these resources. In particular, the threads of a process share its executable code and the values of its dynamically allocated variables and non- thread-local global variables at any given time. The implementation of threads and processes differs between operating systems. Threads made an early appearance under

234-550: A multi-threading model or a single threading model can be preferred. Concurrently with this aspect, which involves considerations regarding memory management (usually operating system related), strategies implied relate to the very diverse aspects of I/O management. The term C10k was coined in 1999 by software engineer Dan Kegel, citing the Simtel FTP host, cdrom.com , serving 10,000 clients at once over 1 gigabit per second Ethernet in that year. The term has since been used for

273-431: A running fiber must explicitly " yield " to allow another fiber to run, which makes their implementation much easier than kernel or user threads . A fiber can be scheduled to run in any thread in the same process. This permits applications to gain performance improvements by managing scheduling themselves, instead of relying on the kernel scheduler (which may not be tuned for the application). Some research implementations of

312-418: A time, such as file servers , FTP servers , proxy servers , web servers , and load balancers . Single threading In computer science , a thread of execution is the smallest sequence of programmed instructions that can be managed independently by a scheduler , which is typically a part of the operating system . In many cases, a thread is a component of a process . The multiple threads of

351-472: A useful abstraction of concurrent execution. Multithreading can also be applied to one process to enable parallel execution on a multiprocessing system. Multithreading libraries tend to provide a function call to create a new thread, which takes a function as a parameter. A concurrent thread is then created which starts running the passed function and ends when the function returns. The thread libraries also offer data synchronization functions. Threads in

390-513: Is a "heavyweight" unit of kernel scheduling, as creating, destroying, and switching processes is relatively expensive. Processes own resources allocated by the operating system. Resources include memory (for both code and data), file handles , sockets, device handles, windows, and a process control block . Processes are isolated by process isolation , and do not share address spaces or file resources except through explicit methods such as inheriting file handles or shared memory segments, or mapping

429-402: Is a "lightweight" unit of kernel scheduling. At least one kernel thread exists within each process. If multiple kernel threads exist within a process, then they share the same memory and file resources. Kernel threads are preemptively multitasked if the operating system's process scheduler is preemptive. Kernel threads do not own resources except for a stack , a copy of the registers including

SECTION 10

#1732935249697

468-477: Is initiated, a system call is made, and does not return until the I/O operation has been completed. In the intervening period, the entire process is "blocked" by the kernel and cannot run, which starves other user threads and fibers in the same process from executing. A common solution to this problem (used, in particular, by many green threads implementations) is providing an I/O API that implements an interface that blocks

507-878: Is less of an issue than with preemptively scheduled threads, and synchronization constructs including spinlocks and atomic operations are unnecessary when writing fibered code, as they are implicitly synchronized. However, many libraries yield a fiber implicitly as a method of conducting non-blocking I/O ; as such, some caution and documentation reading is advised. A disadvantage is that fibers cannot utilize multiprocessor machines without also using preemptive threads; however, an M:N threading model with no more preemptive threads than CPU cores can be more efficient than either pure fibers or pure preemptive threading. In some server programs fibers are used to soft block themselves to allow their single-threaded parent programs to continue working. In this design, fibers are used mostly for I/O access which does not need CPU processing. This allows

546-815: Is not so great a difference except in the cost of an address-space switch, which on some architectures (notably x86 ) results in a translation lookaside buffer (TLB) flush. Advantages and disadvantages of threads vs processes include: Operating systems schedule threads either preemptively or cooperatively . Multi-user operating systems generally favor preemptive multithreading for its finer-grained control over execution time via context switching . However, preemptive scheduling may context-switch threads at moments unanticipated by programmers, thus causing lock convoy , priority inversion , or other side-effects. In contrast, cooperative multithreading relies on threads to relinquish control of execution, thus ensuring that threads run to completion . This can cause problems if

585-405: Is that of thread pools where a set number of threads are created at startup that then wait for a task to be assigned. When a new task arrives, it wakes up, completes the task and goes back to waiting. This avoids the relatively expensive thread creation and destruction functions for every task performed and takes thread management out of the application developer's hand and leaves it to a library or

624-804: Is typically uniformly done preemptively or, less commonly, cooperatively. At the user level a process such as a runtime system can itself schedule multiple threads of execution. If these do not share data, as in Erlang, they are usually analogously called processes, while if they share data they are usually called (user) threads , particularly if preemptively scheduled. Cooperatively scheduled user threads are known as fibers ; different processes may schedule user threads differently. User threads may be executed by kernel threads in various ways (one-to-one, many-to-one, many-to-many). The term " light-weight process " variously refers to user threads or to kernel mechanisms for scheduling user threads onto kernel threads. A process

663-403: Is unaware of them, so they are managed and scheduled in userspace. Some implementations base their user threads on top of several kernel threads, to benefit from multi-processor machines ( M:N model ). User threads as implemented by virtual machines are also called green threads . As user thread implementations are typically entirely in userspace, context switching between user threads within

702-519: The OpenMP parallel programming model implement their tasks through fibers. Closely related to fibers are coroutines , with the distinction being that coroutines are a language-level construct, while fibers are a system-level construct. Threads differ from traditional multitasking operating-system processes in several ways: Systems such as Windows NT and OS/2 are said to have cheap threads and expensive processes; in other operating systems there

741-507: The Pentium ;4 processor, under the name hyper-threading ; in 2005, they introduced the dual-core Pentium D processor and AMD introduced the dual-core Athlon 64 X2 processor. Systems with a single processor generally implement multithreading by time slicing : the central processing unit (CPU) switches between different software threads . This context switching usually occurs frequently enough that users perceive

780-715: The program counter , and thread-local storage (if any), and are thus relatively cheap to create and destroy. Thread switching is also relatively cheap: it requires a context switch (saving and restoring registers and stack pointer), but does not change virtual memory and is thus cache-friendly (leaving TLB valid). The kernel can assign one or more software threads to each core in a CPU (it being able to assign itself multiple software threads depending on its support for multithreading), and can swap out threads that get blocked. However, kernel threads take much longer than user threads to be swapped. Threads are sometimes implemented in userspace libraries, thus called user threads . The kernel

819-399: The M:N implementation, the threading library is responsible for scheduling user threads on the available schedulable entities; this makes context switching of threads very fast, as it avoids system calls. However, this increases complexity and the likelihood of priority inversion , as well as suboptimal scheduling without extensive (and expensive) coordination between the userland scheduler and

SECTION 20

#1732935249697

858-542: The calling thread, rather than the entire process, by using non-blocking I/O internally, and scheduling another user thread or fiber while the I/O operation is in progress. Similar solutions can be provided for other blocking system calls. Alternatively, the program can be written to avoid the use of synchronous I/O or other blocking system calls (in particular, using non-blocking I/O, including lambda continuations and/or async/ await primitives ). Fibers are an even lighter unit of scheduling which are cooperatively scheduled :

897-596: The general issue of large number of clients, with similar numeronyms for larger number of connections, most recently "C10M" in the 2010s to refer to 10 million concurrent connections. By the early 2010s millions of connections on a single commodity 1U rackmount server became possible: over 2 million connections ( WhatsApp , 24 cores, using Erlang on FreeBSD ) and 10–12 million connections (MigratoryData, 12 cores, using Java on Linux ). Common applications of very high numbers of connections include general public servers that have to serve thousands or even millions of users at

936-424: The kernel has no knowledge of the application threads. With this approach, context switching can be done very quickly and, in addition, it can be implemented even on simple kernels which do not support threading. One of the major drawbacks, however, is that it cannot benefit from the hardware acceleration on multithreaded processors or multi-processor computers: there is never more than one thread being scheduled at

975-421: The kernel scheduler. SunOS 4.x implemented light-weight processes or LWPs. NetBSD 2.x+, and DragonFly BSD implement LWPs as kernel threads (1:1 model). SunOS 5.2 through SunOS 5.8 as well as NetBSD 2 to NetBSD 4 implemented a two level model, multiplexing one or more user level threads on each kernel thread (M:N model). SunOS 5.9 and later, as well as NetBSD 5 eliminated user threads support, returning to

1014-650: The kernel's thread scheduler to preempt a busy thread and resume another thread; fibers yield themselves to run another fiber while executing. The key difference between fibers and kernel threads is that fibers use cooperative context switching, instead of preemptive time-slicing. In effect, fibers extend the concurrency taxonomy: Fibers (sometimes called stackful coroutines or user mode cooperatively scheduled threads) and stackless coroutines (compiler synthesized state machines) represent two distinct programming facilities with vast performance and functionality differences. Because fibers multitask cooperatively, thread safety

1053-582: The main program to continue with what it is doing. Fibers yield control to the single-threaded main program, and when the I/O operation is completed fibers continue where they left off. Less support from the operating system is needed for fibers than for threads. They can be implemented in modern Unix systems using the library functions getcontext, setcontext and swapcontext in ucontext.h , as in GNU Portable Threads , or in assembler as boost.fiber . On Microsoft Windows , fibers are created using

1092-438: The multiple cores. Scheduling can be done at the kernel level or user level, and multitasking can be done preemptively or cooperatively . This yields a variety of related concepts. At the kernel level, a process contains one or more kernel threads , which share the process's resources, such as memory and file handles – a process is a unit of resources, while a thread is a unit of scheduling and execution. Kernel scheduling

1131-661: The name of "tasks" in IBM's batch processing operating system, OS/360, in 1967. It provided users with three available configurations of the OS/360 control system, of which Multiprogramming with a Variable Number of Tasks (MVT) was one. Saltzer (1966) credits Victor A. Vyssotsky with the term "thread". The use of threads in software applications became more common in the early 2000s as CPUs began to utilize multiple cores. Applications wishing to take advantage of multiple cores for performance advantages were required to employ concurrency to utilize

1170-568: The operating system that is better suited to optimize thread management. Multithreaded applications have the following advantages vs single-threaded ones: Multithreaded applications have the following drawbacks: Many programming languages support threading in some capacity. Fiber (computer science) In computer science , a fiber is a particularly lightweight thread of execution . Like threads, fibers share address space . However, fibers use cooperative multitasking while threads use preemptive multitasking . Threads often depend on

1209-452: The requirements of the program's workload. However, the use of blocking system calls in user threads (as opposed to kernel threads) can be problematic. If a user thread or a fiber performs a system call that blocks, the other user threads and fibers in the process are unable to run until the system call returns. A typical example of this problem is when performing I/O: most programs are written to perform I/O synchronously. When an I/O operation

C10k problem - Misplaced Pages Continue

1248-540: The same file in a shared way – see interprocess communication . Creating or destroying a process is relatively expensive, as resources must be acquired or released. Processes are typically preemptively multitasked, and process switching is relatively expensive, beyond basic cost of context switching , due to issues such as cache flushing (in particular, process switching changes virtual memory addressing, causing invalidation and thus flushing of an untagged translation lookaside buffer (TLB), notably on x86). A kernel thread

1287-451: The same process is extremely efficient because it does not require any interaction with the kernel at all: a context switch can be performed by locally saving the CPU registers used by the currently executing user thread or fiber and then loading the registers required by the user thread or fiber to be executed. Since scheduling occurs in userspace, the scheduling policy can be more easily tailored to

1326-406: The same process share the same address space. This allows concurrently running code to couple tightly and conveniently exchange data without the overhead or complexity of an IPC . When shared between threads, however, even simple data structures become prone to race conditions if they require more than one CPU instruction to update: two threads may end up attempting to update the data structure at

1365-407: The same time and find it unexpectedly changing underfoot. Bugs caused by race conditions can be very difficult to reproduce and isolate. To prevent this, threading application programming interfaces (APIs) offer synchronization primitives such as mutexes to lock data structures against concurrent access. On uniprocessor systems, a thread running into a locked mutex must sleep and hence trigger

1404-621: The same time. For example: If one of the threads needs to execute an I/O request, the whole process is blocked and the threading advantage cannot be used. The GNU Portable Threads uses User-level threading, as does State Threads . M : N maps some M number of application threads onto some N number of kernel entities, or "virtual processors." This is a compromise between kernel-level ("1:1") and user-level (" N :1") threading. In general, " M : N " threading systems are more complex to implement than either kernel or user threads, because changes to both kernel and user-space code are required . In

1443-502: The term single threading can be used differently to mean "backtracking within a single thread", which is common in the functional programming community. Multithreading is mainly found in multitasking operating systems. Multithreading is a widespread programming and execution model that allows multiple threads to exist within the context of one process. These threads share the process's resources, but are able to execute independently. The threaded programming model provides developers with

1482-510: The threads or tasks as running in parallel (for popular server/desktop operating systems, maximum time slice of a thread, when other threads are waiting, is often limited to 100–200ms). On a multiprocessor or multi-core system, multiple threads can execute in parallel , with every processor or core executing a separate thread simultaneously; on a processor or core with hardware threads , separate software threads can also be executed concurrently by separate hardware threads. Threads created by

1521-558: The user in a 1:1 correspondence with schedulable entities in the kernel are the simplest possible threading implementation. OS/2 and Win32 used this approach from the start, while on Linux the GNU C Library implements this approach (via the NPTL or older LinuxThreads ). This approach is also used by Solaris , NetBSD , FreeBSD , macOS , and iOS . An M :1 model implies that all application-level threads map to one kernel-level scheduled entity;

#696303