Linux Process Communication-semaphore usage-Linux general technology-Linux programming and kernel information. The following is a detailed description. I. Related Knowledge
Semaphores: an integer;
The number of resource entities that tables can be used by concurrent processes when the value is greater than or equal to 0;
Number of processes waiting for using the critical section in the age of less than 0;
The initial semaphore value used for mutex must be greater than 0;
It can only be changed through P and V primitive operations;
Semaphore element composition:
1. The value of the semaphore element;
2. process ID of the final semaphore Element
3. The number of processes with the semaphore element value + 1;
4. Number of processes whose semaphores element value is 0;
Ii. Main Functions
1.1 create semaphores
Int semget (
Key_t key, // The key to identify the semaphore, there are three methods: 1, the use of IPC--PRIVATE for the system to produce,
// 2. Select a random number. 3. Use ftok to generate a random number from the file path.
Int nSemes, // number of elements in the semaphore set
Int flag // IPC_CREAT; IPC_EXCL is created only when the semaphore set does not exist
)
Success: return the semaphore handle.
Failed:-1 is returned.
1.2 use the ftok function to generate a keyword Based on the file path name
Key_t ftok (const char * pathname, int proj_id );
The path name must have the corresponding Permissions
1.3 control semaphores
Int semctl (
Int semid, // semaphore set handle
Int semnum, // number of elements in the semaphore set
Int cmd, // command
/* Union senum arg */...//
)
Success: the corresponding value is returned.
Failed:-1 is returned.
Command details:
Cmd: IPC_RMID: deletes a semaphore.
IPC_EXCL is created only when the semaphore set does not exist.
IPC_SET: Set the semaphore permission
SETVAL: set the value of the element of the specified semaphore to agc. val.
GETVAL obtains the value of a specified semaphore.
GETPID obtains the final process ID that finally controls this element.
GETNCNT obtains the number of processes that wait for the element to change to 1.
GETZCNT obtains the number of processes that wait for the element to change to 0.
Union senum is defined as follows:
Union senum {
Int val;
Struct semid_ds * buf;
Unsigned short * array;
} Agc;
Semid_ds is defined as follows:
Struct semid_ds {
Struct ipc_pem sem_pem; // operation pemission struct
Time_t sem_otime; // last semop () time
Time_t sem_ctime; // last time changed by semctl ()
Struct sem * sembase; // ptr to first semaphore in array
Struct sem_queue * sem_pending; // pending operations
Struct sem_queue * sem_pending_last; // last pending operations
Struct sem_undo * undo; // undo requests on this arrary
Unsigned short int sem_nsems; // number of semaphores in set
};
1.4 semaphore + 1 or-1 or test whether it is 0
Int semop (
Int semid,
Struct sembuf * sops, // point to the element struct ?? BR> unsigned short nsops // Number of element operations in the array
)
Structure sembuf Definition
Sembuf {
Short int sem_num; // semaphore number
Short int sem_op; // semaphore operaion
Short int sem_flg // operation flag
};
Iii. Example:
2.1 servers
# Include
# Include
# Derefined SEGSIZE 1024
# Define READTIME 1
Union semun {
Int val;
Struct semid_ds * buf;
Unsigned short * array;
} Arg;
// Generate a semaphore
Int sem_creat (key_t key)
{
Union semun sem;
Int semid;
Sem. val = 0;
Semid = semget (key, 1, IPC_CREAT | 0666 );
If (-1 = semid ){
Printf ("create semaphore error \ n ");
Exit (-1 );
}
Semctl (semid, 0, SETVAL, sem );
Return semid;
}
// Delete semaphores
Void del_sem (int semid)
{
Union semun sem;
Sem. val = 0;
Semctl (semid, 0, IPC_RMID, sem );
}
// P
Int p (int semid)
{
Struct sembuf sops = {0, + 1, IPC_NOWAIT };
Return (semop (semid, & sops, 1 ));
}
// V
Int v (int semid)
{
Struct sembuf sops = {0,-1, IPC_NOWAIT };
Return (semop (semid, & sops, 1 ));
}
Int main ()
{
Key_t key;
Int shmid, semid;
Char * shm;
Char msg [7] = "-data -";
Char I;
Struct semid_ds buf;
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.