Multi-process programming

xiaoxiao2021-03-06  17

Multi-process programming

Writing in front This article is mainly based on the programming practical experience on UNIX system, as a part of its own programming practices within a period, it can be published. It is an article on UNIX programmers. Little experience is for reference only; if you are not worth it, please advise you.

I. Features of multi-process procedures Because the UNIX system is a module multiple user system, the CPU is assigned to each user by time, and it should be used in essence to be used to use the CPU to all process, each process has its own The operating environment allows "Semi-finished Products" that has been "forgotten" when the CPU is switched, and the process of "semi-finished products" is calculated in DOS. The process of switching is a "DOS Interrupt" processing process, including three levels. : (1) Saving of user data: including the main segment (Text), Data Section (DATA, BSS), Stack, Shared Memory, Save: Save of Register Data: including PC (Program Counter, pointing to the address to be executed), PSW (Processor Status Word, Processor Word), SP (Stack Pointer, Stack Pointer), PCBP (Pointer Of Process Control Block, Process Control Block Pointer ), Fp (Frame Pointer) General register, etc. (3) Save of system hierarchical: including Proc, U, Virtual Storage Space Management Table, Interrupt Processing Stack. To allow it to get the CPU time slice again, since the system has handled all These interrupt processes, what else do we have to worry about? We don't use the multi-process feature provided by the system, let several programs work together, simply and efficiently give it out. In addition, The UNIX system itself is also written in C language. Multi-process programming is the characteristics of UNIX. When we are familiar with multi-process programming, there will be a deeper understanding of the UNIX system mechanism. First, I will introduce multi-process programs. Some of the highlighted features: 1. Parallelization of a complex event is to decompose into a number of simple events, which has formed this concept in the brain of the programmer, first breaking the problem into a small Question, subdivide the small question, and finally make a function on a suitable scale. It is also said in software engineering. If we think about the way, some small problems can not be Interfering, you can Time processing, and in the key point, you need to be unified in one place. This way to run in parallel, at least from the person's time concept. And the calculation of each small problem is more simple. 2 Simple and orderly programs are not asia managed by programmers, and the programmer is designed for each process, and the organically combined with each process is combined, for each process. The design is simple, only carefully cope with the total control section (in fact, it is also very simple), you can complete the construction of the entire program. 3. Mutual discharging this feature is the characteristics of the operating system, each process is independent, no Credit. 4. Serving, for example, in a data phone query system, designs a program to process only a single query, complete a transaction. When the phone query starts, the process is produced to deal with this query; When a phone comes in, the master program produces such a process to deal with it. After each process completes the query task, it disappears. This programming is more simple, as long as you do a query program.

II. Common multi-process programming system call 1.Fork () function: Create a new process. Syntax: #include #include pid_t fork (); Description: This system Call producing a new process, a sub-process, a reputation that calls the process. Calling process is called the father process, the child process inherits almost all of the properties of the parent process:. Actual UID, GID, and valid UID, GID. Environmental variables. Additional gid.. Turn off flag when calling EXEC (). UID Setting the mode bit. The GID set the pattern bit. The process group number.................................................... The root directory. File creation mask umask.. File length limits ulimit.. Subsequent value, such as priority, and any other process predetermined parameters, depending on the species differentiates can be inherited. There are other properties. But the child process also Different attributes with the parent process: process number, sub-process number different from the process group number with any activity. Parent process number. The child process inherits the file descriptor or flow of the Parent process, with one copy and The parent process and other sub-process shared this resource. The user time and system time of the child process are initialized to 0.. The timeout clock of the child process is set to 0.. The signal processing function pointer of the sub-process is set as empty. Child process Do not inherit the record lock of the Parent process. Return value: The call is successful, returns the child process, returns the sub-process number for the parent process, which is also the most convenient way to distinguish the father and child process. Returns -1 to the parent process, the child process does not generate. Example: PID_T PID; IF ((pID = fork ())> 0) {/ * Parent Process Process * /} else == 0 ) {/ * Child process processing * / exit (0); / * CEO must use exit () to exit * /} else {printf ("fork error / n"); exit (0);} 2. System () function: Generate a new process, the child process executes the specified command. Syntax: #include #include int system (string) char * string;

Note: This call passes the parameter string to a command interpreter (typically SH) execution, that is, String is interpreted as a command, executed by the SH. If the parameter string is an empty pointer, check whether the command interpreter exists This command can be the same as the command line command, but since the command is placed in the system call, you should pay attention to the processing of special meaning characters when compiling. The command is found to be defined by the PATH environment variable. Command The consequences of the generated generally do not affect the parent process. Return Value: When the parameter is an empty pointer, the return value is not zero when the command interpreter is valid. If the parameter is not a null pointer, the return value is the return status of the command. (With WaitPID ()), the command is invalid or syntax error returns a non-zero value, and the command executed is terminated. Another situation returns -1. Example: char command [81]; int i; for (i = 1; i <8; i ) {sprintf (Command, "PS -T TTY% 02i", I); System (Command);} 3.exec () function: Perform a file syntax: #include Int Execl (Path, Arg0, ..., Argn, ​​(char *) 0) Char * Path, * Arg0, ..., * argn; int execv (path, argv) char * path, * argv [];

Int Execle (path, arg0, ..., argn, (char *) 0, ENVP) char * path, * arg0, ..., * argn, * envp [];

Int Execve (Path, Argv, Envp) Char * Path, * Argv [], * envp [];

Int Execvp (File, Argv) Char * file, * argv []; Description: This is a system called family, used to transfer a new program into the memory, and overwrite, generate new memory Process image. The new program can be an executable or shell batch command. When the C program is executed, it is called: main (int Argc, char * argv [], char * envp []); argc is the parameter Number, is the array of various parameter string pointer, and ENVP is a pointer array array of new processes. Theargc is at least 1, argv [0] is the program file name, so in the above EXC system call family, Path is the path name of the new process file. File is a new process file name. If File is not a full path name, the system call will automatically find the corresponding executable to the corresponding executable file. If the new process file is not an executable goal Files (such as batch files), execlp () and execvp () will form the file content as a standard input of a command interpreter to form system (). Arg0, ... and other pointers pointing to the end of '/ 0' ending The valid parameters of the new process are set, and the parameter list ends with an empty pointer. In turn, Arg0 must exist and point to the new process file name or path name. Similarly, Argv is a string pointer array, argv [0] points The new process file name or path name is ended with an empty pointer. ENVP is a string pointer array, ending with an empty pointer, which forms a newly-active environment. Opened before calling these system calls to new processes It is also open, unless it has defined a Close-ON-EXEC flag. The open file pointer remains unchanged in the new process, all related file locks are also reserved. Calling process settings and being captured The new process is restored to the default setting, and the other remains unchanged. When the new process starts, press the SUID and GID settings of the file in the file, the UID and GID of the SUID and GID are effective UID and GID. The new process also inherits the following properties:. Additional GID.. Process No... Parent process number.. Process group number Session number. Control terminal. The rest of the ALARM clock signal. The current work directory.. Root directory.. File creation mask.. Resource limit.. User time, system time, child process user time, child process System time.. Record lock.. Process signal mask. Successful return value, because the original process has wonless. Example: Printf ("Now this process will be ps command / n"); Execl ("/ bin / ps", "ps", "- ef", null) ;

4.Popen () function: Initialize the pipeline from / to a process. Syntax: #include file * pop (command, type) char * Command, TYPE; Description: This system call is in calling process and executed Create a pipe between commands. The parameter command is the command line that is executed. TYPE as I / O mode, "R" is read from the executed command, "W" is written to the executed command. Return a standard stream pointer, As a pipeline descriptor, read or write data to the executed command (STDIN or STDOUT as being executed) This system call can be used to call the system command in the program and obtain the output information of the command or enter information to the command. Return Value: Returns NULL back, returns to NULL, returns the file pointer of the pipe. 5.pclose () Features: Close to a process of a process. Syntax: #include int pClose (STRM) File * STRM; Note: This system calls the pipes used to turn off by POPEN () (), and will wait after the command is executed by the POPEN () activation command, turn off the pipe back to read the command return code. Return value: If the file descriptor is not POPEN () opens, returns -1. Example: Printf ("Now this process will call popen system call / n"); file * fd; if ((fd = POPEN ("PS-Ef", "R") ) == null) {Printf ("Call Popen Failed / N"); return;} else {char str [80]; while (FGETS (STR, 80, fd)! = NULL) Printf ("% s / n", str);} PCLOSE (FD); 6.Wait () function: Wait for a child process to return and modify the status syntax: #include #include PID_T WAIT INT * STAT_LOC; Description: Allows the status information of the process to get the child process. The calling process will hang until one child process is terminated. Return Value: Wait when a child process returns, return value For the sub-process number, the return value is -1. Simultaneous STAT_LOC returns the return value of the sub-process. Example: / * Parent Process * / if (fork ()> 0) {Wait (INT * 0); / * The parent process is waiting for the sub-process return * /} else {/ * child process processing * / exit (0);} 7.WaitPID () function: Wait to return and modify the status syntax waiting for the sub-process of the specified process number: #include <

Sys / types.h> #include PID_T WAITPID (PID, STAT_LOC, OPTION) PID_T PID; INT * STAT_LOC, Options; Description: When PID is equal to -1, Options is equal to 0, the system call is equivalent The behavior of the system call is determined by the parameters PID and Options. PID specifies a group of parent processes that know the status of its status: -1: Requires the return status of any child process.> 0: Requirements Know the status of the sub-process of the process number as the PID value. <-1: Requires the status of the child process in which the process group number is the absolute value of the PID. The Options parameter is a bitmap that composes the marker represented by a bit method, each A flag is represented in bytes: wuntraced: Reports any sub-process of any unknown and has stopped running the specified process number. The status of the sub-process has not been reported when the status is stopped. WCONTINUED: Reports any status of the child process that continues to run, the status of the child has not been reported since the continuation of running. WHOHANG: When the system call is called, the status of the child process specifies the child process of the process number. It is not immediately valid (ie, can be read immediately), call the process and suspend execution. Wnowait: Keep the process that sets its status in the STAT_LOC in the waiting state. This process will wait until the next time it is required to return. Value. Return Value: When a child process returns, the return value is the sub-process number, otherwise the return value is -1. At the same time, the return value of the STAT_LOC returns the sub-process. Example: PID_T PID; int stat ;loc; / * Parent Process * / if ((PID = fork ())> 0) {WaitPID (PID, & Stat_loc, 0); / * The parent process waits for the sub-process for the process number to PID * /} else {/ * Process process of process * / exit (1);} / * parent process * / printf ("stat_loc is [% d] / n", stat_loc); / * String "stat_loc is [1]" will be printed * / 8.Setpgrp () function: Set the process group number and session number. Syntax: #include pid_t setpgrp () Description: If the calling process is not the session, set the process group number and session number setting To work with its process number. And release the control terminal of the transfer process. Return value: After the call is successful, return to the new process group number. Example: / * Parent process processing * / if (fork ()> 0) {/ * Parent Process Processing * /} else {setPGRP ();

/ * The process group number of the child process has been modified to be the same as its process number * / exit (0);} 9.exit () function: Termination process. Syntax: #include void exit (status) INT Status; Description: The calling process is terminated by the system call. Cause additional processing ends before the process is terminated before the process is terminated. Return Value: None 10.Signal () Features: Signal Management Function Syntax: #include void * Signal (SIG, DISP)) (INT) INT SIG; VOID (* DISP) (INT); Void (* SIG, DISP)) (INT) INT SIG; Void (* DISP) (int);

INT SIGHOLD (SIG) INT SIG;

Int Sigrelse (SIG) INT SIG

Int Sigignore (SIG) int Sig;

INT Sigpause (SIG) INT SIG; Description: These system calls provide a simple signal processing for the application to specify the specified signal. Signal () and SIGSET () are used to modify the signal positioning. Parameter SIG Specify the signal (except sigkill and sigstop, this Both signals are processed by the system, the user program cannot be captured. DISP specifies a new signal positioning, that is, a new signal processing function pointer. Can be SIG_IGN, SIG_DFL or signal handle address. If SIGNAL (), DISP is a signal handle address When SIG does not receive this signal for SIGILL, SIGTRAP, or SIGPWR, the system first resets the SIG signal handle as SIG_DFL, then executes the signal handle. If SIGSET (), the DISP is the signal handle address, the signal, the system First, add this signal to the signal mask of the calling process, then execute the signal handle. After the signal handle runs, the system will recover the signal mask of the calling process to receive the preamble. In addition, when using sigset (), The DISP is SIG_HOLD, then the signal will join the signal mask of the calling process and the signal is positioned unchanged. Sighold () adds the signal to the signal mask of the calling process. Sigrelse () Signal from the signal mask of the signal from the calling process Delete. SIGIGNORE () Sets the positioning of the signal to SIG_IGN. Sigpause () Remove the signal from the signal mask of the calling process while hanging the calling process until the signal is received. If the signal SIGCHLD is positioned as SIG_IGN, then call The child process of the process will not turn into a zombie process at the end. The calling process also does not have to wait for the child process to return and do the phase handling. Return Value: Signal () Returns the value of the DISP that is recently called Signal () setting. Otherwise, returns SIG_ERR. Example 1: Set the user's own Signal interrupt processing function, as an example: int flag = 0; void myself () {flag = 1; Printf ("get signal sigint / n"); / * To reset the sigint signal interrupt processing function as this function Then execute step * / void (* a); a = myself; signal (sigint, a); flag = 2;} main () {while (1) {SLEEP (2000); / * Waiting interrupt Signal * / if (Flag == 1) {Printf ("Skip System Call Sleep / N); EXIT (0);} IF (Flag == 2) {Printf ("

Skip System Call Sleep / N "); Printf (" Waiting for Next Signal / N ");}}} 11.kill () Features: Send a signal to one or set of processes. Syntax: #include #include int Kill (PID, SIG); PID_T PID; INT SIG; Description: This system call sends a signal to one or set of processes, which is specified by the parameter SIG, which is given One of the signal tables. If a 0 (empty signal) checks the error but does not actually transmit signals, used to check the validity of the PID. PID specifies the process or process group to be transmitted. PID is greater than 0, The signal will be sent to the process number equal to the PID; if the PID is equal to 0, the signal will be sent to the process of the transmission signal in one process group (except for the special process of the system); if the PID is less than 1. The signal will be sent to all process group numbers as the same process group number and PID absolute value; if the PID is equal to -1, the signal will be sent to all processes (except for special system processes). Signal to be sent to the designated process First call the process must have permission to send the signal to the process. If there is a suitable priority, it has permission. If the actual or valid UID of the process is called the actual UID of the process of the receiving signal or use setuid () The UID setting of the system call settings, or SIG is equal to the session number of Sigcont to send and receive the session number of the two processes, then the calling process also has permission to send the signal. If the process has the permissions of any process specified by the process to the PID, call success, otherwise the call failed, No signal is issued. Return Value: Returns 0 after calling, otherwise returns -1. Example: Assuming that the previous example process number is 324, it is now sent to a SIGINT signal, allowing it to signal processing: Kill ((PID_T) 324, SIGINT); 12.Alarm () function: Set a process of timeout clock. Syntax : #include unsigned int a: indicating the timeout clock indicating the calling process Send a SIGALRM signal to the calling process after the specified time. Set the timeout clock time value will not be put In the stack, the last setting will rush the previous (not until timeout). If the sec is 0, cancel any previously set timeout clock. Fork () will initialize the new process timeout clock to 0. When a process uses an Exec () system call new execution file, the timeout clock set before call is still valid after the call. Return value: Return to the last set timeout clock to the time of the time to be rest. Example: int FLAG = 0; void myself () {flag = 1; Printf ("get signal sigalrm / n");

/ * To reset the SIGALRM signal interrupt processing function is this function *: step * / void (* a) (); a = myself; Signal (SigaLRM, A); flag = 2;} main () {ALARM (100); / * 100 seconds post timeout interrupt signal * / while (1) {SLEEP (2000); / * Wait for interrupt signal * / if (flag == 1) {Printf ("Skip System Call Sleep / N" ); Exit (0);} if (flag == 2) {Printf ("Skip System Call Sleep / N); Printf (" Waiting for Next Signal / N ");}}} 13.msgsnd () function: Send a message to the specified message queue. Syntax: #include #include #include int msgsnd (MSQID, MSGP, MSGSZ, MSGFLG) INT MSQID; void * msgp; size_t msgsz; int msgflg; Description: Send a uniform The message queue of the message queue identification number is specified by MSQID. Parameter MSGP points to a user-defined buffer, and the first domain of the buffer should be long integer, specify the message type, and other data placed in the message of the buffer. Other text spaces. The following is the message element definition: long mtype; char mText []; mtype is an integer for receiving the process selection message type. MTEXT is any body for the length of MSGSZ bytes, parameter MSGSZ can be from 0 to 0 The maximum interval between the system allows. MSGFLG Specifies the operation behavior:. If (MSGFLG & IPC_Nowait) is true, the message is not sent immediately and the calling process will return immediately. If (MSGFLG &

IPC_nowait is not true, the calling process will hang up until one of the cases occur: * The message is sent out. * Message Queue Sign is deleted by the system. The system call returns -1. * Calling process receives an unnealated ignore The interrupt signal, the calling process continues to be executed or terminated. After the call is successful, the relevant structure of the specified message queue does the following action:. Message number (MSG_QNUM) plus 1. Message Queuing Recently Send Process No. (MSG_LSPID) Change to call The process number. Message queue transmission time (msg_stime) is changed to the current system time. The above information can be seen by command ipcs -a. Return Value: Return 0, otherwise returns -1. 14.msgrcv () function: From message queue Message of the specified type. Syntax: #include #include #include int MSGRCV (MSQID, MSGP, MSGSZ, MSGTYP, MSGFLG) INT Msqid; void * msgp; int msgsz; long msgtyp; int msgflg; Description: This system calls from the message queue specified by MSQID to a message from the MSGTYP specified type in a buffer pointing by MSGP, the same, The structure of the buffer is as previously described, including the message type and message body. Msgsz is the number of bytes of the received message body. If the length of the received message body is greater than the MSGSZ, it will be truncated to the MSGSZ byte ( When the message flag MSGFLG & MSG_NoError is true), the cut-off part will be lost, and the message transmission process is not notified. MSGTYP designated Message Type:. Take the first message in the message queue. The first type of message in the message queue is received in the message queue. The first type value in the receiving message queue is not less than MsgTyp in the receiving message queue. Values ​​and types of values ​​and smallest. MSGFLG Specifies the action behavior: if (MSGFLG & IPC_Nowait) is true, the calling process will return immediately. If the message is not received, the return value is -1, and errno is set to eNomsg.. If (MSGFLG &

IPC_nowait is not true, the calling process will hang up until one of the cases occur: * The type of message in the queue is valid. * Message Queue flag is deleted by the system. System call returns -1. * Call process Receive To an unloaded interrupt signal, the calling process continues to execute or terminated. After the call is successful, the relevant structure corresponding to the specified message queue does the following action:. Message (MSG_QNUM) minus 1. Message Queue Recent receiving process number ( Msg_lrpid) Change to the process number. Message queue receipt time (MSG_rtime) change to the current system time. The above information can be seen using the command ipcs -a. Return Value: The return value is equal to the number of bytes that receive the actual message body Uncomfortable returns -1. 15.msgctl () function: Message Control Operation Syntax: #include #include #include int msgctl MSQID, CMD, BUF) INT MSQID, CMD; Struct MSQID_DS * BUF; Description: This system call provides a series of message control operations, the operation action is defined by the CMD, the following CMD definition value indicates the definition of each operational action. IPC_STAT: will The current value of each element is placed in the structure pointed to by the BUF related data structure. IPC_set: Set the following elements in the data structure related to the MSQID to the corresponding value in the structure pointing by the BUF. MSG_Perm.uid MSG_Perm .gid msg_perm.mode The MSG_QBYTES This command can only be operated by the active UID equal to the process or effective UID of the MSG_Perm.cuid or MSG_Perm.UID. Only the user with appropriate permissions can increase the value of MSG_QBytes. IPC_rmid: Delete the message queue indicated by MSQID To remove it from the system and destroy the relevant data structure. This command can only be operated by the effective UID equal to the process or effective UID of the MSG_Perm.cuid or MSG_Perm.UID. Return Value: Return value 0, otherwise to -1. 16.msgget () function: obtain a message queue. Syntax: #include #include #include int msgget Key, MSGFLG) Key_T Key; Int Msgflg;

Description: This system calls return the identifier of the message queue associated with the parameter KEY. If the facts are established, the identifier and data structure related to the message queue will be created:. If the parameter key is equal to IPC_Private.. If the parameter Key Without an existing message queue identifier, the value (MSGFLG & IPC_CREAT) is true. When you create a message queue, the data structure associated with the new message queue identifier will be initialized as follows:. MSG_Perm.cuid and Msg_perm.uid is set to call the effective UID of the process. Msg_perm.cgid and msg_perm.gid are set to the valid GID of the calling process. MSG_Perm.Mode Access bit is set to MSGFLG access ratio.. Msg_lrpid, Msg_stime, msg_rtime is set to 0.. MSG_cTime is set to the current system time. Msg_qbytes is set to the maximum value allowed by the system. Return value: Return a non-0 value, called a message queue identifier; otherwise the return value is -1 Example: This example will include system calls that include all message queues described above: #define rkey 0x9001L / * read message queue's key value * / #define WKey 0x9002L / * write message queue's key value * / #Define msgflg 0666 / * Message queue access * / #define ipc_wait 0 / * Wait in the include * / int RMSQID; / * read message queue identifier * / int WMSQID; / * Write message queue identifier * / struct msgbuf { mtype CHAR MTEXT [200];} BUF; / * If the read message queue already has the identifier, it is created and acquired ((rmsqid = msgget (rkey, msgflg | ipc_creat)) <0) { Printf ("GET Read Message Queue Failed / N); EXIT (1);} / * If there is a write message queue already failed, if there is no existence, create and acquire the identifier * / if ((WMSQID = msgget (WKEY, MSGFLG | IPC_CREAT | IPC_TRUNC)) <0) {PrintF ("Get Write Message Queue Failed / N); EXIT (2);} / * Receive all types of messages * / if (MSGRCV (RMSQID, & Buf, Sizeof Msgbuf) -SizeOf (long), 0L, IPC_Wait> 0) Printf ("get% ld type message from queue:% s / n"

, Buf.mtype, buf.mtext); Else {PrintF ("Get Message Failed / N"); EXIT (3);} buf.mtype = 3L IF (Msgsnd (WMSQID, & Buf, Sizeof (Struct Msgbuf) - Sizeof LONG), IPC_NOWAIT> 0) Printf ("Send Message OK / N"); Else {PrintF ("Send Message Failed / N"); Exit (4);} Msgctl (WMSQID, IPC_rmid, (Struct Msqid *) NULL 17. SHMAT () Function: Connect shared memory operation. Syntax: #include #include #include void * shmat (SHMID, Shmaddr, shmflg) int shmid; void * shmaddr; int shmID; Description: Connect the shared memory indicated by the SHMID to the data segment of the calling process. The join segment is placed in the address, which is specified by the following criteria: If ShmAddr is equal to (void *) 0, the segment is coupled to the first available address selected by the system. If the SHMADDR does not equal (void *) 0, the paragraph is connected to (shmaddr-) SHMADDR% SHMLBA)) gives the address. If the SHMADDR does not equal (SHMFLG & SHM_RND) value (SHMFLG & SHM_RND) value, the segment is connected to the address specified by ShmAddr. If the (shmflg & sshm_rdonly) is true and the calling process is read, it is read-only. Otherwise, if the value is not true and the calling process is read and written, the coupled segment is readable. Return Value: Returns the starting address on the data segment if the success is called success. Otherwise the return value is -1. 18.SHMDT () function: Disconnect the shared memory joint operation. Syntax: #include #include #include Void * shmdt (shmdr) void * shmaddr; Description: This system call will leave out of the shared memory segment specified by ShmAddr from the calling process. Return value: If the call is successful, the return value is 0, otherwise the return value is -1 19. SHMGET () function: acquire shared memory segment grammar: #include #include

#include int shMget (key, size, shmflg) key_t key; int size, shmflg; Description: This system calls Returns the key-related shared memory identifier related to Key. Share memory identifier and related data structure and at least The shared memory segment of the Size byte can be created normally, requiring the following facts:. Parameter key is equal to IPC_Private.. Parameter Key does not have a related shared memory identifier, at the same time (shmflg & ipc_creat) value is true. Share memory creation, new generation sharing The memory identification related data structure is initialized as follows: SHM_PERM.CUID and SHM_PERM.UID Set to the valid UID of the calling process.. Shm_perm.cgid and shm_perm.gid set to the valid GID of the call process.. Shm_perm.mode Access Bit The bit is set to the SHMFLG access ratio. SHM_LPID, SHM_NATTCH, SHM_ATIME, SHM_DTIME Set to 0.. SHM_CTIME is set to the current system time. SHM_SEGSZ is set to 0. Return value: Return a non-0 value if the call is successful, called Share the memory identifier, otherwise the return value is -1. 20.SHMCTL () function: shared memory control operation. Syntax: #include #include #include int shmctl (SHMID, CMD, BUF) INT SHMID, CMD; Struct SHMID_DS * BUF; Description: This system call provides a series of shared memory control operations. The operational behavior is specified by CMD. The following is the valid value of CMD: IPC_STAT: places the current value of each element in the data structure related to the SHMID into the structure pointed to by the BUF.. IPC_set: Set the following elements in the data structure related to the SHMID to by BUF The corresponding value in the structure. SHM_PERM.UID SHM_PERM.GID SHM_PERM.MODE This command can only be operated by the effective UID equal to the process or the effective UID of SHM_PERM.CUID or SHM_PERM.UID.. IPC_rmid: Delete by the SHMID Shared memory. Delete it from the system and destroy the related data structure. This command can only be operated by the effective UID equal to the process or the effective UID of SHM_PERM.CUID or SHM_PERM.UID. Return Value: If you call success Then returns 0, otherwise returns -1. Example: This example includes all shared memory operating system calls: #include #include #include

#define shmkey 74 #define k 1024 int shmid; cleanup () {shmctl (shmid, ipc_rmid, 0); exit (0);} main () {INT * PINT; char * addr1, * addr2; extern char * shmat ); Extern cleanup (); for (i = 0; i <20; i ) signal (i, cleanup); shmid = shmget (shmKey, 128 * k, 0777 | ipc_creat); addr1 = shmat (shmid, 0,0 ); Addr2 = shmat (shmid, 0,0); Printf ("AddR1 0x% x addr2 0x% x / n", addr1, addr2; PINT = (int *) addr1; for (i = 0; i <256 i ) * Pint = i; PINT = (int *) addr1; * Pint = 256; PINT = (int *) addr2; for (i = 0; i <256; i ) printf ("index% d / TValue% D / N ", I, * Pint ; shmdt (addr1); shmdt (addr2); PA Use ();} 21.SemctL () function: Semicalation control operation. Syntax: #include #include #include int Semctl (SEMID , MEMNUM, CMD, Arg Int Semid, Semnum, Cmd; Union Semun {Int Val; Struct Semid_ds * BUF; Ushort * Array;} Arg;

Note: This system call provides a semaphore control operation, and the operational behavior is defined by the CMD. These commands are operated on the amount specified by SEMID and semnum. Each command requires a corresponding permission level:. GetVal: Returns the value of Semval, requires read rights.. SetVal: Set the value of the semval to arg.val. After successful execution, the semadj's value of all the processes corresponding to all processes are cleared, requiring the renovation. GetPid: Returns read permissions. Getncnt: Returns the value of semncnt, requires read privileges.. Getzcnt: Returns the value of Semzcnt, requires read rights. The following command is on a set of semval in a set of semaples Operation: getall: Returns the value of each SemVal while placing each value into an array pointed to by arg.aray. When this command is successfully executed, the number of semadj's values ​​corresponding to all processes are cleared, requiring Modify the permissions. Set the respective SEMVAL values ​​according to the arguments pointed to by arg.aray. When this command is successfully executed, the semadj's value corresponding to all processes are cleared, and there is a modification permission. The following command is in any situation. It is effective: IPC_STAT: Put the value of each member of the data structure related to the SEMID into the structure points to the arg.buf. Requires read privileges.. IPC_set: Set the following members of the SEMID related data structure, set The data is read from the structure points to the arg.buf: SEM_PERM.UID SEM_PERM.GID SEM_PERM.MODE This command is only It can be operated by a process operation of the appropriate permissions with a valid UID equal to SEM_PERM.CUID or SEM_PERM.UID.. IPC_rmid: Deletes a seminding amount identifier and associated set of seminated signals and data structures specified by SemID. This command is only A process operation that can be appropriately permissions by the effective UID equal to SEM_PERM.CUID or SEM_PERM.UID. Return Value: If the call is successful, return the following value according to CMD: getVal: Semval value. GetPid: sempid value. Getncnt: The value of semncnt: semzcnt: Others: 0. Returns -1. 22.Semget () function: Get a set of semaphors. Syntax: #include #include #include int semget (key, nsems, semflg) key_t key; int NSEMS, SEMFLG;

Description: Returns the signal amount identifier associated with the key. If the following fact is established, the amount of SemiD_DS data structure and a set of NSEMS semons associated with the semaphore:. KEY is equal to IPC_Private. .. There is no signal volume related to Key, and the Semflg & IPC_CREAT is true. The SEMID_DS data structure related to the new semaphore is initialized as follows:. In the operational permissions structure, SEM_PERM.CUID and SEM_PERM.UID settings are equal to the invoking process UID.. SEM_PERM.CGID and SEM_PERM.GID settings are equal to the valid GID of the calling process.. Access permission bit SEM_PERM.MODE setting is equal to SEMFLG's access ratio.. SEM_IME setting is equal to 0, SEM_CTIME settings equal to Current system time. Return value: If the call is successful, return a non-0 value, called the signal quantifier; otherwise return -1. 23.Semop () function: Signal operation. Syntax: #include #include #include int SMOP (SEMID, SOPS, NSOPS) int SMID; Struct Sembuf * SOPS; unsigned nsops; Description: This system call is used to execute user-defined The behavior collection in a set of semaphones. This group semaphore is related to SEMID. Parameter SOPS is a user-defined semaphore operation structure array pointer. Parameter nSOPS is the number of elements of the array. Each element structure of the array. Includes the following members: s EM_NUM; / * Signal number * / SEM_OP; / * Semand operation * / SEM_FLG; / * Operation flag * / Each semaphore operation in this system call definition is a signal specified by SEMID and SEM_NUM. Variable SEM_OP specifies one of three semaphysics: If SEM_OP is a negative number and the calling process has modified permissions, the following case will happen: * If SEMVAL is not less than the absolute value of SEM_OP, the absolute value of SEM_OP is reduced To the value of Semval. If (SEMFLG & SEM_UNDO) is true, the SemAdj value of the signal specified by the SEM_OP is added. * If the SEMVAL is less than the absolute value of SEM_OP (SEMFLG & IPC_NOWAIT), the call is returned immediately. * If SEMVAL is less than the absolute value of SEM_OP (SEMFLG &

IPC_nowait) is false, the system call will increase the SemncNT value (plus one) associated with the specified semaphore, and the calling process is not until one of the following conditions is satisfied: (1). Semval value becomes unspeakable than SEM_OP absolute value When this happens, the specified semncNT is reduced, if (SEMFLG & SEM_UNDO) is true, the absolute value of SEM_OP is added to the SemAdj value of the call process specifies the amount of semaphore. (2). Call the process waiting for SEMID Has been deleted. (3). Calling process sways to the signal, at this time, the SEMNCNT value of the specified semaphore is reduced, the process executes the interrupt service program. If SEM_OP is a positive value, the call process has modified permissions, The value of SEM_OP is added to the value of Semval, if the SEM_OP subtracts the semadj value of the calling process to specify the semaphore. If the SEM_OP is 0, the calling process has read rights, one of the following cases will be generated : * If SEMVAL is 0, the system calls return immediately. * If SEMVAL is not equal to 0 and (SEMFLG & IPC_Nowait) is true, the system calls return immediately. * If SEMVAL is not equal to 0 and (SEMFLG & IPC_Nowait), this system call will The SemzcNT value of the specified semaphore is plus one, and the calling process happens until one of the following occurs: (1). Semval value is 0, the SemzcNT value of the specified semaphore is reduced. (2). Call the process waiting for SEMID Has been deleted by the system. (3). Calling process traps to the signal, at this time, specify the amount of semaphore The semncnt value is reduced, the process is called execution interrupt service program. Return Value: Returns 0, otherwise returns -1. Example: This example will include all system calls included in the above semaphore: #include #include #include #define semkey 75 int SEMID; unsigned int count; / * SemBuf structure defined in file sys / sem.h * Struct Sembuf {* unsigned short SEM_NUM; * Short Sem_op; * Short Sem_fl; *} * / Struct Sembuf Psembuf, vsembuf; / * p and v operation * / cleanup () {SemctL (SEMID, 2, IPC_RMID, 0); exit (0);

} Main (argc, argv) int Argc; char * argv []; {INT I, FIRST, SECOND; Short InitArray [2], OutArray [2]; Extern cleanup (); if (argc == 1) {for (ARGC == 1) {for i = 0; I <20; i ) Signal (I, Clearup); SEMID = Semget (SemKey, 2,0777 | IPC_CREAT); IPC_Creat); IPCRAY [0] = IITARRAY [1] = 1; Semctl (SEMID, 2, SETALL, InitArray); SemctL (SemID, 2, Getall, OutArray); Printf ("SEM INIT VALS% D% D / N", OutArray [0], OutArray [1]); Pause (); / * Sleep to be a software Interrupt signal wake-up * /} else if (argv [1] [0] == 'a') {first = 0; second = 1;} else {first = 1; second = 0;} SEMID = Semget (SEMKEY, 2 , 0777); psembuf.se m_op = -1; psembuf.sem_flg = SEM_UNDO; vsembuf.sem_op = 1; vsembuf.sem_flg = SEM_UNDO; for (count = 0 ;; xcount ) {psembuf.sem_num = first; semop (semid, & psembuf, 1); psembuf. SEM_NUM = Second; Semop (SemID, & Psembuf, 1); Printf ("proc% d count% d / n", getpid (), count); vsembuf.sem_num = second; semop (semid, & vsembuf, 1); vsembuf. SEM_NUM = First; Semop (SemID, & Vsembuf, 1);

}} 24.sdener () function: Share data segment Synchronous Access, Plock. Syntax: #nclude int Sdenter (addr, flags) char * addr; int flags; Description: Used to indicate calling process The content in the shared data segment will be accessed. The parameter addr is a valid return code that will call a SDGET () call. The action executed depends on the value of the Flags:. SD_NOWAIT: If another process has called the specified segment And there is no call (), and this segment is not created with the SD_UNLOCK flag, then the transfer process is not waiting for the paragraph idle but immediately returns an error code. SD_WRITE: Indicates the calling process to write data to the shared data segment. At this time, The other process uses the SD_RDONLY flag to join the shared data segment is not allowed. Return Value: Returns 0, otherwise returns -1. 25.sdleave () function: Share data segment Synchronous Access, unlock. Syntax: # Include int Sdleave (addr, flags) Char * addr; Description: The content used to indicate the call process has completed the content in the shared data segment. Return Value: Returns 0, otherwise returns -1. 26.SDGET () Function: Connect the shared data segment to the data space of the calling process. Syntax: #include char * sdget (path, frame.mode) char * path; int flags; long SIZE; int mode; Description: This system call connects the shared data segment into the data segment of the calling process, the specific action is defined by the value of the FLAGS: SD_RDONLY: The join segment is read-only. SD_WRITE: The join paragraph is readable. SD_CREAT: If the paragraph named by the path is not in use, the role of this flag is created in the same paragraph, otherwise This segment is created according to the value process of Size and MODE. The authority of the read / write access to the segment is the same as the MODE, the function is the same as the general file. Segment is initialized to all 0.. SD_UNLOCK: Create this flag Segment, there is a number of processes to be accessed simultaneously (in reading and writing). Return Value: Return the join segment address if the success is successful, otherwise returns -1. 27.sdfree () function: Will share data segment from call The data space of the process is interrupted. Syntax: #include int SDFree (addr) char * addr; Description: This system calls Separate the shared data segment from the specified address of the data segment of the calling process. If the call is called, Sdleave () is called to call this system call, then SdleVe () is called, and then the work of this call. Return value: Return the join segment Address. Otherwise, returning -1. 28.sdgetv () function: Sync Share Data Access. Syntax: #include <

Sys / sd.h> int SDGETV (AddR) Char * Addr; Description: Used to synchronize coordination is using the process of shared data segments. Return to the version number of the shared data segment. When there is a process to do Sdleave () operations When the version number will be modified. Return value: If the call is successful, return to the version number of the shared data segment, otherwise return -1. 29.sdwaitv () function: Synchronize Share Data Access. Syntax: #include int SDWAITV (AddR, Vnum) Char * Addr; INT VNUM; Description: Used to synchronize coordination is using the process of shared data segments. Return to the version number of the shared data segment. Call process sleep until the version of the specified segment No longer equal to VNUM; return value: If the call is successful, return to the version number of the shared data segment, otherwise return -1. 30.sbrk () function: Modify the data segment space allocation. Syntax: char * sbrk (incr) INT InCr; Description: Used to dynamically modify the spatial assignment of the Call process data segment. Process will reset the segment value of the process and assign a space for a suitable size. The segmentation value is the first assigned address outside the data segment. To assign The amount of space is equal to the amount of segment value. The newly allocated space is set to 0. If the same memory space is reassigned to the same process, the content of the space is uncertain. Return value: If successful call, return value is 0, otherwise returns -1. Example: This example will include all system calls including the above shared data space operation: char buf [21]; intv; / * acquire or create a shared data space (system special file) , Name / tmp / area1, length is 640, user access is 0777 * / area1 = sdget ("/ tmp / area1", sd_write | SD_CREAT 640, 0777); if ((int) area1 == - 1) {Printf ("Get Share Data Segment Area1 Failed / N"); EXIT (1);} / * acquisition the version number of the shared data segment Area1 * / v = SDGETV (Area1); / * Request Access Shared Data Section Area1, if the process is accessed, the process is sent, otherwise, enter the access and write the data segment * / sdenter (Area1, sd_write) ; / * Access to the shared data segment, write 10 A * / STRCPY (Area1, "Aaaaaaaaaaa); / * Apply for access to access, if the process application access is activated, the process * / SDLEAVE (Area1); / * Process processing process * / / * Wait to take the version number * / SDWAITV (Area1, V) of the shared data segment Area1; / * Re-apply access shared data segment area1 * / sdenter (area1, sd_write); / * Read shared data segment Data * / Memcpy (buf, area1, 20);

/ * Apply for access to access, activate the process * / sdleave ("" The Data Now IN Area1 IS [% S] / N ", BUF); 31.Getenv () function is activated if the process application access is accessed. : Get the specified environment variable. Syntax: #include #include int Putenv ( String) CHAR * STRING; Description: Parameter string points to a string, the format is as follows: Name = value This system call will enumerate the environment variable name equal to value value, modify, or add an environment variable, string string into part of the environment. Return Value : If Putenv () cannot get the appropriate memory space, return a non-0 value, otherwise return 0. Example: / * Parent process * / Putenv ("home = / home / abcdef"); Putenv ("Path = / bin" ); If (fork ()> 0) exit (0); / * Parent process exits the run * / / * child process processing * / SetPgrp (); / * The environment variable set by the parent process has been passed to the child process * / char * value1; value1 = getenv ("home"); value2 = getenv ("PATH"); printf ("home = [% s], path = [% s] / n ", value1, value2); / * will print" home = / home / abcdef "and" path = / bin "* /

III. Multi-process programming skills 1. Main program structure (1) Event master mode If the application belongs to the transaction mode, the main function is designed to monitor the event. When the event occurs, a new process can be generated. The transaction can allow the child process to exit the system after the transaction is complete. This processing method generally does not need messaging. (2) If the information coordination method is completed by multiple process coordination processing, you can generate these processes, through the message Delivery between processes, enabling each process to coordinate and complete a transaction. This means of processing is generally after generating several processes, calling other program files with EXEC (), making different procedures at the same time The system is running in the system. Then via the IPC mechanism, allow each program to be coordinated. 2. Select the main bifurcation point (1) Event initial generation program structure corresponding to the event master mode. Key point to select events The initial generation point, such as the chain information given by the network program. The master program is recognized as an event after receiving the message, then generate a child process processing: Receive transaction information, transaction processing , Send return transaction information, turn off the link, etc. After the completion, the sub-process exit the system. (2) The main program produces a program structure corresponding to the information coordination mode. The master program is only responsible for generating several child processes, each sub-process calls EXEC () Different execution files into memory operation, the master program can exit the system after generating all child processes, and leave the child process in memory. 3. Process relationship processing (1) Parental process relationship. The concept of the process group handling the process group is like this. When the system starts, the first process is init, the process group number is equal to the process number, and the process group number of all the sub-process it produces is the same, the child process of the child process Also inherited the process group number, so that all sub-processes generated by init belong to the same process group. However, the parent-child process of the same process group may communicate on the signal, if the parent process is prior to the child process Exit the system, then the child process will become an orphan process, which may become a zombie process. Thus, the child process is running in the case of not "willing". To solve this problem, the child process can form a new process. Group, toned SE The TPGRP () is separated from the original process group, producing a new process group, the process group number is the same as its process number. In this way, the parent process will not affect the child process before running. Sub-process signal processing However, doing the above handles can not solve another difficulty, that is, the child process does not find its parent process (the parent process has exited, the child process is changed to 1). Send child process exits the signal Without the parent process, the child is not possible to completely exit running, which may enter a zombie state. So the parent process will return the processing, generated sub-process, in the parent process before generating child processes. After exiting the run, the sub-process returns the processing of its process returns signal will give the default processing by the system. The child process can be properly exited. (2) Brothers process relationship. Exchange process number for information coordination mode, Each brother process needs to know each other to understand the input number in order to make the signal processing mechanism. The relatively reasonable method is that the parent process generates a space for shared memory, and each child process sets its own process number in the shared memory when starting. When a child process sends a signal to another or because other originals need to know another child process number, you can access the required process number in shared memory. 4. Inter-process communication processing ( 1) Shared memory requires locking mechanisms that do not have a lock mechanism when there is a design in the shared, so that multiple processes will have problems when accessing shared memory. Such as:

A process modifies a shared memory unit, and another process may modify the unit immediately when reading the shared memory unit, which will affect the correctness of the program. At the same time, there is also a time-time system for each process. Processing, may cause different correctness. According to the operating system's mode of operation, there is a read lock and write lock to ensure the consistency of the data. So there is no lock mechanism sharing memory, you must use the semaphore To ensure the correct operation of shared memory. (2) Message Queue Requires Key Value Message Queuing Operation After the access to the message queue, you must read the same key value in the message queue through a key value, write a message. Enter the message key. This can distinguish between different transactions through different key values, so that the same message queue can be used simultaneously without conflict. If the reading message queue uses the key value 0, read the first queue in the message queue. Message, regardless of its key value. (3) Signals require signal processing function settings and reset When the user process needs to do handles yourself, you can define the interrupt processing function yourself, and set the interrupt processing function and the Interrupts associated. In this way, the user process then calls the user-defined function after receiving the interrupt, and the user process continues to run from the interruption after the processing is completed (if the user-defined interrupt function does not have a long hop function or exiting operation. Change the system call of the running command address. After the interrupt signal is processed, the interrupt processing function will be restored to the last default processing function instead of maintaining a user-defined function, so in the user-defined interrupt processing function. Refine the interrupt and function's own association. (4) IPC's permissions set in the message queue, shared memory, and semaphore, have user access settings, settings with access to files, such as files, such as (777) RWXRWXRWX) Use the command IPCS to see the message queue generated in the system, shared memory, and semaphore. The meaning is similar to file access. Just execution bits are invalid. In the name of the famous pipe and file, share memory The way the file defines the user's access rights. You can see that they can use the command LS -L to exist and have access to the right limit, and you can see the file type of the famous pipeline is P, file mode sharing memory file type. For s. (5) signal interrupt, the system calls the first-level effective system to consider the interrupt when the design system is called. Problem. When the process runs to a system call, the process enters the interrupt process. After the processing is complete, the process will skip the system call to enter the next program directive. It should be noted that the interrupt occurs in system call Level 1 instead of subroutine or function level. The timeout interrupt is set before a subscriber is called in a subroutine, and the system is interrupted after the subroutine is interrupted. The instruction after the program is interrupted, instead of continuing processing from the latter instruction to call the subroutine directive. (6) Features of various IPC mode. Message queue:

Define and generate a message queue through the message queue. Any process can access the message queue as if there is access to the message. The message queue is listed as a memory block mode. The length of the message element in the message queue can be anywhere in the system parameter limit. Length. The message element is classified by the message type, and its access is accessed by type. You must obtain a message queue identifier before a read and write operation, that is, access to the access relationship. A message in the message queue is After reading, you can delete it from the queue. The message queue has a lock mechanism process, that is, a process is not accessible when the process is access. The system resources and efficiency should be paid to the operation. When the permission is allowed, the message queue information is bidirectional Shared Memory defines and generates shared memory by shared memory. Any process can access shared memory as long as access rights and know Key. Sharing memory data segment. Shared memory can be systematically Any length within the parameter limit. Access to the shared memory is the same as the access method of the group. After obtaining the shared memory identifier, the shared memory is coupled to the process data segment, you can start reading and write, and do it after all operations are completed Shared memory and process data segment is detached to complete all shared memory access processes. The data in the shared memory will not disappear after the data is read. Shared memory has no lock mechanism processing, that is, multiple processes may be accessed at the same time. The same data unit shared memory. The shared memory is used to operate with the signal to have a lock mechanism to ensure that the data is consistent. When permission is allowed, the information transfer of shared memory is two-way.. Sample In generating lock mechanisms, avoiding data inconsistency. No other data information. No parent and child relationship or brother relationship. The signal signal is defined by the system. The transmission of the signal can be permissible. The signal is an event-occurring information flag without other information. The signal does not have a data block. The signal processing can be defined by the user. The signal may be issued by the user process, the operating system (software or hardware reason). Some signals are It is not possible to be shielded. The signal is interrupted is a function of the system call level. The information transfer of the signal is unidirectional. The pipeline is a special device file for the system, which can be a memory method, or a deposit method. Pipeline Transmission is generally unidirectional, that is, one pipelines, if two processes need to do two-way passwords require 2 pipes. There are two ends when the pipe is generated, one end is read, one end is written, two processes should be coordinated, A process read from the read party, another process writes to the write side. The read and write function of the pipeline uses the flow device, namely:

转载请注明原文地址:https://www.9cbs.com/read-41691.html

New Post(0)