OS Lab Manual (4th Expt) - Muddappa
OS Lab Manual (4th Expt) - Muddappa
OS Lab Manual (4th Expt) - Muddappa
4.a) Linux commands related to process creation and management- system calls fork()
and exec(); bg, fg, nohup, pkill, nice, top, ps.
Process :
A process is any active (running) instance of a program. In other words, process is a program under
execution.
A new process can be created by the fork() system call.
The new process consists of a copy of the address space of the original process. fork()
creates new process from existing process.
Existing process is called the parent process and the process is created newly is called
child process.
Starting a Process
A process executes in two ways they are,
Foreground Processes
Background Processes
Foreground Processes
Every process by default runs in Foreground (which means the output is printed on the screen.) The
best example for the foreground process is the ls command which prints the output on the screen by
listing the files and directories.
When a program is running in the foreground, user cannot start another process without completing the
previous process. [ It is because of this reason foreground process is considered a time-consuming
process.]
Background Processes
When a process starts running and it is not visible on the screen it is called a background process.
User can simultaneously run ‘n’ number of commands in the background process.
To enable the background process, provide ampersand symbol (&) at the end of the command.
Example : ls &
bg: Puts a process in the background, allowing it to continue execution while the user interacts with the terminal.
15 | P a g e
when you run sleep 10 &, you are starting a process that sleeps for 10 seconds in the background, and you can
continue to use the terminal for other commands without waiting for the sleep command to complete.
After you execute sleep 10 &, you will see output similar to [2] 69. Here's what this output means: the background
job you started is the second background job, and its process ID is 69.
fg: Brings a background process to the foreground, making it the active process.
When you run fg %1, the job with job number 1 is moved to the foreground, and the shell waits for it to complete.
You can then interact with the process directly in the terminal.
Remember that you can use the jobs command to see a list of background jobs and their job numbers before
using fg.
(Note: If you want to bring a specific process to the foreground by using its PID, you can use the % symbol
followed by the PID as a job specification with fg.)
nohup: short for no hang up is a command in Linux systems that keep processes running even after
exiting the shell or terminal.
nohup prevents the processes or jobs from receiving the SIGHUP (Signal Hang UP)signal.
This is a signal that is sent to a process upon closing or exiting the terminal.
Every command in Linux starts a process at the time of its execution, which automatically
gets terminated upon exiting the terminal.
Suppose, user executing programs over SSH and if the connection drops, the session will be terminated,
all the executed processes will stop, and user may face a huge accidental crisis.
In such cases, running commands in the background can be very helpful to the user and this is where
nohup command comes into the picture. nohup (No Hang Up) is a command in Linux systems that runs
the process even after logging out from the shell/terminal.
Usually, every process in Linux systems is sent a SIGHUP (Signal Hang UP) which is responsible for
terminating the process after closing/exiting the terminal.
nohup command prevents the process from receiving this signal upon closing or exiting the
terminal/shell.
16 | P a g e
Once a job is started or executed using the nohup command, stdin will not be available to the user and
nohup.out file is used as the default file for stdout and stderr.
If the output of the nohup command is redirected to some other file, nohup.out file is not generated.
Syntax: nohup command [command-argument ...]
nohup command can be used to run multiple commands in the background.
Example 1:
nohup bash -c 'cal && ls'
in the above example bash -c 'cal && ls': This part of the command invokes the Bash shell (bash) and passes it a
series of commands to execute. The -c option tells Bash to read commands from the following string. In this case,
the string 'cal && ls' contains two commands separated by &&. cal is a command to display a calendar for the
current month, and ls is a command to list files and directories in the current directory.
Here, the output will be by default stored in nohup.out. To redirect it, type:
Syntax: nohup bash -c 'commands' > filename.txt
Example 2:
nohup bash -c 'cal && ls' > output.txt
Using nohup is particularly useful when you want to run a process that takes a long time to complete, and you
don't want it to be terminated when you log out or close your terminal session.
pkill:
Sends signals to processes based on their name or other attributes.
pkill: The command used to send signals to processes based on certain criteria.
My_process: The pattern or criteria used to match against process names. In this case, it targets
processes whose names contain the string "my_process."
The nice command in Unix-like operating systems is used to adjust the scheduling priority of a process.
The priority, also known as the "niceness," ranges from -20 (highest priority) to 19 (lowest priority). By
default, processes have a niceness of 0.
top:
Displays a dynamic view of system processes, updating in real-time.
18 | P a g e
hundredths of a second.
Ps(Process Status):
The default output of ps is a simple list of the processes running in current terminal.
In this output:
PID: Process ID
TTY: Terminal associated with the process
TIME: CPU time consumed by the process
CMD: Command being executed by the process
Provides a snapshot of the current processes or a detailed list of processes (Like Task Manager In
Windows.).
We can get the PID of a process using the command ‘pidof processname’
19 | P a g e
Crontab syntax:
To schedule a task using cron, you need to edit a special file called the crontab file in a text editor and add your
task in it in a particular format. Then cron will run the task for you at the time you specify in the crontab file.
You can specify any intervals of time, from seconds to weeks and even years
20 | P a g e
Example 1:
Run /home/folder/gfg-code.sh every hour, from 9:00 AM to 6:00 PM, everyday.
o 00 09-18 * * * /home/folder/gfg-code.sh
Run /usr/local/bin/backup at 11:30 PM, every weekday.
o 30 23 * * Mon, Tue, Wed, Thu, Fri /usr/local/bin/backup
Run sample-command.sh at 07:30, 09:30, 13:30 and 15:30.
o 30 07, 09, 13, 15 * * * sample-command.sh
at :
at command is a command-line utility that is used to schedule a command to be
executed at a particular time in the future.
Jobs created with at command are executed only once.
The at command can be used to execute any program or mail at any time in the future.
It executes commands at a particular time and accepts times of the form HH:MM to run a job
at a specific time of day.
In the command, expression like noon, midnight, teatime, tomorrow, next week, next
Monday, etc. could be used with at command to schedule a job.
Schedule a job for the coming Monday at a time twenty minutes later than the current
time:
o at Monday +20 minutes
Schedule a job to run at 1:45 Aug 12 2020:
o at 1:45 081220
21 | P a g e
Schedule a job to run at 3pm four days from now:
o at 3pm + 4 days
Schedule a job to shutdown the system at 4:30 today:
o echo "shutdown -h now" | at -m 4:30
Schedule a job to run five hour from now:
o at now +5 hours
22 | P a g e