Lab 5
Lab 5
BS IT F20
Fall 2022
Prompt:
Prompt command changes the Cmd.exe command prompt, including displaying any text you
want, such as the name of the current directory, the time and date, or the Microsoft Windows
version number.
If used without parameters, this command resets the command prompt to the default setting,
which is the current drive letter and directory followed by the greater than symbol (>).
To set command prompt with “OS Lab BITF20M” followed by greater than sign ($g):
To set a two-line command prompt with the current time and date on the first line and the
greater than sign on the next line, type:
To run the above program by passing argument from command line will take place with
the following commands:
g++ source.cpp –o main
main hello os lab
Example # 2:
Using the c program below:
To run the above program by passing argument from command line will take place with
the following commands:
gcc source.c –o main
main hello os lab
Properties:
a)- They are passed to main() function.
b)- They are parameters/arguments supplied to the program when it is invoked.
c)- argv[agrc] is a NULL pointer.
d)- argv[0] holds the name of the program.
e)- argv[1] points to the first command line argument and argv[n] points last argument.
Note:
You pass all the command line arguments separated by a space, but if argument itself has a
space then you can pass such arguments by putting them inside double quotes “” or single
quotes ‘’.
Linux Commands:
Head Command:
Head command is used to print the top n number of lines of the given input.
By default, it prints the first 10 lines of the specified file.
If more than one file name is provided then data from each file is preceded by its file name.
Syntax: head [options] … [file]…
In the above snapshot, we see that 16 lines are there in file named “exm”.
Now, we will use the head command to see only the first 10 lines of this file.
Example # 1:
head exm
Options Action
-n Displays the lines specified after –n
-c Displays the bytes specified after –n
Example # 2:
head –n <number of lines>exm
Prints the first ‘n’ lines instead of first 10 lines.
Example # 3:
head –n 3 file.txt
Example # 4:
Using pipe:
Tail Command:
Tail command is used to print the last n number of lines of the given input.
By default, it prints the first 20 lines of the specified file.
If more than one file name is provided, then data from each file is preceded by its file name.
Syntax: tail [options] … [file]…
Example # 1:
We can print the last 10 lines of any file by using the tail command as shown below:
tail exm
Example # 2:
We can also limit the number of line we want to display by sing the tail command.
tail –n 5 exm
Now, the command above will only display the last 5 lines of the file “exm” as shown below:
Tee Command:
Tee is an external command which is not the feature of the shell.
Handles character stream by duplicating its input.
It saves the one copy in the file and and writes the other in the standard output.
Example # 1:
tee f1.txt
The command above will make the file f1.txt if it does not exist then whatever we write
on the screen, it will be shown again on the standard output and also it will be saved in
our newly created file f1.txt. All this will happen due to the tee command.
Example # 2:
We can also use the tee command with pipes as shown below:
ls | tee list
In the above command, ‘list’ is the file name in which tee command will save the output.
Let’s have a look on the newly created file ‘list’:
gzip Command:
Gzip is one of the most popular compression algorithms that allow you to reduce the size of a
file and keep the original file mode, ownership, and timestamp. Gzip also refers to the .gz file
format and the gzip utility which is used to compress and decompress files.
Gzip compresses only single files and creates a compressed file for each given file. By
convention, the name of a file compressed with Gzip should end with either .gz or .z.
To compress a single file invoke the gzip command followed by the filename:
gzip filenameCopy
gzip will create a file filename.gz and delete the original file.
By default, gzip keeps the original file timestamp, mode, ownership, and name in the
compressed file.
Example # 1:
In the example below you can check the file size the before and after using gzip command.
Example # 2:
If you want to keep the input (original) file, use the -k option:
gzip -k filename
Example # 3:
Use the -v option if you want to see the percentage reduction and the names of the files
that are being processed:
gzip -v filename
output: filename: 7.5% -- replaced with filename.gz
Example # 4:
gzip -r directoryCopy
gzip will recursively traverse through the whole directory structure and compress all the
files in the directory and it’s subdirectories.
gunzip Command:
A command that you can use to decompress a Gzip file is gunzip . This command is basically an
alias to gzip -d:
gunzip -r directoryCopy
gzip will recursively traverse through the whole directory structure and decompress all the
files in the directory and it’s subdirectories.
Nice & Renice Commands:
nice command in Linux helps in execution of a program/process with modified scheduling
priority. It launches a process with a user-defined scheduling priority. In this, if we give a
process a higher priority, then Kernel will allocate more CPU time to that process. Whereas
the renice command allows you to change and modify the scheduling priority of an already
running process. Linux Kernel schedules the process and allocates CPU time accordingly for
each of them.
The range of nice value is from -20 to +19. The more the nice value the lower will be the
priority and vice versa.
Example # 1:
In the example below, you can see that the by default the priority of every process is 80 and
nice value of every process is 0. We have given the nice value on our own to the sleep process
which is running in the background. You can see that the nice value of sleep process is the
same as we have given and according to that nice value it’s CPU also sets it’s priority.
Renice:
The purpose of renice command is same as that the nice command but we use renice command
to change the nice value of already running processes.
Note:
We can use nice command only if we run it as admin i.e. as root user.
Example # 2:
In the snapshot below, you can see that the terminal did not changed the nice value because a
normal user was running this command but as soon as we ran it as a root user by it changed the
nice value of our sleep process from +10 to -10 and it’s priority is changed from 90 to 70.
Schedtool Command:
This command is used to Query and set per process scheduling parameters. It is used to:
To change the Scheduling Policy
To change the nice value
To change the static priority(1-99) It’s for only real time processes. It is normally
0.
Scheduling Policy:
It is of two types:
1)- Real Time:
It is further divided into:
SCHED_RR
SCHED_FIFO
2)- Conventional
It is further divided into:
SCHED_NORMAL By Default
SCHED_BATCH cpu intensive
SCHED_ISO unused
SCHED_IDLEPRIO for very low priority
If we want to see the attributes of a process we can do this with the following command:
schedtool 3199
The command above will display the attributes of a process whose PID is 3199
schedtool –r It displays the scheduling policies.
Example # 1:
See the below example in which we have changed the scheduling policy of a process
whose PID is 22536. By default, is was SCHED_NORMAL and we changed it to
SCHED_BATCH by using the command below:
schedtool -B 22536
Example # 2:
In this example, I will change the nice value of the same process with the following
command:
schedtool –n 7 22536
After the execution of the above command, the nice value of this process will be
changed from -10 to 7.
Command Line Arguments (In Linux):
As we have discussed about passing command line arguments in windows let’s do this in linux.
Comand Line arguments are given after the name of the program in command-line shell of
operating systems. To pass command line arguments, we typically define main() with two
arguments : first argument is the number of command line arguments and second is list of
command-line arguments.
Example # 1:
Let’s assume the following c++ program.
Now we will pass the arguments to this program from the command line by using:
g++ fCode.cpp –o exe
./exe hello world
Example # 2:
Using simple fork program:
Let’s assume the following c program in which we will use fork()
Now we will pass the arguments to this program from the command line by using and will see
how this program will run:
g myFork.c –o exe
./exe hello 2
You can clearly see the different behavior of this program. When I passed more than 2
arguments it went inside the if condition otherwise it just simply terminated.
Fork:
Fork system call is used for creating a new process, which is called child process, which runs
concurrently with the process that makes the fork() call (parent process). After a new child
process is created, both processes will execute the next instruction following the fork()
system call. A child process uses the same pc(program counter), same CPU registers, same
open files which use in the parent process.
It takes no parameters and returns an integer value. Below are different values returned by
fork().
Negative Value: creation of a child process was unsuccessful.
Zero: Returned to the newly created child process.
Positive value: Returned to parent or caller. The value contains process ID of newly created
child process.