linux internals interview questions

Linux Internals Interview Questions : Multitasking | Memory Management | IPC Mechanisms

Table of Contents
    Add a header to begin generating the table of contents

    Introduction

    Linux Internals and networking module has various topics describing OS concepts, kernel, IPC, Multi-threading etc., Students should try to relate the concepts, understand and remember it.

     

    In one of the recent classes, some of our students had doubts in few concepts, which were asked in recent interviews that they have gone through. We have collected all queries and created a blog containing Linux internals interview questions. Please find the questions and answers for the same.

    Linux Internals Interview Questions Are

    1. Question Category: Multi-tasking and fork() Usage

    Question: In the below mentioned program, when the user tried to print variable addresses from parent and child the values were same. Whenever fork() system call is called, it is understood that the parent and child will have different address space, which is not matching with this output. Why?

    #include "common.h"
    int main()
    {
     int a, b;
     switch (fork())
     {
                    case 0:             
       printf("In child:\n &a: %p\t&b: %p\n", &a, &b);
      break;
      default:
      printf("In  parent:\n &a: %p\t&b: %p\n", &a, &b);
      break;
     } 
    }

                                                        Fig 1: Example โ€“ fork() system call

     

    #ifndef COMMON_H
    #define COMMON_H
    
    #include <stdio.h>
    #include <unistd.h>
    
    #endif

                                                       Fig 2: Header File โ€“ โ€œcommon.hโ€

    Output

    Fig 3: Output โ€“ virtual address

    Explanation

    1. In Linux, whenever we print the addresses of the variables in the program, we will be getting the virtual address not the physical one.
    2. Same applies for the above program, since the virtual address space for both parent and the child is same, the addresses of the variables also will be same.
    3. Virtual address is a binary number in virtual memory which enables any process to use a specific location in main memory independently from other processes. It allows the process to use more space than already existing space in primary storage by temporarily transferring some contents to a hard disk or internal flash drive.
    Fig 4: Virtual address โ€“ physical address โ€“ mapping

    Related Links

    2. Question Category: Deadlock and Starvation

    Question: In one of the recent interviews, some questions were asked about deadlock and starvation conditions in the OS. Can you please provide me with some reference materials?

     

    Deadlock
    In an Operating System, when a process or thread enters a waiting state due to unavailability of requested resource which is locked by another waiting process, which in turn waiting for another resource locked by another waiting process, deadlock occurs.Since the requested resource is not available for the process/thread and it is unable to change its state, the system is said to be in deadlock.

     

    Starvation
    In an operating system, when a process is unable to acquire the necessary resource (be it the processor/ I/O devices etc.,) to do its work, for a long period, the process is said to be in starvation.This may occur due to various reasons. For example, if there are more number of high priority tasks, then the low priority task will never get the resource assigned to it. In this state, the low priority task is said to be in starvation.

     

    Related Links for deadlock and starvation

    1. Deadlock and Starvation differences
    2. Deadlock vs Starvation

    3. Question Category: Debugging Using GDB

    Question: How to use GDB for multi-threading applications?

     

    Multi-threading application

    Multi-threading is a widespread programming and execution model which allows multiple threads to exist within the context of single process. The processโ€™ resources is shared among the threads, still the threads are able to execute independently. For example, Consider MS-word, where one thread may check spelling and other thread is processing inputs simultaneously. And there may be another thread checking for grammar but still they all are operating under the same Application called process.

     

    Why it is challenging to debug a multithreaded application?

    In a practical embedded system the sequence of thread execution is asynchronous in nature. Sometimes it is hard to recreate issues which happened in actual execution, hence it is becoming hard to debug the same. Often this is called as โ€œreproducing timing problemโ€!

     

    GDB on threads?

    GDB can be used to debug multi-threading scenario.Refer the link: GDB/MI Thread Commands.

    4. Question Category: Process โ€“ IPC Mechanism

    Question: Is it possible to re-allocate shared memory?

     

    Re-allocating shared memory is not possible and by using shmctl(), we can change some of the entities but not the size. Also, by using ipcs, we canโ€™t change the shared memory size, but the following fields can be changed:

    1. shm_perm.uid,

    2. shm_perm.gid, and

    3. (the least significant 9 bits of) shm_perm.mode

    5. Question category: System calls

    Question:  How sleep() system call works?

     

    • When the control hits the sleep() in the process, the kernel will Suspend execution of the process and mark it as not runnable.
    • Puts the process in the pending queue. Scheduler maintains the queue.
    • Sets a timer for the given wait time.
    • When the timer expires, puts back the process/thread into the running state.
    • Sleep causes the thread or process to give up the remainder of its time slice and stay in the Non Runnable state for the specified duration. While there is generally a guarantee for the minimum time period, there is no strict guarantee that the thread will run immediately or soon, or even at all, once the specified time has passed.
    • It is up to the schedulerโ€™s discretion, and dependent on thread priorities and implementation details such as timer resolutions when the sleeping thread will run again.
    • On POSIX systems, the nanosleep and related syscalls are interruptible by signals, returning the remaining sleep time. The sleep library function, on the other hand, is implemented via the alarm syscall on many older systems. Thus it only works by delivering a signal.

    6. Question Category: Process โ€“ IPC Mechanisms

    Question: Where the shared memory is created, in user space or kernel space?

     

    Shared memory allows multiple processes to share the same memory segment. Usually, a process (owner) creates the shared memory using shmget function in linux. The memory is allocated in the kernel space.

     

    Processes can access the shared memory segment by attaching to the shared memory (shmat function in linux). By doing this, the shared memory segment is mapped to the users address space. The shmat function (attach function) returns a virtual address which is mapped to the shared memory address. You can access the shared memory by using the virtual address.

     

    In conclusion, shared memory can be utilized by a process, just like utilizing a global variable. Since, a virtual address is used to access the shared memory, there is no system call overhead involved. Hence it is a fast IPC.

    Fig 5: Shared memory

    Conclusion :

    After reading linux internals interview questions we got know that in modern OS, which is designed to perform the multitasking, sleep() will not consume the CPU cycles. In conclusion, โ€œsleepโ€ guarantee that you sleep no less than but possibly more than the requested sleep duration.

    Share this material with your friend:

    Leave a Comment

    Your email address will not be published. Required fields are marked *