Explain what is embedded system in a computer system?
An embedded system in a computer system is part of a larger system or machine. It is a system with a dedicated function within a larger electrical or mechanical system.
Mention what are the essential components of embedded system?
Essential components of embedded system includes
- I/O circuits
- System application specific circuits
- It ensures the availability of System Memory
- It checks the Processor Speed availability
- The need to limit power lost when running the system continuously
Real Time Operating System
- It runs a process as per scheduling and do the switching from one process to another
Mention how I/O devices are classified for embedded system?
The I/O devices of embedded system are classified into two categories
|Synchronous :||Audio/Video signal||Audio/Video signal|
|Asynchronous :||Keypad, Mouse, Modem||Printer, Modem|
|Single bit :||Rotation, Threshold sensors||Pulses to external circuit|
|Multi bit :||Vp from ADC, sensors||LCD, Printer|
Why embedded system is useful?
With embedded system, it is possible to replace dozens or even more of hardware logic gates, input buffers, timing circuits, output drivers, etc. with a relatively cheap microprocessor.
Explain what are real-time embedded systems?
Real-time embedded systems are computer systems that monitor, respond or control an external environment. This environment is connected to the computer system through actuators, sensors, and other input-output interfaces.
Explain what is microcontroller?
The microcontroller is a self-contained system with peripherals, memory and a processor that can be used as embedded system.
Mention what is the difference between microprocessor and microcontroller?
Microprocessor is managers of the resources (I/O, memory) which lie outside of its architecture
Microcontroller have I/O, memory, etc. built into it and specifically designed for control
What does DMA address will deal with?
DMA address deals with physical addresses. It is a device which directly drives the data and address bus during data transfer. So, it is purely physical address.
Explain what is interrupt latency? How can you reduce it?
Interrupt latency is a time taken to return from the interrupt service routine post handling a specific interrupt. By writing minor ISR routines, interrupt latency can be reduced.
Mention what are buses used for communication in embedded system?
For embedded system, the buses used for communication includes
- I2C: It is used for communication between multiple ICs
- CAN: It is used in automobiles with centrally controlled network
- USB: It is used for communication between CPU and devices like mouse, etc.
While ISA, EISA, PCI are standard buses for parallel communication used in PCs, computer network devices, etc.
List out various uses of timers in embedded system?
Timers in embedded system are used in multiple ways
- Real Time Clock (RTC) for the system
- Initiating an event after a preset time delay
- Initiating an even after a comparison of preset times
- Capturing the count value in timer on an event
- Between two events finding the time interval
- Time slicing for various tasks
- Time division multiplexing
- Scheduling of various tasks in RTOS
Explain what is a Watchdog Timer?
A watchdog timer is an electronic device or electronic card that execute specific operation after certain time period if something goes wrong with an electronic system.
Explain what is the need for an infinite loop in embedded systems?
Embedded systems require infinite loops for repeatedly processing or monitoring the state of the program. For instance, the case of a program state continuously being verified for any exceptional errors that might just happen during run-time such as memory outage or divide by zero, etc.
List out some of the commonly found errors in Embedded Systems?
Some of the commonly found errors in embedded systems are
- Damage of memory devices static discharges and transient current
- Address line malfunctioning due to a short in circuit
- Data lines malfunctioning
- Due to garbage or errors some memory locations being inaccessible in storage
- Inappropriate insertion of memory devices into the memory slots
- Wrong control signals
Explain what is semaphore?
A semaphore is an abstract datatype or variable that is used for controlling access, by multiple processes to a common resource in a concurrent system such as multiprogramming operating system. Semaphores are commonly used for two purposes
- To share a common memory space
- To share access to files
Explain what is the difference between mutexes and semaphores?
|A mutex object enables one thread into a controlled section, forcing other threads which tries to gain access to that section to wait until the first thread has moved out from that section||Semaphore allows multiple access to shared resources|
|Mutex can only be released by thread which had acquired it||A semaphore can be signaled from any other thread or process.|
|Mutex will always have a known owner||While for semaphore you won’t know which thread we are blocking on|
|Mutex is also a tool that is used to provide deadlock-free mutual exclusion (either consumer or producer can have the key and proceed with their work)||Semaphore is a synchronization tool to overcome the critical section problem|
|Mutexes by definition are binary semaphores, so there are two states locked or unlocked||Semaphores are usually referred to counted locks|
When one must use recursion function? Mention what happens when recursion functions are declared inline?
Recursion function can be used when you are aware of the number of recursive calls is not excessive. Inline functions property says whenever it will called, it will copy the complete definition of that function. Recursive function declared as inline creates the burden on the compilers execution.
Explain whether we can use semaphore or mutex or spinlock in interrupt context in Linux Kernel?
Semaphore or Mutex cannot be used for interrupt context in Linux Kernel. While spinlocks can be used for locking in interrupt context.
What is Semaphore?
Semaphore is simply a variable that is non-negative and shared between threads. A semaphore is a signaling mechanism, and a thread that is waiting on a semaphore can be signaled by another thread. It uses two atomic operations, 1)wait , and 2) signal for the process synchronization.
A semaphore either allows or disallows access to the resource, which depends on how it is set up.
What is Mutex?
The full form of Mutex is Mutual Exclusion Object. It is a special type of binary semaphore which used for controlling access to the shared resource. It includes a priority inheritance mechanism to avoid extended priority inversion problems. It allows current higher priority tasks to be kept in the blocked state for the shortest time possible. However, priority inheritance does not correct priority- inversion but only minimizes its effect.
What are key differences between Mutex and Semaphore ?
- Mutex is a locking mechanism whereas Semaphore is a signaling mechanism
- Mutex is just an object while Semaphore is an integer
- Mutex has no subtype whereas Semaphore has two types, which are counting semaphore and binary semaphore.
- Semaphore supports wait and signal operations modification, whereas Mutex is only modified by the process that may request or release a resource.
- Semaphore value is modified using wait () and signal () operations, on the other hand, Mutex operations are locked or unlocked.
Use of Semaphore
In the case of a single buffer, we can separate the 4 KB buffer into four 1 KB buffers. Semaphore can be associated with these four buffers. This allows users and producers to work on different buffers at the same time.
Use of Mutex
A mutex provides mutual exclusion, which can be either producer or consumer that can have the key (mutex) and proceed with their work. As long as producer fills buffer, the user needs to wait, and vice versa. In Mutex lock, all the time, only a single thread can work with the entire buffer.
Differences between Semaphore vs. Mutex
|Mechanism||It is a type of signaling mechanism.||It is a locking mechanism.|
|Data Type||Semaphore is an integer variable.||Mutex is just an object.|
|Modification||The wait and signal operations can modify a semaphore.||It is modified only by the process that may request or release a resource.|
|Resource management||If no resource is free, then the process requires a resource that should execute wait operation. It should wait until the count of the semaphore is greater than 0.||If it is locked, the process has to wait. The process should be kept in a queue. This needs to be accessed only when the mutex is unlocked.|
|Thread||You can have multiple program threads.||You can have multiple program threads in mutex but not simultaneously.|
|Ownership||Value can be changed by any process releasing or obtaining the resource.||Object lock is released only by the process, which has obtained the lock on it.|
|Types||Types of Semaphore are counting semaphore and binary semaphore.||Mutex has no subtypes.|
|Operation||Semaphore value is modified using wait () and signal () operation.||Mutex object is locked or unlocked.|
|Resources Occupancy||It is occupied if all resources are being used and the process requesting for resource performs wait () operation and blocks itself until semaphore count becomes >1.||In case if the object is already locked, the process requesting resources waits and is queued by the system before lock is released.|
Common Facts about Mutex and Semaphore
Here, are few common facts about Mutex and Semaphore:
- Only one task can acquire the mutex. So, there is ownership associated with a mutex, and only the owner can release the mutex.
- The reasons for using mutex and semaphore are different maybe because of similarity in their implementation, a mutex would be referred to as binary semaphore.
- One highly known misconception is that Mutexes and Semaphores are almost same, with the only difference being that a Mutex is capable of counting to 1, while Semaphores able to count from 0 to N.
- There is always uncertainty between binary semaphore and mutex. You may hear that a mutex is a binary semaphore, which is not correct.
Advantages of Semaphore
Here, are pros/benefits of using Semaphore:
- It allows more than one thread to access the critical section
- Semaphores are machine-independent.
- Semaphores are implemented in the machine-independent code of the microkernel.
- They do not allow multiple processes to enter the critical section.
- As there is busy waiting in semaphore, there is never a wastage of process time and resources.
- They are machine-independent, which should be run in the machine-independent code of the microkernel.
- They allow flexible management of resources.
Advantages of Mutex
Here, are important pros/benefits of Mutex
- Mutexes are just simple locks obtained before entering its critical section and then releasing it.
- Since only one thread is in its critical section at any given time, there are no race conditions, and data always remain consistent.
Disadvantage of Semaphores
Here, are cons/drawback of semaphore
- One of the biggest limitations of a semaphore is priority inversion.
- The operating system has to keep track of all calls to wait and signal semaphore.
- Their use is never enforced, but it is by convention only.
- In order to avoid deadlocks in semaphore, the Wait and Signal operations require to be executed in the correct order.
- Semaphore programming is a complex method, so there are chances of not achieving mutual exclusion.
- It is also not a practical method for large scale use as their use leads to loss of modularity.
- Semaphore is more prone to programmer error.
- It may cause deadlock or violation of mutual exclusion due to programmer error.
Disadvantages of Mutex
Here, are cons/drawback of Mutex
- If a thread obtains a lock and goes to sleep or it is preempted, then the other thread may not able to move forward. This may lead to starvation.
- It can't be locked or unlocked from a different context than the one that acquired it.
- Only one thread should be allowed in the critical section at a time.
- The normal implementation may lead to busy waiting state, which wastes CPU time.
Mutex vs SpinLock
In theory, when a thread tries to lock a mutex and it does not succeed, because the mutex is already locked, it will go to sleep, immediately allowing another thread to run. It will continue to sleep until being woken up, which will be the case once the mutex is being unlocked by whatever thread was holding the lock before. When a thread tries to lock a spinlock and it does not succeed, it will continuously re-try locking it, until it finally succeeds; thus it will not allow another thread to take its place (however, the operating system will forcefully switch to another thread, once the CPU runtime quantum of the current thread has been exceeded, of course).
The problem with mutexes is that putting threads to sleep and waking them up again are both rather expensive operations, they'll need quite a lot of CPU instructions and thus also take some time. If now the mutex was only locked for a very short amount of time, the time spent in putting a thread to sleep and waking it up again might exceed the time the thread has actually slept by far and it might even exceed the time the thread would have wasted by constantly polling on a spinlock. On the other hand, polling on a spinlock will constantly waste CPU time and if the lock is held for a longer amount of time, this will waste a lot more CPU time and it would have been much better if the thread was sleeping instead.
Using spinlocks on a single-core/single-CPU system makes usually no sense, since as long as the spinlock polling is blocking the only available CPU core, no other thread can run and since no other thread can run, the lock won't be unlocked either. IOW, a spinlock wastes only CPU time on those systems for no real benefit. If the thread was put to sleep instead, another thread could have ran at once, possibly unlocking the lock and then allowing the first thread to continue processing, once it woke up again.
On a multi-core/multi-CPU systems, with plenty of locks that are held for a very short amount of time only, the time wasted for constantly putting threads to sleep and waking them up again might decrease runtime performance noticeably. When using spinlocks instead, threads get the chance to take advantage of their full runtime quantum (always only blocking for a very short time period, but then immediately continue their work), leading to much higher processing throughput.
Since very often programmers cannot know in advance if mutexes or spinlocks will be better (e.g. because the number of CPU cores of the target architecture is unknown), nor can operating systems know if a certain piece of code has been optimized for single-core or multi-core environments, most systems don't strictly distinguish between mutexes and spinlocks. In fact, most modern operating systems have hybrid mutexes and hybrid spinlocks. What does that actually mean?
A hybrid mutex behaves like a spinlock at first on a multi-core system. If a thread cannot lock the mutex, it won't be put to sleep immediately, since the mutex might get unlocked pretty soon, so instead the mutex will first behave exactly like a spinlock. Only if the lock has still not been obtained after a certain amount of time (or retries or any other measuring factor), the thread is really put to sleep. If the same code runs on a system with only a single core, the mutex will not spinlock, though, as, see above, that would not be beneficial.
A hybrid spinlock behaves like a normal spinlock at first, but to avoid wasting too much CPU time, it may have a back-off strategy. It will usually not put the thread to sleep (since you don't want that to happen when using a spinlock), but it may decide to stop the thread (either immediately or after a certain amount of time) and allow another thread to run, thus increasing chances that the spinlock is unlocked (a pure thread switch is usually less expensive than one that involves putting a thread to sleep and waking it up again later on, though not by far).
If in doubt, use mutexes, they are usually the better choice and most modern systems will allow them to spinlock for a very short amount of time, if this seems beneficial. Using spinlocks can sometimes improve performance, but only under certain conditions and the fact that you are in doubt rather tells me, that you are not working on any project currently where a spinlock might be beneficial. You might consider using your own "lock object", that can either use a spinlock or a mutex internally (e.g. this behavior could be configurable when creating such an object), initially use mutexes everywhere and if you think that using a spinlock somewhere might really help, give it a try and compare the results (e.g. using a profiler), but be sure to test both cases, a single-core and a multi-core system before you jump to conclusions (and possibly different operating systems, if your code will be cross-platform).