0% found this document useful (0 votes)
146 views14 pages

Unix Pyq

Character class in UNIX matches any one character from a set of characters enclosed in brackets. For example, "p[aeiou]t" would match words like "pat", "pet", etc. The kernel manages operations of the computer hardware and runs first when the operating system loads. The shell provides an interface for users to input commands and run programs. There are different types of shells like Bourne and C shells that have different command sets. The date command displays the current system date and time by default. It can also be used to set the date/time with root privileges. The echo command displays text or strings passed as arguments. File attributes like compressed, immutable, etc. can be adjusted using

Uploaded by

Sourav Das
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
146 views14 pages

Unix Pyq

Character class in UNIX matches any one character from a set of characters enclosed in brackets. For example, "p[aeiou]t" would match words like "pat", "pet", etc. The kernel manages operations of the computer hardware and runs first when the operating system loads. The shell provides an interface for users to input commands and run programs. There are different types of shells like Bourne and C shells that have different command sets. The date command displays the current system date and time by default. It can also be used to set the date/time with root privileges. The echo command displays text or strings passed as arguments. File attributes like compressed, immutable, etc. can be adjusted using

Uploaded by

Sourav Das
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

1. What is character class in UNIX?

A character class matches any one character from a set of characters. A


typical format for this is the bracketed character class [...], which
matches any one character from the non-empty sequence of characters
enclosed within the brackets.
Thus, #rx"p[aeiou]t" matches pat, pet, pit, pot, put, and
nothing else.
_________________________________________________________
2. Briefly explain Karnel and Shell in UNIX.

Kernel is central component of an operating system that manages operations of


computer and hardware. It basically manages operations of memory and CPU time. It
is core component of an operating system. Kernel acts as a bridge between
applications and data processing performed at hardware level using inter-process
communication and system calls. 
Kernel loads first into memory when an operating system is loaded and remains into
memory until operating system is shut down again. It is responsible for various tasks
such as disk management, task management, and memory management. 

A Shell provides you with an interface to the Unix system. It


gathers input from you and executes programs based on that
input. When a program finishes executing, it displays that
program's output.

Shell is an environment in which we can run our commands,


programs, and shell scripts. There are different flavors of a shell,
just as there are different flavors of operating systems. Each flavor
of shell has its own set of recognized commands and functions.

Shell Types
In Unix, there are two major types of shells −
 Bourne shell − If you are using a Bourne-type shell, the $ character is the default
prompt.
 C shell − If you are using a C-type shell, the % character is the default prompt.
3. How to display system date in unix?

date command is used to display the system date and time. date command is also used
to set date and time of the system. By default the date command displays the date in the
time zone on which unix/linux operating system is configured.You must be the super-
user (root) to change the date and time. 
Syntax: 
date [OPTION]... [+FORMAT]
date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]].

Simply type the following command at the shell prompt to get the current date
in MM-DD-YYYY format: $ NOW = $(date +"%m-%d-%Y")

Current Month – Full Name


In the following script, we get current month as complete name of the month
using date command, and print it to standard console.
Example.sh
$month = $(date +%B)
echo "Current Month : $month"

4. Uses of echo command in unix.


echo command in linux is used to display line of text/string that are passed as
an argument . This is a built in command that is mostly used in shell scripts and
batch files to output status text to the screen or a file. 
Syntax :
echo [option] [string]
Displaying a text/string : Syntax :
echo [string]

5. File attributes in linux.


list file attribute using lsattr command

Attributes in Linux
Some filesystems support additional attributes (other than those described in the
preceding sections). In particular, some Linux-native filesystems support several
attributes that you can adjust with the chattr command. The files and directories
can have the following attributes:

Define each file attributes


The detailed meaning of these attributes according to the manual page is:

 a - append only: this attribute allows a file to be added to, but not


to be removed. It prevents accidental or malicious changes to files.
 c - compressed: it causes the kernel to compress data written to the
file automatically and uncompress it when it’s read back.
 d - no dump: it makes sure the file is not backed up in backups.
 e - extent format: it indicates that the file is using extents for
mapping the blocks on disk.
 i - immutable: it makes a file immutable, The file can’t be deleted,
links to it can’t be created, and the file can’t be renamed.
 s - secure deletion: it makes sure that recovery of a file is not
possible after it has been deleted.
 u - undeletable: When a file is deleted, its contents are saved which
allows a utility to be developed that works with that information to
salvage deleted files.

 A - no a time updates: Linux won’t update the access time stamp


when you access a file.
 D - synchronous directory updates: it makes sure that changes to
files are written to disk immediately, and not to cache first.
 S - synchronous updates: the changes on a file are written
synchronously on the disk.

6. Hard link and soft link.

What is the hard link?


A Hard Link is a copy of the original file that serves as a pointer to the same file,
allowing it to be accessed even if the original file is deleted or relocated. Unlike soft
links, modifications to hard-linked files affect other files, and the hard link remains active
even if the source file is deleted from the system.

On UNIX-based systems, a hard link is just another name for a file that already exists. It's
typically found in file systems that allow various hard links to the same file. Hard links
have the same Inode value, unlike soft links, but they point to the file location rather
than the directory.

What is the soft link?


A soft link is a short pointer file that links a filename to a pathname. It's nothing more
than a shortcut to the original file, much like the Windows OS's shortcut option. The soft
link serves as a pointer to another file without the file's actual contents. It allows the
users to delete or the soft links without affecting the original file's contents.

You may also use soft links to link files across the file system. Generally, the soft link is
an alias for the original file or directory. It helps to redirect to the specified file and
directory when accessed via the pathname indicated in the soft link's subject.

Hard Links Soft Links

It is a copy of the original file that serves as a It is a short pointer file that links a filename to a
pointer to the same file, allowing it to be pathname. It's nothing more than a shortcut to the
accessed even if the original file is deleted or original file, much like the Windows OS's shortcut
relocated. option.
It has a similar inode number to the target file. It has a different inode number.

It is not allowed the relative path. It allows both relative and absolute paths.

It cannot be established outside the file system. It may be established in the file system.

It is faster. It is slower.

The "ln" command is used to make a hard link The "ln -s" command is used to make a soft link in
in Linux. Linux.

It has an additional name for the original file It is different from the original file and is an
that references to the target file through inode. alternative for it, but it does not use inode.

It may only link to a file. It may link both to a directory or a file.

It remains valid even if the target file is deleted. It becomes invalid when the originating file is
deleted.

7. Explain tty , cat and cp command in unix.


tty : The tty command of terminal basically prints the file name of the terminal
connected to standard input. tty is short of teletype, but popularly known as a
terminal it allows you to interact with the system by passing on the data (you
input) to the system, and displaying the output produced by the system.
Syntax:
tty [OPTION]....
 -s, ––silent, ––quiet : Prints nothing, only returns an exit status.
 ––help : It will display the help message and exit.
 ––version : Prints the version information and exits.

cp : cp stands for a copy. This command is used to copy files or groups of files
or directories. It creates an exact image of a file on a disk with a different file
name. cp  command requires at least two filenames in its arguments. 
Syntax:
cp [OPTION] Source Destination
cp [OPTION] Source Directory
cp [OPTION] Source-1 Source-2 Source-3 Source-n Directory
Cat : (concatenate) command is very frequently used in Linux. It reads data from the
file and gives their content as output. It helps us to create, view, concatenate files. So let
us see some frequently used cat commands. 

1) To view a single file 


Command: 
 
$cat filename
Output 
 
It will show content of given filename

8. Unix process life cycle.

 Created-Process is newly created by system call, is not ready to run


 User running-Process is running in user mode which means it is a user
process.
 Kernel Running-Indicates process is a kernel process running in kernel
mode.
 Zombie- Process does not exist/ is terminated.
 Preempted- When process runs from kernel to user mode, it is said to be
preempted.
 Ready to run in memory- It indicated that process has reached a state where
it is ready to run in memory and is waiting for kernel to schedule it.
 Ready to run, swapped– Process is ready to run but no empty main memory
is present
 Sleep, swapped- Process has been swapped to secondary storage and is at a
blocked state.
 Asleep in memory- Process is in memory(not swapped to secondary storage)
but is in blocked state.

9. Aliases in UNIX

alias command instructs the shell to replace one string with another string while
executing the commands. 
When we often have to use a single big command multiple times, in those cases, we
create something called as alias for that command. Alias is like a shortcut command
which will have same functionality as if we are writing the whole command. 
Syntax:  
alias [-p] [name[=value] ... ]

In Linux, an alias is a shortcut that references a command. An alias replaces a string


that invokes a command in the Linux shell with another user-defined string.

Aliases are mostly used to replace long commands, improving efficiency and avoiding
potential spelling errors. Aliases can also replace commands with additional options,
making them easier to use.

Absolute and Relative Pathnames in UNIX

A path is a unique location to a file or a folder in a file system of an OS.A path to a file
is a combination of / and alpha-numeric characters.
Absolute Path-name

An absolute path is defined as specifying the location of a file or directory from the
root directory(/). In other words,we can say that an absolute path is a complete path
from start of actual file system from / directory.

To write an absolute path-name:


 Start at the root directory ( / ) and work down.
 Write a slash ( / ) after every directory name (last one is optional)
For Example :

cat /home/kt/abc.sql
 In the above example, if the first character of a pathname is /, the file’s location
must be determined with respect to root. When you have more than one / in a
pathname, for each such /, you have to descend one level in the file system like
in the above kt is one level below home, and thus two levels below root.

Relative path
Relative path is defined as the path related to the present working directly(pwd). It starts
at your current directory and never starts with a / .

To be more specific let’s take a look on the below figure in which if we are looking for
photos then absolute path for it will be provided as : /home/jono/photos but assuming that
we are already present in jono directory then the relative path for the same can be written
as simple photos.

Can two files have the same inode number?


Unix filesystems use the inode as the actual unique identifier of a file. So on a
single filesystem an inode is unique.

However on two different filesystems inode numbers can be the same. You’d
need to combine the inode with the device ID to be sure to have a unique
identifier.
And device ID can repeat on different systems. For that you’d need to combine it
with some unique system identifier.

Fork and Exec


process is a running instance of a program. Processes are created through different
system calls, most popular are fork() and exec().

fork()
pid_t pid = fork();
fork() creates a new process by duplicating the calling process, The new process,
referred to as child, is an exact duplicate of the calling process, referred to as parent,
except for the following :
1. The child has its own unique process ID, and this PID does not match the ID
of any existing process group.
2. The child’s parent process ID is the same as the parent’s process ID.
3. The child does not inherit its parent’s memory .

exec()
The exec() family of functions replaces the current process image with a new
process image. 

fork vs exec
 fork starts a new process which is a copy of the one that calls it, while exec
replaces the current process image with another one.
 Both parent and child processes are executed simultaneously in case of fork()
while Control never returns to the original program unless there is an exec()
error.

What is an inode?
An inode is a file data structure that stores information about any Linux file except its
name and data.
What are inodes used for?
Data is stored on your disk in the form of fixed-size blocks. If you save a file that exceeds a
standard block, your computer will find the next available segment on which to store the rest of
your file. Over time, that can get super confusing.

That’s where inodes come in. While they don’t contain any of the file’s actual data, it stores the
file’s metadata, including all the storage blocks on which the file’s data can be found. 

In what way programmer can use i-node?


In Unix based operating system each file is indexed by an Inode. Inode are special disk
blocks they are created when the file system is created. The number of Inode limits the
total number of files/directories that can be stored in the file system. 

Role of set-user-id

Setuid, which stands for set user ID on execution, is a special type of file permission in
Unix and Unix-like operating systems such as Linux and BSD. It is a security tool that
permits users to run certain programs with escalated privileges.

When an executable file's setuid permission is set, users may execute that program with a
level of access that matches the user who owns the file. For instance, when a user wants
to change their password, they run the passwd command. The passwd program is owned
by the root account and marked as setuid, so the user is temporarily granted root access
for that limited purpose.

What Does Sticky Bit Mean?


In Unix-like operating systems, a sticky bit is a permission bit which is set on
a file or folder, thereby permitting only the owner or root user of the file or
folder to modify, rename or delete the concerned directory or file. No other
user would be permitted to have these privileges on a file which has a sticky
bit. In Unix-like systems, without the sticky bit on, any user can modify,
rename or delete the directory or file regardless of the owner of the file or
folder.
Structure of the UNIX file system
All the files in the UNIX system are related to one another.

The Directory structure of the UNIX File system are given


below:
Root ( / ): The top of the directory structure is called root. It is also called parent
directory as it contains all the subdirectories of the UNIX file system. It is represented by
using slash symbol in UNIX system.

/lib: This directory contains all the information of system libraries functions and some
critical files such as kernel modules or device drivers. It also contains the system calls
that the compiler can include in a program.

/bin: This directory contains the system's binary files and certain fundamental utilities. It
is the directory for admin-level commands such as ls or cp. The path (var) always shows
this directory in the list.

/dev: It stands for "devices" and contains all the information about the device files used
in the UNIX system. These files do not occupy any space on the disk. It contains al;
information about hard disks, file representations of peripheral devices, floppy disks,
device information, and pseudo-devices.

/tmp: This directory contains the information of all the temporary files created by UNIX
or by the user in the UNIX system.
/usr: This directory contains all the information related to the users in a UNIX system. It
means it contains all the user account such as user1, user2, and so on.

Multi-programming VS Multi-tasking
CPU is a super fast device and keeping it occupied for a single task is never a good
idea. Considering the huge differences between CPU speed and IO speed, many
concepts like multiprogramming, multitasking, multithreading, etc have been
introduced to make better CPU utilisation. 
Multi programming:- 
Multi-programming increases CPU utilisation by organising jobs (code and data) so that
the CPU always has one to execute. The idea is to keep multiple jobs in main memory.
If one job gets occupied with IO, CPU can be assigned to other job. 
 

Multi-tasking:- 
Multi-tasking is a logical extension of multiprogramming. Multitasking is the ability of
an OS to execute more than one task simultaneously on a CPU machine.  These
multiple tasks share common resources (like CPU and memory). In multi-tasking
systems, the CPU executes multiple jobs by switching among them typically using a
small time quantum, and the switches occur so quickly that the users feel like interact
with each executing task at the same time. 
 
Note: 1. A task in a multitasking system is not whole application program but it can
refers to a “thread of execution” when one process is divided into sub-tasks. 
2. Multitasking OS are time sharing systems.
Difference between Multiprogramming and Multi-tasking 
Sr.no  Multiprogramming  Multi-tasking 

It includes the single CPU to execute It uses multiple tasks for the task
1. 
the program. allocation.

Concept of Context Switching is Concept of Context Switching and Time


2.
used. Sharing is used.

The processor is typically used in time


In multiprogrammed system, the sharing mode. Switching happens when
operating system simply switches to, either allowed time expires or where
3.
and executes, another job when there other reason for current process
current job needs to wait.  needs to wait (example process needs to
do IO).

In multi-tasking also increases CPU


Multi-programming increases CPU
4. utilization, it also increases
utilization by organizing jobs .
responsiveness.

5. The idea is to reduce the CPU idle The idea is to further extend the CPU
Sr.no  Multiprogramming  Multi-tasking 

Utilization concept by increasing


time for as long as possible.
responsiveness Time Sharing.

What is a daemon?
A daemon is also called background processes. It is a UNIX or Linux program that
executes inside the background. Almost every daemon contains names that finish with
"d" the letter. For example, sshd, this manages connections of SSH remote access, or the
httpd daemon that manages the Apache server. Often, Linux begins daemons at starting
time.

You might also like