Inter-process communication between Linux-signal volume

Source: Internet
Author: User

Semaphore (semaphore) set

In a multitasking operating system environment, multiple processes or threads run concurrently, and multiple tasks may work together in order to accomplish the same goal, creating a synchronization relationship between tasks, as well as competing between tasks in order to compete for limited system resources (hardware or software resources). This is the mutex between the tasks.

The source of synchronization and mutual exclusion between tasks is the critical resource. A critical resource is a resource that allows only limited (usually only one) tasks to access (read) or modify (write) at the same time, usually including hardware resources and software resources, and code that accesses critical resources is called a critical section.

The use of anonymous semaphores in the synchronization of POSIX threads
Inter-process synchronization using IPC objects [semaphore set]

Beacon set: A set of lights, each semaphore can be used to represent a class of resources, whose value represents the number of resources

Where the semaphore (semaphore set) corresponds to a certain resource, take a nonnegative integer value, the semaphore value refers to the current number of available resources, if it is 0 this means that there is currently no available resources

1. Get the key value

The first type:
key_t Ftok (const char *pathname, int proj_id);
Parameters:
@pathname a file path that already exists
@proj_id gets the low 8bit of this integer
return value:
Successful return key value, failure return-1


The second type:
Specify the key value as ipc_private when the IPC object communicates between the affinity processes

(1) Create a semaphore set
int Semget (key_t key, int nsems, int semflg);
Parameters:
@key ipc_private, Ftok ()
Number of lights in the @nsems signal set
@semflg Ipc_creat | 0666,ipc_creat | Ipc_excl
return value:
Successful return ID, failure return-1

(2) Initialize the value of the signal set signal

int semctl (int semid, int semnum, int cmd, Union semun Arg);
Parameters:
ID of @semid Semaphore set
Number of @semnum lights [numbering starting from 0]
@cmd setval[Set the value of the semaphore], getval (get the value of the semaphore), ipc_rmid[delete the semaphore set]
return value:
Successful return 0, failure return-1

Think: Initialize signal 1th in the beacon set to 1?

Where ARG: is the Union semnum structure, may not give the definition of the structure in some systems, it must be defined by the programmer

Union Semun {
intval;/* Value for Setval */
struct Semid_ds *buf;/* Buffer for Ipc_stat, Ipc_set */
Unsigned short *array;/* array for GETALL, SETALL */
struct Seminfo *__buf;/* Buffer for Ipc_info (linux-specific) */
};

Initializes the specified numbered Sem_num semaphore set to value

void Init_sem_value (int sem_id,int sem_num,int value)
{
Union Semun Sem_val;

Sem_val.val = value;

if (Semctl (Sem_id,sem_num,setval,sem_val) < 0)
{
...
}

return;
}

Specific definitions of PV atomic operations:

P Operation: If there is a resource available (semaphore value greater than 0), then occupy one resource (semaphore value minus one, enter the critical section code); If no resource is available (the signal value equals 0), it will be blocked until the system assigns the resource to the task (into the waiting queue, until the resource is awakened)

V Operation: If the semaphore has a task waiting for a resource in the wait queue, wake up a blocking task and release a resource if no task waits for him (semaphore value plus 1)

(3) PV operation
int semop (int semid, struct sembuf *sops, unsigned nsops);
Function: Complete PV operation
Parameters:
ID of @semid Semaphore set
@sops How to do structure body first address
Number of @nsops operation lights
return value:
Successful return 0, failure return-1

struct SEMBUF
{
unsigned short sem_num; /* semaphore number, semaphore numbers, when using a single semaphore, the value is usually 0*/
Short Sem_op; /* Semaphore Operation Semaphore operation, take 1 for P operation, take +1 for v operation */
Short SEM_FLG; /* Operation flags are typically set to Sem_undo so that when the process does not release a semaphore, the system automatically frees the amount of semaphores that are not released in the process */
};

Sem_op:
<1>0 wait for the semaphore to change to 0.
<2>1 Releasing resources, v operation
<3>-1 application resources, p operations

SEM_FLG:
0: Blocking mode
Ipc_nowait: Non-blocking method invocation
Sem_undo: At the end of the process, the resource it requested is automatically released

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

The p/v operation package is as follows:
void P (int sem_id,int sem_num)
{
struct SEMBUF sem;

Sem.sem_num = Sem_num;
Sem.sem_op =-1;
SEM.SEM_FLG = 0;

if (Semop (sem_id,&sem,1) < 0)
{
....
}
}

void V (int sem_id,int sem_num)
{
struct SEMBUF sem;

Sem.sem_num = Sem_num;
Sem.sem_op = 1;
SEM.SEM_FLG = 0;

if (Semop (sem_id,&sem,1) < 0)
{
....
}
}

Inter-process communication between Linux-signal volume

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.