1 @node Resource Usage And Limitation, Non-Local Exits, Date and Time, Top
2 @c %MENU% Functions for examining resource usage and getting and setting limits
3 @chapter Resource Usage And Limitation
4 This chapter describes functions for examining how much of various kinds of
5 resources (CPU time, memory, etc.) a process has used and getting and setting
6 limits on future usage.
9 * Resource Usage:: Measuring various resources used.
10 * Limits on Resources:: Specifying limits on resource usage.
11 * Priority:: Reading or setting process run priority.
12 * Memory Resources:: Querying memory available resources.
13 * Processor Resources:: Learn about the processors available.
18 @section Resource Usage
20 @pindex sys/resource.h
21 The function @code{getrusage} and the data type @code{struct rusage}
22 are used to examine the resource usage of a process. They are declared
23 in @file{sys/resource.h}.
25 @comment sys/resource.h
27 @deftypefun int getrusage (int @var{processes}, struct rusage *@var{rusage})
28 This function reports resource usage totals for processes specified by
29 @var{processes}, storing the information in @code{*@var{rusage}}.
31 In most systems, @var{processes} has only two valid values:
34 @comment sys/resource.h
37 Just the current process.
39 @comment sys/resource.h
42 All child processes (direct and indirect) that have already terminated.
45 In the GNU system, you can also inquire about a particular child process
46 by specifying its process ID.
48 The return value of @code{getrusage} is zero for success, and @code{-1}
53 The argument @var{processes} is not valid.
57 One way of getting resource usage for a particular child process is with
58 the function @code{wait4}, which returns totals for a child when it
59 terminates. @xref{BSD Wait Functions}.
61 @comment sys/resource.h
63 @deftp {Data Type} {struct rusage}
64 This data type stores various resource usage statistics. It has the
65 following members, and possibly others:
68 @item struct timeval ru_utime
69 Time spent executing user instructions.
71 @item struct timeval ru_stime
72 Time spent in operating system code on behalf of @var{processes}.
74 @item long int ru_maxrss
75 The maximum resident set size used, in kilobytes. That is, the maximum
76 number of kilobytes of physical memory that @var{processes} used
79 @item long int ru_ixrss
80 An integral value expressed in kilobytes times ticks of execution, which
81 indicates the amount of memory used by text that was shared with other
84 @item long int ru_idrss
85 An integral value expressed the same way, which is the amount of
86 unshared memory used for data.
88 @item long int ru_isrss
89 An integral value expressed the same way, which is the amount of
90 unshared memory used for stack space.
92 @item long int ru_minflt
93 The number of page faults which were serviced without requiring any I/O.
95 @item long int ru_majflt
96 The number of page faults which were serviced by doing I/O.
98 @item long int ru_nswap
99 The number of times @var{processes} was swapped entirely out of main memory.
101 @item long int ru_inblock
102 The number of times the file system had to read from the disk on behalf
105 @item long int ru_oublock
106 The number of times the file system had to write to the disk on behalf
109 @item long int ru_msgsnd
110 Number of IPC messages sent.
112 @item long int ru_msgrcv
113 Number of IPC messages received.
115 @item long int ru_nsignals
116 Number of signals received.
118 @item long int ru_nvcsw
119 The number of times @var{processes} voluntarily invoked a context switch
120 (usually to wait for some service).
122 @item long int ru_nivcsw
123 The number of times an involuntary context switch took place (because
124 a time slice expired, or another process of higher priority was
129 @code{vtimes} is a historical function that does some of what
130 @code{getrusage} does. @code{getrusage} is a better choice.
132 @code{vtimes} and its @code{vtimes} data structure are declared in
137 @deftypefun int vtimes (struct vtimes @var{current}, struct vtimes @var{child})
139 @code{vtimes} reports resource usage totals for a process.
141 If @var{current} is non-null, @code{vtimes} stores resource usage totals for
142 the invoking process alone in the structure to which it points. If
143 @var{child} is non-null, @code{vtimes} stores resource usage totals for all
144 past children (which have terminated) of the invoking process in the structure
147 @deftp {Data Type} {struct vtimes}
148 This data type contains information about the resource usage of a process.
149 Each member corresponds to a member of the @code{struct rusage} data type
154 User CPU time. Analogous to @code{ru_utime} in @code{struct rusage}
156 System CPU time. Analogous to @code{ru_stime} in @code{struct rusage}
158 Data and stack memory. The sum of the values that would be reported as
159 @code{ru_idrss} and @code{ru_isrss} in @code{struct rusage}
161 Shared memory. Analogous to @code{ru_ixrss} in @code{struct rusage}
163 Maximent resident set size. Analogous to @code{ru_maxrss} in
166 Major page faults. Analogous to @code{ru_majflt} in @code{struct rusage}
168 Minor page faults. Analogous to @code{ru_minflt} in @code{struct rusage}
170 Swap count. Analogous to @code{ru_nswap} in @code{struct rusage}
172 Disk reads. Analogous to @code{ru_inblk} in @code{struct rusage}
174 Disk writes. Analogous to @code{ru_oublk} in @code{struct rusage}
179 The return value is zero if the function succeeds; @code{-1} otherwise.
184 An additional historical function for examining resource usage,
185 @code{vtimes}, is supported but not documented here. It is declared in
188 @node Limits on Resources
189 @section Limiting Resource Usage
190 @cindex resource limits
191 @cindex limits on resource usage
194 You can specify limits for the resource usage of a process. When the
195 process tries to exceed a limit, it may get a signal, or the system call
196 by which it tried to do so may fail, depending on the resource. Each
197 process initially inherits its limit values from its parent, but it can
198 subsequently change them.
200 There are two per-process limits associated with a resource:
205 The current limit is the value the system will not allow usage to
206 exceed. It is also called the ``soft limit'' because the process being
207 limited can generally raise the current limit at will.
208 @cindex current limit
212 The maximum limit is the maximum value to which a process is allowed to
213 set its current limit. It is also called the ``hard limit'' because
214 there is no way for a process to get around it. A process may lower
215 its own maximum limit, but only the superuser may increase a maximum
217 @cindex maximum limit
221 @pindex sys/resource.h
222 The symbols for use with @code{getrlimit}, @code{setrlimit},
223 @code{getrlimit64}, and @code{setrlimit64} are defined in
224 @file{sys/resource.h}.
226 @comment sys/resource.h
228 @deftypefun int getrlimit (int @var{resource}, struct rlimit *@var{rlp})
229 Read the current and maximum limits for the resource @var{resource}
230 and store them in @code{*@var{rlp}}.
232 The return value is @code{0} on success and @code{-1} on failure. The
233 only possible @code{errno} error condition is @code{EFAULT}.
235 When the sources are compiled with @code{_FILE_OFFSET_BITS == 64} on a
236 32-bit system this function is in fact @code{getrlimit64}. Thus, the
237 LFS interface transparently replaces the old interface.
240 @comment sys/resource.h
242 @deftypefun int getrlimit64 (int @var{resource}, struct rlimit64 *@var{rlp})
243 This function is similar to @code{getrlimit} but its second parameter is
244 a pointer to a variable of type @code{struct rlimit64}, which allows it
245 to read values which wouldn't fit in the member of a @code{struct
248 If the sources are compiled with @code{_FILE_OFFSET_BITS == 64} on a
249 32-bit machine, this function is available under the name
250 @code{getrlimit} and so transparently replaces the old interface.
253 @comment sys/resource.h
255 @deftypefun int setrlimit (int @var{resource}, const struct rlimit *@var{rlp})
256 Store the current and maximum limits for the resource @var{resource}
257 in @code{*@var{rlp}}.
259 The return value is @code{0} on success and @code{-1} on failure. The
260 following @code{errno} error condition is possible:
266 The process tried to raise a current limit beyond the maximum limit.
269 The process tried to raise a maximum limit, but is not superuser.
273 When the sources are compiled with @code{_FILE_OFFSET_BITS == 64} on a
274 32-bit system this function is in fact @code{setrlimit64}. Thus, the
275 LFS interface transparently replaces the old interface.
278 @comment sys/resource.h
280 @deftypefun int setrlimit64 (int @var{resource}, const struct rlimit64 *@var{rlp})
281 This function is similar to @code{setrlimit} but its second parameter is
282 a pointer to a variable of type @code{struct rlimit64} which allows it
283 to set values which wouldn't fit in the member of a @code{struct
286 If the sources are compiled with @code{_FILE_OFFSET_BITS == 64} on a
287 32-bit machine this function is available under the name
288 @code{setrlimit} and so transparently replaces the old interface.
291 @comment sys/resource.h
293 @deftp {Data Type} {struct rlimit}
294 This structure is used with @code{getrlimit} to receive limit values,
295 and with @code{setrlimit} to specify limit values for a particular process
296 and resource. It has two fields:
299 @item rlim_t rlim_cur
302 @item rlim_t rlim_max
306 For @code{getrlimit}, the structure is an output; it receives the current
307 values. For @code{setrlimit}, it specifies the new values.
310 For the LFS functions a similar type is defined in @file{sys/resource.h}.
312 @comment sys/resource.h
314 @deftp {Data Type} {struct rlimit64}
315 This structure is analogous to the @code{rlimit} structure above, but
316 its components have wider ranges. It has two fields:
319 @item rlim64_t rlim_cur
320 This is analogous to @code{rlimit.rlim_cur}, but with a different type.
322 @item rlim64_t rlim_max
323 This is analogous to @code{rlimit.rlim_max}, but with a different type.
328 Here is a list of resources for which you can specify a limit. Memory
329 and file sizes are measured in bytes.
332 @comment sys/resource.h
336 The maximum amount of CPU time the process can use. If it runs for
337 longer than this, it gets a signal: @code{SIGXCPU}. The value is
338 measured in seconds. @xref{Operation Error Signals}.
340 @comment sys/resource.h
344 The maximum size of file the process can create. Trying to write a
345 larger file causes a signal: @code{SIGXFSZ}. @xref{Operation Error
348 @comment sys/resource.h
352 The maximum size of data memory for the process. If the process tries
353 to allocate data memory beyond this amount, the allocation function
356 @comment sys/resource.h
360 The maximum stack size for the process. If the process tries to extend
361 its stack past this size, it gets a @code{SIGSEGV} signal.
362 @xref{Program Error Signals}.
364 @comment sys/resource.h
368 The maximum size core file that this process can create. If the process
369 terminates and would dump a core file larger than this, then no core
370 file is created. So setting this limit to zero prevents core files from
373 @comment sys/resource.h
377 The maximum amount of physical memory that this process should get.
378 This parameter is a guide for the system's scheduler and memory
379 allocator; the system may give the process more memory when there is a
382 @comment sys/resource.h
385 The maximum amount of memory that can be locked into physical memory (so
386 it will never be paged out).
388 @comment sys/resource.h
391 The maximum number of processes that can be created with the same user ID.
392 If you have reached the limit for your user ID, @code{fork} will fail
393 with @code{EAGAIN}. @xref{Creating a Process}.
395 @comment sys/resource.h
398 @vindex RLIMIT_NOFILE
401 The maximum number of files that the process can open. If it tries to
402 open more files than this, its open attempt fails with @code{errno}
403 @code{EMFILE}. @xref{Error Codes}. Not all systems support this limit;
404 GNU does, and 4.4 BSD does.
406 @comment sys/resource.h
410 The maximum size of total memory that this process should get. If the
411 process tries to allocate more memory beyond this amount with, for
412 example, @code{brk}, @code{malloc}, @code{mmap} or @code{sbrk}, the
413 allocation function fails.
415 @comment sys/resource.h
419 The number of different resource limits. Any valid @var{resource}
420 operand must be less than @code{RLIM_NLIMITS}.
423 @comment sys/resource.h
425 @deftypevr Constant int RLIM_INFINITY
426 This constant stands for a value of ``infinity'' when supplied as
427 the limit value in @code{setrlimit}.
431 The following are historical functions to do some of what the functions
432 above do. The functions above are better choices.
434 @code{ulimit} and the command symbols are declared in @file{ulimit.h}.
439 @deftypefun int ulimit (int @var{cmd}, ...)
441 @code{ulimit} gets the current limit or sets the current and maximum
442 limit for a particular resource for the calling process according to the
445 If you are getting a limit, the command argument is the only argument.
446 If you are setting a limit, there is a second argument:
447 @code{long int} @var{limit} which is the value to which you are setting
450 The @var{cmd} values and the operations they specify are:
454 Get the current limit on the size of a file, in units of 512 bytes.
457 Set the current and maximum limit on the size of a file to @var{limit} *
462 There are also some other @var{cmd} values that may do things on some
463 systems, but they are not supported.
465 Only the superuser may increase a maximum limit.
467 When you successfully get a limit, the return value of @code{ulimit} is
468 that limit, which is never negative. When you successfully set a limit,
469 the return value is zero. When the function fails, the return value is
470 @code{-1} and @code{errno} is set according to the reason:
474 A process tried to increase a maximum limit, but is not superuser.
480 @code{vlimit} and its resource symbols are declared in @file{sys/vlimit.h}.
483 @comment sys/vlimit.h
485 @deftypefun int vlimit (int @var{resource}, int @var{limit})
487 @code{vlimit} sets the current limit for a resource for a process.
489 @var{resource} identifies the resource:
493 Maximum CPU time. Same as @code{RLIMIT_CPU} for @code{setrlimit}.
495 Maximum file size. Same as @code{RLIMIT_FSIZE} for @code{setrlimit}.
497 Maximum data memory. Same as @code{RLIMIT_DATA} for @code{setrlimit}.
499 Maximum stack size. Same as @code{RLIMIT_STACK} for @code{setrlimit}.
501 Maximum core file size. Same as @code{RLIMIT_COR} for @code{setrlimit}.
503 Maximum physical memory. Same as @code{RLIMIT_RSS} for @code{setrlimit}.
506 The return value is zero for success, and @code{-1} with @code{errno} set
507 accordingly for failure:
511 The process tried to set its current limit beyond its maximum limit.
517 @section Process CPU Priority And Scheduling
518 @cindex process priority
520 @cindex priority of a process
522 When multiple processes simultaneously require CPU time, the system's
523 scheduling policy and process CPU priorities determine which processes
524 get it. This section describes how that determination is made and
525 GNU C library functions to control it.
527 It is common to refer to CPU scheduling simply as scheduling and a
528 process' CPU priority simply as the process' priority, with the CPU
529 resource being implied. Bear in mind, though, that CPU time is not the
530 only resource a process uses or that processes contend for. In some
531 cases, it is not even particularly important. Giving a process a high
532 ``priority'' may have very little effect on how fast a process runs with
533 respect to other processes. The priorities discussed in this section
534 apply only to CPU time.
536 CPU scheduling is a complex issue and different systems do it in wildly
537 different ways. New ideas continually develop and find their way into
538 the intricacies of the various systems' scheduling algorithms. This
539 section discusses the general concepts, some specifics of systems
540 that commonly use the GNU C library, and some standards.
542 For simplicity, we talk about CPU contention as if there is only one CPU
543 in the system. But all the same principles apply when a processor has
544 multiple CPUs, and knowing that the number of processes that can run at
545 any one time is equal to the number of CPUs, you can easily extrapolate
548 The functions described in this section are all defined by the POSIX.1
549 and POSIX.1b standards (the @code{sched...} functions are POSIX.1b).
550 However, POSIX does not define any semantics for the values that these
551 functions get and set. In this chapter, the semantics are based on the
552 Linux kernel's implementation of the POSIX standard. As you will see,
553 the Linux implementation is quite the inverse of what the authors of the
554 POSIX syntax had in mind.
557 * Absolute Priority:: The first tier of priority. Posix
558 * Realtime Scheduling:: Scheduling among the process nobility
559 * Basic Scheduling Functions:: Get/set scheduling policy, priority
560 * Traditional Scheduling:: Scheduling among the vulgar masses
565 @node Absolute Priority
566 @subsection Absolute Priority
567 @cindex absolute priority
568 @cindex priority, absolute
570 Every process has an absolute priority, and it is represented by a number.
571 The higher the number, the higher the absolute priority.
573 @cindex realtime CPU scheduling
574 On systems of the past, and most systems today, all processes have
575 absolute priority 0 and this section is irrelevant. In that case,
576 @xref{Traditional Scheduling}. Absolute priorities were invented to
577 accommodate realtime systems, in which it is vital that certain processes
578 be able to respond to external events happening in real time, which
579 means they cannot wait around while some other process that @emph{wants
580 to}, but doesn't @emph{need to} run occupies the CPU.
583 @cindex preemptive scheduling
584 When two processes are in contention to use the CPU at any instant, the
585 one with the higher absolute priority always gets it. This is true even if the
586 process with the lower priority is already using the CPU (i.e. the
587 scheduling is preemptive). Of course, we're only talking about
588 processes that are running or ``ready to run,'' which means they are
589 ready to execute instructions right now. When a process blocks to wait
590 for something like I/O, its absolute priority is irrelevant.
592 @cindex runnable process
593 @strong{Note:} The term ``runnable'' is a synonym for ``ready to run.''
595 When two processes are running or ready to run and both have the same
596 absolute priority, it's more interesting. In that case, who gets the
597 CPU is determined by the scheduling policy. If the processes have
598 absolute priority 0, the traditional scheduling policy described in
599 @ref{Traditional Scheduling} applies. Otherwise, the policies described
600 in @ref{Realtime Scheduling} apply.
602 You normally give an absolute priority above 0 only to a process that
603 can be trusted not to hog the CPU. Such processes are designed to block
604 (or terminate) after relatively short CPU runs.
606 A process begins life with the same absolute priority as its parent
607 process. Functions described in @ref{Basic Scheduling Functions} can
610 Only a privileged process can change a process' absolute priority to
611 something other than @code{0}. Only a privileged process or the
612 target process' owner can change its absolute priority at all.
614 POSIX requires absolute priority values used with the realtime
615 scheduling policies to be consecutive with a range of at least 32. On
616 Linux, they are 1 through 99. The functions
617 @code{sched_get_priority_max} and @code{sched_set_priority_min} portably
618 tell you what the range is on a particular system.
621 @subsubsection Using Absolute Priority
623 One thing you must keep in mind when designing real time applications is
624 that having higher absolute priority than any other process doesn't
625 guarantee the process can run continuously. Two things that can wreck a
626 good CPU run are interrupts and page faults.
628 Interrupt handlers live in that limbo between processes. The CPU is
629 executing instructions, but they aren't part of any process. An
630 interrupt will stop even the highest priority process. So you must
631 allow for slight delays and make sure that no device in the system has
632 an interrupt handler that could cause too long a delay between
633 instructions for your process.
635 Similarly, a page fault causes what looks like a straightforward
636 sequence of instructions to take a long time. The fact that other
637 processes get to run while the page faults in is of no consequence,
638 because as soon as the I/O is complete, the high priority process will
639 kick them out and run again, but the wait for the I/O itself could be a
640 problem. To neutralize this threat, use @code{mlock} or
643 There are a few ramifications of the absoluteness of this priority on a
644 single-CPU system that you need to keep in mind when you choose to set a
645 priority and also when you're working on a program that runs with high
646 absolute priority. Consider a process that has higher absolute priority
647 than any other process in the system and due to a bug in its program, it
648 gets into an infinite loop. It will never cede the CPU. You can't run
649 a command to kill it because your command would need to get the CPU in
650 order to run. The errant program is in complete control. It controls
651 the vertical, it controls the horizontal.
653 There are two ways to avoid this: 1) keep a shell running somewhere with
654 a higher absolute priority. 2) keep a controlling terminal attached to
655 the high priority process group. All the priority in the world won't
656 stop an interrupt handler from running and delivering a signal to the
657 process if you hit Control-C.
659 Some systems use absolute priority as a means of allocating a fixed
660 percentage of CPU time to a process. To do this, a super high priority
661 privileged process constantly monitors the process' CPU usage and raises
662 its absolute priority when the process isn't getting its entitled share
663 and lowers it when the process is exceeding it.
665 @strong{Note:} The absolute priority is sometimes called the ``static
666 priority.'' We don't use that term in this manual because it misses the
667 most important feature of the absolute priority: its absoluteness.
670 @node Realtime Scheduling
671 @subsection Realtime Scheduling
672 @cindex realtime scheduling
674 Whenever two processes with the same absolute priority are ready to run,
675 the kernel has a decision to make, because only one can run at a time.
676 If the processes have absolute priority 0, the kernel makes this decision
677 as described in @ref{Traditional Scheduling}. Otherwise, the decision
678 is as described in this section.
680 If two processes are ready to run but have different absolute priorities,
681 the decision is much simpler, and is described in @ref{Absolute
684 Each process has a scheduling policy. For processes with absolute
685 priority other than zero, there are two available:
689 First Come First Served
694 The most sensible case is where all the processes with a certain
695 absolute priority have the same scheduling policy. We'll discuss that
698 In Round Robin, processes share the CPU, each one running for a small
699 quantum of time (``time slice'') and then yielding to another in a
700 circular fashion. Of course, only processes that are ready to run and
701 have the same absolute priority are in this circle.
703 In First Come First Served, the process that has been waiting the
704 longest to run gets the CPU, and it keeps it until it voluntarily
705 relinquishes the CPU, runs out of things to do (blocks), or gets
706 preempted by a higher priority process.
708 First Come First Served, along with maximal absolute priority and
709 careful control of interrupts and page faults, is the one to use when a
710 process absolutely, positively has to run at full CPU speed or not at
713 Judicious use of @code{sched_yield} function invocations by processes
714 with First Come First Served scheduling policy forms a good compromise
715 between Round Robin and First Come First Served.
717 To understand how scheduling works when processes of different scheduling
718 policies occupy the same absolute priority, you have to know the nitty
719 gritty details of how processes enter and exit the ready to run list:
721 In both cases, the ready to run list is organized as a true queue, where
722 a process gets pushed onto the tail when it becomes ready to run and is
723 popped off the head when the scheduler decides to run it. Note that
724 ready to run and running are two mutually exclusive states. When the
725 scheduler runs a process, that process is no longer ready to run and no
726 longer in the ready to run list. When the process stops running, it
727 may go back to being ready to run again.
729 The only difference between a process that is assigned the Round Robin
730 scheduling policy and a process that is assigned First Come First Serve
731 is that in the former case, the process is automatically booted off the
732 CPU after a certain amount of time. When that happens, the process goes
733 back to being ready to run, which means it enters the queue at the tail.
734 The time quantum we're talking about is small. Really small. This is
735 not your father's timesharing. For example, with the Linux kernel, the
736 round robin time slice is a thousand times shorter than its typical
737 time slice for traditional scheduling.
739 A process begins life with the same scheduling policy as its parent process.
740 Functions described in @ref{Basic Scheduling Functions} can change it.
742 Only a privileged process can set the scheduling policy of a process
743 that has absolute priority higher than 0.
745 @node Basic Scheduling Functions
746 @subsection Basic Scheduling Functions
748 This section describes functions in the GNU C library for setting the
749 absolute priority and scheduling policy of a process.
751 @strong{Portability Note:} On systems that have the functions in this
752 section, the macro _POSIX_PRIORITY_SCHEDULING is defined in
755 For the case that the scheduling policy is traditional scheduling, more
756 functions to fine tune the scheduling are in @ref{Traditional Scheduling}.
758 Don't try to make too much out of the naming and structure of these
759 functions. They don't match the concepts described in this manual
760 because the functions are as defined by POSIX.1b, but the implementation
761 on systems that use the GNU C library is the inverse of what the POSIX
762 structure contemplates. The POSIX scheme assumes that the primary
763 scheduling parameter is the scheduling policy and that the priority
764 value, if any, is a parameter of the scheduling policy. In the
765 implementation, though, the priority value is king and the scheduling
766 policy, if anything, only fine tunes the effect of that priority.
768 The symbols in this section are declared by including file @file{sched.h}.
772 @deftp {Data Type} {struct sched_param}
773 This structure describes an absolute priority.
775 @item int sched_priority
776 absolute priority value
782 @deftypefun int sched_setscheduler (pid_t @var{pid}, int @var{policy}, const struct sched_param *@var{param})
784 This function sets both the absolute priority and the scheduling policy
787 It assigns the absolute priority value given by @var{param} and the
788 scheduling policy @var{policy} to the process with Process ID @var{pid},
789 or the calling process if @var{pid} is zero. If @var{policy} is
790 negative, @code{sched_setscheduler} keeps the existing scheduling policy.
792 The following macros represent the valid values for @var{policy}:
796 Traditional Scheduling
803 @c The Linux kernel code (in sched.c) actually reschedules the process,
804 @c but it puts it at the head of the run queue, so I'm not sure just what
805 @c the effect is, but it must be subtle.
807 On success, the return value is @code{0}. Otherwise, it is @code{-1}
808 and @code{ERRNO} is set accordingly. The @code{errno} values specific
809 to this function are:
815 The calling process does not have @code{CAP_SYS_NICE} permission and
816 @var{policy} is not @code{SCHED_OTHER} (or it's negative and the
817 existing policy is not @code{SCHED_OTHER}.
820 The calling process does not have @code{CAP_SYS_NICE} permission and its
821 owner is not the target process' owner. I.e. the effective uid of the
822 calling process is neither the effective nor the real uid of process
824 @c We need a cross reference to the capabilities section, when written.
828 There is no process with pid @var{pid} and @var{pid} is not zero.
833 @var{policy} does not identify an existing scheduling policy.
836 The absolute priority value identified by *@var{param} is outside the
837 valid range for the scheduling policy @var{policy} (or the existing
838 scheduling policy if @var{policy} is negative) or @var{param} is
839 null. @code{sched_get_priority_max} and @code{sched_get_priority_min}
840 tell you what the valid range is.
843 @var{pid} is negative.
852 @deftypefun int sched_getscheduler (pid_t @var{pid})
854 This function returns the scheduling policy assigned to the process with
855 Process ID (pid) @var{pid}, or the calling process if @var{pid} is zero.
857 The return value is the scheduling policy. See
858 @code{sched_setscheduler} for the possible values.
860 If the function fails, the return value is instead @code{-1} and
861 @code{errno} is set accordingly.
863 The @code{errno} values specific to this function are:
868 There is no process with pid @var{pid} and it is not zero.
871 @var{pid} is negative.
875 Note that this function is not an exact mate to @code{sched_setscheduler}
876 because while that function sets the scheduling policy and the absolute
877 priority, this function gets only the scheduling policy. To get the
878 absolute priority, use @code{sched_getparam}.
885 @deftypefun int sched_setparam (pid_t @var{pid}, const struct sched_param *@var{param})
887 This function sets a process' absolute priority.
889 It is functionally identical to @code{sched_setscheduler} with
890 @var{policy} = @code{-1}.
892 @c in fact, that's how it's implemented in Linux.
898 @deftypefun int sched_getparam (pid_t @var{pid}, const struct sched_param *@var{param})
900 This function returns a process' absolute priority.
902 @var{pid} is the Process ID (pid) of the process whose absolute priority
905 @var{param} is a pointer to a structure in which the function stores the
906 absolute priority of the process.
908 On success, the return value is @code{0}. Otherwise, it is @code{-1}
909 and @code{ERRNO} is set accordingly. The @code{errno} values specific
910 to this function are:
915 There is no process with pid @var{pid} and it is not zero.
918 @var{pid} is negative.
927 @deftypefun int sched_get_priority_min (int *@var{policy});
929 This function returns the lowest absolute priority value that is
930 allowable for a process with scheduling policy @var{policy}.
932 On Linux, it is 0 for SCHED_OTHER and 1 for everything else.
934 On success, the return value is @code{0}. Otherwise, it is @code{-1}
935 and @code{ERRNO} is set accordingly. The @code{errno} values specific
936 to this function are:
940 @var{policy} does not identify an existing scheduling policy.
947 @deftypefun int sched_get_priority_max (int *@var{policy});
949 This function returns the highest absolute priority value that is
950 allowable for a process that with scheduling policy @var{policy}.
952 On Linux, it is 0 for SCHED_OTHER and 99 for everything else.
954 On success, the return value is @code{0}. Otherwise, it is @code{-1}
955 and @code{ERRNO} is set accordingly. The @code{errno} values specific
956 to this function are:
960 @var{policy} does not identify an existing scheduling policy.
967 @deftypefun int sched_rr_get_interval (pid_t @var{pid}, struct timespec *@var{interval})
969 This function returns the length of the quantum (time slice) used with
970 the Round Robin scheduling policy, if it is used, for the process with
971 Process ID @var{pid}.
973 It returns the length of time as @var{interval}.
974 @c We need a cross-reference to where timespec is explained. But that
975 @c section doesn't exist yet, and the time chapter needs to be slightly
976 @c reorganized so there is a place to put it (which will be right next
977 @c to timeval, which is presently misplaced). 2000.05.07.
979 With a Linux kernel, the round robin time slice is always 150
980 microseconds, and @var{pid} need not even be a real pid.
982 The return value is @code{0} on success and in the pathological case
983 that it fails, the return value is @code{-1} and @code{errno} is set
984 accordingly. There is nothing specific that can go wrong with this
985 function, so there are no specific @code{errno} values.
991 @deftypefun int sched_yield (void)
993 This function voluntarily gives up the process' claim on the CPU.
995 Technically, @code{sched_yield} causes the calling process to be made
996 immediately ready to run (as opposed to running, which is what it was
997 before). This means that if it has absolute priority higher than 0, it
998 gets pushed onto the tail of the queue of processes that share its
999 absolute priority and are ready to run, and it will run again when its
1000 turn next arrives. If its absolute priority is 0, it is more
1001 complicated, but still has the effect of yielding the CPU to other
1004 If there are no other processes that share the calling process' absolute
1005 priority, this function doesn't have any effect.
1007 To the extent that the containing program is oblivious to what other
1008 processes in the system are doing and how fast it executes, this
1009 function appears as a no-op.
1011 The return value is @code{0} on success and in the pathological case
1012 that it fails, the return value is @code{-1} and @code{errno} is set
1013 accordingly. There is nothing specific that can go wrong with this
1014 function, so there are no specific @code{errno} values.
1018 @node Traditional Scheduling
1019 @subsection Traditional Scheduling
1020 @cindex scheduling, traditional
1022 This section is about the scheduling among processes whose absolute
1023 priority is 0. When the system hands out the scraps of CPU time that
1024 are left over after the processes with higher absolute priority have
1025 taken all they want, the scheduling described herein determines who
1026 among the great unwashed processes gets them.
1029 * Traditional Scheduling Intro::
1030 * Traditional Scheduling Functions::
1033 @node Traditional Scheduling Intro
1034 @subsubsection Introduction To Traditional Scheduling
1036 Long before there was absolute priority (See @ref{Absolute Priority}),
1037 Unix systems were scheduling the CPU using this system. When Posix came
1038 in like the Romans and imposed absolute priorities to accommodate the
1039 needs of realtime processing, it left the indigenous Absolute Priority
1040 Zero processes to govern themselves by their own familiar scheduling
1043 Indeed, absolute priorities higher than zero are not available on many
1044 systems today and are not typically used when they are, being intended
1045 mainly for computers that do realtime processing. So this section
1046 describes the only scheduling many programmers need to be concerned
1049 But just to be clear about the scope of this scheduling: Any time a
1050 process with a absolute priority of 0 and a process with an absolute
1051 priority higher than 0 are ready to run at the same time, the one with
1052 absolute priority 0 does not run. If it's already running when the
1053 higher priority ready-to-run process comes into existence, it stops
1056 In addition to its absolute priority of zero, every process has another
1057 priority, which we will refer to as "dynamic priority" because it changes
1058 over time. The dynamic priority is meaningless for processes with
1059 an absolute priority higher than zero.
1061 The dynamic priority sometimes determines who gets the next turn on the
1062 CPU. Sometimes it determines how long turns last. Sometimes it
1063 determines whether a process can kick another off the CPU.
1065 In Linux, the value is a combination of these things, but mostly it is
1066 just determines the length of the time slice. The higher a process'
1067 dynamic priority, the longer a shot it gets on the CPU when it gets one.
1068 If it doesn't use up its time slice before giving up the CPU to do
1069 something like wait for I/O, it is favored for getting the CPU back when
1070 it's ready for it, to finish out its time slice. Other than that,
1071 selection of processes for new time slices is basically round robin.
1072 But the scheduler does throw a bone to the low priority processes: A
1073 process' dynamic priority rises every time it is snubbed in the
1074 scheduling process. In Linux, even the fat kid gets to play.
1076 The fluctuation of a process' dynamic priority is regulated by another
1077 value: The ``nice'' value. The nice value is an integer, usually in the
1078 range -20 to 20, and represents an upper limit on a process' dynamic
1079 priority. The higher the nice number, the lower that limit.
1081 On a typical Linux system, for example, a process with a nice value of
1082 20 can get only 10 milliseconds on the CPU at a time, whereas a process
1083 with a nice value of -20 can achieve a high enough priority to get 400
1086 The idea of the nice value is deferential courtesy. In the beginning,
1087 in the Unix garden of Eden, all processes shared equally in the bounty
1088 of the computer system. But not all processes really need the same
1089 share of CPU time, so the nice value gave a courteous process the
1090 ability to refuse its equal share of CPU time that others might prosper.
1091 Hence, the higher a process' nice value, the nicer the process is.
1092 (Then a snake came along and offered some process a negative nice value
1093 and the system became the crass resource allocation system we know
1096 Dynamic priorities tend upward and downward with an objective of
1097 smoothing out allocation of CPU time and giving quick response time to
1098 infrequent requests. But they never exceed their nice limits, so on a
1099 heavily loaded CPU, the nice value effectively determines how fast a
1102 In keeping with the socialistic heritage of Unix process priority, a
1103 process begins life with the same nice value as its parent process and
1104 can raise it at will. A process can also raise the nice value of any
1105 other process owned by the same user (or effective user). But only a
1106 privileged process can lower its nice value. A privileged process can
1107 also raise or lower another process' nice value.
1109 GNU C Library functions for getting and setting nice values are described in
1110 @xref{Traditional Scheduling Functions}.
1112 @node Traditional Scheduling Functions
1113 @subsubsection Functions For Traditional Scheduling
1115 @pindex sys/resource.h
1116 This section describes how you can read and set the nice value of a
1117 process. All these symbols are declared in @file{sys/resource.h}.
1119 The function and macro names are defined by POSIX, and refer to
1120 "priority," but the functions actually have to do with nice values, as
1121 the terms are used both in the manual and POSIX.
1123 The range of valid nice values depends on the kernel, but typically it
1124 runs from @code{-20} to @code{20}. A lower nice value corresponds to
1125 higher priority for the process. These constants describe the range of
1129 @comment sys/resource.h
1132 The lowest valid nice value.
1134 @comment sys/resource.h
1137 The highest valid nice value.
1140 @comment sys/resource.h
1142 @deftypefun int getpriority (int @var{class}, int @var{id})
1143 Return the nice value of a set of processes; @var{class} and @var{id}
1144 specify which ones (see below). If the processes specified do not all
1145 have the same nice value, this returns the lowest value that any of them
1148 On success, the return value is @code{0}. Otherwise, it is @code{-1}
1149 and @code{ERRNO} is set accordingly. The @code{errno} values specific
1150 to this function are:
1154 The combination of @var{class} and @var{id} does not match any existing
1158 The value of @var{class} is not valid.
1161 If the return value is @code{-1}, it could indicate failure, or it could
1162 be the nice value. The only way to make certain is to set @code{errno =
1163 0} before calling @code{getpriority}, then use @code{errno != 0}
1164 afterward as the criterion for failure.
1167 @comment sys/resource.h
1169 @deftypefun int setpriority (int @var{class}, int @var{id}, int @var{niceval})
1170 Set the nice value of a set of processes to @var{niceval}; @var{class}
1171 and @var{id} specify which ones (see below).
1173 The return value is @code{0} on success, and @code{-1} on
1174 failure. The following @code{errno} error condition are possible for
1179 The combination of @var{class} and @var{id} does not match any existing
1183 The value of @var{class} is not valid.
1186 The call would set the nice value of a process which is owned by a different
1187 user than the calling process (i.e. the target process' real or effective
1188 uid does not match the calling process' effective uid) and the calling
1189 process does not have @code{CAP_SYS_NICE} permission.
1192 The call would lower the process' nice value and the process does not have
1193 @code{CAP_SYS_NICE} permission.
1198 The arguments @var{class} and @var{id} together specify a set of
1199 processes in which you are interested. These are the possible values of
1203 @comment sys/resource.h
1206 One particular process. The argument @var{id} is a process ID (pid).
1208 @comment sys/resource.h
1211 All the processes in a particular process group. The argument @var{id} is
1212 a process group ID (pgid).
1214 @comment sys/resource.h
1217 All the processes owned by a particular user (i.e. whose real uid
1218 indicates the user). The argument @var{id} is a user ID (uid).
1221 If the argument @var{id} is 0, it stands for the calling process, its
1222 process group, or its owner (real uid), according to @var{class}.
1226 @deftypefun int nice (int @var{increment})
1227 Increment the nice value of the calling process by @var{increment}.
1228 The return value is the new nice value on success, and @code{-1} on
1229 failure. In the case of failure, @code{errno} will be set to the
1230 same values as for @code{setpriority}.
1233 Here is an equivalent definition of @code{nice}:
1237 nice (int increment)
1239 int result, old = getpriority (PRIO_PROCESS, 0);
1240 result = setpriority (PRIO_PROCESS, 0, old + increment);
1242 return old + increment;
1249 @node Memory Resources
1250 @section Querying memory available resources
1252 The amount of memory available in the system and the way it is organized
1253 determines oftentimes the way programs can and have to work. For
1254 functions like @code{mmap} it is necessary to know about the size of
1255 individual memory pages and knowing how much memory is available enables
1256 a program to select appropriate sizes for, say, caches. Before we get
1257 into these details a few words about memory subsystems in traditional
1258 Unix systems will be given.
1261 * Memory Subsystem:: Overview about traditional Unix memory handling.
1262 * Query Memory Parameters:: How to get information about the memory
1266 @node Memory Subsystem
1267 @subsection Overview about traditional Unix memory handling
1269 @cindex address space
1270 @cindex physical memory
1271 @cindex physical address
1272 Unix systems normally provide processes virtual address spaces. This
1273 means that the addresses of the memory regions do not have to correspond
1274 directly to the addresses of the actual physical memory which stores the
1275 data. An extra level of indirection is introduced which translates
1276 virtual addresses into physical addresses. This is normally done by the
1277 hardware of the processor.
1279 @cindex shared memory
1280 Using a virtual address space has several advantage. The most important
1281 is process isolation. The different processes running on the system
1282 cannot interfere directly with each other. No process can write into
1283 the address space of another process (except when shared memory is used
1284 but then it is wanted and controlled).
1286 Another advantage of virtual memory is that the address space the
1287 processes see can actually be larger than the physical memory available.
1288 The physical memory can be extended by storage on an external media
1289 where the content of currently unused memory regions is stored. The
1290 address translation can then intercept accesses to these memory regions
1291 and make memory content available again by loading the data back into
1292 memory. This concept makes it necessary that programs which have to use
1293 lots of memory know the difference between available virtual address
1294 space and available physical memory. If the working set of virtual
1295 memory of all the processes is larger than the available physical memory
1296 the system will slow down dramatically due to constant swapping of
1297 memory content from the memory to the storage media and back. This is
1298 called ``thrashing''.
1302 @cindex page, memory
1303 A final aspect of virtual memory which is important and follows from
1304 what is said in the last paragraph is the granularity of the virtual
1305 address space handling. When we said that the virtual address handling
1306 stores memory content externally it cannot do this on a byte-by-byte
1307 basis. The administrative overhead does not allow this (leaving alone
1308 the processor hardware). Instead several thousand bytes are handled
1309 together and form a @dfn{page}. The size of each page is always a power
1310 of two byte. The smallest page size in use today is 4096, with 8192,
1311 16384, and 65536 being other popular sizes.
1313 @node Query Memory Parameters
1314 @subsection How to get information about the memory subsystem?
1316 The page size of the virtual memory the process sees is essential to
1317 know in several situations. Some programming interface (e.g.,
1318 @code{mmap}, @pxref{Memory-mapped I/O}) require the user to provide
1319 information adjusted to the page size. In the case of @code{mmap} is it
1320 necessary to provide a length argument which is a multiple of the page
1321 size. Another place where the knowledge about the page size is useful
1322 is in memory allocation. If one allocates pieces of memory in larger
1323 chunks which are then subdivided by the application code it is useful to
1324 adjust the size of the larger blocks to the page size. If the total
1325 memory requirement for the block is close (but not larger) to a multiple
1326 of the page size the kernel's memory handling can work more effectively
1327 since it only has to allocate memory pages which are fully used. (To do
1328 this optimization it is necessary to know a bit about the memory
1329 allocator which will require a bit of memory itself for each block and
1330 this overhead must not push the total size over the page size multiple.
1332 The page size traditionally was a compile time constant. But recent
1333 development of processors changed this. Processors now support
1334 different page sizes and they can possibly even vary among different
1335 processes on the same system. Therefore the system should be queried at
1336 runtime about the current page size and no assumptions (except about it
1337 being a power of two) should be made.
1339 @vindex _SC_PAGESIZE
1340 The correct interface to query about the page size is @code{sysconf}
1341 (@pxref{Sysconf Definition}) with the parameter @code{_SC_PAGESIZE}.
1342 There is a much older interface available, too.
1346 @deftypefun int getpagesize (void)
1347 The @code{getpagesize} function returns the page size of the process.
1348 This value is fixed for the runtime of the process but can vary in
1349 different runs of the application.
1351 The function is declared in @file{unistd.h}.
1354 Widely available on @w{System V} derived systems is a method to get
1355 information about the physical memory the system has. The call
1357 @vindex _SC_PHYS_PAGES
1360 sysconf (_SC_PHYS_PAGES)
1364 returns the total number of pages of physical the system has.
1365 This does not mean all this memory is available. This information can
1368 @vindex _SC_AVPHYS_PAGES
1371 sysconf (_SC_AVPHYS_PAGES)
1374 These two values help to optimize applications. The value returned for
1375 @code{_SC_AVPHYS_PAGES} is the amount of memory the application can use
1376 without hindering any other process (given that no other process
1377 increases its memory usage). The value returned for
1378 @code{_SC_PHYS_PAGES} is more or less a hard limit for the working set.
1379 If all applications together constantly use more than that amount of
1380 memory the system is in trouble.
1382 The GNU C library provides in addition to these already described way to
1383 get this information two functions. They are declared in the file
1384 @file{sys/sysinfo.h}. Programmers should prefer to use the
1385 @code{sysconf} method described above.
1387 @comment sys/sysinfo.h
1389 @deftypefun {long int} get_phys_pages (void)
1390 The @code{get_phys_pages} function returns the total number of pages of
1391 physical the system has. To get the amount of memory this number has to
1392 be multiplied by the page size.
1394 This function is a GNU extension.
1397 @comment sys/sysinfo.h
1399 @deftypefun {long int} get_avphys_pages (void)
1400 The @code{get_phys_pages} function returns the number of available pages of
1401 physical the system has. To get the amount of memory this number has to
1402 be multiplied by the page size.
1404 This function is a GNU extension.
1407 @node Processor Resources
1408 @section Learn about the processors available
1410 The use of threads or processes with shared memory allows an application
1411 to take advantage of all the processing power a system can provide. If
1412 the task can be parallelized the optimal way to write an application is
1413 to have at any time as many processes running as there are processors.
1414 To determine the number of processors available to the system one can
1417 @vindex _SC_NPROCESSORS_CONF
1420 sysconf (_SC_NPROCESSORS_CONF)
1424 which returns the number of processors the operating system configured.
1425 But it might be possible for the operating system to disable individual
1426 processors and so the call
1428 @vindex _SC_NPROCESSORS_ONLN
1431 sysconf (_SC_NPROCESSORS_ONLN)
1435 returns the number of processors which are currently inline (i.e.,
1438 For these two pieces of information the GNU C library also provides
1439 functions to get the information directly. The functions are declared
1440 in @file{sys/sysinfo.h}.
1442 @comment sys/sysinfo.h
1444 @deftypefun int get_nprocs_conf (void)
1445 The @code{get_nprocs_conf} function returns the number of processors the
1446 operating system configured.
1448 This function is a GNU extension.
1451 @comment sys/sysinfo.h
1453 @deftypefun int get_nprocs (void)
1454 The @code{get_nprocs} function returns the number of available processors.
1456 This function is a GNU extension.
1459 @cindex load average
1460 Before starting more threads it should be checked whether the processors
1461 are not already overused. Unix systems calculate something called the
1462 @dfn{load average}. This is a number indicating how many processes were
1463 running. This number is average over different periods of times
1464 (normally 1, 5, and 15 minutes).
1468 @deftypefun int getloadavg (double @var{loadavg}[], int @var{nelem})
1469 This function gets the 1, 5 and 15 minute load averages of the
1470 system. The values are placed in @var{loadavg}. @code{getloadavg} will
1471 place at most @var{nelem} elements into the array but never more than
1472 three elements. The return value is the number of elements written to
1473 @var{loadavg}, or -1 on error.
1475 This function is declared in @file{stdlib.h}.