Monday 29 August 2022

Experiment - 1 Installing Linux using CD-ROM

Installing Linux using CD-ROM

Those who like the way a CD runs should try using this method.


how to install Linux on PC

  • Step 1– Insert the Red Hat Linux DVD into the DVD‐drive of your computer. As soon as the following screen pops up, press ‘Enter’ to install Red Hat Enterprise Linux (RHEL) through GUI mode.
  • Step 2‐ RHEL installer would then prompt you conduct a check as to whether the CD media from which you’re installing is functioning correctly or not. Choose ‘Skip’, press enter and the installation would begin.
  • Step 3‐ Next, we need to select the language‐ English or any other language as per your preference, and then press ‘Next’.
  • Step 4‐ In this step, the RHEL installer would ask you about the appropriate type of keyboard for the system. We take the ‘US English’ keyboard, you can pick any other option depending on the type of your keyboard. Then press ‘Next’ to move to the next step
  • Step 5‐ Next, the installer would ask for an ‘installation number’ if you wish to install full set of Red Hat functionalities. Enter the installation number and press ‘OK’ if you have an officially licensed installation number
  • Step 6‐ The Red Hat installer would then require you to create partitions in your computer’s hard disk for the installation. You can do it in four ways but the simplest way is to select ‘Use free space on selected drives and create default layout’ as this option will not affect any other OS residing in your system.
  • Step 7‐ In this step you must create the required system partitions and mount points such as ‘/boot’, ‘/home’, ‘swap’ etc which are required for the Linux’s proper functioning. 
  • Step 8‐ This step pertains to the default OS that will be loaded by the GRUB loader
  • Step 9‐ This step pertains to the network settings of the Linux system that you are going to install. You can select the Ethernet devices through which the system would communicate with other devices in the network.
  • Step 10‐ The next step is to adjust the system clock to your particular time zone. Select your time zone and then click ‘Next’.
  • Step 11– This is a very important step that deals with the root(super‐user) password for the system . Type the password and confirm it and then click next.
  • Step 12– The RHEL installer would then prompt you about if you wish to install some extra ‘Software Development’ or ‘Web Server’ features. By default, keep it at ‘Customize later’ and press ‘Next’.
  • Step 13‐ This next step will initiate the installation of Red Hat Linux, press ‘Next’ to begin the process.
  • Step 14‐ Upon the completion of installation you should the following screen. Press Reboot and you’d be ready to use your newly installed Red Hat Linux OS.

 

Theory -10 :- What is a Printer and what are the different types of Printers?

What is a Printer and what are the different types of Printers?

Printers

Definition: Printers are the most commonly used and popular output devices. They provide information in a permanent readable from. They produce printed output of results, programs and data. Prints text or image on paper called hard copy. The printing speed of a printer is measured in pages per minute (ppm).Printer resolution is often measured in dots per inch (dpi).


There are two types of printers:

  1. Impact Printers
  2. Non-Impact Printers

 

IMPACT PRINTER 

Impact printers these Printers use an Electro-mechanical Mechanism.  An impact printer refers to all those printers whose printing heads touch the paper. His mechanism uses hammers or pins to strike against a ribbon and paper to print the text or image.

Characteristics of Impact Printers are the following:  

  • Very low consumable costs
  • Very noisy
  •  Useful for bulk printing due to low cost
  •  There is physical contact with the paper to produce an image

 

Types of Impact Printers

Impact printers are of two Types:               

  • Character Printer        
  • Line Printer

 

CHARACTER PRINTER                

  
A Character printer prints a single character at a time. They are low speed printers. Their printing speed lies in the range of 10-600 character / second.       


Types of Character printers    


Character printers are generally of two types:-

  • Daisy wheel Printer
  •  Dot Matrix Printer

 

DAISY-WHEEL PRINTERS


Also known as Letter Quality Printer. These printers use a print wheel font, which is termed as daisy wheel. Each petal or hammer of Daisy-Wheel has a character raised on it. A motor spins the wheel at a rapid rate. When the desired character spins to the correct position, a print hammer strikes it to produce the output.   

   
The speed of these Daisy-Wheel Printers normally ranged between 10-75 characters per second. The noise levels of these printers are high. These printers are most commonly used in electronic typewriter.


 


Advantages of a Daisy-Wheel Printer

  • Can print letter quality characters.
  • Gives fine quality output. 

Disadvantages of a Daisy-Wheel Printer

        • Printing speed is very slow
        • Noisy
        • Cannot print Graphics
        • They are very costly..




DOT MATRIX PRINTER 



These printers print each character as a pattern of dots. The print head is made up of a matrix of tiny needles, typically 9 rows with 7 columns (9 *7 matrix needles). These shapes of each character are formed in the form of tiny dots.   


The printing quality of a dot matrix printer is inferior to a daisy wheel printer. But the printing speed of a Dot Matrix Printer is range from 40 to 600 characters per second. Dot Matrix Printers are less expensive than daisy – wheel printer.


The main advantage of dot matrix printer over latter – quality printer is that dot – matrix printer can print characters in different shape, size and fonts. It has ability to print charts and graphics.

 

Advantages of a dot matrix printer

  • Dot matrix printer uses continuous paper rather than requiring individual sheet.
  • Dot matrix printer is fast and cheep.
  • Dot matrix printer can print charts and graphics.

Disadvantages of a dot matrix printer

  • Dot matrix printers are noisy.
  • Dot matrix printers do not produce high quality.

 

LINE PRINTERS            


Line printers are impact printers used with mini and mainframe computers. For producing large volume outputs. These printers print one line of the text at a time that is why known as line printer. Its printing speed lies in the range of 300-3000 lines per minute (LPM). Actually line printer prints only one character at a time but because of its printing speed, observer observes that it prints a whole line a time.

 


Types of Line Printers

Line Printers are of two types

  •   Drum Printer
  •  Chain Printer

 

DRUM PRINTER  


The drum printer consists of a cylindrical drum. Asset of characters are embossed on its surface t print. A set of print hammers is associated with each character. As the drum rotates, the hammer wait for desired character and is activated when character appear in front of hammer. The hammer is placed behind the paper. There hammers strike the paper along with ribbon (ribbon is placed between hammer and drum) against the embossed character on the surface one revolution of drum is needed to print a line. As the line is printed the paper moves upward to print next line.



CHAIN PRINTER  

A chain containing characters is used for printing. The chain knows as print chain rotates very rapidly. With each link of the chain is character font. Magnetically driven hammers are there in each print position and all the characters which are to be printed are received by the printer through processor. When the desired character comes in the print position the hammers strike the ribbon and paper against the character thus it prints one line at a time. It is very noisy and its speed ranges from 400-24000 line per minute (LPM).          

 

NON -IMPACT PRINTER

These printer uses ink and special electrical machines for producing outputs. Non – impact printers are all those printers whose printing heads do not touch paper. A non impact printer forms characters and image on a piece of paper without actually striking the paper.

 


Types of Non -Impact Printers

1.     Laser printer

2.     Inkjet printer

3.     Thermal printer

 

LASER PRINTERS  


They print one page at a time thus laser printer is also referred as Page Printer. A laser printer uses electronics, lasers, xerography and other techniques, which is called electro photographic technique.  


A laser beam is directed across the surface of a light or photosensitive drum. An image is produced, with the use of raster scan principal, in the form of tiny dots. The laser exposed areas attract toner (or ink power). There after the drum transfers the toner to the paper. The paper then moves to a fusing station where the toner is permanently fused on the paper with heat or pressure. After this the drum is discharged and cleaned and ready for processing the next page.    


Advantages of Laser Printers

  • Very high speed .
  • Low noise level
  • Low maintenance requirement.
  • Very high quality output on ordinary paper.
  • Good graphics quality (300 dpi to 1200 dpi) and excellent graphics capabilities.
  • Supports many fonts and different character size.
  • Color printing possible

 Disadvantages of Laser Printers

  • Very Expensive.
  • Require periodic maintenance

 

INKJET PRINTERS       
 

Inkjet printer is non impact character printer. It uses the dot matrix approach to print to print text and graphics. However the dots are formed by tiny droplets of ink. They print characters by spraying small drops of ink onto paper. For spraying drops nozzles are used. Special type of ink having high iron content is used for forming drops. Droplets of ink are electrically charged after leaving a nozzle when it passes through a valve. The droplets are then guided to the proper position on the paper by electrically charged horizontally and vertically deflection plates.
These printers can print 40-300 CPS (character/sec.) and can produce multi-color printouts.


Advantages of Ink jet Printers

  • High quality text and graphics.
  • Produces no noise

Disadvantages of Thermal Printers

  • Its speed is slow than other impact printer and no- impact printers.
  • Its image quality is also lower than other printer [low dpi (dot per inch) , bout 180 dpi].
  • Ink cartridge is costly.




THERMAL PRINTER     


The printer that produces images by pushing electrically heated pins against special heat – sensitive paper, is known as thermal printer.
This type of printer uses a special heat sensitive paper. These papers have a special heat sensitive coating. When a spot on the special paper is heated, it becomes dark. A character is printed with a matrix of dots. The heating element is heated by electric current. We can also say the heat sensitive paper ass chemically treated paper. In this type of printer neither the ink nor the ribbon in involved.
To print a character the printing head is moved first to the correct character position. Then the heating elements of desired character are turned on. After a short time they are turned off. There after the print head is moved to the next character. Such printers have a speed of about 200 characters per second.

Advantages of Thermal Printers

  • Low noise
  • Can produce high quality color output.

Disadvantages of Thermal Printers

  • Expensive
  • Slow Speed
  • Required special paper



 

Monday 8 August 2022

THEORY -9 :- What are Shell Scripts? in LINUX

What are Shell Scripts?

In the simplest terms, a shell script is a file containing a series of commands. The shell reads this file and carries out the commands as though they have been entered directly on the command line.

The shell is somewhat unique, in that it is both a powerful command line interface to the system and a scripting language interpreter. As we will see, most of the things that can be done on the command line can be done in scripts, and most of the things that can be done in scripts can be done on the command line.


How to determine Shell

You can get the name of your shell prompt, with following command :

Syntax:

echo $SHELL  

Look at the above snapshot, with the help of above command we got the name of our shell which is 'bash'.

The $ sign stands for a shell variable, echo will return the text whatever you typed in


What are "Commands?"


 

Commands can be one of 4 different kinds:

 

  1. An executable program like all those files we saw in /usr/bin. Within this category, programs can be compiled binaries such as programs written in C and C++, or programs written in scripting languages such as the shell, Perl, Python, Ruby, etc. 
  2. A command built into the shell itself.                                        bash provides a number of commands internally called shell built-ins. The cd command, for example, is a shell built-in.
  3. A shell function.                                                                        These are miniature shell scripts incorporated into the environment. We will cover configuring the environment and writing shell functions in later lessons, but for now, just be aware that they exist.
  4. An alias.                                                                        Commands that we can define ourselves, built from other commands. This will be covered in a later lesson.
 















shell as a programming language.

The following steps are required to write shell script:

 1. Use any editor like pico or vi to write shell script.

2. After writing shell script, set execute permission for your script file as follows: >chmod u+x your‐script‐name

3. Execute your script as:

>./your‐script‐name

 >sh your‐script‐name

>bash your‐script‐name

Now, open an editor (for example by using pico), give the name first.sh to your file and write in this file the following commands:

 # My first shell script

clear

echo "Hello World"

After saving the above script, you can run it as follows:

 >./first.sh

Just notice what happens.

>bash your‐script‐name

Now, open an editor (for example by using pico), give the name first.sh to your file and write in this file the following commands: # My first shell script

clear echo "Hello World"

After saving the above script, you can run it as follows:

 >./first.sh Just notice what happens.

Tip: For shell script file try to give file extension such as .sh or .scr, which can be easily identified by you as shell script.

In shell script,

 there are two types of variable:

 1. System variables ‐ Created and maintained by Unix/Linux itself. This type of variables is defined in CAPITAL LETTERS.

2. User defined variables (UDV) ‐ Created and maintained by user. This type of variables is defined in lower letters. You can see system variables by giving the command

>set To define UDV use the following syntax variable_name=value A value value is assigned to the given variable name. The value must be on right side

of the sign =.

Syntax: expr op1 math‐operator op2

Examples:           expr 1 + 3

expr 2 ‐ 1

 expr 10 / 2

expr 20 % 3

expr 10 \* 3

echo `expr 6 + 3`

>echo "Today is date"

Can't print message with today's date.

 >echo "Today is `date`"

 It will print today's date as Today is Fri April... Can you see that the `date` statement

uses back quote?

Try the following command:

>ls grate_stories_of It will print message‐ grate_stories_of:

No such file or directory

ls is the name of a command, which will be executed by the shell. What do we mean by commands? Which are the most used commands?




while [ condition ] do command1 .... done 4. Useful shell commands Purpose Command ‐ Syntax Example Translate a range of characters into another range of characters tr "pattern‐1" "pattern‐2" >tr "[a‐z]" "[A‐Z]" Pattern scanning and processing language awk 'pattern‐action' file >awk '/zebra/{ print $3 }' test Stream editor sed 'expression' file sed '{ expression1; expression2; }' file >sed '/zebra/s//monkey/g' test Report or filter out repeated lines in a file uniq file >sort test | uniq Search a file for a pattern grep 'pattern' file >grep –v 'zebra' test 5. Commands related with process In the following, there are the most commonly used command(s) related with process: Purpose Command Example To see currently running processes ps >ps To stop any process by PID, i.e. to kill a process kill {PID} >kill 1012 To stop processes by name, i.e. to kill a process killall{process‐name} >killall httpd To get information about all running processes ps –ag >ps ‐ag Shell Script Tutorial Paraskevi Raftopoulou p. 12/12 To stop all processes except your shell kill 0 >kill 0 For background processing command & >./myScript.sh & To display the owner of the processes along with the processes ps aux >ps aux To see if a particular process is running or not. ps ax | grep process‐  U‐want‐to see >ps ax | grep httpd To see currently running processes and other information like memory and CPU usage with real time updates. top Note that to exit from top command press q. >top To display a tree of processes pstree >pstree 




Saturday 6 August 2022

Theory - 8 : -Processes in Linux

Processes in Linux

A Linux process is a program running in the Linux system. Depending on Linux distributions, it's also known as service. In Linux community however, a Linux process is called daemon.

When you start a program or running an application in Linux, you actually execute that program. A Linux process (a daemon), running in foreground or in the background, uses memory and CPU resources. That's why we need to manage Linux process. Keeping unused Linux process running in the system is a waste and also expose your system to security threat.

We are going to learn and practice Linux process management using Linux ps command, pstree command and top command in this tutorial.

In Linux, every running process or daemon is given an identity number called PID (Process ID). The process id is unique. We can terminate unused program in the system by stopping its process id.

In order to manage Linux processes, we need to identify some process information such as who's responsible for the process, which terminal the process is running from and what command used to run the process.

We can view a particular Linux process by providing its characteristic. A Linux command which can be used to check the ID of a running process in Linux system is the ps command.

1. ps (Processes)

Processes are the programs that are running on your machine. They are managed by the kernel and each process has an ID associated with it called the process ID (PID). This PID is assigned in the order that processes are created.

Go ahead and run the ps command to see a list of running processes:

$ ps

PID              TTY                       STAT                 TIME              CMD

41230          pts/4                        Ss                    00:00:00            bash

51224          pts/4                        R+                   00:00:00              ps


This shows you a quick snapshot of the current processes: 

  • PID: Process ID
  • TTY: Controlling terminal associated with the process (we'll go in detail about this later)
  • STAT: Process status code
  • TIME: Total CPU usage time
  • CMD: Name of executable/command

 

If you look at the man page for ps you'll see that there are lots of command options you can pass, they will vary depending on what options you want to use - BSD, GNU or Unix. In my opinion the BSD style is more popular to use, so we're gonna go with that. If you are curious the difference between the styles is the amount of dashes you use and the flags.

$ ps aux

The a displays all processes running, including the ones being ran by other users. The u shows more details about the processes. And finally the x lists all processes that don't have a TTY associated with it, these programs will show a ? in the TTY field, they are most common in daemon processes that launch as part of the system startup.

You'll notice you're seeing a lot more fields now, no need to memorize them all, in a later course on advanced processes, we'll go over some of these again:

  • USER: The effective user (the one whose access we are using)
  • PID: Process ID
  • %CPU: CPU time used divided by the time the process has been running
  • %MEM: Ratio of the process's resident set size to the physical memory on the machine
  • VSZ: Virtual memory usage of the entire process
  • RSS: Resident set size, the non-swapped physical memory that a task has used
  • TTY: Controlling terminal associated with the process
  • STAT: Process status code
  • START: Start time of the process
  • TIME: Total CPU usage time
  • COMMAND: Name of executable/command

The ps command can get a little messy to look at, for now the fields we will look at the most are PID, STAT and COMMAND.

Another very useful command is the top command, top gives you real time information about the processes running on your system instead of a snapshot. By default you'll get a refresh every 10 seconds. Top is an extremely useful tool to see what processes are taking up a lot of your resources.

$ top


2. Controlling Terminal

We discussed how there is a TTY field in the ps output. The TTY is the terminal that executed the command.

There are two types of terminals, regular terminal devices and pseudo terminal devices. A regular terminal device is a native terminal device that you can type into and send output to your system, this sounds like the terminal application you've been launching to get to your shell, but it's not.

We're goanna segue so you can see this action, go ahead and type Ctrl-Alt-F1 to get into TTY1 (the first virtual console), you'll notice how you don't have anything except the terminal, no graphics, etc. This is considered a regular terminal device; you can exit this with Ctrl-Alt-F7.

A pseudo terminal is what you've been used to working in, they emulate terminals with the shell terminal window and are denoted by PTS . If you look at ps again, you'll see your shell process under pts/*.

Ok, now circling back to the controlling terminal, processes are usually bound to a controlling terminal. For example, if you were running a program on your shell window such as find and you closed the window, your process would also go with it.

There are processes such as daemon processes, which are special processes that are essentially keeping the system running. They often start at system boot and usually get terminated when the system is shutdown. They run in the background and since we don't want these special processes to get terminated they are not bound to a controlling terminal. In the ps output, the TTY is listed as a? Meaning it does not have a controlling terminal.

3. Process Details

Before we get into more practical applications of processes, we have to first understand what they are and how they work. This part can get confusing since we are diving into the nitty gritty, so feel free to come back to this lesson if you don't want to learn about it now.

A process like we said before is a running program on the system, more precisely it's the system allocating memory, CPU, I/O to make the program run. A process is an instance of a running program, go ahead and open 3 terminal windows, in two windows, run the cat command without passing any options (the cat process will stay open as a process because it expects stdin). Now in the third window run: ps aux | grep cat. You'll see that there are two processes for cat, even though they are calling the same program.

The kernel is in charge of processes, when we run a program the kernel loads up the code of the program in memory, determines and allocates resources and then keeps tabs on each process, it knows:

  • The status of the process
  • The resources the process is using and receives
  • The process owner
  • Signal handling (more on that later)
  • And basically everything else

All processes are trying to get a taste of that sweet resource pie, it's the kernel's job to make sure that processes get the right amount of resources depending on process demands. When a process ends, the resources it used are now freed up for other processes.

4. Process Creation

Again this lesson and the next are purely information to let you see what's under the hood, feel free to circle back to this once you've worked with processes a bit more.

When a new process is created, an existing process basically clones itself using something called the fork system call (system calls will be discussed very far into the future). The fork system call creates a mostly identical child process, this child process takes on a new process ID (PID) and the original process becomes its parent process and has something called a parent process ID PPID. Afterwards, the child process can either continue to use the same program its parent was using before or more often use the execve system call to launch up a new program. This system call destroys the memory management that the kernel put into place for that process and sets up new ones for the new program.

We can see this in action:

$ ps l

The l option gives us a "long format" or even more detailed view of our running processes. You'll see a column labelled PPID, this is the parent ID. Now look at your terminal, you'll see a process running that is your shell, so on my system I have a process running bash. Now remember when you ran the ps l command, you were running it from the process that was running bash. Now you'll see that the PID of the bash shell is the PPID of the ps l command.

So if every process has to have a parent and they are just forks of each other, there must be a mother of all processes right? You are correct, when the system boots up, the kernels creates a process called init, it has a PID of 1. The init process can't be terminated unless the system shuts down. It runs with root privileges and runs many processes that keep the system running. We will take a closer look at init in the system bootup course, for now just know it is the process that spawns all other processes.

5. Process Termination

Now that we know what goes on when a process gets created, what is happening when we don't need it anymore? Be forewarned, sometimes Linux can get a little dark...

A process can exit using the _exit system call, this will free up the resources that process was using for reallocation. So when a process is ready to terminate, it lets the kernel know why it's terminating with something called a termination status. Most commonly a status of 0 means that the process succeeded. However, that's not enough to completely terminate a process. The parent process has to acknowledge the termination of the child process by using the wait system call and what this does is it checks the termination status of the child process. I know it's gruesome to think about, but the wait call is a necessity, after all what parent wouldn't want to know how their child died?

There is another way to terminate a process and that involves using signals, which we will discuss soon.

Orphan Processes

When a parent process dies before a child process, the kernel knows that it's not going to get a wait call, so instead it makes these processes "orphans" and puts them under the care of init (remember mother of all processes). Init will eventually perform the wait system call for these orphans so they can die.

Zombie Processes

What happens when a child terminates and the parent process hasn't called wait yet? We still want to be able to see how a child process terminated, so even though the child process finished, the kernel turns the child process into a zombie process. The resources the child process used are still freed up for other processes, however there is still an entry in the process table for this zombie. Zombie processes also cannot be killed, since they are technically "dead", so you can't use signals to kill them. Eventually if the parent process calls the wait system call, the zombie will disappear, this is known as "reaping". If the parent doesn't perform a wait call, init will adopt the zombie and automatically perform wait and remove the zombie. It can be a bad thing to have too many zombie processes, since they take up space on the process table, if it fills up it will prevent other processes from running.


6. Signals

A signal is a notification to a process that something has happened.

Why we have signals

They are software interrupts and they have lots of uses:

  • A user can type one of the special terminal characters (Ctrl-C) or (Ctrl-Z) to kill, interrupt or suspend processes
  • Hardware issues can occur and the kernel wants to notify the process
  • Software issues can occur and the kernel wants to notify the process
  • They are basically ways processes can communicate

Signal process

When a signal is generated by some event, it's then delivered to a process, it's considered in a pending state until it's delivered. When the process is ran, the signal will be delivered. However, processes have signal masks and they can set signal delivery to be blocked if specified. When a signal is delivered, a process can do a multitude of things: 

  • Ignore the signal
  • "Catch" the signal and perform a specific handler routine
  • Process can be terminated, as opposed to the normal exit system call
  • Block the signal, depending on the signal mask

Common signals 

Each signal is defined by integers with symbolic names that are in the form of SIGxxx. Some of the most common signals are: 

  • SIGHUP or HUP or 1: Hangup
  • SIGINT or INT or 2: Interrupt
  • SIGKILL or KILL or 9: Kill
  • SIGSEGV or SEGV or 11: Segmentation fault
  • SIGTERM or TERM or 15: Software termination
  • SIGSTOP or STOP: Stop

Numbers can vary with signals so they are usually referred by their names.

Some signals are unblock able, one example is the SIGKILL signal. The KILL signal destroys the process.

7. kill (Terminate)

You can send signals that terminate processes, such a command is aptly named the kill command.

$ kill 12445

The 12445 is the PID of the process you want to kill. By default it sends a TERM signal. The SIGTERM signal is sent to a process to request its termination by allowing it to cleanly release its resources and saving its state.

You can also specify a signal with the kill command:

$ kill -9 12445

This will run the SIGKILL signal and kill the process.

 

Differences between SIGHUP, SIGINT, SIGTERM, SIGKILL, SIGSTOP?

These signals all sound reasonably similar, but they do have their differences.

  • SIGHUP - Hangup, sent to a process when the controlling terminal is closed. For example, if you closed a terminal window that had a process running in it, you would get a SIGHUP signal. So basically you've been hung up on
  • SIGINT - Is an interrupt signal, so you can use Ctrl-C and the system will try to gracefully kill the process
  • SIGTERM - Kill the process, but allow it to do some cleanup first
  • SIGKILL - Kill the process, kill it with fire, doesn't do any cleanup
  • SIGSTOP - Stop/suspend a process

8. niceness

When you run multiple things on your computer, like perhaps Chrome, Microsoft Word or Photoshop at the same time, it may seem like these processes are running at the same time, but that isn't quite true.

Processes use the CPU for a small amount of time called a time slice. Then they pause for milliseconds and another process gets a little time slice. By default, process scheduling happens in this round-robin fashion. Every process gets enough time slices until it's finished processing. The kernel handles all of these switching of processes and it does a pretty good job at it most of the time.

Processes aren't able to decide when and how long they get CPU time, if all processes behaved normally they would each (roughly) get an equal amount of CPU time. However, there is a way to influence the kernel's process scheduling algorithm with a nice value. Niceness is a pretty weird name, but what it means is that processes have a number to determine their priority for the CPU. A high number means the process is nice and has a lower priority for the CPU and a low or negative number means the process is not very nice and it wants to get as much of the CPU as possible.


$ top

You can see a column for NI right now, that is the niceness level of a process.

To change the niceness level you can use the nice and renice commands:

$ nice -n 5 apt upgrade

The nice command is used to set priority for a new process. The renice command is used to set priority on an existing process.

$ renice 10 -p 3245


9. Process States

Let's take a look at the ps aux command again:

$ ps aux

In the STAT column, you'll see lots of values. A linux process can be in a number of different states. The most common state codes you'll see are described below:

  • R: running or runnable, it is just waiting for the CPU to process it
  • S: Interruptible sleep, waiting for an event to complete, such as input from the terminal
  • D: Uninterruptible sleep, processes that cannot be killed or interrupted with a signal, usually to make them go away you have to reboot or fix the issue
  • Z: Zombie, we discussed in a previous lesson that zombies are terminated processes that are waiting to have their statuses collected
  • T: Stopped, a process that has been suspended/stopped

10. /proc filesystem

Remember everything in Linux is a file, even processes. Process information is stored in a special filesystem known as the /proc filesystem.

$ ls /proc

You should see multiple values in here, there are sub-directories for every PID. If you looked at a PID in the ps output, you would be able to find it in the /proc directory.

Go ahead and enter one of the processes and look at that file:

$ cat /proc/12345/status

You should see process state information and well as more detailed information. The /proc directory is how the kernel is views the system, so there is a lot more information here than what you would see in ps.

11. Job Control

Let's say you're working on a single terminal window and you're running a command that is taking forever. You can't interact with the shell until it is complete, however we want to keep working on our machines, so we need that shell open. Fortunately we can control how our processes run with jobs:

Sending a job to the background

Appending an ampersand (&) to the command will run it in the background so you can still use your shell. Let's see an example:

  • $ sleep 1000 &
  • $ sleep 1001 &
  • $ sleep 1002 &

 

View all background jobs

Now you can view the jobs you just sent to the background.

 

$ jobs

 

[1]    Running     sleep 1000 &

[2]-   Running     sleep 1001 &

[3]+   Running     sleep 1002 &

 

This will show you the job id in the first column, then the status and the command that was run. The + next to the job ID means that it is the most recent background job that started. The job with the - is the second most recent command.

 

Sending a job to the background on existing job

If you already ran a job and want to send it to the background, you don't have to terminate it and start over again. First suspend the job with Ctrl-Z, then run the bg command to send it to the background.

pete@icebox ~ $ sleep 1003

^Z

[4]+    Stopped     sleep 1003

pete@icebox ~ $ bg

[4]+    sleep 1003 &

pete@icebox ~ $ jobs

  1.  Running     sleep 1000 &
  2.   Running     sleep 1001 &
  3.   Running     sleep 1002 &
  4.  Running     sleep 1003 &

 

Moving a job from the background to the foreground

To move a job out of the background just specify the job ID you want. If you run fg without any options, it will bring back the most recent background job (the job with the + sign next to it)

$ fg %1

Kill background jobs

Similar to moving jobs out of the background, you can use the same form to kill the processes by using their Job ID.

kill %1

Popular Posts