Linux Interview questions-part4 (Runlevels,cgroup and more)
Linux Interview questions and preparation- part , is a third part of linux interview question/reading materials. Focused on Runlevels,cgroup and more (namespaces)
LINUX
- Luminari
7/10/20244 min read
Linux is a free and open-source operating system that has been gaining popularity over the past few decades. It was created by Linus Torvalds in 1991 as a Unix-like operating system, and since then, it has become one of the most widely used operating systems in the world.
Explain Runlevels in Linux?
In Linux, runlevels are defined as numbers from 0 to 6, each representing a specific system state. Here's a brief overview of the most commonly used runlevels:
- Runlevel 0: Halt - The system is shut down, and all processes are terminated.
- Runlevel 1: Single-user mode - The system boots in single-user mode, allowing only the root user to log in. This runlevel is used for system maintenance and troubleshooting.
- Runlevel 2: Multi-user mode - The system boots in multi-user mode, allowing all users to log in. This is the default runlevel for most Linux systems.
- Runlevel 3: Multi-user mode with a graphical interface - This runlevel is similar to runlevel 2, but it also starts the graphical interface (X Window System).
- Runlevel 4: Unused - This runlevel is not used by default and is reserved for custom configurations.
- Runlevel 5: Multi-user mode with a graphical interface and network services - This runlevel is similar to runlevel 3, but it also starts network services like SSH and HTTP.
- Runlevel 6: Reboot - The system reboots.
Runlevels are configured using the /etc/inittab file or the systemd initialization system (on systems that use systemd). The default runlevel is specified in the /etc/inittab file using the id keyword, followed by the runlevel number (e.g., id:2:initdefault:).
When the system boots, the init process (or systemd) reads the /etc/inittab file and starts the services and processes specified for the default runlevel. You can change the runlevel using the init or telinit command, followed by the runlevel number (e.g., init 3 or telinit 3).
Runlevels are essential for managing Linux systems, allowing administrators to control the system's behavior and ensure that critical services are running or stopped as needed.
What are cgroups?
cgroups (control groups) are a Linux kernel feature that allows you to control and limit the resources used by a process or group of processes. cgroups provide a way to enforce limits on CPU, memory, disk I/O, and network bandwidth, ensuring that a single process or container doesn't consume all available resources.
cgroups are organized in a hierarchical structure, with each cgroup representing a set of processes. Each cgroup has its own set of limits and constraints, which are inherited by its child cgroups. This allows for a fine-grained control over resource allocation and ensures that resources are allocated efficiently.
What are Namespaces?
Namespaces are another Linux kernel feature that allows you to isolate processes from each other. Namespaces provide a way to create a virtualized environment for a process or group of processes, isolating them from the rest of the system.
There are several types of namespaces, including:
- PID namespaces: isolate process IDs
- Network namespaces: isolate network interfaces and routing tables
- Mount namespaces: isolate file systems and mount points
- IPC namespaces: isolate inter-process communication (IPC) resources
- User namespaces: isolate user and group IDs
How Linux Processes Work?
When a user runs a command or executes a program, the Linux kernel creates a new process. The process is allocated a unique Process ID (PID) and is added to the process table. The process table is a data structure maintained by the kernel, containing information about all running processes.
Here's a step-by-step overview of how Linux processes work:
1. Process Creation: When a user runs a command or executes a program, the kernel creates a new process by:
- Allocating a unique PID.
- Creating a new entry in the process table.
- Assigning memory and system resources.
2. Process Execution: The process executes the program or command, using the allocated resources.
3. Process Scheduling: The kernel schedules the process for execution, using a scheduling algorithm (e.g., Round-Robin or Priority Scheduling).
4. Process Communication: Processes can communicate with each other using Inter-Process Communication (IPC) mechanisms, such as pipes, sockets, or shared memory.
5. Process Termination: When a process completes its execution or encounters an error, it terminates, and the kernel:
- Removes the process from the process table.
- Releases allocated resources.
Types of Linux Processes
Linux processes can be classified into several types, based on their characteristics and behavior:
1. User Processes: These processes are created by users, either directly or indirectly, to execute programs or commands.
2. System Processes: These processes are created by the system to perform system-level tasks, such as managing system resources or providing services.
3. Daemon Processes: These processes run in the background, performing tasks like system logging, network services, or scheduling.
4. Thread Processes: These processes are lightweight processes that share the same memory space and resources as the parent process.
Related Concepts
Several concepts are closely related to Linux processes:
1. Process Group: A group of processes that share the same process group ID (PGID).
2. Session: A collection of processes that share the same session ID (SID).
3. Job: A collection of processes that are managed together as a single unit.
4. Zombie Process: A process that has finished execution but still has a entry in the process table.
Tools for Managing Linux Processes
Several tools are available for managing and monitoring Linux processes:
1. ps: Displays information about running processes.
2. top: Displays real-time information about system processes.
3. htop: An interactive process viewer.
4. kill: Sends signals to processes to terminate or suspend them.
5. pgrep: Searches for processes based on criteria like name, PID, or PGID.
My interests
As a techie + proud Hindhu i love to know/write about technology, spiritual knowledge.
Hey!, I am not living library. But if there is topic if you want me to cover, I will do my research and write about it, if it is unfamiliar to me. Its fun to learn and grow together.
Contact ID
Contact
author@luminari.info
© 2024. All rights reserved.
Well usually everyone goes with explaining containers and pod, like workloads but we feel it's better to know architectures first with those questions in the head. we will explain about work loads but now lets jump in with architecture and components.
Well usually everyone goes with explaining containers and pod, like workloads but we feel it's better to know architectures first with those questions in the head. we will explain about work loads but now lets jump in with architecture and components.