#include<unistd.h> // create a pipe // pipefd: // {file descriptor to read into pipe, file descriptor to write into pipe} // return value: // return 0 for success, or -1 for error intpipe(int pipefd[2]);
#include<sys/types.h> #include<sys/stat.h> // create a FIFO special file // pathname: // FIFO file path to create // mode: // create mode, same to mode in "open" // return value: // return 0 for success, or -1 for errors including file exist and etc. intmkfifo(constchar *pathname, mode_t mode); // $ man 3 mkfifo
#include<fcntl.h> // open a FIFO file in read or write mode // pathname: // FIFO file to read or write // flags: // O_RDONLY for FIFO read mode, // O_WRONLY for FIFO write mode, // O_NONBLOCK (optional) for non-block IO // return value: // return file descriptor of FIFO, or -1 for error intopen(constchar *pathname, int flags);
#include<signal.h> // remove all the signums from the set intsigemptyset(sigset_t *set); // set = 0; return 0; // add all the signums into the set intsigfillset(sigset_t *set); // set = -1ull; return 0; // add the signum into set intsigaddset(sigset_t *set, int signum); // set = set | 1 << signum - 1; return 0; // remove the signum from the set intsigdelset(sigset_t *set, int signum); // set = set & ~(1 << signum - 1); return 0; // examine if the signum is in the set intsigismember(constsigset_t *set, int signum); // return set >> signum - 1 & 1;
操作阻塞信号集
sigprocmask,用户无法直接操作阻塞信号集,必须使用内核提供的函数完成操作。
注意:SIGKILL与SIGSTOP信号在操作中会被过滤。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include<signal.h> // examine and change the block signal set // how: // SIG_BLOCK: // add the signals with 1 flag in the given set into the block signal set // SIG_UNBLOCK: // remove the signals with 1 flag in the given set from the block signal set // SIG_SETMASK: // set the block sognal set to the given set // set: // Operating signal set whose actual meaning is depending on "how". // oldset: // to receive the former block signal set, and can be NULL // return value: // return 0 for success, -1 for error intsigprocmask(int how, constsigset_t *set, sigset_t *oldset);
// About more: // $ man 2 sigprocmask
只获取oldset如下:
1 2 3
constunsignedlonglong zero = 0; unsignedlonglong oldset; sigprocmask(SIG_BLOCK, &zero, &oldset);
发送信号
kill用于向任意进程或任意进程组发射信号。
pid:分为下述情况:
< -1:将信号发送到所有组ID为-pid(pid的绝对值)的进程。
-1:将信号发送到所有允许发送到的进程。
0:将信号发送到所有组ID与调用进程组ID相同的进程。
> 0:将信号发送到PID为pid的进程。
返回值:成功返回0,失败返回-1。
1 2 3 4 5
#include<sys/types.h> #include<signal.h> // send a signal to a process intkill(pid_t pid, int sig); // $ man 2 kill
#include<signal.h> typedefvoid(*sighandler_t)(int); // ANSI C signal handling // signum: // signal number // handler: // one of the following: // SIG_DFL for using the default action // SIG_IGN for ignore the signal // callback function pointer // return value: // Return the last handler, or SIG_ERR for error. The inital handler is SIG_DFL sighandler_tsignal(int signum, sighandler_t handler);
// About more // $ man 2 signal
示例:
1 2 3 4 5 6 7 8 9 10 11 12 13
#include<signal.h> #include<stdio.h>
voidfunc(int sig){ printf("Get a signal %d\n", sig); }
#include<signal.h> structsigaction { void (*sa_handler)(int); // handler 1, same to "signal" void (*sa_sigaction)(int, siginfo_t *, void *); // handler 2, more options, see "About more" sigset_t sa_mask; // temporary block signal mask which will rollback after calling int sa_flags; // usual 0 (handler 1), SA_SIGINFO for choosing handler 2, more flags see "About more" void (*sa_restorer)(void); // deprecated }; // change signal action // signum: // signal number // act: // the structure describe callback function, see "struct sigaction" // oldact: // to receive the last action // return value: // return 0 for success, -1 for error intsigaction(int signum, conststruct sigaction *act, struct sigaction *oldact);
// About more // $ man 2 sigaction
示例:(与上个示例等价)
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include<signal.h> #include<stdio.h>
voidfunc(int sig){ printf("Get a signal %d\n", sig); }
#include<sys/mman.h> // map a file or device into memory // addr: // Address base of the memory, it's a hint of address to kernel but not exactly the address. // Usually NULL is given. // length: // address bound of the mapping memory in bytes // usually lseek(fd, 0, SEEK_END) is given // prot: // optional: // PROT_READ: // read permisson to the mapping memory // PROT_WRITE: // write permisson to the mapping memory, PROT_WRITE should given with PROT_READ // PROT_EXEC: // execute permisson to the mapping memory // flags: // exactly one bellow: // MAP_SHARED: // mapping memory can shared with other process // usually MAP_SHARED is choosing // MAP_PRIVATE: // mapping in copy-on-write mode, and can not shared with other process // optional: // MAP_ANON (MAP_ANONYMOUS): // anonymous mapping, not backed by any file // "fd" should be -1 and "offset" should be 0 if it's chosen. // fd: // file descriptor // offset: // offset in the file // ususally 0 is given // return value: // return exact address, or -1 for error void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset);
// unmap a file or device // addr: // address base of mapping memory // length: // address bound of mapping memory intmunmap(void *addr, size_t length);
#include<sys/ipc.h> #include<sys/shm.h> // get or create a shared memory // key: // the shared memory key to get or create (depending on "shmflg") // size: // the size of shared memory to create // if getting a shared memory, size can be any but no more than exactly size of shared memory // shmflg: // (default): // get a shared memory, otherwise raise an error // IPC_CREAT: // if the shared memory id not found, a new memory will be create // IPC_EXCL: // if the shared memory id already exists, an error will be raise // it should be used with IPC_CREAT // * others: // if IPC_CREAT is chosen, you should OR-ed with the permission mode on "shmflg" // return value: // the shmid (different to key), or -1 for error intshmget(key_t key, size_t size, int shmflg);
// attach the shared memory to the process // shmid: // shmid getting from "shmget" // shmaddr: // Address base of the memory, it's a hint of address to kernel but not exactly the address. // Usually NULL is given. // shmflg: // (default): // read and write permisson // SHM_EXEC: // execute permission // SHM_RDONLY: // readonly (remove the write permisson) // return value: // exact memory address, or -1 for error void *shmat(int shmid, constvoid *shmaddr, int shmflg);
// detach the shared memory from the process // shmaddr: // address of shared memory getting from "shmat" // return value: // return 0 for success, or -1 for error intshmdt(constvoid *shmaddr);
// About more // $ man 2 shmat
shmctl
1 2 3 4 5 6 7 8 9 10 11 12
// free the shared memory (and other functioms, see in more) // shmid: // shmid getting from "shmget" // cmd: // IPC_RMID: // remove the shared memory // buf: // NULL intshmctl(int shmid, int cmd, struct shmid_ds *buf);