1437 views|0 replies

164

Posts

0

Resources
The OP
 

i.MX6ULL development board thread synchronization POSIX unnamed semaphore [Copy link]

To ensure that each thread has valid access to the same resource, for example, if a thread wants to read data from a shared resource, and these resources are being modified by other threads, then the read data is invalid. Then we need to find a way to let other threads modify them before reading them. This is when the synchronization mechanism is used. The mechanism provided by the Linux system can be used to synchronize the order in which threads access resources. This document selects semaphores, mutexes, and conditional variables to introduce the thread synchronization mechanism. The experimental code is in the sync/directory.

1 POSIX unnamed semaphores
This chapter introduces POSIX unnamed semaphores, hereinafter referred to as semaphores. Semaphores are similar to counters, and the operation method is basically the same as the previous System V semaphores.
Steps to use semaphores:
1. Define semaphores in the program global area;
2. Use seminit() to initialize the semaphore;
3. Use sem_wait() and sem_post() to perform P/V operations on the semaphore;
4. Use sem_destroy() to destroy the semaphore.

Common functions for semaphores are as follows:
sem_init(): Initialize the semaphore value,
#include
int sem_init(sem_t *sem, int pshared, unsigned int value);
Parameter meaning:
sem: pointer, pointing to the defined semaphore;
pshared: indicates whether this semaphore is used for process or thread, 0 for thread, not equal to 0 for process, fill in 0 in this section.
value: initial value of semaphore
Return value: 0 for success, -1 for error.

sem_wait(): Wait for a semaphore, perform P operation, semaphore value -1;
sem_post(): Wake up a semaphore, perform V operation, semaphore value +1, defined as follows:
#include
int sem_wait(sem_t *sem);
int sem_post(sem_t *sem);
Parameter meaning:
sem: semaphore to be operated;
Return value: 0 for success, -1 for error.

sem_destroy(): Destroy the initialized semaphore
#include
int sem_destroy(sem_t *sem);
sem: The semaphore to be operated;
Return value: 0 for success, -1 for error.

The experimental code is in sync/sem.c: The path is: 11_Linux System Development Advanced\Linux System Programming_Chapter Usage Materials.
Use semaphores to control the read and write threads. When initialized, the write semaphore is 1 and the read semaphore is 0. Then the read thread will be blocked, and the write thread will execute and set the write signal -1. The write thread waits for input in fgets. When the input is completed, the read signal +1 will wake up the read thread. In this way, the read and write threads are executed alternately to complete the synchronization.




This post is from ARM Technology
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list