In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/03 Report--
The principle does not say much, may check out the unix advanced environment programming, only mention a few points of attention
Semaphore system limit
Maximum semaphore value 32767 maximum semaphore set 128each set contains up to 250semaphore
What is difficult to understand is the flag bit SEM_UNDO
The following is my humble opinion:
The first is to understand the "semaphore adjustment value". Each semaphore should have an adjustment value and be maintained when the semaphore is operated. If SEM_UNDO is set, it will be updated immediately. If there is no update, I don't know. Maybe it's the system scheduling.
When exiting, whether it is normal or abnormal, the kernel will traverse the adjustment value of the process once and deal with it, but how to deal with it needs to be studied.
Personal experience: if blocking is needed in V and P operations, it is best to assign SEM_UNDO on the flag bit, and if there is no blocking, it is not necessary, so there will be fewer unexpected errors.
Online explanation:
Each separate semaphore operation may need to maintain an adjustment action. Linux is at least for each process
Each semaphore array maintains a sem_undo data structure. If the requested process does not have one, create one for it when needed. This new sem_undo data structure is queued in both the process's task_struct data structure and the semaphore queue's semid_ds data structure. When an operation is performed on a semaphore in the semaphore queue, the value offset by this operation value is added to the entry of the semaphore in the adjustment queue of the process's sem_undo data structure. So, if the operation value is 2, then this adds-2 to the adjustment entry of the semaphore. When a process is deleted, such as exiting, Linux iterates through its sem_undo data structure group and implements adjustments to the semaphore array. If you delete a semaphore, its sem_undo data structure remains in the task_struct queue of the process, but the corresponding semaphore array identifier is marked as invalid. In this case, the code to clear the semaphore simply discards the sem_undo data structure.
Http://3521632.blog.163.com/blog/static/110237933201032041353708/
The following routines
First: first, perform init.c initialization so that the first signal value of the semaphore set is 1, and of course the computer index starts at 0.
Second: execute multiple test.c
Intit.c
# include # include int open_semque (void); void set_sem (int, int); # if 1
Union semun {int val;struct semid_ds buf;unsigned short * array;}; # endifint main (int argc,char** argv) {int ret;int semque_id;semque_id = open_semque (); set_sem (semque_id, 1); ret = semctl (semque_id, 0, GETVAL); printf ("the value of the first semaphore in the set is:% d\ n", ret); return 0;} int open_semque (void) {
/ / create a key key_t key = ftok (". /", 100); if (key = =-1) {perror ("ftok\ n"); exit (1);} / / create a semaphore set, open if / / IPC_CREAT exists, or create a new one if it does not exist. / / 0666 is the permission / / set this semaphore set contains several semaphores, set to 1int semque_id = semget (key, 1Personalized creat | 0666) If (semque_id = =-1) {perror ("msgget\ n"); exit (1);} return semque_id;} void set_sem (int semque_id, int val) {/ / this consortium needs to be created by itself, which is needed when initializing semaphores. Union semun sem_union;sem_union.val = val;if (semctl (semque_id, 0, SETVAL, sem_union) =-1) {perror ("semctl_set\ n"); exit (1);}}
Test.c
# include # include void v_sem (int); void p_sem (int); int open_semque (void); void set_sem (int, int); # if 1union semun {int val;struct semid_ds buf;unsigned short * array;}; # endifint main (int argc,char** argv) {int ret;int semque_id;semque_id = open_semque (); / / set_sem (semque_id, 1); ret = semctl (semque_id, 0, GETVAL) Printf ("the value of the first semaphore in the semaphore is:% d\ n", ret); fputs ("execute operation V\ n", stdout); v_sem (semque_id); ret = semctl (semque_id, 0, GETVAL); printf ("the value of the first semaphore in the semaphore is:% d\ n", ret); int second = 10 while position (second) {sleep (1); printf ("% ds\ n", second); ret = semctl (semque_id, 0, GETVAL) Printf ("the value of the first semaphore in the semaphore is:% d\ n", ret); second--;} fputs ("perform operation P\ n", stdout); p_sem (semque_id); ret = semctl (semque_id, 0, GETVAL); printf ("the value of the first semaphore in the semaphore is:% d\ n", ret); / / semctl (semque_id, 0, IPC_RMID); return 0 } int open_semque (void) {key_t key = ftok (". /", 100); if (key = =-1) {perror ("ftok\ n"); exit (1);} int semque_id = semget (key, 1 last IPC resume | 0666); if (semque_id = =-1) {perror ("msgget\ n"); exit (1);} return semque_id;} void set_sem (int semque_id, int val) {union semun sem_union;sem_union.val = val If (semctl (semque_id, 0, SETVAL, sem_union) = =-1) {perror ("semctl_set\ n"); exit (1);}} void p_sem (int semque_id) {struct sembuf arry [1]; arry [0] .sem _ num = 0There arry [0] .sem _ op = 1There is an int semque_id arry [0] .sem _ flg = SEM_UNDO;int ret. / / need to assign if (ret = semop (semque_id, arry, 1)) {perror ("semop\ n"); exit (1);}} void v_sem (int semque_id) {struct sembuf arry [1]; arry [0] .sem _ num = 0Tracterarry [0] .sem _ op =-1 void v_sem Universe [0] .sem _ flg = IPC_NOWAIT;arry [0] .sem _ flg = SEM_UNDO;int ret If (ret = semop (semque_id, arry, 1)) {perror ("semop\ n"); exit (1);}}
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.