200 Embedded and IoT Software Engineering Interview Questions – Part 4 Operating Systems
So far in part-1 of this interview questions series, we saw about
- Questions about yourself
- Questions about the projects you have done and
- Questions from C programming language
Then in part-2, we saw some questions about the Basics of electronics.
In part-3 we same some questions about Microcontrollers and peripherals.
You can find parts 1, 2 and 3 in the links below.
Now lets head onto the next part and in this part, we will look at some questions about Operating Systems. Many embedded systems are built on top of operating systems and hence a good understanding of how operating systems works is necessary if you need to write applications on them. So that’s what this part is going to be about.
As usual, I have divided the questions into 3 categories of easy medium and hard. So let’s begin!
Difficulty level: Easy
Question#1: What is the primary job of an operating system?
Answer#1: To virtualize and abstract the underlying hardware to make the system easier to use.
Question#2:Give the Functions of the kernel in an operating system
Answer#2: The kernel of an operating system is considered to be its core and it is responsible for the following
- Resource allocation
- Scheduling processes
- Memory management
- Device management and
- Device Security
Question#3: Explain Process Control Blocks
Answer#3: These are data structures that are used by the kernel to store information about tasks. They contain information about
- The ID of the task which is a unique number that’s allotted to it at the moment it was created
- Run time state data, which is basically the values in CPU registers such as program counter and stack pointers, so that when a process is restored to a running state this information can be restored.
- the control data of the task like how long it’s waiting in a queue, is it running or idle or waiting of I/O, etc.
Question#4: Expand and explain PID
PID stands for Process ID, it is the name given to a process that’s executing on a system. It is usually a number in most operating systems.
Question#5: Explain the functions of a device manager
Answer#5: Device manager is responsible for detecting the devices present in a system, making sure that proper drivers are used when communicating with the devices and managing power delivery to devices that are not self-powered.
If you have ever plugged a USB device into a computer, you would have probably noticed a pop-up saying the driver is being installed and the device is ready to use. That’s the device manager doing its job.
Question#6: Explain Multiprocessing/Multitasking
Answer#6: Multiprocessing/Multitasking refers to sharing of CPU time by several processes/tasks so that they can share the processing resource and run in a pseudo parallel manner. To give us the illusion that all the processes are running at the same time, operating systems use a scheduler to let the processes to share the processor time.
You can read more about it in this article about FreeRTOS.
Question#7: Explain Multithreading
Answer#7: Multithreading is one process getting divided into a number of threads so that it can compute faster as opposed to running in one single thread. For example, in almost all PC applications there is one thread responsible for the User Interface and another thread running in the background doing some processing and handling the business logic.
Question#8: How to detect data corruptions?
Answer#8: Checksums can be used to detect data corruption. Checksum Algorithms take in a file of any size as input and give out a fixed-length string called Checksum as output.
There are several of these algorithms available. The most famous ones include MD5 (Stands for Message Digest 5) and SHA1 (Secure Hashing Algorithm1)
The checksum can be computed every time a write operation takes place on the file so that when we read it again, we can see our records to ensure that a given file is not corrupted.
Question#9: What is an address space?
Answer#9: Address spaces are Operating system abstractions. It is the memory space that a process sees as it is executing on a system.
It is important to isolate processes so that they are not able to read or write to each other’s memory. To make this possible the computer scientists came up with this idea of providing each process with its own chunk of memory so that all the process’s data are living in that small chunk and the process is restricted to this space and has no idea what lies outside it.
This small chunk of memory is called its address space.
It looks something like this
As you can see in the picture above, the main parts of this address space include the code of the process, the stack, and the heap.
Question#10: Expand and explain CLI
Answer#10: CLI stands for Command Line Interface. It is a text-based user interface used to manage files and processes running on a computer. Some examples can include the PowerShell on windows PCs and Terminal on Linux and macOS.
Difficulty level: Medium
Question#1: Explain Cooperative Scheduling
Answer#1: In Cooperative Scheduling the process is in control of when to give up the CPU and give some other processes a chance to run. This is normally done when the entire software running on a system is designed by a single manufacturer and the software itself is a very simple one. Since there is no Context Switching, Cooperative Scheduling is more efficient as compared to preemptive scheduling. But it cannot be used on complex systems.
Question#2: Explain Preemptive scheduling
Answer#2: In preemptive scheduling, the scheduler decides which process to give the CPU to. So when the scheduler decides its time, it will preemptively take the CPU from a running process and store its state in memory and puts the process in the ready queue and then proceed to give the CPU to another task. When the time comes for the process to run again the state that was stored in memory is restored so that the task can continue from the point it was stopped in.
Even though preemptive scheduling is costlier than cooperative scheduling due to lots of context switching overheads it is better than cooperative scheduling because
- It can be used in complex systems
- Every task will get an equal chance to run
Hence most operating systems use some form of preemptive scheduling
Question#3: How does an application programmer communicate with an operating system?
Answer#3: Through system calls a.k.a the operating system API.
Operating systems virtualize the hardware and provide the application programmer with a set of functions called system calls, using which the application can talk indirectly to the underlying hardware.
Question#4: List some methods to do inter-process communication
Question#5: What are mutexes? Where are they used?
Mutex stands for mutual exclusion. It is an intertask communication object used when many processes are sharing a single resource to make sure only one process has access to a resource at any given time.
You can read more about it in this article on the section about Intertask communication.
Question#6: What is the difference between a physical address and a virtual address?
Answer#6: Physical address is the actual address on the memory chip that is being addressed. In contrast to this, the virtual address is the address the process thinks it is addressing. All processes can only see virtual memory which is part of their address spaces.
Question#7: Explain caching
Answer#7: Caching is the idea of storing frequently accessed data from the secondary memory in the primary memory to improve I/O speeds.
For example, consider a system whose job is to get data from a temperature sensor every second and store it in a file on the secondary flash memory.
Now if the file is directly accessed each time from flash, the I/O will take a long duration to do this since flash memory is very slow compared to DRAMs.This, in turn, will lead to inefficient use of the CPU as it will be sleeping most of the time waiting for I/O.
A better way to do this is by copying the entire file temporarily to the DRAM and then do all the reads and writes while its there and then finally when the system shuts down the updated file can be copied back to the flash memory.
Since DRAM is many times faster than Flash memory, the CPU will waste less time waiting on I/O and this will improve the efficiency of the system. This idea of keeping frequently used data on the RAM is known as caching. The memory allocated to hold this data is called cache memory.
In some systems which need fast performance, even DRAM will not be fast enough and Cache memory will be implemented on an SRAM (SRAM is approximately 4 times faster than DRAM) and frequently used data from DRAM will be stored on the SRAM.
Need something even faster than SRAM? We can use the CPU registers. Modern PC processors have several levels of high-speed memory inbuilt for this caching purpose. Thus there can be several levels of cache memory.
Question #8: What is a page fault?
Answer #8: If the primary memory(RAM) is full, then the operating system will move the least frequently used (or least recently used, depending on the policy used) pages into secondary memory (example, Harddrive, or in case of embedded systems, flash storage).
Now if a process that is running needs a page that was moved by the operating system to secondary memory, address translation from the virtual address to the physical address will fail and this condition is called a page fault.
Once this condition occurs the operating system will get involved and page-fault handler routine will take care of bringing those pages back from the secondary memory to the primary memory so that the process can continue to run. In case there is no space in the primary memory, the operating system will move another rarely used page to the secondary memory so that space can be provided for this page.
Question#9: Explain the several states a given process/task can be in
Answer#9: A task can be in one of the following states
- Suspended or
You can read more about this in this article on multitasking and changing states
Question#10: Give some design goals of an operating system.
Design goals may include
- Performance in the form of fewer overheads
- Security and protection by isolating the processes from each other and the operating system and
- Usability or Ease of Use by implementing intuitive user interfaces
Difficulty level: Hard
Question#1: Explain deadlock. How to prevent and recover from a deadlock once it has occurred?
Answer#1: Deadlock is a scenario where 2 processes are waiting for each other for some required resources and hence both of them are essentially stuck and unable to move forward.
For example, consider 2 processes process1 and process2. Process1 is having the mutex to resourceA and process2 is having the mutex to resourceB. Now, Process1 needs a mutex to resourceB to continue its operation and process2 needs access to resourceA. Since the mutexes are already busy, the scheduler puts both these processes in the ready queue. In the ready queue, both these processes will stay since they will never get the resource they need to continue. This is deadlock.
The only way to recover from a deadlock is by terminating one of those processes or by rebooting the entire system.
Best practice to avoid it is by taking all mutexes needed together instead of taking them one by one. That way the process can run and release the mutexes later once it’s done and then the other processes can use them thus avoiding deadlock.
Question#2: Give the 2 modes the CPU can execute processes in.
Answer#2: User mode and Kernel mode
User mode has restricted access. Typically user applications are run in user mode. Kernel-mode, on the other hand, is used by the operating system processes.
Question#3: Explain the role of a super-user in a UNIX based operating system
Answer#3: Superuser has the most privileges in a system. A normal user can go into superuser mode by typing the word sudo before entering any command on the CLI.
It will allow the person using the system to stop any process that’s running on the system, install apps, update the kernel, etc.
Question#4: Give 3 scheduling algorithms typically used by schedulers
- First in First Out
- Shortest Job First
- Shortest time to completion First
- Round Robin
Question#5: Give the name of the operating system call used to create new processes in UNIX based operating systems.
Answer#5: fork() call is used to create new processes in UNIX based operating system. The process invoking a fork() is called the parent process and the process being created is called the child process. The new process created will be a copy of the parent process in terms of its Process Control Block but later it can take over by rewriting the PCB with its own details.
Question#6: How can one process stop the execution of another process in Linux?
Answer#6: It can be done via signals like SIGINT SIGTERM and SIGKILL.
For example in the figure above, the code lies in the area from virtual address 0 to virtual address 10k, the stack starts from 64k and the heap starts from 10k with free space between them for them to grow.
But they could be really placed in the physical in some other random location and not even in one piece. For example, the code could be in 20091 to 20101 and the stack and heap could be in a completely different location like 45678 to 45742 but the OS did some behind the scenes magic to let the process think it is using the address spaces mentioned in the figure.
This is done so that the process has no idea where it is and who its neighbors are, in an attempt to isolate them so that a harmful process like say a virus will not be able to mess with a good one like a user application.
Question#7: Explain paging
Answer#7: Paging is an abstraction in operating systems used for memory management. Here the memory is chopped up into equal-sized chunks called pages. This is done so as to optimize the usage of memory by avoiding wastage due to segmentation.
The physical memory is divided into segments called page frames and each of these frames can hold a single page.
Instead of allocating a random number of bytes to the address space of each process, a number of pages are allocated to each process. Now each page from the virtual memory of each process’s address space is mapped to a page frame in physical memory.
Consider an example as below.
Here the address space of processA has 3 pages each 32byte in size. So the total memory in the address space is 96bytes. This is virtual memory.
Next, let’s have a look at the physical memory in the pic below.
Here the memory can hold 100 of these pages and has a total of 3200 bytes.
As you can see, the pages from processA are allocated in different places and thus need not be allocated contiguously (not in a single sequence). So that the operating system can allocate these pages wherever there is space, so that say if only 3 pages are available in memory that too in 3 different places, the operating system can still use this space efficiently.
Question#8: Explain Address Translation in Operating Systems
Answer#8: Address translation is the process of translating the virtual memory addresses into physical addresses
This is done with the help of the hardware on each and every memory access such that it happens so quickly and efficiently and the process has no idea that this is happening in the background!
Question#9: What are critical sections? How to make a code section to be a critical section?
Answer#9: These are portions of code that needs to be executed atomically. This is achieved with the help of mutex and semaphores
They are usually marked as critical sections because they might be accessing some data which is shared with other processes and the Operating system must not do a context switch when the critical section is executing so that data integrity can be maintained.
Say for example 2 processes are executing and writing some data to the end of a shared file. If the code section that is responsible for writing to this file is not marked as a critical section, then as process#1 is writing to it, the OS may interrupt and make a context switch and leaving half-written data, and process#2 may append to this half-written data ending up in a corrupted file.
To prevent this the code section is usually surrounded by a shared mutex or semaphore so that only one process can access the shared resource at any given time.
Question#10: Explain race conditions
Answer#10: Race conditions are situations that produce incorrect results due to the wrong order of execution of the code.
But how can code be executed in the wrong order? In earlier computer systems that ran just one program, this was not a problem.
But as computers evolved so did the complexity. As we introduced multi-tasking capabilities to our computers, where the operating system needs to make use of the CPU efficiently by letting multiple processes run at the same time, we started running into these kinds of problems due to the context switching of processes.
Since context switching is done preemptively in most operating systems, care must be taken to ensure race conditions do not occur. This can be done via the usage of critical sections of code as discussed in the next question.
Okay with that question I will stop this article. I hope you got some values out of this.
If you liked the post, feel free to share this post with your friends and colleagues!