Task Control Interfaces
Task Control Interfaces
Tasks. NuttX is a flat address OS. As such it does not support processes in the way that, say, Linux does. NuttX only supports simple threads running within the same address space. However, the programming model makes a distinction between tasks and pthreads:
- tasks are threads which have a degree of independence
- pthreads share some resources.
File Descriptors and Streams. This applies, in particular, in the area of opened file descriptors and streams. When a task is started using the interfaces in this section, it will be created with at most three open files.
If CONFIG_DEV_CONSOLE is defined, the first three file descriptors
(corresponding to stdin, stdout, stderr) will be duplicated for the new
task. Since these file descriptors are duplicated, the child task can
free close them or manipulate them in any way without effecting the
parent task. File-related operations (open, close, etc.) within a task
will have no effect on other tasks. Since the three file descriptors are
duplicated, it is also possible to perform some level of redirection.
pthreads, on the other hand, will always share file descriptors with the parent thread. In this case, file operations will have effect only all pthreads the were started from the same parent thread.
Executing Programs within a File System. NuttX also provides internal interfaces for the execution of separately built programs that reside in a file system. These internal interfaces are, however, non-standard and are documented with the NuttX binary loader and NXFLAT documentation.
Resource Limits. A task keeps its resource limits, and the tasks that it creates get the same limits. NuttX enforces two of them:
RLIMIT_NOFILE: a new file descriptor must be below the soft limit. The default and the largest value isOPEN_MAX.RLIMIT_STACK: when the soft limit is set, it is the default stack size of a new thread and the stack size of a new ELF program that does not setnx_stacksize. The value is kept betweenPTHREAD_STACK_MINand the largest stack that a task can have. By default there is no limit, and the stack sizes areCONFIG_PTHREAD_STACK_DEFAULTandCONFIG_ELF_STACKSIZE.
getrlimit() reports the other resources as RLIM_INFINITY, and
setrlimit() fails with EINVAL for them.
File Mode Creation Mask. A task keeps its file mode creation mask
(umask()), and the tasks that it creates get the same mask. All the
threads of a task use one mask. Kernel threads use the mask 0.
Task Control Interfaces. The following task control interfaces are provided by NuttX:
Note
Maybe this can be converted into a table, or could otherwise be replaced by the index if these are sectioned in this way.
Non-standard task control interfaces inspired by VxWorks interfaces:
task_create()task_delete()task_restart()
Non-standard extensions to VxWorks-like interfaces to support POSIX Cancellation Points.
Standard interfaces
Standard fork/vfork and exec[v|l] interfaces:
Standard posix_spawn interfaces:
posix_spawn()andposix_spawnp()posix_spawn_file_actions_init()posix_spawn_file_actions_destroy()posix_spawn_file_actions_addclose()posix_spawn_file_actions_adddup2()posix_spawn_file_actions_addopen()posix_spawnattr_init()posix_spawnattr_getflags()posix_spawnattr_getschedparam()posix_spawnattr_getschedpolicy()posix_spawnattr_getpriority()posix_spawnattr_getsigmask()posix_spawnattr_setflags()posix_spawnattr_setschedparam()posix_spawnattr_setschedpolicy()posix_spawnattr_setpriority()posix_spawnattr_setsigmask()
Non-standard task control interfaces inspired by posix_spawn:
Functions
int task_create(char *name, int priority, int stack_size, main_t entry, char *const argv[]) : This function creates and activates a new task with a specified priority and returns its system-assigned ID.
The entry address entry is the address of the “main” function of the
task. This function will be called once the C environment has been set
up. The specified function will be called with four arguments. Should
the specified routine return, a call to [`exit()`](#c.exit) will automatically be
made.
Note that an arbitrary number of arguments may be passed to the spawned
functions.
The arguments are copied (via `strdup`) so that the life of the passed
strings is not dependent on the life of the caller to [`task_create()`](#c.task_create).
The newly created task does not inherit scheduler characteristics from
the parent task: The new task is started at the default system priority
and with the `SCHED_FIFO` scheduling policy. These characteristics may be
modified after the new task has been started.
The newly created task does inherit the first three file descriptors
(corresponding to stdin, stdout, and stderr) and redirection of standard
I/O is supported.
Parameters:
: - **name** – Name of the new task
- **priority** – Priority of the new task
- **stack\_size** – size (in bytes) of the stack needed
- **entry** – Entry point of a new task
- **argv** – A pointer to an array of input parameters. The array should
be terminated with a NULL argv[] value. If no parameters are
required, argv may be NULL.
Returns:
: the non-zero task ID of the new task or ERROR if memory is
insufficient or the task cannot be created
(``errno` <#ErrnoAccess>`\_\_ is not set).
**Defined in:** `sched.h`
**POSIX Compatibility:** This is a NON-POSIX interface. VxWorks provides
the following similar interface:
```cint taskSpawn(char *name, int priority, int options, int stackSize, FUNCPTR entryPt, int arg1, int arg2, int arg3, int arg4, int arg5, int arg6, int arg7, int arg8, int arg9, int arg10);
The NuttX [`task_create()`](#c.task_create) differs from VxWorks’ `taskSpawn()` in the
following ways:
> - Interface name
> - Various differences in types of arguments
> - There is no options argument.
> - A variable number of parameters can be passed to a task (VxWorks
> supports ten).
<a id="c.task_delete"></a> int task\_delete([pid\_t](/u/5606252b9680a938#c-pid-t) pid)
: This function causes a specified task to cease to
exist. Its stack and TCB will be deallocated. This function is the
companion to `task_create()`. This is the version of the function
exposed to the user; it is simply a wrapper around the internal,
`nxtask_terminate()` function.
The logic in this function only deletes non-running tasks. If the
`pid` parameter refers to the currently running task, then processing
is redirected to `exit()`. This can only happen if a task calls
`task_delete()` in order to delete itself.
This function obeys the semantics of pthread cancellation: task deletion
is deferred if cancellation is disabled or if deferred cancellation is
supported (with [Cancellation
Points](https://cwiki.apache.org/confluence/display/NUTTX/Cancellation+Points)
enabled).
Parameters:
: - **pid** – The task ID of the task to delete. An ID of zero signifies
the calling task. Any attempt by the calling task will be
automatically re-directed to `exit()`.
Returns:
: `OK`, or `ERROR` if the task cannot be deleted. The
``errno` <#ErrnoAccess>`\_\_ is set to indicate the nature of the
failure. This function can fail, for example, if the provided pid
does not correspond to a currently executing task.
**Assumptions/Limitations:**
`task_delete()` must be used with caution: If the task holds resources
(for example, allocated memory or semaphores needed by other tasks),
then `task_delete()` can strand those resources.
**POSIX Compatibility:** This is a NON-POSIX interface. VxWorks provides
the following similar interface:
The NuttX task\_delete() differs from VxWorks’ taskDelete() in the
following ways:
- No support is provided for calling the tasks deletion routines
(because the VxWorks `taskDeleteHookAdd()` is not supported).
However, if `atexit()` or `on_exit` support is enabled, those
will be called when the task deleted.
- Deletion of self is supported, but only because `task_delete()`
will re-direct processing to `exit()`.
<a id="c.task_setcancelstate"></a> int task\_setcancelstate(int state, int \*oldstate)
: This function atomically sets
both the calling task’s cancellability state to the indicated state and
returns the previous cancellability state at the location referenced by
oldstate. Legal values for state are TASK\_CANCEL\_ENABLE and
TASK\_CANCEL\_DISABLE.
Any pending thread cancellation may occur at the time that the
cancellation state is set to TASK\_CANCEL\_ENABLE.
The cancellability state and type of any newly created tasks are
TASK\_CANCEL\_ENABLE and TASK\_CANCEL\_DEFERRED respectively.
Parameters:
: - **state** – New cancellation state. One of PTHREAD\_CANCEL\_ENABLE or
PTHREAD\_CANCEL\_DISABLE.
- **oldstate** – Location to return the previous cancellation state.
Returns:
: Zero (`OK`) on success; `ERROR` is returned on any failure
with the `errno` value set appropriately:
> - `ESRCH`. No thread could be found corresponding to that specified
> by the given thread ID.
**POSIX Compatibility:** This is a non-standard interface. It extends
the functionality of `pthread_setcancelstate()` to tasks and supports
use of `task_delete()`.
<a id="c.task_setcanceltype"></a> int task\_setcanceltype(int type, FAR int \*oldtype);
: This function atomically both
sets the calling task’s cancellability type to the indicated type and
returns the previous cancellability type at the location referenced by
`oldtype`. Legal values for type are `TASK_CANCEL_DEFERRED` and
`TASK_CANCEL_ASYNCHRONOUS`.
The cancellability state and type of any newly created tasks are
`TASK_CANCEL_ENABLE` and `TASK_CANCEL_DEFERRED` respectively.
Parameters:
: - **type** – New cancellation state. One of `PTHREAD_CANCEL_DEFERRED`
or `PTHREAD_CANCEL_ASYNCHRONOUS`.
- **oldtype** – Location to return the previous cancellation type.
Returns:
: Zero (`OK`) on success; `ERROR` is returned on any failure with the
`errno` value set appropriately:
- `ESRCH`. No thread could be found corresponding to that specified
by the given thread ID.
**POSIX Compatibility:** This is a non-standard interface. It extends
the functionality of `pthread_setcanceltype()` to tasks and supports
use of `task_delete()`.
<a id="c.task_testcancel"></a> void task\_testcancel(void)
: Creates a [Cancellation
Point](https://cwiki.apache.org/confluence/display/NUTTX/Cancellation+Points)
in the calling task. The `task_testcancel()` function has no effect if
cancellability is disabled.
**POSIX Compatibility:** This is a non-standard interface. It extends
the functionality of `pthread_testcancel()` to tasks and supports use
of `task_delete()`.
<a id="c.exit"></a> void exit(int code)
void \_exit(int code)
: Causes the calling task to cease to exist
– its stack and TCB will be deallocated. exit differs from \_exit in
that it flushes streams, closes file descriptors and will execute any
function registered with `atexit()` or `on_exit()`.
Parameters:
: - **code** – (ignored)
**POSIX Compatibility:** This is equivalent to the ANSI interface:
```text
void exit(int code);And the UNIX interface:
```textvoid _exit(int code);
The NuttX exit() differs from ANSI exit() in the following ways:
> - The `code` parameter is ignored.
<a id="c.getpid"></a> [pid\_t](/u/5606252b9680a938#c-pid-t) getpid(void)
<a id="c.fork"></a> [pid\_t](/u/5606252b9680a938#c-pid-t) fork(void)
: `fork()` creates a new process. The child process is an exact copy of
the calling process: it receives **its own copy** of the parent’s memory,
at the same virtual addresses. Writes by the child are invisible to the
parent and writes by the parent are invisible to the child. The child may
modify anything, call any function, return from the function in which
`fork()` was called, and run indefinitely; it runs concurrently with the
parent. None of `vfork()`’s restrictions apply.
> NOTE: `fork()` requires an address environment that can be
> duplicated, and so it is available only where `CONFIG_ARCH_HAVE_FORK`
> is selected – which in turn requires `CONFIG_ARCH_ADDRENV` and an
> architecture that implements `up_addrenv_fork()`. **Where it cannot
> be provided it is not provided at all**: the declaration is
> absent from `unistd.h` and code that calls it fails to build. That
> is deliberate. A build error naming the function is strictly better
> than a `fork()` that silently gives the child the parent’s memory.
> See [Migrating to separate fork/vfork](https://nuttx.apache.org/docs/latest/guides/concurrency/fork_vfork_migration.html) for how to move code that
> relied on the previous behaviour.
>
> There is no copy-on-write, because NuttX has no demand paging to build
> it on, so the copy is eager: forking a large process needs as much free
> memory as the process occupies and fails with `ENOMEM` otherwise.
> Spawn-heavy code should prefer [`posix_spawn()`](#c.posix_spawn) or
> [`vfork()`](#c.vfork), on NuttX as anywhere.
>
> Applications that relied on the historical NuttX `fork()` behaviour –
> shared memory, private stack, both running – want
> [`pthread_create()`](/u/6f234804a81c95b6#c-pthread-create), which is that memory relationship spelled
> clearly, or [`posix_spawn()`](#c.posix_spawn).
Returns:
: Upon successful completion, `fork()` returns 0 to the child
process and returns the process ID of the child process to the parent
process. Otherwise, -1 is returned to the parent, no child process is
created, and `errno` is set to indicate the error.
**POSIX Compatibility:** Compatible with the POSIX interface of the same
name.
<a id="c.vfork"></a> [pid\_t](/u/5606252b9680a938#c-pid-t) vfork(void)
: The `vfork()` function has the same effect as
`fork()`, except that the behavior is undefined if the process created
by `vfork()` either modifies any data other than a variable of type
`pid_t` used to store the return value from `vfork()`, or returns
from the function in which `vfork()` was called, or calls any other
function before successfully calling `_exit()` or one of the `exec`
family of functions.
The child **shares** the parent’s memory – nothing is copied, which is the
entire point of `vfork()` and the reason a caller chooses it – and the
parent is **suspended** until the child calls `_exit()` or one of the
`exec` family. That suspension is what makes the sharing safe, and it is
also the price: the restrictions above exist because the child is running
in the parent’s address space on borrowed time.
> NOTE: `vfork()` is implementable with or without an MMU and is
> available wherever `CONFIG_ARCH_HAVE_VFORK` is selected. The
> suspension lives in the kernel primitive rather than in a libc
> `waitpid()`, so the parent is resumed at `exec()` as POSIX requires,
> and `vfork()` does not depend on `CONFIG_SCHED_WAITPID`.
Returns:
: Upon successful completion, `vfork()` returns 0 to
the child process and returns the process ID of the child process to the
parent process. Otherwise, -1 is returned to the parent, no child
process is created, and `errno` is set to indicate the error.
**POSIX Compatibility:** Compatible with the BSD/Linux interface of the
same name. POSIX marks this interface as Obsolete.
<a id="c.exec"></a> int exec(FAR const char \*filename, FAR char \*const \*argv, FAR const struct symtab\_s \*exports, int nexports)
: This non-standard, NuttX function is similar to
`execv()` and `posix_spawn()` but differs in the following ways;
- Unlike `execv()` and `posix_spawn()` this function accepts symbol
table information as input parameters. This means that the symbol
table used to link the application prior to execution is provided by
the caller, not by the system.
- Unlike `execv()`, this function always returns.
This non-standard interface is included as a official NuttX API only
because it is needed in certain build modes: `exec()` is probably the
only want to load programs in the PROTECTED mode. Other file execution
APIs rely on a symbol table provided by the OS. In the PROTECTED build
mode, the OS cannot provide any meaningful symbolic information for
execution of code in the user-space blob so that is the `exec()`
function is really needed in that build case
The interface is available in the FLAT build mode although it is not
really necessary in that case. It is currently used by some example code
under the `apps/` that that generate their own symbol tables for
linking test programs. So although it is not necessary, it can still be
useful.
The interface would be completely useless and will not be supported in
the KERNEL build mode where the contrary is true: An application process
cannot provide any meaning symbolic information for use in linking a
different process.
**NOTE**: This function is flawed and useless without
`CONFIG_SCHED_ONEXIT` and `CONFIG_SCHED_HAVE_PARENT` because without
those features there is then no mechanism to unload the module once it
exits.
Parameters:
: - **filename** – The program to be executed. With
`CONFIG_LIBC_ENVPATH`, a name that contains no `/` is searched for
in the directories of the `PATH` environment variable. Any other name
is a path and is used as it is: an absolute path, or a path relative to
the current working directory, such as `./hello` or `bin/hello`.
Without `CONFIG_LIBC_ENVPATH`, no `PATH` search is done, and each
name is used as a path.
- **argv** – A pointer to an array of string arguments. The end of the
array is indicated with a NULL entry.
- **exports** – The address of the start of the caller-provided symbol
table. This symbol table contains the addresses of symbols exported
by the caller and made available for linking the module into the
system.
- **nexports** – The number of symbols in the `exports` table.
Returns:
: Zero (OK) is returned on success; On any failure, `exec()`
will return -1 (`ERROR`) and will set the `errno` value
appropriately.
**POSIX Compatibility:** This is a non-standard interface unique to
NuttX. Motivation for inclusion of this non-standard interface in
certain build modes is discussed above.
<a id="c.execv"></a> int execv(FAR const char \*path, FAR char \*const argv[])
: The standard `exec` family of functions will replace
the current process image with a new process image. The new image will
be constructed from a regular, executable file called the new process
image file. There will be no return from a successful `exec`, because
the calling process image is overlaid by the new process image.
Simplified `execl()` and `execv()` functions are provided by NuttX
for compatibility. NuttX is a tiny embedded RTOS that does not support
processes and hence the concept of overlaying a tasks process image with
a new process image does not make any sense. In NuttX, these functions
are wrapper functions that:
> 1. Call the non-standard `binfmt` function `exec()`, and then
> 2. `exit(0)`.
Note the inefficiency when `execv()` or `execl()` is called in the
normal, two-step process: (1) first call `vfork()` to create a new
thread, then (2) call `execv()` or `execl()` to replace the new
thread with a program from the file system. Since the new thread will be
terminated by the `execv()` or `execl()` call, it really served no
purpose other than to support POSIX compatibility. [`posix_spawn()`](#c.posix_spawn)
does the same job in one step and should be preferred.
Note also that `exec()` does not overlay the calling process: it starts
the new program as a separate task. `exec_swap()` then exchanges the two
pids, so that from the parent’s point of view the pid `vfork()` returned
does name the running program – but the `vfork()` stub itself exits, and
it is that exit which releases the suspended `vfork()` parent. The parent
therefore resumes at `exec()`, as POSIX requires.
The non-standard binfmt function `exec()` needs to have (1) a symbol
table that provides the list of symbols exported by the base code, and
(2) the number of symbols in that table. This information is currently
provided to `exec()` from `execv()` or `execl()` via NuttX
configuration settings:
> - `CONFIG_LIBC_EXECFUNCS`: Enable `execv()` and `execl()` support
> - `CONFIG_EXECFUNCS_SYMTAB_ARRAY`: Name of the symbol table used by
> `execv()` or `execl()`.
> - `CONFIG_EXECFUNCS_NSYMBOLS_VAR`: Name of the `int` variable
> holding the number of symbols in the symbol table
As a result of the above, the current implementations of `execl()` and
`execv()` suffer from some incompatibilities that may or may not be
addressed in a future version of NuttX. Other than just being an
inefficient use of MCU resource, the most serious of these is that the
`exec`’ed task will not have the same task ID as the `vfork`’ed
function. So the parent function cannot know the ID of the `exec`’ed
task.
Parameters:
: - **path** – The program to be executed. With `CONFIG_LIBC_ENVPATH`, a
name that contains no `/` is searched for in the directories of the
`PATH` environment variable. Any other name is a path and is used as
it is: an absolute path, or a path relative to the current working
directory. Without `CONFIG_LIBC_ENVPATH`, no `PATH` search is done,
and each name is used as a path.
Returns:
: This function does not return on success. On
failure, it will return -1 (`ERROR`) and will set the `errno` value
appropriately.
**POSIX Compatibility:** Similar with the POSIX interface of the same
name. There are, however, several compatibility issues as detailed in
the description above.
<a id="c.execl"></a> int execl(FAR const char \*path, ...)
: `execl()` is functionally equivalent to
[execv()](#execv), differing only in the form of its input
parameters. See the description of [execv()](#execv) for additional
information.
Parameters:
: - **path** – The program to be executed. With `CONFIG_LIBC_ENVPATH`, a
name that contains no `/` is searched for in the directories of the
`PATH` environment variable. Any other name is a path and is used as
it is: an absolute path, or a path relative to the current working
directory. Without `CONFIG_LIBC_ENVPATH`, no `PATH` search is done,
and each name is used as a path.
Returns:
: This function does not return on success. On
failure, it will return -1 (`ERROR`) and will set the `errno` value
appropriately.
**POSIX Compatibility:** Similar with the POSIX interface of the same
name. There are, however, several compatibility issues as detailed in
the description of [execv()](#execv).
<a id="c.posix_spawn"></a> int posix\_spawn(FAR [pid\_t](/u/5606252b9680a938#c-pid-t) \*pid, FAR const char \*path, FAR const posix\_spawn\_file\_actions\_t \*file\_actions, FAR const posix\_spawnattr\_t \*attr, FAR char \*const argv[], FAR char \*const envp[])
<a id="c.posix_spawnp"></a> int posix\_spawnp(FAR [pid\_t](/u/5606252b9680a938#c-pid-t) \*pid, FAR const char \*file, FAR const posix\_spawn\_file\_actions\_t \*file\_actions, FAR const posix\_spawnattr\_t \*attr, FAR char \*const argv[], FAR char \*const envp[]);
: The `posix_spawn()` and `posix_spawnp()` functions
will create a new, child task, constructed from a regular executable
file.
Parameters:
: - **pid** – Upon successful completion, `posix_spawn()` and
`posix_spawnp()` will return the task ID of the child task to the
parent task, in the variable pointed to by a non-NULL `pid`
argument. If the `pid` argument is a null pointer, the process ID
of the child is not returned to the caller.
- **path** –
The `path` argument to `posix_spawn()` and the `file`
argument to `posix_spawnp()` identify the file to execute. With
`CONFIG_LIBC_ENVPATH`, a name that contains no `/` is searched for
in the directories of the `PATH` environment variable. Any other name
is a path and is used as it is: an absolute path, or a path relative to
the current working directory, such as `./hello` or `bin/hello`.
NOTE: NuttX provides only one implementation: If
`CONFIG_LIBC_ENVPATH` is defined, then only `posix_spawnp()`
behavior is supported; otherwise, only `posix_spawn` behavior is
supported.
- **file\_actions** – If `file_actions` is a null pointer, then file
descriptors open in the calling process will remain open in the child
process (unless `CONFIG_FDCLONE_STDIO` is defined). If
`file_actions` is not NULL, then the file descriptors open in the
child process will be those open in the calling process as modified
by the spawn file actions object pointed to by `file_actions`.
- **attr** –
If the value of the `attr` parameter is `NULL`, the all
default values for the POSIX spawn attributes will be used.
Otherwise, the attributes will be set according to the spawn flags.
The `posix_spawnattr_t` spawn attributes object type is defined in
`spawn.h`. It will contains these attributes, not all of which are
supported by NuttX:
- `POSIX_SPAWN_SETPGROUP`: The new task joins the process group of
`posix_spawnattr_setpgroup()`, or a new process group if it is
`0` (with `CONFIG_SCHED_PROCESS_GROUPS`, else ignored).
- `POSIX_SPAWN_SETSID`: The new task is the leader of a new
session (with `CONFIG_SCHED_PROCESS_GROUPS`, else ignored).
- `POSIX_SPAWN_SETSCHEDPARAM`: Set new tasks priority to the
`sched_param` value.
- `POSIX_SPAWN_SETSCHEDULER`: Set the new task’s scheduler policy
to the `sched_policy` value.
- `POSIX_SPAWN_RESETIDS` Resetting of the effective user ID of the
child process is not supported. NuttX does not support effective
user IDs.
- `POSIX_SPAWN_SETSIGMASK`: Set the new task’s signal mask.
- `POSIX_SPAWN_SETSIGDEF`: Resetting signal default actions is not
supported. NuttX does not support default signal actions.
- **argv** –
`argv[]` is the argument list for the new task.
`argv[]` is an array of pointers to null-terminated strings. The
list is terminated with a null pointer.
A new task can get at most `CONFIG_TASK_MAX_ARGS` arguments (default
256). A longer list fails with `E2BIG`. `CONFIG_TASK_ARG_MAX` sets
`ARG_MAX` and `sysconf(_SC_ARG_MAX)` (default 4096 bytes): the size
of the arguments and the environment that a program may pass, counted
as the bytes of the strings with their terminators and of the pointers
to them. A larger list fails with `E2BIG`. The arguments are copied
to the stack of the new task; if they do not fit there, the call also
fails with `E2BIG`.
- **envp** – `envp[]` is an array of pointers to null-terminated
`name=value` strings that is the environment of the new task. The
array is terminated by a null pointer. If `envp` is `NULL`, the new
task gets a copy of the environment of the caller (`environ`).
Returns:
: Zero on success. Otherwise, an error number will be returned as the
function return value to indicate the error:
- `EINVAL`: The value specified by `file_actions` or `attr` is
invalid.
- Any errors that might have been return if `vfork()` and
`exec[l|v]()` had been called.
**Assumptions/Limitations:**
- NuttX provides only `posix_spawn()` or `posix_spawnp()` behavior
depending upon the setting of `CONFIG_LIBC_ENVPATH`: If
`CONFIG_LIBC_ENVPATH` is defined, then only `posix_spawnp()`
behavior is supported; otherwise, only `posix_spawn()` behavior is
supported.
- Process groups need `CONFIG_SCHED_PROCESS_GROUPS` (See
`POSIX_SPAWN_SETPGROUP` above).
- Effective user IDs are not supported (See `POSIX_SPAWN_RESETIDS`
above).
- Signal default actions cannot be modified in the newly task executed
because NuttX does not support default signal actions (See
`POSIX_SPAWN_SETSIGDEF`).
**POSIX Compatibility:** The value of the `argv[0]` received by the
child task is assigned by NuttX. For the caller of `posix_spawn()`,
the provided argv[0] will correspond to `argv[1]` received by the new
task.
<a id="c.posix_spawn_file_actions_init"></a> int posix\_spawn\_file\_actions\_init(FAR posix\_spawn\_file\_actions\_t \*file\_actions)
: Initializes the object referenced by `file_actions` to an empty set of
file actions for subsequent use in a call to `posix_spawn()` or
`posix_spawnp()`.
**Input Parameters:**
- `file_actions`: The address of the `posix_spawn_file_actions_t`
to be initialized.
**Returned Value:** On success, this function returns 0; on failure it
will return an error number from `<errno.h>`.
<a id="c.posix_spawn_file_actions_destroy"></a> int posix\_spawn\_file\_actions\_destroy(FAR posix\_spawn\_file\_actions\_t \*file\_actions)
: Destroys the object referenced by `file_actions` which was previously
initialized by `posix_spawn_file_actions_init()`, returning any
resources obtained at the time of initialization to the system for
subsequent reuse. A `posix_spawn_file_actions_t` may be reinitialized
after having been destroyed, but must not be reused after destruction,
unless it has been reinitialized.
Parameters:
: - **file\_actions** – The address of the `posix_spawn_file_actions_t`
to be destroyed.
Returns:
: On success, this function returns 0; on failure it
will return an error number from `<errno.h>`
<a id="c.posix_spawn_file_actions_addclose"></a> int posix\_spawn\_file\_actions\_addclose(FAR posix\_spawn\_file\_actions\_t \*file\_actions, int fd)
: Adds a *close* operation to the list of operations associated with the
object referenced by `file_actions`, for subsequent use in a call to
`posix_spawn()` or `posix_spawnp()`. The descriptor referred to by
`fd` is closed as if `close()` had been called on it prior to the
new child process starting execution.
Parameters:
: - **file\_actions** – The address of the `posix_spawn_file_actions_t`
object to which the *close* operation will be appended.
- **fd** – The file descriptor to be closed.
Returns:
: On success, this function returns 0; on failure it
will return an error number from `<errno.h>`
<a id="c.posix_spawn_file_actions_adddup2"></a> int posix\_spawn\_file\_actions\_adddup2(FAR posix\_spawn\_file\_actions\_t \*file\_actions, int fd1, int fd2)
: Adds a *dup2* operation to the list of operations associated with the
object referenced by `file_actions`, for subsequent use in a call to
`posix_spawn()` or `posix_spawnp()`. The descriptor referred to by
`fd2` is created as if `dup2()` had been called on `fd1` prior to
the new child process starting execution.
Parameters:
: - **file\_actions** – The address of the `posix_spawn_file_actions_t`
object to which the *dup2* operation will be appended.
- **fd1** – The file descriptor to be be duplicated. The first file
descriptor to be argument to `dup2()`.
- **fd2** – The file descriptor to be be created. The second file
descriptor to be argument to `dup2()`.
Returns:
: On success, this function returns 0; on failure it
will return an error number from `<errno.h>`
<a id="c.posix_spawn_file_actions_addopen"></a> int posix\_spawn\_file\_actions\_addopen(FAR posix\_spawn\_file\_actions\_t \*file\_actions, int fd, FAR const char \*path, int oflags, mode\_t mode);
: Adds an *open* operation to the list of operations associated with the
object referenced by `file_actions`, for subsequent use in a call to
`posix_spawn()` or `posix_spawnp()`. The descriptor referred to by
`fd` is opened using the `path`, `oflag`, and `mode` arguments
as if `open()` had been called on it prior to the new child process
starting execution. The string path is copied by the
`posix_spawn_file_actions_addopen()` function during this process, so
storage need not be persistent in the caller.
Parameters:
: - **file\_actions** – The address of the `posix_spawn_file_actions_t`
object to which the *open* operation will be appended.
- **fd** – The file descriptor to be opened.
- **path** – The path to be opened.
- **oflags** – Open flags.
- **mode** – File creation mode/
Returns:
: On success, this function returns 0; on failure it
will return an error number from `<errno.h>`
<a id="c.posix_spawnattr_init"></a> int posix\_spawnattr\_init(FAR posix\_spawnattr\_t \*attr)
: The `posix_spawnattr_init()` function initializes the
object referenced by `attr`, to an empty set of spawn attributes for
subsequent use in a call to `posix_spawn()` or `posix_spawnp()`.
Then the spawn attributes are no longer needed, they should be destroyed
by calling `posix_spawnattr_destroyed()`. In NuttX, however,
`posix_spawnattr_destroyed()` is just stub:
For portability, the convention of calling
`posix_spawnattr_destroyed()` when the attributes are not longer
needed should still be followed.
Parameters:
: - **attr** – The address of the spawn attributes to be initialized.
Returns:
: On success, this function returns 0; on failure it
will return an error number from `<errno.h>`
<a id="c.posix_spawnattr_getflags"></a> int posix\_spawnattr\_getflags(FAR const posix\_spawnattr\_t \*attr, FAR short \*flags)
: The `posix_spawnattr_getflags()` function will obtain
the value of the *spawn-flags* attribute from the attributes object
referenced by `attr`.
Parameters:
: - **attr** – The address spawn attributes to be queried.
- **flags** – The location to return the spawn flags
Returns:
: On success, this function returns 0; on failure it
will return an error number from `<errno.h>`
<a id="c.posix_spawnattr_getschedparam"></a> int posix\_spawnattr\_getschedparam(FAR const posix\_spawnattr\_t \*attr, FAR struct [sched\_param](/u/5606252b9680a938#c-sched-param) \*param)
: The `posix_spawnattr_getschedparam()` function will
obtain the value of the *spawn-schedparam* attribute from the attributes
object referenced by `attr`.
Parameters:
: - **attr** – The address spawn attributes to be queried.
- **param** – The location to return the *spawn-schedparam* value.
Returns:
: On success, this function returns 0; on failure it
will return an error number from `<errno.h>`
<a id="c.posix_spawnattr_getschedpolicy"></a> int posix\_spawnattr\_getschedpolicy(FAR const posix\_spawnattr\_t \*attr, FAR int \*policy)
: The `posix_spawnattr_getschedpolicy()` function will
obtain the value of the *spawn-schedpolicy* attribute from the
attributes object referenced by `attr`.
Parameters:
: - **attr** – The address spawn attributes to be queried.
- **policy** – The location to return the *spawn-schedpolicy* value.
Returns:
: On success, this function returns 0; on failure it
will return an error number from `<errno.h>`
<a id="c.posix_spawnattr_getpriority"></a> uint8\_t posix\_spawnattr\_getpriority(FAR posix\_spawnattr\_t \*attr)
: The `posix_spawnattr_getpriority()` function will obtain
the value of the *spawn-priority* attribute from the attributes object
referenced by `attr`.
This is a non-standard helper API.
Parameters:
: - **attr** – The address spawn attributes to be queried.
Returns:
: The priority value stored in the attributes object.
<a id="c.posix_spawnattr_getsigmask"></a> int posix\_spawnattr\_getsigmask(FAR const posix\_spawnattr\_t \*attr, FAR [sigset\_t](/u/5606252b9680a938#c-sigset-t) \*sigmask)
: `posix_spawnattr_getsigdefault()` function will
obtain the value of the *spawn-sigmask* attribute from the attributes
object referenced by `attr`.
Parameters:
: - **attr** – The address spawn attributes to be queried.
- **sigmask** – The location to return the *spawn-sigmask* value.
Returns:
: On success, this function returns 0; on failure it
will return an error number from `<errno.h>`
<a id="c.posix_spawnattr_setflags"></a> int posix\_spawnattr\_setflags(FAR posix\_spawnattr\_t \*attr, short flags)
: The `posix_spawnattr_setflags()` function will set
the *spawn-flags* attribute in an initialized attributes object
referenced by `attr`.
Parameters:
: - **attr** – The address spawn attributes to be used.
- **flags** – The new value of the *spawn-flags* attribute.
Returns:
: On success, this function returns 0; on failure it
will return an error number from `<errno.h>`
<a id="c.posix_spawnattr_setschedparam"></a> int posix\_spawnattr\_setschedparam(FAR posix\_spawnattr\_t \*attr, FAR const struct [sched\_param](/u/5606252b9680a938#c-sched-param) \*param)
: The `posix_spawnattr_setschedparam()` function will
set the *spawn-schedparam* attribute in an initialized attributes object
referenced by `attr`.
Parameters:
: - **attr** – The address spawn attributes to be used.
- **param** – The new value of the *spawn-schedparam* attribute.
Returns:
: On success, this function returns 0; on failure it
will return an error number from `<errno.h>`
<a id="c.posix_spawnattr_setschedpolicy"></a> int posix\_spawnattr\_setschedpolicy(FAR posix\_spawnattr\_t \*attr, int policy)
: The `posix_spawnattr_setschedpolicy()` function will
set the *spawn-schedpolicy* attribute in an initialized attributes
object referenced by `attr`.
Parameters:
: - **attr** – The address spawn attributes to be used.
- **policy** – The new value of the *spawn-schedpolicy* attribute.
Returns:
: On success, this function returns 0; on failure it
will return an error number from `<errno.h>`
<a id="c.posix_spawnattr_setpriority"></a> int posix\_spawnattr\_setpriority(FAR posix\_spawnattr\_t \*attr, uint8\_t priority)
: The `posix_spawnattr_setpriority()` function will set
the *spawn-priority* attribute in an initialized attributes object
referenced by `attr`.
This is a non-standard helper API.
Parameters:
: - **attr** – The address spawn attributes to be used.
- **priority** – The new priority value to set.
Returns:
: On success, this function returns 0; on failure it
will return an error number from `<errno.h>`
<a id="c.posix_spawnattr_setsigmask"></a> int posix\_spawnattr\_setsigmask(FAR posix\_spawnattr\_t \*attr, FAR const [sigset\_t](/u/5606252b9680a938#c-sigset-t) \*sigmask)
: The `posix_spawnattr_setsigmask()` function will set
the *spawn-sigmask* attribute in an initialized attributes object
referenced by `attr`.
Parameters:
: - **attr** – The address spawn attributes to be used.
- **sigmask** – The new value of the *spawn-sigmask* attribute.
Returns:
: On success, this function returns 0; on failure it
will return an error number from `<errno.h>`
<a id="c.task_spawn"></a> int task\_spawn(FAR const char \*name, [main\_t](/u/5606252b9680a938#c-main-t) entry, FAR const posix\_spawn\_file\_actions\_t \*file\_actions, FAR const posix\_spawnattr\_t \*attr, FAR char \*const argv[], FAR char \*const envp[])
: The `task_spawn()` function will create a new, child
task, where the entry point to the task is an address in memory.
Parameters:
: - **name** – The name to assign to the child task.
- **entry** – The child task’s entry point (an address in memory).
- **file\_actions** – If `file_actions` is a null pointer, then file
descriptors open in the calling process will remain open in the child
process (unless `CONFIG_FDCLONE_STDIO` is defined). If
`file_actions` is not NULL, then the file descriptors open in the
child process will be those open in the calling process as modified
by the spawn file actions object pointed to by `file_actions`.
- **attr** –
If the value of the `attr` parameter is `NULL`, the all
default values for the POSIX spawn attributes will be used.
Otherwise, the attributes will be set according to the spawn flags.
The `posix_spawnattr_t` spawn attributes object type is defined in
`spawn.h`. It will contains these attributes, not all of which are
supported by NuttX:
- `POSIX_SPAWN_SETPGROUP`: The new task joins the process group of
`posix_spawnattr_setpgroup()`, or a new process group if it is
`0` (with `CONFIG_SCHED_PROCESS_GROUPS`, else ignored).
- `POSIX_SPAWN_SETSID`: The new task is the leader of a new
session (with `CONFIG_SCHED_PROCESS_GROUPS`, else ignored).
- `POSIX_SPAWN_SETSCHEDPARAM`: Set new tasks priority to the
`sched_param` value.
- `POSIX_SPAWN_SETSCHEDULER`: Set the new task’s scheduler policy
to the `sched_policy` value.
- `POSIX_SPAWN_RESETIDS` Resetting of the effective user ID of the
child process is not supported. NuttX does not support effective
user IDs.
- `POSIX_SPAWN_SETSIGMASK`: Set the new task’s signal mask.
- `POSIX_SPAWN_SETSIGDEF`: Resetting signal default actions is not
supported. NuttX does not support default signal actions.
And the non-standard:
- `TASK_SPAWN_SETSTACKSIZE`: Set the stack size for the new task.
- **argv** – `argv[]` is the argument list for the new task.
`argv[]` is an array of pointers to null-terminated strings. The
list is terminated with a null pointer. The limits of
[`posix_spawn()`](#c.posix_spawn) apply.
- **envp** – The environment of the new task, as for
[`posix_spawn()`](#c.posix_spawn). If `envp` is `NULL`, the new task gets a
copy of the environment of the caller.
Returns:
: `task_spawn()` will return process ID of new task on success.
Otherwise, a negative number will be returned as the function return
value to indicate the error:
**POSIX Compatibility:** This is a non-standard interface inspired by
`posix_spawn()`.
<a id="c.posix_spawnattr_getstacksize"></a> int posix\_spawnattr\_getstacksize(FAR const posix\_spawnattr\_t \*attr, FAR [size\_t](/u/5606252b9680a938#c-size-t) \*stacksize)
: The `posix_spawnattr_getstacksize()` function will
obtain the value of the *spawn-stacksize* attribute from the attributes
object referenced by `attr`.
Parameters:
: - **attr** – The address spawn attributes to be queried.
- **policy** – The location to return the *spawn-stacksize* value.
Returns:
: On success, this function returns 0; on failure it
will return an error number from `<errno.h>`
<a id="c.posix_spawnattr_setstacksize"></a> int posix\_spawnattr\_setstacksize(FAR posix\_spawnattr\_t \*attr, [size\_t](/u/5606252b9680a938#c-size-t) stacksize)
: The `posix_spawnattr_setstacksize()` function will set
the *spawn-stacksize* attribute in an initialized attributes object
referenced by `attr`.
Parameters:
: - **attr** – The address spawn attributes to be used.
- **policy** – The new value of the *spawn-stacksize* attribute.
Returns:
: On success, this function returns 0; on failure it
will return an error number from `<errno.h>`
<a id="c.getrlimit"></a> int getrlimit(int resource, FAR struct rlimit \*rlp)
: The `getrlimit()` function returns the soft and the hard limit of a
resource of the calling task.
Parameters:
: - **resource** – The resource, `RLIMIT_*`.
- **rlp** – The location that receives the limits.
Returns:
: 0 on success. On failure, -1 is returned and `errno` is set
to `EINVAL`: `rlp` is `NULL` or `resource` is not valid.
**POSIX Compatibility:** Comparable to the POSIX interface of the same
name. Only `RLIMIT_NOFILE` and `RLIMIT_STACK` are limits of the
task.
<a id="c.setrlimit"></a> int setrlimit(int resource, FAR const struct rlimit \*rlp)
: The `setrlimit()` function sets the soft and the hard limit of a
resource of the calling task. The tasks that it creates later get the
same limits.
Parameters:
: - **resource** – `RLIMIT_NOFILE` or `RLIMIT_STACK`.
- **rlp** – The new limits.
Returns:
: 0 on success. On failure, -1 is returned and `errno` is set:
- `EINVAL`: `rlp` is `NULL`, the soft limit is above the hard
limit, or NuttX does not enforce the resource.
- `EPERM`: an unprivileged task raises a hard limit, or the limit
of open files is above `OPEN_MAX`.
**POSIX Compatibility:** Comparable to the POSIX interface of the same
name. As on Linux, `RLIMIT_STACK` is also the default stack size of
new threads.