---
name: "kproc_kthread_add"
section: "9"
platform: "freebsd"
package: "base"
version: "16.0-CURRENT-20261027-61c70dc16a79"
release: "FreeBSD 16.0-CURRENT (2026-10-27)"
platforms: ["freebsd"]
url: "https://new.carta.tech/kproc"
---

# kproc\_kthread\_add(9)

kernel processes

Part of [kproc(9)](/kproc/9.md).

## NAME

**kproc\_start**, **kproc\_shutdown**, **kproc\_create**, **kproc\_exit**, **kproc\_resume**, **kproc\_suspend**, **kproc\_suspend\_check** – kernel processes

## SYNOPSIS

**#include \<sys/kthread.h\>**

*void*\

**kproc\_start**(*const void \*udata*);

*void*\

**kproc\_shutdown**(*void \*arg*, *int howto*);

*int*\

**kproc\_create**(*void (\*func)(void \*)*, *void \*arg*, *struct proc \*\*newpp*, *int flags*, *int pages*, *const char \*fmt*, *...*);

*void*\

**kproc\_exit**(*int ecode*);

*int*\

**kproc\_resume**(*struct proc \*p*);

*int*\

**kproc\_suspend**(*struct proc \*p*, *int timo*);

*void*\

**kproc\_suspend\_check**(*struct proc \*p*);

*int*\

**kproc\_kthread\_add**(*void (\*func)(void \*)*, *void \*arg*, *struct proc \*\*procptr*, *struct thread \*\*tdptr*, *int flags*, *int pages*, *char \* procname*, *const char \*fmt*, *...*);

## DESCRIPTION

In FreeBSD 8.0, the **kthread\***(*9*) family of functions was renamed to be the **kproc\***(*9*) family of functions, as they were misnamed and actually produced kernel processes. A new family of *different* **kthread\_\***(*9*) functions was added to produce *real* kernel *threads*. See the [kthread(9)](/kthread/9.md) man page for more information on those calls. Also note that the **kproc\_kthread\_add**(*9*) function appears in both pages as its functionality is split.

The function **kproc\_start**() is used to start “internal” daemons such as **bufdaemon**, **pagedaemon**, **vmdaemon**, and the **syncer** and is intended to be called from [SYSINIT(9)](/SYSINIT/9.md). The *udata* argument is actually a pointer to a *struct kproc\_desc* which describes the kernel process that should be created:

```
struct kproc_desc {
	char		*arg0;
	void		(*func)(void);
	struct proc	**global_procpp;
};
```

The structure members are used by **kproc\_start**() as follows:

- *arg0*

  String to be used for the name of the process. This string will be copied into the *p\_comm* member of the new process' *struct proc*.

- *func*

  The main function for this kernel process to run.

- *global\_procpp*

  A pointer to a *struct proc* pointer that should be updated to point to the newly created process' process structure. If this variable is NULL, then it is ignored.

The **kproc\_create**() function is used to create a kernel process. The new process shares its address space with process 0, the **swapper** process, and runs in kernel mode only. The *func* argument specifies the function that the process should execute. The *arg* argument is an arbitrary pointer that is passed in as the only argument to *func* when it is called by the new process. The *newpp* pointer points to a *struct proc* pointer that is to be updated to point to the newly created process. If this argument is NULL, then it is ignored. The *flags* argument specifies a set of flags as described in [rfork(2)](/rfork/2.md). The *pages* argument specifies the size of the new kernel process's stack in pages. If 0 is used, the default kernel stack size is allocated. The rest of the arguments form a [printf(9)](/printf/9.md) argument list that is used to build the name of the new process and is stored in the *p\_comm* member of the new process's *struct proc*.

The **kproc\_exit**() function is used to terminate kernel processes. It should be called by the main function of the kernel process rather than letting the main function return to its caller. The *ecode* argument specifies the exit status of the process. While exiting, the function exit1(9) will initiate a call to [wakeup(9)](/wakeup/9.md) on the process handle.

The **kproc\_resume**(), **kproc\_suspend**(), and **kproc\_suspend\_check**() functions are used to suspend and resume a kernel process. During the main loop of its execution, a kernel process that wishes to allow itself to be suspended should call **kproc\_suspend\_check**() passing in *curproc* as the only argument. This function checks to see if the kernel process has been asked to suspend. If it has, it will [tsleep(9)](/tsleep/9.md) until it is told to resume. Once it has been told to resume it will return allowing execution of the kernel process to continue. The other two functions are used to notify a kernel process of a suspend or resume request. The *p* argument points to the *struct proc* of the kernel process to suspend or resume. For **kproc\_suspend**(), the *timo* argument specifies a timeout to wait for the kernel process to acknowledge the suspend request and suspend itself.

The **kproc\_shutdown**() function is meant to be registered as a shutdown event for kernel processes that need to be suspended voluntarily during system shutdown so as not to interfere with system shutdown activities. The actual suspension of the kernel process is done with **kproc\_suspend**().

The **kproc\_kthread\_add**() function is much like the **kproc\_create**() function above except that if the kproc already exists, then only a new thread (see [kthread(9)](/kthread/9.md)) is created on the existing process. The *func* argument specifies the function that the process should execute. The *arg* argument is an arbitrary pointer that is passed in as the only argument to *func* when it is called by the new process. The *procptr* pointer points to a *struct proc* pointer that is the location to be updated with the new proc pointer if a new process is created, or if not NULL, must contain the process pointer for the already existing process. If this argument points to NULL, then a new process is created and the field updated. If not NULL, the *tdptr* pointer points to a *struct thread* pointer that is the location to be updated with the new thread pointer. The *flags* argument specifies a set of flags as described in [rfork(2)](/rfork/2.md). The *pages* argument specifies the size of the new kernel thread's stack in pages. If 0 is used, the default kernel stack size is allocated. The procname argument is the name the new process should be given if it needs to be created. It is *NOT* a printf style format specifier but a simple string. The rest of the arguments form a [printf(9)](/printf/9.md) argument list that is used to build the name of the new thread and is stored in the *td\_name* member of the new thread's *struct thread*.

## RETURN VALUES

The **kproc\_create**(), **kproc\_resume**(), and **kproc\_suspend**() functions return zero on success and non-zero on failure.

## EXAMPLES

This example demonstrates the use of a *struct kproc\_desc* and the functions **kproc\_start**(), **kproc\_shutdown**(), and **kproc\_suspend\_check**() to run the **bufdaemon** process.

```
static struct proc *bufdaemonproc;
static struct kproc_desc buf_kp = {
	"bufdaemon",
	buf_daemon,
	&bufdaemonproc
};
SYSINIT(bufdaemon, SI_SUB_KTHREAD_BUF, SI_ORDER_FIRST, kproc_start,
    &buf_kp)
static void
buf_daemon()
{
	...
	/*
	 * This process needs to be suspended prior to shutdown sync.
	 */
	EVENTHANDLER_REGISTER(shutdown_pre_sync, kproc_shutdown,
	    bufdaemonproc, SHUTDOWN_PRI_LAST);
	...
	for (;;) {
		kproc_suspend_check(bufdaemonproc);
		...
	}
}
```

## ERRORS

The **kproc\_resume**() and **kproc\_suspend**() functions will fail if:

- \[EINVAL\]

  The *p* argument does not reference a kernel process.

The **kproc\_create**() function will fail if:

- \[EAGAIN\]

  The system-imposed limit on the total number of processes under execution would be exceeded. The limit is given by the [sysctl(3)](/sysctl/3.md) MIB variable KERN\_MAXPROC.

- \[EINVAL\]

  The RFCFDG flag was specified in the *flags* parameter.

## SEE ALSO

[rfork(2)](/rfork/2.md), exit1(9), [kthread(9)](/kthread/9.md), [SYSINIT(9)](/SYSINIT/9.md), [wakeup(9)](/wakeup/9.md)

## HISTORY

The **kproc\_start**() function first appeared in FreeBSD 2.2. The **kproc\_shutdown**(), **kproc\_create**(), **kproc\_exit**(), **kproc\_resume**(), **kproc\_suspend**(), and **kproc\_suspend\_check**() functions were introduced in FreeBSD 4.0. Prior to FreeBSD 5.0, the **kproc\_shutdown**(), **kproc\_resume**(), **kproc\_suspend**(), and **kproc\_suspend\_check**() functions were named **shutdown\_kproc**(), **resume\_kproc**(), **shutdown\_kproc**(), and **kproc\_suspend\_loop**(), respectively. Originally they had the names **kthread\_\***() but were changed to **kproc\_\***() when real kthreads became available.
