September 17, 2024

System Programming Q&A

System Programming
Share :

System Programming Question and Answer


(Suggest Find the question by search page and keep refreshing the page for updated content)

Q. In & Linux system you want to open an existing file a.txt in vi editor. Write appropriate vi commands to fulfill the following requiments 1) a.txt should be opened with cursor at the last line of the file

Answer.

To open an existing file a.bxt in vi editor with the cursor at the last line of the file, you can use the following command:

vi+$a.txt

This will open the file a.txt in vi editor and place the cursor at the last line of the file.

2) Delete the last line and paste it as the first line of the same file.

Answer.

To delete the last line and paste it as the first line of the same file, you

  1. Move the cursor to the last line of the file by pressing G.
  2. Press dd to delete the last line.
  3. Move the cursor to the first line of the file by pressing gg.
  4. Press P to paste the deleted line at the beginning of the file.

3) Write the line 1 through 15 to a new file a txt.bak.

Answer.

To write the first 15 lines of the file to a new file a bxt bak, you can use the following command:

1,15w a.txt.bak

This will write the first 15 lines of the file to a new file named a.txt.bak.

4) Exit vi editor without saving any changes.

Answer

To exit vi editor without saving any changes, you can use the following command:
:q!
This will quit vi editor without saving any charges.



Q. A) If you execute a Shell Script called WASE at command prompt by saying

$sh WASE WIMS EXAM

What will be the values of $0, $$, $#, $”, $@, $?, ?

Answer.

  • The value of $0 will be the name of the shell script which is `WASE`.
  • The value of `$$ will be the
  • process ID of the current shell.
  • The value of `$#` will be 2, which is the number of arguments passed to the script (`WIMS` and `EXAM`).
  • The value of `$” (or `$IFS`) will be the default value of the internal
  • The value of `$” (or `$IFS) will be the default value of the internal field separator, which is a space character by default.
  • The value of $@ will be the list of arguments passed to the script, which are ‘WIMS and EXAM`.
  • The value of `$?` will depend on the execution status of the `ssh` command. If the command executed successfully, it will be 0. Otherwise, it will be a non-zero value indicating the error code. The value of `?` will also be affected by the execution of the `WASE script if it contains any commands that exit with a non- zero status

 

Q. Discuss the purpose of shift command with an example.
Answer.
The ‘shift’ command in shell scripting is used to shift the positional parameters to the left. The positional parameters are the command-line arguments passed to the script or function.

Each positional parameter is represented by a dollar sign followed by a number, starting from `$0`, which represents the name of the script or function, and `$1`, `$2`, `$3`, and so on, which represent the first, second, and third argument, respectively.

The ‘shift’ command allows you to access the positional parameters beyond `$9 by shifting the parameters to the left, so that the value of `$1` becomes `$0`, the value of $2` becomes `$1`, and so on. The value of `$0` is not changed by the ‘shift’ command.

Here is an example of the `shift` command:

#!/bin/bash 
echo "Script name: $0" 
echo "Argument 1: $1" 
echo "Argument 2: $2" 
echo "Argument 3: $3" 
shift 
echo "Shifted..." 
echo "Script name: $0" 
echo "Argument 1: $1" 
echo "Argument 2: $2"

Suppose you save this script as `shift-example.sh’ and execute it with the command `./shift-example.sh a b c’. The output will be:

Script name: ./shift-example.sh 
Argument 1: a 
Argument 2: b 
Argument 3: c 
Shifted... 
Script name: ./shift-example.sh 
Argument 1: b 
Argument 2: c

As you can see, the ‘shift’ command shifted the positional parameters to the left, so that `$1`became `b` and `$2` became `c`. The value of `$0` did not change. This allows you to access the you use the ‘shift command again, positional parameters beyond `$9`. If the value of $1` will become `c` and `$2’ will become empty.

 

B) i) What is an infinite loop? Explain with any one example?
Answer.
An infinite loop is a type of programming loop that repeats indefinitely and does not terminate unless the program is interrupted or terminated externally. In an infinite loop, the loop condition is always true, and the loop body keeps executing repeatedly, causing the program to get stuck in an endless loop. This can cause the program to consume system resources, crash or become unresponsive.
Here’s an example of an infinite loop in Python:

while True: 
print("This is an infinite loop")

An infinite loop is a type of programming loop that repeats indefinitely and does not terminate unless the program is interrupted or terminated externally. In an infinite loop, the loop condition is always true, and the loop body keeps executing repeatedly, causing the program to get stuck in an endless loop. This can cause the program to consume system resources, crash or become unresponsive.

Here’s an example of an infinite loop in Python:

while True:
print(“This is an infinite loop”)

In this example, the `while` loop condition is set to `True`, which means the loop will never terminate on its own. The loop body simply prints a message to the console repeatedly, causing the program to get stuck in an infinite loop.

To avoid infinite loops, it’s important to ensure that the loop condition will eventually become false at some point, or to include a conditional statement that will cause the loop to terminate when a specific condition is met.

 

ii) Write a shell script which takes filename as a command line argument and replaces all string1 present in that file with string2 and save its output in output.txt file.

Answer.

Here’s a shell script that takes a filename as a command line argument, replaces all occurrences of string1 in the file with string2, and saves the output in a file named output.txt:

#!/bin/bash

if [ $# -eq 0 ]; then
echo "Usage: $0 filename"
exit 1
fi

filename=$1

if [ ! -f $filename ]; then
echo "$filename not found"
exit 1
fi

echo "Enter string1 to replace:"
read string1

echo "Enter string2 to replace with:"
read string2

sed "s/$string1/$string2/g" $filename > output.txt

echo "Replacement complete. Check output.txt for the result."

Here’s how this script works:

  1. It first checks whether a filename is provided as a command line argument. If not, it displays an error message and exits the script.
  2. It then checks whether the specified file exists or not. If the file does not exist, it displays an error message and exits the script.
  3. The user is prompted to enter string1 and string2, which are the strings to be replaced and the string to replace them with, respectively.
  4. The sed command is used to perform the replacement. The `s` option tells `sed to search and replace, and the `g option tells it to replace all occurrences of string1 in the file. The output of the `sed` command is redirected to a file named output.txt.
  5. Finally, a message is displayed to indicate that the replacement is complete and the user is prompted to check the output file for the result.


Q. A. Your client is using Linux based systems and they are having regular power outage and system crashes, a lot of work from the application goes uncommitted causing data integrity issues. You have asked to design a solution using journaling filesystem that just reads through the pumal the and processes any uncommitted data left over – Explain your approach with a diagram using the different methods of journaling and their levels of protection.

Answer.

To address the issue of data integrity caused by power outages and system crashes, we can use a journaling file system that maintains a log of all changes made to the file system. This log, or journal, can be used to recover uncommitted data in case of a crash or power outage. There are three main types of journaling file systems:

  1. Write-through journaling: In this approach, changes are first written to the journal and then to the main file system. This ensures that all changes are recorded in the journal before they are committed to the file system. However, it does not provide any protection against data loss in case of a power outage or system crash during the write operation.
  2. Write-back journaling: In this approach, changes are first written to the main file system and then to the journal. This provides better performance, but it also increases the risk of data loss in case of a power outage or system crash during the write operation.
  3. Ordered journaling: In this approach, changes are grouped together and written to the journal in a specific order. This ensures that all changes are recorded in the journal before they are committed to the file system, while also providing better performance than write-through journaling.

Based on the requirements, an ordered journaling file system would be suitable for this scenario, as it provides a good balance between performance and data integrity. Here’s how the journaling process works in an ordered journaling file system:

  1. A write operation is requested on the file system.
  2. The changes are recorded in the journal in a specific order.
  3. The changes are then written to the main file system in the same order they were recorded in the journal.
  4. Once the changes are successfully committed to the main file system, they are removed from the journal.

In case of a power outage or system crash, the file system can use the journal to recover any uncommitted data. The recovery process involves replaying the journal and applying any changes that were not committed to the main file system before the crash occurred.

With an ordered journaling file system in place, any uncommitted data left over due to power outages or system crashes can be recovered by reading through the journal and applying the changes that were not committed to the main file system. This ensures data integrity and reduces the risk of data loss.

B. 1) Write a shell script which will accept minimum 12 arguments in command line and display the script name, number of argents passed and the value of 1st and 12th arguments.
Answer.
Here’s a shell script that accepts at least 12 arguments from the command line and displays the script name, the number of arguments passed, and the values of the first and twelfth arguments:

#!/bin/bash

if [ $# -lt 12 ]; then
echo "Error: This script requires at least 12 arguments."
exit 1
fi

echo "Script name: $0"
echo "Number of arguments passed: $#"
echo "First argument: $1"
echo "Twelfth argument: ${12}"

B. 2) A directory named ‘classDir’ contains lot of text files. Show the command(s) to find a string hundred inside this ‘classDir directory case- insensitive manner. The output should be written to the stdout and also to a file named ‘/home/user/found.txt.

Answer.

To find the string “hundred” inside the “classDir” directory in a case-insensitive manner and write the output to both stdout and a file named “/home/user/found.txt”, we can use the following command:

grep -i "hundred" classDir/* > /home/user/found.txt

Here’s how this command works:

  1. The grep command is used to search for the string “hundred” in all files inside the “classDir” directory. The -i option makes the search case-insensitive.
  2. The * wildcard is used to specify that we want to search in all files inside the “classDir” directory.
  3. The > operator is used to redirect the output of the grep command to a file named “/home/user/found.txt”. This file will be created if it doesn’t exist and overwritten if it already exists.

Since we redirected the output to a file, the output will not be displayed on the stdout. However, we can display the output on the stdout and write it to the file at the same time by using the tee command like this:

grep -i "hundred" classDir/* | tee /home/user/found.txt

This will display the output of the grep command on the stdout and write it to the file named “/home/user/found.txt” at the same time.



Q. A) Justify the need for a linking loader to have two passes. Explain the data structures used in a linking loader and how each pass makes use of them.
Answer.

A linking loader is a software utility that links and loads object files generated by a compiler to create a single executable program. The linking loader performs this task by combining multiple object files, resolving external references between them, and generating the necessary relocation and symbol tables to enable the program to be executed. A two-pass linking loader is a type of loader that performs these tasks in two separate passes. In this answer, I will justify the need for a two-pass linking loader and explain the data structures used in it.

Justification for two-pass linking loader: The primary reason for using a two-pass linking loader is to resolve external references. External references are symbols (such as variables or functions) that are defined in one object file but used in another object file. In a single-pass linking loader, the linker processes each object file only once and generates a single output file. This means that the linker must resolve all external references in a single pass. However, it may not be possible to resolve all external references in a single pass because some external references may not be defined until later in the linking process. Therefore, a two-pass linking loader is needed to resolve external references that cannot be resolved in a single pass.

Data structures used in a linking loader: The two-pass linking loader uses several data structures to perform its task. The two most important data structures are the symbol table and the relocation table.

  1. Symbol table: The symbol table is a data structure that stores information about symbols defined or referenced in the object files being linked. The symbol table is built during the first pass of the linker, and it is used during the second pass to resolve external references. The symbol table contains information about the symbol’s name, type (function or variable), location (memory address), and visibility (local or global).
  2. Relocation table: The relocation table is a data structure that stores information about how the linker should modify the object files being linked to resolve external references. The relocation table is built during the first pass of the linker, and it is used during the second pass to modify the object files. The relocation table contains information about the location of the external reference in the object file, the type of modification needed (addition, subtraction, or multiplication), and the symbol to be used for the modification.

 

 

Q. B) 1) Are the outputs of the codes shown below? Justify.

I)

while [$choice = “y” ]; do
echo Hello World >>hello.txt
echo Enter y/n?; read choice
done

Answer.
The output of this code will depend on the user’s input. If the user enters “y” multiple times, the string “Hello World” will be appended to the “hello.txt” file multiple times. If the user enters “n” or some other input, the loop will exit and no more output will be generated. If the “hello.txt” file did not exist before running the script, it will be created. If it did exist, the “Hello World” string will be appended to the end of the file.

ii)

while [$choice = “y” ]; do
echo Hello World
echo Enter y/n?; read choice
done>>hello.txt

Answer.

The output of this code will be different than the previous code snippet because the output is being redirected to the “hello.txt” file after the loop completes. Specifically, if the user enters “y” multiple times, the string “Hello World” will be output to the console multiple times, and then all of the output (including the prompt and “Hello World” messages) will be appended to the end of the “hello.txt” file. If the user enters “n” or some other input, the loop will exit and no more output will be generated. If the “hello.txt” file did not exist before running the script, it will be created. If it did exist, the output from the loop will be appended to the end of the file.

 

2) What is the purpose of the following system program and explain purpose of each statement in the program

int main()
{
int fd = open(“BITS.INI”, O_RDONLY | O_CREAT);
printf(“fd=%d\n”, fd).;
if(fd==-1)
{
printf(“ERROR NUMBER %d\n”, errno);
perror(“PROGRAM”);
}
return();
}

Answer.

The purpose of the program is to open the file “BITS.INI” (or create it if it doesn’t exist) and print the file descriptor number to the console. It also checks whether the open() function call succeeded or failed, and prints an error message with the corresponding error number if the call failed.

Here is a step-by-step explanation of the program:

  1. int main() declares the main function of the program, which returns an integer value.
  2. int fd = open(“BITS.INI”, O_RDONLY | O_CREAT); declares an integer variable fd and assigns it the file descriptor number returned by the open() function call. The open() function is used to open the file named “BITS.INI” with read-only (O_RDONLY) and create (O_CREAT) flags. If the file doesn’t exist, it will be created.
  3. printf(“fd=%d\n”, fd); prints the file descriptor number to the console.
  4. if(fd==-1) checks whether the open() function call failed, as indicated by the file descriptor value of -1. If the call failed, the following statements will be executed.
  5. printf(“ERROR NUMBER %d\n”, errno); prints the value of the global errno variable, which contains the error number associated with the most recent system call that failed.
  6. perror(“PROGRAM”); prints a user-friendly error message “PROGRAM: <error message>” to the console, where “<error message>” is the error message associated with the error number in errno.
  7. return(); exits the main function and returns an integer value. If no value is specified, the default value of 0 is returned.

Overall, the program is a simple example of how to use the open() function to open a file and how to handle errors using the errno and perror() functions.


For More Updates Join Our Channels :