Unit Test Question Bank
Unit Test Question Bank
A shell script is usually created for command sequences in which a user has a
need to use repeatedly in order to save time. Like other programs, the shell
script can contain parameters, comments and subcommands that the shell
must follow. Users initiate the sequence of commands in the shell script by
simply entering the file name on a command line.
Types of shells
In Unix and Linux, the two major types of shell scripts are:
- Bourne again shells (BASH)- BASH is the default shell for Unix version 7.
The character for prompting a bourne again shell is $.
- C shells- A C shell is run in a text terminal window and is able to easily read
file commands. The character for prompting a C shell is %.
Using a shell script is most useful for repetitive tasks that may be time
consuming to execute by typing one line at a time. A few examples of
applications shell scripts can be used for include:
• Automating the code compiling process.
• Completing batch
• Manipulating files.
• Monitoring a system.
Advantages and disadvantages of shell scripts
Shell scripting is meant to be simple and efficient. It uses the same syntax in
the script as it would on the shell command line, removing any interpretation
issues. Writing code for a shell script is also faster and requires less of
learning curve than other programming languages.
The process executes when it changes the state. The state of a process is defined by
the current activity of the process.
Each process may be in any one of the following states −
• New − The process is being created.
• Running − In this state the instructions are being executed.
• Waiting − The process is in waiting state until an event occurs like I/O operation
completion or receiving a signal.
• Ready − The process is waiting to be assigned to a processor.
• Terminated − the process has finished execution.
• It is important to know that only one process can be running on any processor at
any instant. Many processes may be ready and waiting.
Explanation
Step 1 − Whenever a new process is created, it is admitted into ready state.
Step 2 − If no other process is present at running state, it is dispatched to running
based on scheduler dispatcher.
Step 3 − If any higher priority process is ready, the uncompleted process will be sent to
the waiting state from the running state.
Step 4 − Whenever I/O or event is completed the process will send back to ready state
based on the interrupt signal given by the running state.
Step 5 − Whenever the execution of a process is completed in running state, it will exit
to terminate state, which is the completion of process.
Synchronization is the cooperative act of two or more threads that ensures that each
thread reaches a known point of operation in relationship to other threads before
continuing. Attempting to share resources without correctly using synchronization is the
most common cause of damage to application data.
Typically, synchronizing two threads involves the use of one or more synchronization
primitives. Synchronization primitives are low-level functions or application objects
(not IBM® i objects) that your application uses or creates to provide the synchronization
behavior that the application requires.
Here are the most common synchronization primitives in order of least to most
computationally expensive:
• Compare and swap
• Object locks
8. Define kernel and user space
Ans:
Kernel space is strictly reserved for running a privileged operating system kernel, kernel
extensions, and most device drivers. In contrast, user space is the memory area
where application software and some drivers execute.
The term user space (or userland) refers to all code that runs outside the operating system's
kernel.[1] User space usually refers to the various programs and libraries that the operating system
uses to interact with the kernel: software that performs input/output, manipulates file
system objects, application software, etc.
Each user space process normally runs in its own virtual memory space, and, unless explicitly
allowed, cannot access the memory of other processes. This is the basis for memory protection in
today's mainstream operating systems, and a building block for privilege separation. A separate user
mode can also be used to build efficient virtual machines – see Popek and Goldberg virtualization
requirements. With enough privileges, processes can request the kernel to map part of another
process's memory space to its own, as is the case for debuggers. Programs can also request shared
memory regions with other processes, although other techniques are also available to
allow inter-process communication.
9. Explain process creation in Unix
Ans:
Process creation is achieved through the fork() system call. The newly created process is
called the child process and the process that initiated it (or the process when execution is
started) is called the parent process. After the fork() system call, now we have two
processes - parent and child processes.
#include <sys/types.h>
#include <unistd.h>
pid_t fork(void);
program:
File name: basicfork.c
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main() {
fork();
printf("Called fork() system call\n");
return 0;
}
10.Define zombie, orphan for a process
Ans:
Zombie Process :
A Zombie is a process that has completed its task but still, it shows an entry in
a process table. The zombie process usually occurred in the child process. Very
short time the process is a zombie. After the process has completed all of its
tasks it reports the parent process that it has about to terminate.
Zombie process states always indicated by Z.
The zombie process has controlling terminals.
The zombie process treated as dead they are not used for system processing.
To remove the zombie process execute the kill command.
Orphan Process :
A child process that remains running even after its parent process is
terminated or completed without waiting for the child process execution is
called an orphan. A process becomes an orphan unintentionally. Some time
intentionally becomes orphans due to long-running time to complete the
assigned task without user attention. The orphan process has controlling
terminals.
The orphan process was created unknowingly due to a system crash.
Orphan The zombie process has controlling terminals.
An orphan process is a computer process even after their parent terminates
init is become a parent and continue the remaining task.
Terminate the Orphan process use the SIGHUP signal.
11.Define Mutex and its syntax and its operations.
Ans:
Mutex is a mutual exclusion object that synchronizes access to a resource. It is created with
a unique name at the start of a program. The Mutex is a locking mechanism that makes sure
only one thread can acquire the Mutex at a time and enter the critical section.
• A Mutex is a lock that we set before using a shared resource and release
after using it.
• When the lock is set, no other thread can access the locked region of code.
• So we see that even if thread 2 is scheduled while thread 1 was not done
accessing the shared resource and the code is locked by thread 1 using
mutexes then thread 2 cannot even access that region of code.
• So this ensures synchronized access of shared resources in the code.
Working of a mutex:
Suppose one thread has locked a region of code using mutex and is executing
that piece of code.
Now if scheduler decides to do a context switch, then all the other threads
which are ready to execute the same region are unblocked.
Only one of all the threads would make it to the execution but if this thread
tries to execute the same region of code that is already locked then it will
again go to sleep.
Context switch will take place again and again but no thread would be able to
execute the locked region of code until the mutex lock over it is released.
Mutex lock will only be released by the thread who locked it.
So this ensures that once a thread has locked a piece of code then no other
thread can execute the same region until it is unlocked by the thread who
locked it.
Syntax:
1. int pthread_mutex_init(pthread_mutex_t *restrict mutex, const
pthread_mutexattr_t *restrict attr) : Creates a mutex, referenced by mutex,
with attributes specified by attr. If attr is NULL, the default mutex attribute
(NONRECURSIVE) is used.
Returned value
If successful, pthread_mutex_init() returns 0, and the state of the mutex
becomes initialized and unlocked.
If unsuccessful, pthread_mutex_init() returns -1.
2. int pthread_mutex_lock(pthread_mutex_t *mutex) : Locks a mutex object,
which identifies a mutex. If the mutex is already locked by another thread,
the thread waits for the mutex to become available. The thread that has
locked a mutex becomes its current owner and remains the owner until the
same thread has unlocked it. When the mutex has the attribute of
recursive, the use of the lock may be different. When this kind of mutex is
locked multiple times by the same thread, then a count is incremented and
no waiting thread is posted. The owning thread must call
pthread_mutex_unlock() the same number of times to decrement the
count to zero.
Returned value
If successful, pthread_mutex_lock() returns 0.
If unsuccessful, pthread_mutex_lock() returns -1.
int pthread_mutex_unlock(pthread_mutex_t *mutex)
int pthread_mutex_destroy(pthread_mutex_t *mutex)