Process and signal and IPC Mechanism: help me remember

Source: Internet
Author: User

I. Signal

* <Signal. h> <sys/type. h> <unistd. h>

* Int sigaction (SIGINT, & act, 0); Act struct: struct sigaction Act; {act. sa_handler = ABC; sigemptyset (& act. sa_mask); Act. sa_flags = 0 ;}

* Int kill (pid_t PID, int sig) unsigned int alarm (unsigned int seconds); pause () getppid () getpid () void Ding (ing sig) {} This is the signal registration function. Pid_t waitpid () fork () = 0 sub-process-1 failed system () Wait () parent wait Child

--------------------------------------------------------

II. Inter-process communication: Pipelines

File = popen (command, R or W); fwrite reads fread writes through file

FIFO first-in-first-out lifo, first-in-first-out, pipe call (file [2]): No need to start a shell, ---> read/write communication between parent and child processes, DUP () dup2 () FIFO file

Pipelines are congested-Process Synchronization and Data bidirectional transmission.

----------------------------------------------------------

3. IPC Mechanism

I. Semaphores: 0 and 1: Binary semaphores multiple positive integers: Common semaphores

* Linux semaphore functions operate on a group of common semaphores.

* <Sys/SEM. h>

* Int semget (key_t key, int num_sems, int sem_flags); create a semaphore or obtain an existing semaphore.

Parameter 1: Give a random number as the key. Parameter 2: Number of signals: generally 1 parameter 3: IPC _) creat | 0666 ipc_excl

* Int semop (INT sem_id, struct sembuf * sem_ops, size_t num_sem_ops );

Used to change the semaphore value: + 1-1

Struct sembuf {

Short sem_num; semaphore number. Generally, 0 is used unless you use a set of semaphores.

Short sem_op; + 1 P operation-1 V Operation

Short sem_flg; generally set to sem_undo, the system must automatically clear the semaphore.

}

* Int semctl (INT sem_id, int sem_num, int command,...); controls the semaphore Function

Parameter 2: generally 0, except for a group. Parameter 3: Command: set or delete. If set, parameter 4 is a struct. That is, deprecato

Insert code: Page 493

The key lies in: It can be encapsulated into five functions, 1 get to create or obtain 2. initialize it 3. + 1 P operation function 4.-1 V Operation Function 5. Delete and clear it

------------------------------------------------------

II. Shared Memory: Synchronize access to the memory through small messages (synchronized by programmers ). (Make a small member in the shared memory to identify whether it is readable or can be written now .)

The functions used by shared memory are similar to semaphores. There are four basic functions:

* <Sys/SHM. h> four basic functions: shm_ I.

* Int shmget (key_t key, size_t size, int shmflg); create shared memory; parameter 2: memory size (bytes); parameter 3: Set the read/write permission ipc_creat | 0666

* Void * shmat (INT shm_id, const void * shm_addr, int shmflg); map physical memory to process memory

Parameter 2: NULL pointer. The system automatically selects address ing parameter 3: 0. The system selects a connection address.

* The Void shmdt (void *) parameter is the address pointer returned by shmat. Unmappings are not deleted. That is, the shared memory is unavailable for the current process.

* Int shmctl (INT shm_id, int command, sturct shmid_ds * BUF); shared memory control function: delete, set value.

(The deleted shared memory will not be deleted until it is detached from the last process .)

? Consumers and producers?

Insert code: Page 499 ------ shared memory 1. Message mark (): used to control synchronization 2. Shared data: used to share drops.

------------------------------------------------------

4. Message Queue: provides a simple and small way to transmit data between two unrelated processes.

* <Sys/msg. h> header file

* Int msgget (key_t key, int msg_flg); creates and accesses a message queue. Parameter 2: A special value and permission flag defined by ipc_creat can create a new message queue by or.

* Int msgsnd (INT msqid, const void * msg_ptr, size_t msg_sz, int msg_flg); Add the message to the message queue.

Parameter 2: Message pointer parameter 3: message length parameter 4: If the message queue is full,-1 is returned or blocking is returned. Wait until one is subtracted. So this is the identification space.

The message type should be similar to the following structure: struct my_msg {long int msg_type; char mydata [100];} the second member is a random type.

* Int msgrcv (INT msgid, void * msg_ptr, size_t msg_sz, long int msgtype, int msgflg); get the message

Parameter 2: pointer parameter for receiving the Message 3: message length parameter 4: Message Type 0> 0 <0 parameter 4: What should I do if there is no message?

* Int msgctl (INT msqid, int command, struct msqid _ DS * BUF); message queue control function, similar to shared memory function.

Insert code! Message Queue does not require a process to provide a synchronization method.

-----------------------------------------------------------------------

Big Data: using shared memory

Small Data: Message Queue

IPC command (shell command): used to clean up the system's free IPC Mechanism.

IPCS-s IPCS-m ipcs-Q; view semaphore shared memory Message Queue

Ipcrm-s ID ipcrm-m id ipcrm-q id; Delete the IPC Mechanism of the system game.

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.