0% found this document useful (0 votes)
2 views12 pages

16-17 Final Question Solve

The document discusses various aspects of system programming, including the role of APIs, operating systems, and system-level services. It explains the importance of understanding system calls for optimizing performance and security, as well as analyzing shell commands and their features. Additionally, it covers UNIX file permissions, the significance of user IDs, and how the kernel manages free inodes and disk blocks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views12 pages

16-17 Final Question Solve

The document discusses various aspects of system programming, including the role of APIs, operating systems, and system-level services. It explains the importance of understanding system calls for optimizing performance and security, as well as analyzing shell commands and their features. Additionally, it covers UNIX file permissions, the significance of user IDs, and how the kernel manages free inodes and disk blocks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

1.

a) Distinguish system program based on the following keywords: API, operating


system, system level service, resource, service, and functionality of the operating
system.

API (Application Programming Interface): An API is a set of functions or routines


provided by the operating system that allows applications to interact with it. These
functions abstract the underlying complexity of the operating system and provide a
standardized way for applications to request services from the OS.

Operating System: The operating system itself is not a system program. Instead, it is the
core software that manages computer hardware and provides services for computer
programs. System programs interact with the operating system to perform tasks and
manage resources.

System Level Service: System level services refer to the various functionalities provided
by the operating system to facilitate the execution and management of applications.
These services may include process management, memory management, file system
access, and device management, among others.

Resource: In the context of system programs, resources refer to various components of


a computer system, such as CPU time, memory, files, and devices. System programs
interact with the OS to allocate and manage these resources efficiently.

Service: A service, in the context of system programs, is a specific functionality provided


by the operating system to support application execution. Services may include
functions like creating a new process, reading/writing files, or allocating memory.

Functionality of the Operating System: This refers to the capabilities and services
offered by the operating system as a whole. These functionalities enable applications to
run efficiently and provide an interface for users to interact with the computer system.

b) Suppose the values 2 and 3 are store in memory locations 940 and 941 respectively.
Now, explain the program execution with figures to store the sum of the two values in
the memory location 941.

AVAILABLE AT:

Onebyzero Edu - Organized Learning, Smooth Career


The Comprehensive Academic Study Platform for University Students in Bangladesh (www.onebyzeroedu.com)
c) To a programmer, a system call looks like any other call to a library procedure. Is it
important that a programmer know which library procedures result in system calls?
Under what circumstances and why?

It is important for a programmer to know which library procedures result in system calls
under certain circumstances and for specific reasons:

System calls are typically more time-consuming than regular library function calls
because they involve a transition from user mode to kernel mode. Therefore,
understanding which library procedures result in system calls allows a programmer to
optimize their code and minimize the number of system calls made, leading to improved
performance.
Knowing which library procedures involve system calls also helps the programmer
understand the potential security implications. System calls can have significant
privileges in the operating system, and if a library procedure results in a system call, it

AVAILABLE AT:

Onebyzero Edu - Organized Learning, Smooth Career


The Comprehensive Academic Study Platform for University Students in Bangladesh (www.onebyzeroedu.com)
means the application is requesting a sensitive operation that may need extra security
measures.
Moreover, some system calls may have restrictions or limitations, and being aware of
which library procedures result in system calls enables the programmer to handle
potential errors or exceptions properly.

Overall, understanding which library procedures result in system calls allows the
programmer to write more efficient, secure, and reliable code while making the best
use of the underlying operating system services.

2.a) Analyze the following commands and describe which shell feature(s) these are
used:
i) ls mydir > outfilel &
ii) vi *
iii)ls mydir > outfilel & date > outfile2 &

a) Analysis of shell commands:


i) ls mydir > outfile1 &
Shell feature(s) used: Redirection and Background Execution
Explanation: This command lists the files and directories in the "mydir" directory and
redirects the output to a file named "outfile1." The ">" symbol is used for output
redirection. The "&" symbol at the end of the command puts it in the background,
allowing the shell to continue executing other commands without waiting for this one to
complete.

ii) vi *
Shell feature(s) used: Wildcards (globbing)
Explanation: This command opens the "vi" editor and attempts to open all files in the
current directory (matching any file names) for editing. The "*" symbol is a wildcard that
matches any character or set of characters in the shell, which is useful for performing
operations on multiple files at once.

iii) ls mydir > outfile1 & date > outfile2 &


Shell feature(s) used: Redirection and Background Execution
Explanation: This command performs two tasks in the background simultaneously. It
first lists the files and directories in the "mydir" directory and redirects the output to a file
named "outfile1." Then, it calls the "date" command to display the current date and time
and redirects the output to a file named "outfile2." The "&" symbol is used to execute
both commands in the background.

AVAILABLE AT:

Onebyzero Edu - Organized Learning, Smooth Career


The Comprehensive Academic Study Platform for University Students in Bangladesh (www.onebyzeroedu.com)
b) A file whose file descriptor is fd contains the following sequence of bytes: 3, 1, 4, 1,
5, 9, 2, 6, 5, 3, 5. The following system calls are made:
lseek (fd, 3, SEEK_SET);
read (fd, &buffer, 4);
where the lseek call makes a seek to byte 3 of the file. What does buffer contain
after the read has completed?

The contents of the file descriptor fd are: 3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5.

lseek(fd, 3, SEEK_SET);: This system call sets the file offset to byte 3 from the beginning
of the file (SEEK_SET). So, the file pointer is moved to byte 3, which is the number 1 in
the sequence.

read(fd, &buffer, 4);: This system call reads 4 bytes from the current file offset (byte 3)
and stores them in the buffer. The buffer will contain the bytes starting from the current
offset: 1, 5, 9, 2.

So, after the read has completed, the buffer will contain the sequence of bytes: 1, 5, 9, 2.

c) What is shell scripting? Explain about it and write some commands.

A shell script is a text file that contains a sequence of commands for a UNIX-based
operating system. It is called a shell script because it combines a sequence of
commands, that would otherwise have to be typed into the keyboard one at a time, into
a single script.

Shell scripts contain ASCII text and are written using a text editor, word processor or
graphical user interface (GUI). The content of the script is a series of commands in a
language that can be interpreted by the shell. Functions that shell scripts support
include loops, variables, if/then/else statements, arrays and shortcuts. Once complete,
the file is saved typically with a .txt or .sh extension and in a location that the shell can
access.

Advantages of Shell Script:


● Easy commands and syntax.
● Quick coding and file management.
● Connects with existing programs.
● Simple customization for users.

AVAILABLE AT:

Onebyzero Edu - Organized Learning, Smooth Career


The Comprehensive Academic Study Platform for University Students in Bangladesh (www.onebyzeroedu.com)
Disadvantages of Shell Script:
● Can have mistakes.
● Runs slowly with many steps.
● May not work well on different systems.

#!/bin/bash
gzip myfile.txt

zip myarchive.zip file1.txt file2.txt file3.txt


unzip myarchive.zip

find /path/to/directory -type f -name "*.txt"

echo "Hello, World!"


message="This is a message."
echo $message

3. a) In a multi-user operating system, the operating system must protect users from
Reach other and protect itself from users. However, while providing an operating
environment for all users, the operating system creates this illusion (permission for
read, write, execution, etc.) by creating data paths between user processes and
devices and files. How does UNIX create this illusion?

UNIX creates the illusion of permissions for users by implementing a robust file
permission system based on three types of permissions: read (r), write (w), and execute
(x). These permissions are assigned to three classes of users: owner, group, and others.
When a user interacts with files or directories, the operating system checks their
permissions against the requested operation to determine whether it is allowed or not.

The key mechanisms UNIX uses to create this illusion are:

User Identification: Each process running in UNIX is associated with a specific user,
known as the "effective user ID" or "EUID." The EUID determines the permissions
available to the process for accessing files and resources.

File Ownership: Every file and directory in UNIX is associated with an owner and a
group. The owner is the user who created the file, and the group is a collection of users
who share common access rights to the file.

AVAILABLE AT:

Onebyzero Edu - Organized Learning, Smooth Career


The Comprehensive Academic Study Platform for University Students in Bangladesh (www.onebyzeroedu.com)
File Permission Bits: UNIX assigns specific permission bits (read, write, and execute) to
the owner, group, and others, representing the access rights for each class of users.

Access Control Lists (ACLs): In addition to the standard permissions, some UNIX
systems support ACLs, which provide more fine-grained control over file access by
allowing specific permissions for individual users and groups.

With these mechanisms in place, UNIX can ensure that users can only access files and
resources they have the proper permissions for, protecting users from each other and
the system from unauthorized access.

b) Unix files have a full set of permission bits, including a set-user-ID bit and a
set-group-ID bit. If you turn on the set-group-ID bit for a directory, does it have any
effect? If so, what and why? If not, could you think of some use for this bit?

Effect of the set-group-ID bit on a directory:

When the set-group-ID bit (SGID) is turned on for a directory in UNIX, it has an effect on
the files and subdirectories created within that directory. If a new file or directory is
created within a directory with the SGID bit set, the new file or directory inherits the
group ownership of the parent directory, rather than the group ownership of the user
creating the file or directory.

The reason behind this behavior is to ensure that files created within a shared directory
maintain the same group ownership as the parent directory, facilitating group
collaboration and consistent access control.

c) Each user has a username and a number, the UID, why? Wouldn't it be simpler to
record the username of the user as the owner of a file? Why not have a single identifier
for each user?

Having both a username and a UID (user identifier) serves important purposes in
multi-user operating systems like UNIX:

User Convenience: Assigning usernames allows users to easily identify and remember
each other and themselves on the system. It provides a more human-readable
representation of users.

AVAILABLE AT:

Onebyzero Edu - Organized Learning, Smooth Career


The Comprehensive Academic Study Platform for University Students in Bangladesh (www.onebyzeroedu.com)
User Information Separation: Usernames are meant for user identification and
interaction, while UIDs are used internally by the system for security and access control
purposes. By having separate identifiers, UNIX can maintain a clear distinction between
user-facing information and internal system operations.

Flexibility and Security: Usernames can be changed without affecting the system's
internal processes since UIDs remain constant. This flexibility allows users to update
their display names without affecting their permissions and access rights.

Compatibility and Portability: Using numerical UIDs instead of usernames improves


system performance and reduces the risk of conflicts when migrating data between
different UNIX systems or networks.

In summary, having both usernames and UIDs provides a balanced approach between
user convenience, system security, and ease of administration in multi-user operating
systems like UNIX.

4.a) The kernel had to locate a free inode and free disk blocks when it created a new
file. How does the kernel know which blocks are free? How does the kernel know
which inodes are free? What method does the file system on your machine use to keep
track of unused blocks and inodes?

When the kernel needs to create a new file, it must find free disk blocks and a free inode
to allocate for the new file. The kernel relies on the file system to keep track of which
blocks and inodes are free and available for allocation.

Free Blocks: The file system maintains a data structure, often called the "free block
bitmap" or "free block list," to track which disk blocks are available for use. This bitmap
is a binary representation, where each bit corresponds to a block on the disk. A "0" bit
indicates that the block is free, and a "1" bit indicates that the block is already allocated
and in use. The kernel can quickly scan this bitmap to find available free blocks.

Free Inodes: Similarly, the file system uses a "free inode bitmap" or "free inode list" to
track which inodes are free for allocation. Each inode represents a file or directory on
the file system. Like the free block bitmap, the free inode bitmap uses bits to represent
the availability of inodes. A "0" bit means the inode is free, and a "1" bit means it is in
use.

AVAILABLE AT:

Onebyzero Edu - Organized Learning, Smooth Career


The Comprehensive Academic Study Platform for University Students in Bangladesh (www.onebyzeroedu.com)
Allocation Algorithms: The file system employs allocation algorithms to efficiently
manage the allocation and deallocation of blocks and inodes. These algorithms ensure
that the kernel can quickly find free blocks and inodes when creating new files or
directories.

b) A directory is just a node in a set of linked nodes. Using the pwd command you can
simply know the current directory. Based on the functionalities of pwd answer the
following:
i) What repetitive steps are performed to compute its current directory?
ii) How do we know when we read the top of the tree?
iii) How do we print the directory names in the correct order?

The pwd (print working directory) command displays the current working directory of
the user in the shell.
/
└── home
└── user
└── documents
└── projects
└── current
The pwd command will display the complete path:
/home/user/documents/projects/current.

i) Repetitive steps to compute the current directory using pwd:


The pwd command starts at the top of the directory tree (the root directory) and follows
linked nodes through each subdirectory until it reaches the current directory. It keeps
track of the visited directories to form the complete path.

ii) How we know when we read the top of the tree:


When using pwd, we know we have reached the top of the directory tree (the root
directory) when there are no more parent directories above the current directory being
visited.

iii) How we print the directory names in the correct order:


To print the directory names in the correct order, pwd uses a stack to store the names of
visited directories. As it traverses down the tree, it pushes the names onto the stack.
When it reaches the current directory, it pops the names from the stack in reverse order,
giving the correct path from the root to the current directory.

AVAILABLE AT:

Onebyzero Edu - Organized Learning, Smooth Career


The Comprehensive Academic Study Platform for University Students in Bangladesh (www.onebyzeroedu.com)
5.a) What do the read and write permission bits on a terminal special file control? Use
tty to determine the name of your terminal, then use chmod 000 /dev/yourtty to make
your terminal unreadable even to yourself. What happens? Why?

On a terminal special file (e.g., /dev/tty), the read permission controls the ability to read
input from the terminal, and the write permission controls the ability to send output to
the terminal. These permissions determine whether a user or a process can interact
with the terminal device.

The numeric value 000 represents no permissions for the owner, group, and others,
respectively. When you run chmod 000 /dev/yourtty, you are removing all permissions
from the terminal special file.

Read Permission (R) - Bit Value 4:


When the read permission is removed (bit value set to 0), it means you (the owner of the
terminal) can no longer read input from the terminal. Without read permission, you won't
be able to see any characters you type into the terminal.

Write Permission (W) - Bit Value 2:


When the write permission is removed (bit value set to 0), it means you (the owner of
the terminal) can no longer send output to the terminal. Without write permission, you
won't see any output from commands or processes executed in the terminal.

If you use tty to determine the name of your terminal and then use chmod 000
/dev/yourtty to make your terminal unreadable even to yourself, the result will be that
you lose the ability to read and write from/to the terminal. This means you won't be able
to input commands or see any output on that terminal. You essentially lock yourself out
from interacting with that terminal.

b) cd [<dir>] means that change pwd to HOME directory, or <dir> if it is supplied.


However, "Changing directories" and "being in a directory" are imprecise phrases.
When your cd to a directory named dir, you may think of yourself as being "in dir", but
this is not true. What is the true meaning for cd [<dir>] in order to resolve the relative
pathnames?

b) True meaning of cd [<dir>] to resolve relative pathnames:

AVAILABLE AT:

Onebyzero Edu - Organized Learning, Smooth Career


The Comprehensive Academic Study Platform for University Students in Bangladesh (www.onebyzeroedu.com)
The cd [<dir>] command changes the current working directory to the specified directory
(if provided), or to the user's home directory if no directory is given. It updates the shell's
internal record of the current directory to resolve relative pathnames.

When you use cd to change to a directory named dir, you are not physically moving into
that directory. Instead, the shell updates its internal record of the current working
directory to the specified directory. The shell then uses this updated information to
resolve relative pathnames. Relative pathnames are pathnames that do not start with
the root directory ("/") but instead are based on the current working directory.

c) The return value form fork allows a process to determine if it is the parent or child
process. What other technique can a process use?

c) Other technique for a process to determine if it is the parent or child process:


The return value from fork() allows a process to determine if it is the parent (receives
the child's PID) or the child (receives 0). The getpid() system call can also be used to
retrieve the process's own PID.

d) Standard Unix shells do not die when the user sends the interrupt or quit signal
when the child is running. How does a standard Unix shell respond to these signals
when regarding a command line?

d) How a standard Unix shell responds to interrupt or quit signals when regarding a
command line:

When a standard Unix shell is running a command line and receives an interrupt signal
(SIGINT, usually triggered by pressing Ctrl+C) or a quit signal (SIGQUIT, usually triggered
by pressing Ctrl+), it typically responds as follows:

Interrupt Signal (SIGINT):


The shell will stop the currently running foreground process (the command being
executed) and return control to the shell prompt.
The process being interrupted may terminate if it does not handle the SIGINT signal.

Quit Signal (SIGQUIT):


The shell will stop the currently running foreground process (the command being
executed) and produce a core dump. The core dump contains information about the

AVAILABLE AT:

Onebyzero Edu - Organized Learning, Smooth Career


The Comprehensive Academic Study Platform for University Students in Bangladesh (www.onebyzeroedu.com)
process's memory and state at the time of termination, which can be used for
debugging purposes.

In summary, the standard Unix shell reacts to these signals by interrupting or quitting
the current foreground process and returning control to the shell. It allows the user to
stop a running command or produce debugging information when needed.

6.a) What do you know about thread of execution? Explain multiple threads of
execution with proper examples.

A thread of execution, commonly known as a thread, is the smallest sequence of


programmed instructions that can be managed independently by a scheduler in an
operating system. Threads are part of a process and share the same resources (e.g.,
memory space), but each thread has its own execution path and can run concurrently
with other threads within the same process. Threads enable concurrent execution of
multiple tasks, improving the efficiency and responsiveness of programs.

Example of multiple threads of execution:

Let's consider a simple example of a web server. The web server process receives
incoming client requests and processes them to serve web pages. Instead of handling
each request sequentially, the server can use multiple threads to handle multiple
requests concurrently. Each thread will be responsible for processing one client request
independently. This way, the web server can efficiently serve multiple clients
simultaneously, making better use of the available resources.

b) List the common functions performed by UNIX APIs. Give example.

Common functions performed by UNIX APIs include:

File I/O Operations:


open: Opens a file for reading, writing, or both.
read: Reads data from a file into a buffer.
write: Writes data from a buffer to a file.
close: Closes an open file descriptor.

AVAILABLE AT:

Onebyzero Edu - Organized Learning, Smooth Career


The Comprehensive Academic Study Platform for University Students in Bangladesh (www.onebyzeroedu.com)
Process and Thread Management:
fork: Creates a new process by duplicating the calling process.
exec: Loads and executes a new program in the current process space.
wait: Waits for the termination of a child process.
pthread_create: Creates a new thread.

Interprocess Communication (IPC):


pipe: Creates a unidirectional pipe.
shmget: Allocates shared memory.
msgget: Creates a message queue.

Signal Handling:
signal: Sets up signal handlers for specific signals.
kill: Sends a signal to a specified process or process group.

c) Write the syntax of if unconditional statement in UNIX. Example with an example.

The syntax of the unconditional statement in UNIX is the if statement without any
condition, followed by the then keyword.

#!/bin/bash
if true; then
echo "This will always be executed."
fi

AVAILABLE AT:

Onebyzero Edu - Organized Learning, Smooth Career


The Comprehensive Academic Study Platform for University Students in Bangladesh (www.onebyzeroedu.com)

You might also like