kill() — Linux system interface
Request signal delivery, not necessarily process termination. A positive PID selects one process; zero and negative values have group or broader semantics and are inappropriate for this lab. Signal 0 checks existence and permission without delivering a signal, but cannot prove identity or liveness later. ESRCH, EPERM and EINVAL can indicate failure. This single-thread example signals only itself; real process supervision must address PID reuse. Compile the complete C example in a fresh private working directory using gcc -std=c17 -Wall -Wextra -Wpedantic -Werror example.c -o example; run ./example. POSIX interface; tested on Linux/glibc only. These are libc-facing signatures, not raw architecture-specific syscall ABIs.
Formal Syntax & Structure
#include <signal.h>int kill(pid_t pid, int sig);Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| pid | pid_t | Target selection; use only own positive PID in this exercise. |
| sig | int | Signal number or 0 for an existence/permission check. |
Return Value
0 on success. On failure, returns -1 and sets errno.
Working Example
#define _GNU_SOURCE#define _POSIX_C_SOURCE 200809L#include <errno.h>#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <signal.h>
/* A failed check ends this short-lived demonstration. */#define CHECK(x) do { if (!(x)) { \ fprintf(stderr, "check failed at line %d: %s\n", __LINE__, #x); \ return EXIT_FAILURE; \} } while (0)
int main(void){ sigset_t set, old; CHECK(sigemptyset(&set) == 0); CHECK(sigaddset(&set, SIGUSR1) == 0); CHECK(sigprocmask(SIG_BLOCK, &set, &old) == 0); CHECK(kill(getpid(), 0) == 0); CHECK(kill(getpid(), SIGUSR1) == 0); struct timespec t = {1, 0}; CHECK(sigtimedwait(&set, NULL, &t) == SIGUSR1); CHECK(sigprocmask(SIG_SETMASK, &old, NULL) == 0); puts("own SIGUSR1 received synchronously"); return EXIT_SUCCESS;}