Complete linux top 8 viva questions
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 readwrite()
: Transfers data from buffer to file, returning bytes writtenclose()
: Releases the file descriptor and flushes buffers
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 processgetppid()
: Returns the Parent Process ID (PPID)getpgid()
: Returns the Process Group ID (PGID) of specified process
- Debugging process trees
- Managing process groups for job control
- Handling orphan processes
- Implementing process supervision
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
6. What is process management in Linux?
Process management encompasses:
- Creation: Via
fork()
(creates child process) andexec()
(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
- Process states: Running, Sleeping, Stopped, Zombie
- Process hierarchy: Parent-child relationships
- Process groups and sessions
- Signals for inter-process communication
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
- 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
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 terminatekill -9 [pid]
: Sends SIGKILL (9) - immediate forced termination
- 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
- SIGHUP (1): Reload configuration
- SIGINT (2): Interrupt (Ctrl+C)
- SIGSTOP (19): Pause process (cannot be caught)
Comments
Post a Comment