The signal quantity is mutually exclusive, Wang Ming learns learn

Source: Internet
Author: User

The signal volume is mutually exclusive

Semaphore (AKA: Semaphore) is not the same as other process communication, the main purpose is to protect critical resources (process mutex). The process can determine, based on it, whether certain shared resources can be accessed. In addition to access control, it can also be used for process synchronization.

First, the signal volume classification

Two-value beacon: The value of the semaphore can only be 0 or 1

Count semaphore: The value of the semaphore can be arbitrary non-negative.

Second, function learning

2.1 Creating/Opening semaphores

Name of 2.1.1 function

Semget

2.1.2 Function prototype

int Semget (key_t key,int nsems,int SEMFLG);

2.1.3 Function function

Get Semaphore Set Identifier

When the semaphore specified by key does not exist, and SEMFLG contains Ipc_creat, a semaphore set is created.

2.1.4 Owning header file

<sys/types.h> <sys/ipc.h> <sys/sem.h>

2.1.5 return value

Success: Returns the representation of the semaphore collection

Failed:-1

2.1.6 parameter Description

Key: Value

Nsems: The number of semaphores contained within the set of semaphores created

SEMFLG: Flag, can take ipc_creat

2.2 Specifying Key values

The system establishes an IPC communication (interprocess communication) and must specify an ID value.

1. Arbitrarily specify a number

Disadvantage: This number has been used by other IPC objects (Message Queuing, shared memory) and will fail when associated with the newly created semaphore.

2. Construct a number that will not be used by other IPC objects

Method: Use key_t ftok (char * fname, int id)

Name of 2.2.1 function

Ftok

2.2.2 Function prototype

key_t Ftok (char * fname, int id)

2.2.3 function function

Construct a number that will not be used by other IPC objects

2.2.4 Owning header file

<sys/types.h><sys/ipc.h>

2.2.5 return value

Key value

2.2.6 parameter Description

FName is the specified filename (the file name already exists)

ID is a sub-ordinal. Although it is of type int, only 8bits (1-255) is used.

2.3 Operating Signal Volume

Name of 2.3.1 function

Semop

2.32 function prototype

int semop (int semid,struct sembuf *sops,unsigned nsops);

2.3.3 Function function

The amount of semaphore in the set of Operation Semaphores

2.3.4 Owning header file

<sys/types.h><sys/ipc.h><sys/sem.h>

2.3.5 return value

Success: 0

Failed:-1

2.3.6 parameter Description

int Semid: The identifier of the semaphore set to manipulate

struct SEMBUF *sops: to manipulate multiple semaphores

Unsigned nsops: What to do with the semaphore

The signal quantity is mutually exclusive, Wang Ming learns learn

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.