Operating system Linux operating system program development

Linux operating system program development

Preliminaries:

  1, process operations: Linux system is a multitasking operating system of using the process as a task scheduling unit, the concept of process under the Linux system is the first execution of the program code, including the operation code and run the required data, parameters, and other resources.

  2, the difference between processes and procedures: on the one hand: In the Linux system, the process is executing program code, so the program was a run, a viable program, is a dynamic concept; refers to a program stored on a disk or other static code storage medium. On the other hand: a process is run based on a program, and a program loaded into memory can be repeated to form multiple processes!

  . 3, CPU time slice (Linux system about 1ms) : i.e. CPU time slice allocated to each program time, each thread is assigned a period of time, called its time slice, the time that is allowed to run the process.

  4, Linux system processes macroscopically parallel microscopically (a CPU) serial. Macroscopically parallel : a plurality of processes can be opened simultaneously; each process has a time slice and priority. At the micro serial : a CPU in each time slice, each process has a chance to run, the greater the probability of a higher priority process is running. If the end of the time slice, the process is still running, CPU will deprive and assigned to another process; if the process ends before the end of the time slice or enter the blocked state, CPU immediately switch! A CPU, executes only part of the program.

  5, mmu and multi-process systems:

  6, PID (process ID Process ID): On Linux systems, each process has a process ID (PID), all processes using a tree structure organization. The first process is started when the Linux system root process , the process further calls to other processes on the system . Linux system, there is a clear process between inheritance , all processes are PID init process 1 of offspring.

  7, the parent and child processes: Linux system, each process must have a parent process, and correspondingly, each process may be zero or more of sub-processes. With all processes have a parent process is called a brother . The relationship between processes stored in the process descriptor task_struct in. Each task_struct contains a pointer to its parent's task_struct pointer parent , also referred to as children of a child process list.  

  8, a thread can be created and destroyed another thread, multiple threads in the same process run concurrently .

  9, the existing process, the exchange process, init process, daemons

  10, zombie process, orphaned

  11, process environment:

  12, the process descriptor:

  13, the group identification code ( gid- Group ID): each login user would assume at least two ID, a user ID (UserID, referred to as the UID), a user group ID (Group ID, referred to as the GID); each files are the so-called owner of user ID and group ID.

  14, PCB (progress control block- process control block ), through the PCB system, process control and process description. In the Linux system, PCB is task_struct structure .

  15, a schematic diagram of memory allocation process (32-bit OS): kernel space and user space, kernel space kernel compared to all processes and shared .

  

Linux operating system process functions:

1, fork / vfork , create a child process

2, exec family of functions : execv / execl, execp / execlp, execve / execle; exec function can be used to replace the current process is a new process and a new process with the original process has the same PID , a process which will create and load a new process image separation .

  int execl(const char *path, const char *arg, ...);

  int execlp(const char *file, const char *arg, ...);

  int execle(const char *path, const char *arg, ..., char * const envp[]);

  int execv(const char *path, char *const argv[]);

  int execvp(const char *file, char *const argv[]);

  int execve(const char *path, const char *argv[], char * const envp[]);

Description:

path parameter indicates the name of the program you want to start including the path name

file parameter indicates the name of the file command

arg parameter represents the parameters start the program carried by the general first parameter is the name of the command you want to execute, not with the path and arg must end with NULL

argv parameter is a parameter to be passed to the command, the array of NULL terminated

envp parameter is an array of environment variables

3, getpid, getppid: get progress codes

4, setpgid: Sets the specified process group identification code ; getpgid: acquiring the specified process group identification code ; (setpgrp, of getpgrp special parameter 0)

5, setpriority: setting the specified process, process group, user priority ; getpriority: acquiring specified process (process), the process group (PIN) or user (user ID) of the priority , the priority is -20 to 20 between integers, the larger the value the higher the priority .

6, wait / waitpid: Pause the current process, waiting for the child process is interrupted or ended. wait, Parameter Description : pid_t the wait (int * Status) , Status save the child process interrupt or end state can be set to NULL, the return process ID of the child process is interrupted or ended. waitpid , Parameters : pid_t waitpid (PID pid_t, Status int *, int Options) PID: represents any child processes -1, 0: the same group identification code for all sub-processes; Status to save the child process is interrupted or the end state, can be set it is NULL; options; 0; WNOHANG (if no child process ends immediately returned), WUNTRACED (suspended immediately if the child is returned); WNOHANG | WUNTRACED (or whichever),

7, process termination function:

  _exit: direct the process stops running, clear the memory space of its use, and clear its various data structures in the kernel. exit () function and _exit () function is the biggest difference is that the exit () function before calling the exit system call to check the file to open the case , the contents of the file buffer writing a file , or " clean-up I / O buffer . "

  atexit: the ISO C requirements, a process can be registered up to 32 functions , these functions will be automatically called by Exit , which function is called termination function , and call function atexit to register these functions.

  on_exit:

 

Linux operating system semaphore and manipulation functions:

Preliminaries:

0, the critical area refers to the same time there can be only one process execution code segment in which the code . : Deadlock After a process modifies the semaphore into the critical zone, because the collapse or "Kill (the kill)" , but did not exit the critical section, any pending on the semaphore never be run opportunity.

1, exclusive resources and resource synchronization. Resource mutually exclusive: refers to a resource while only allowing a visitor to access it, having unique and exclusive , e.g. Deadlock : Process A A footprint, the application resource B, B Process B footprint, the application resource A it will result in a resource exclusive case "deadlock"; resource synchronization : is (in most cases), to achieve a visitor to resources through other mechanisms on the basis of mutually exclusive orderly access .

2, the relationship between the intersection of two main processes, synchronization and mutual exclusion.

3, several mutually exclusive manner in synchronization with the processing resources: spin locks, semaphores , mutexes and atomic variables .

 4, the signal is the amount of one kind capable of effectively processing resources and synchronization between processes mutex lock mechanism , is a semaphore counter . A semaphore indicates the number of available resources . Two semaphore operations, P, and V: P, waiting for the semaphore when the semaphore value is 0, the program waits; when the signal value is greater than 0, the signal minus 1 , the program continues ( process resources needed for the operation ) . V, a transmission signal amount of the signal magnitude plus 1, (process of generating the resource) .

5, communication mechanism (IPC) under Linux : pipes, message queues, channel number, signal values, shared memory (fastest IPC mode) , shared mapped files, socket (process communication between different machines) Wait.

6, Semaphore collection mechanism : multiple processes (queue structure) to be obtained are more critical resource (semaphore array) after in order to work, this is the semaphore collection mechanism . If the process is suspended, Linux operating state must be saved semaphore and the current process into a waiting queue ( sem_queue ).

7, the thread occupied resources : shared memory, anonymous pipes, malloc out of space, socket descriptor, epoll descriptor and thread lock.

Note: October 26, 2019 18:07:53, resources are those threads? What is the relationship between resources and virtual memory is? Why resources can not be occupied by a different process, but to release it? Not by different processes using different pointers called, and the virtual space (remove shared kernel space) are independent, the program is loaded into memory each process, and then into the register, this is loaded into memory is not independent the code? Because the register that is quite unique, so there will be Resources mutually exclusive? Personal understanding, the data structure implemented by the dictionary signal, and the key signal value is set.

8, the semaphore data structure (sem):

 

I struct {

  semval to int;          / * current value of the semaphore * /

           sempid int;          / * last process identifier in the semaphore operation *                    

        };

 

9, the data structure semaphore set (sem_ds):

struct semid_ds {

  ipc_perm sem_perm struct;           / * IPC permissions * /

   sem_otime Long;         / * last time semaphore operations (a semop) of * /

  sem_ctime Long;         / * the last time this structural modifications * /

  SEM struct       * sem_base; / * pointer to the first pointer to the semaphore in the semaphore array * /

   sem_queue struct * sem_pending; / * suspend operation pending * /

  sem_queue struct ** sem_pending_last; / * last pending operation * /

  SEM_UNDO struct * Use the undo; / * Use the undo requests on this array * /

  sem_nsems ushort;            / * semaphore at the semaphore number array * /

   };

10, queue structure (amount of each signal system set sem_queue )

 

struct sem_queue {   

    sem_queue struct * Next; / * the next queue node * /

    sem_queue struct ** PREV; / * previous node in the queue, * (q-> prev) == q * /

    wait_queue struct * Sleeper; / * process * is sleeping /

    Sem_undo Struct * Undo; / * Undo结构* /

    int PID;     / * process identification number of the requesting process * /

    Status int; / * operation completion status * /

    the semid_ds struct * SMA; / * set the amount of the signal with arrays * /

    The sembuf struct * SOPs; / * array of pending operations * /

    The nsops int;   / * number of operations * /

};

 

11, the relationship between three data structures

 

 

 

Function semaphore operation ( system calls ):

. 1, a semget (key_t Key, nsems is int, int The semflg), creates and opens a set of signal

2, a semop (the semid of int, struct * The sembuf SOPs, unsigned The nsops), according to the index value of the specified semaphore specified operation ( P, V operation)

A, sops parameter points to an array of type sembuf

struct sembuf {

                sem_num ushort;         / * index value in the semaphore array * /

                short   sem_op;         /* 信号量操作值(正数、负数或0) */

                short   sem_flg;        /* 操作标志,为IPC_NOWAIT或SEM_UNDO*/

        };

注:1、sem_op为负数,那么就从信号量的值中减去sem_op的绝对值,这意味着进程要获取资源如果sem_op是正数,把它的值加到信号量,这意味着把资源归还给应用程序的集合

命令(cmd)

解   释

IPC_STAT

从信号量集合上检索semid_ds结构,并存到semun联合体参数的成员buf的地址中

IPC_SET

设置一个信号量集合的semid_ds结构中ipc_perm域的值,并从semun的buf中取出值

IPC_RMID

从内核中删除信号量集合

GETALL

从信号量集合中获得所有信号量的值,并把其整数值存到semun联合体成员的一个指针数组中

GETNCNT

返回当前等待资源的进程个数

GETPID

返回最后一个执行系统调用semop()进程的PID

GETVAL

返回信号量集合内单个信号量的值

GETZCNT

返回当前等待100%资源利用的进程个数

SETALL

与GETALL正好相反

SETVAL

用联合体中val成员的值设置信号量集合中单个信号量的值

 

Guess you like

Origin www.cnblogs.com/yinminbo/p/11736038.html