📄 linux_ipc_api.txt
字号:
General Header Files used================#include <linux/config.h>#include <linux/version.h>#include <linux/kernel.h>#include <linux/sched.h>#include <linux/tqueue.h>#include <linux/wait.h>#include <linux/signal.h>#include <linux/kthread.h>#include <asm/unistd.h>#include <asm/semaphore.h>#include <linux/syscalls.h> //check the file#include <unistd.h>struct task_structstruct tq_structstruct work_structstruct workqueue_struct<Signal IPC mechanism is reduced in Linux kernel usage>sigaction_tsigaction() - Signal handlerSignals - SIGKILL, SIGSTOP, SIGSEGVkig_sig(signal_no,PID);void sema_init(struct semaphore *sem, int val);inline void down(struct semaphore *sem);inline int down interruptible(struct semaphore *sem);inline int down trylock(struct semaphore *sem);inline void up(struct semaphore *sem);Process Management:------------------#include <sys/types.h> #include <sys/wait.h>#include <unistd.h>pid_t fork(void);nice value <priority value>execve(Exe_path,char* argv[],environ){This function should not return, it load the Executable into the Process space and execute}It has 4 wrapper functionsProcess Exist system-call==================exit(1); The value return to parent process and it can be interpret a meaning return (value);abort(); comitting suicide CTRL-C sends an "interrupt" signal, and CTRL-Z sends a job control "stop" signal. pid_t wait4 (pid_t pid, int *status, int options, struct rusage *rusage); flag -WNOHANG Non-block wait call, clean the child zombie process. -Wait for child process to exit -struct rusage (BSD struct, supported in GNU/LINUX) has the timeval of User-space execution & Kernel Space executionA zombie process is a process that has terminated but has not been cleaned up yet.Before Parent call the wait, if child terminates, it will be in zombie state. The Wait() call clean thechild process. If parent fails to clean the child process, it continues the zombie stateAsynchronous notification:---------------------SIGCHLD - Linux notify the parent process, if a child terminates Process GroupID: --------------It is used for Job-control, the set of process belongs to same process grouppid_t getpgrp(void);int setpgid(pid_t pid, pid_t pgid);Terminal-SessionSession - Group of Process GroupProcess Group - Group ProcessIPC****PIPE:int pipefd[2]; pipefd[0] - Read Descriptor pipefd[1] - write Descriptor pipe(pipefd)close(pipefd) The communication node has to be in same hierarchy.FIFO <Named PIPE>**********************The communication node can be two independent processPIPE is file based.Types IPC:SIGNAL, Semaphore, Mutex <Synchronization>PIPE, FIFO, Shared Memory, Message Queue, socket <Data x-change>Get, Control-attribute, Operationcmd line argument: $ipcsFunction to generate Key==================key_t ftok (__const char *__pathname, int __proj_id);Shared Memory===========#include <sys/shm.h>#include <sys/stat.h>/*Allocate and get the ID */int shmget (key_t __key, size_t __size, int __shmflg)int shmctl (int __shmid, int __cmd, struct shmid_ds *__buf) /*Return the Access memory*/void *shmat (int __shmid, __const void *__shmaddr, int __shmflg)int shmdt (__const void *__shmaddr)Process Semaphore - System V=======================#include <sys/sem.h>struct sembuf{ unsigned short int sem_num; /* semaphore number */ short int sem_op; /* semaphore operation */ short int sem_flg; /* operation flag */};/* Return the Semaphore ID*/int semget (key_t __key, int __nsems, int __semflg)int semctl (int __semid, int __semnum, int __cmd, ...)int semop (int __semid, struct sembuf *__sops, size_t __nsops)/* We must define union semun ourselves. */union semun { int val; struct semid_ds *buf; unsigned short int *array; struct seminfo *__buf;};Initialize the semaphore with Semctl()Perform Post Or Pend operation with semaphore<Memory Mapping>==============In LINUX, the file is mapped to the user-process space and synchronized to the diskmmap();//Synchronizemsync (mem_addr, mem_length, MS_SYNC | MS_INVALIDATE);<IPC-Message Queue>=================struct msgbuf { long int mtype; /* type of received/sent message */ char mtext[1]; /* text of the message */ };int msgget (key_t __key, int __msgflg)int msgctl (int __msqid, int __cmd, struct msqid_ds *__buf)ssize_t msgrcv (int __msqid, void *__msgp, size_t __msgsz, long int __msgtyp, int __msgflg); int msgsnd (int __msqid, __const void *__msgp, size_t __msgsz, int __msgflg);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -