Complete linux top 8 viva questions

Top 8 Linux & Shell Scripting Viva Questions | Computer Engineering

Top 20 Viva Questions on Linux & Shell Scripting

For Computer Engineering Students - Bharat College of Engineering (Mumbai University)

1. What is the purpose of the forward command in Linux?
The forward command typically refers to port forwarding or email forwarding in Linux systems. For network port forwarding, commands like ssh -L or iptables are used to redirect traffic between ports. Email forwarding is configured using .forward files in user directories or through mail server configurations like Postfix. This functionality is crucial for server administration and remote access scenarios. Network administrators use forwarding to expose services securely. Common applications include accessing databases behind firewalls or redirecting web traffic. Always ensure proper authentication when setting up forwarding rules. Security risks like open relays should be mitigated. Forwarding is extensively used in cloud environments and container networking.
2. How would you write a shell script to display processes sorted by memory usage?
#!/bin/bash
# Display top 5 memory-consuming processes
ps aux --sort=-%mem | head -n 6
This script uses the ps command with aux options to show all processes. The --sort=-%mem parameter sorts processes by memory usage in descending order. head -n 6 displays the header plus top 5 processes. For more detailed analysis, you can pipe to awk to format specific columns. To run continuously, add a while loop with sleep intervals. Such scripts are valuable for system monitoring and debugging memory leaks. Remember to make the script executable with chmod +x. For production use, consider logging output to a file with timestamp.
3. Explain the system calls open(), read(), write(), and close().
These fundamental system calls handle file operations in Linux:
  • open(): Creates a file descriptor for accessing a file, with modes like O_RDONLY (read-only) or O_CREAT (create if missing)
  • read(): Copies data from the file to a buffer, returning bytes read
  • write(): Transfers data from buffer to file, returning bytes written
  • close(): Releases the file descriptor and flushes buffers
All return -1 on error and set errno. Used in low-level file handling, device drivers, and database systems. Proper error handling is critical - always check return values. These are declared in fcntl.h and unistd.h. Performance can be optimized using buffer sizing strategies.
4. What are getpid(), getppid(), and getpgid() used for?
These process identification functions serve distinct purposes:
  • getpid(): Returns the Process ID (PID) of the calling process
  • getppid(): Returns the Parent Process ID (PPID)
  • getpgid(): Returns the Process Group ID (PGID) of specified process
Essential for understanding process hierarchies and relationships. Used when:
  • Debugging process trees
  • Managing process groups for job control
  • Handling orphan processes
  • Implementing process supervision
Always include unistd.h. Never assume PIDs will be sequential. In modern systems, PIDs are recycled and limited by /proc/sys/kernel/pid_max.
5. How would you implement basic commands like cp, mv, and rm using system calls?
#include <fcntl.h>
#include <unistd.h>

void copy_file(const char *src, const char *dest) {
    int fd_src = open(src, O_RDONLY);
    int fd_dest = open(dest, O_WRONLY|O_CREAT, 0644);
    char buf[4096];
    ssize_t bytes;
    
    while ((bytes = read(fd_src, buf, sizeof(buf))) {
        write(fd_dest, buf, bytes);
    }
    
    close(fd_src);
    close(fd_dest);
}
For mv, add unlink(src) after successful copy. For rm, simply use unlink(). Key considerations:
  • Handle all error cases (permissions, missing files)
  • Preserve file permissions and metadata where applicable
  • Use appropriate buffer sizes for performance
  • Implement progress reporting for large files
These implementations demonstrate core file system operations used in actual command utilities.
6. What is process management in Linux?
Process management encompasses:
  • Creation: Via fork() (creates child process) and exec() (replaces process image)
  • Scheduling: Handled by kernel using priorities (nice values) and policies
  • Termination: Through exit() or signals (SIGTERM, SIGKILL)
  • Monitoring: Tools like ps, top, htop
Key concepts:
  • Process states: Running, Sleeping, Stopped, Zombie
  • Process hierarchy: Parent-child relationships
  • Process groups and sessions
  • Signals for inter-process communication
Essential for multi-tasking, daemons, and server applications. Modern systems add cgroups and namespaces for isolation.
7. Explain memory management in Linux.
Linux memory management involves:
  • Virtual Memory: Each process gets its own address space
  • Paging: Memory divided into fixed-size pages (typically 4KB)
  • Swapping: Moving inactive pages to disk when RAM is full
  • Allocation: malloc()/free() in userspace, kmalloc()/vmalloc() in kernel
Key components:
  • Memory Mapping (mmap()) for files/devices
  • Copy-on-Write (COW) optimization for fork()
  • OOM Killer for handling out-of-memory situations
  • Transparent Huge Pages (THP) for performance
Tools like free, vmstat, and /proc/meminfo help monitor memory usage.
8. What is the difference between kill and kill -9?
The kill command sends signals to processes:
  • kill [pid]: Sends SIGTERM (15) - polite request to terminate
  • kill -9 [pid]: Sends SIGKILL (9) - immediate forced termination
Key differences:
  • SIGTERM allows process cleanup (closing files, releasing resources)
  • SIGKILL cannot be caught or ignored by the process
  • Use SIGTERM first for graceful shutdown
  • Resort to SIGKILL only when process is unresponsive
Other useful signals:
  • SIGHUP (1): Reload configuration
  • SIGINT (2): Interrupt (Ctrl+C)
  • SIGSTOP (19): Pause process (cannot be caught)

Comments

Popular posts from this blog

Analysis of algorithms viva questions / Interview questions - set1 /sorting algorithms

Operating System Viva questions/interview questions 2025

Recommendation System viva questions/ Interview questions