Difference between revisions of "Tutorial8: Links / Process Management"

From CDOT Wiki
Jump to: navigation, search
(INVESTIGATION 1: LINKING FILES)
(INVESTIGATION 1: LINKING FILES)
Line 164: Line 164:
 
# Enter the following text displayed below:<br><br><span style="font-family:courier;font-weight:bold;">This is line one<br>This is line two<br>This is line three<br><br></span>
 
# Enter the following text displayed below:<br><br><span style="font-family:courier;font-weight:bold;">This is line one<br>This is line two<br>This is line three<br><br></span>
 
# Issue the following Linux command to create the following symbolic link in the same directory: <br><span style="color:blue;font-weight:bold;font-family:courier;">ln ~/links2/text-file.txt ~/links2/text-file.sym.lnk</span><br><br>
 
# Issue the following Linux command to create the following symbolic link in the same directory: <br><span style="color:blue;font-weight:bold;font-family:courier;">ln ~/links2/text-file.txt ~/links2/text-file.sym.lnk</span><br><br>
# Issue the '''ls -li''' command for the '''~/links2''' directory.<br><br>What do you notice about both of these file's i-node numbers?<br>What do you notice about the size of the file ~/links2/text-file.sym.lnk? What pathname do you think it represents?<br>
+
# Issue the '''ls -li''' command for the '''~/links2''' directory.<br><br>What do you notice about both of these file's i-node numbers?<br>What do you notice about the size of the file ~/links2/text-file.sym.lnk?<br>What pathname do you think it represents?<br>
 
# Change to your '''home''' directory.<br><br>
 
# Change to your '''home''' directory.<br><br>
 
# Issue the following Linux command to create the following symbolic link in your home directory: <br><span style="color:blue;font-weight:bold;font-family:courier;">ln ~/links2/text-file.txt ~/text-file.sym.lnk</span><br><br>
 
# Issue the following Linux command to create the following symbolic link in your home directory: <br><span style="color:blue;font-weight:bold;font-family:courier;">ln ~/links2/text-file.txt ~/text-file.sym.lnk</span><br><br>

Revision as of 16:56, 4 February 2020

LINKING FILES / MANAGING PROCESSES


Main Objectives of this Practice Tutorial

  • Understand the purpose and why links are used in Unix / Linux
  • Define the term inode number as it relates to a file on Unix / Linux
  • Define the terms: Hard Link and Symbolic Link
  • Issue the ln command to create hard and symbolic links
  • Define and understand the purpose of a process in Unix / Linux
  • Run and terminate processes in the foreground and background
  • Display and manipulate background and foreground processes


Tutorial Reference Material

Course Notes
Concepts / Commands
YouTube Videos
Course Notes:


Links
  • Hard Links
  • Symbolic Links

Managing Processes

  • Process Information
  • Manipulating Processes
  • Running commands / programs in background with &
Linux Commands Brauer Instructional Videos:

KEY CONCEPTS

Linking Files

Links are powerful and add flexibility to Linux filesystems because everything is a file.

There are two types of Linux filesystem links: hard and soft. The difference between the two types of links is significant, but both types are used to solve similar problems. They both provide multiple directory entries (or references) to a single file, but they do it quite differently.


Reference: https://opensource.com/article/17/6/linking-linux-filesystem


inode (index) Number of a File:

Each inode stores the attributes and disk block locations of the object's data.
(Image licensed under cc)
The inode number is like a finger-print, and usually is unique for each file on the Unix / Linux file system.

The inode (index node) is a data structure in a Unix-style file system that describes a file-system object such as a file or a directory. Each inode stores the attributes and disk block locations of the object's data. File-system object attributes may include metadata (times of last change, access, modification), as well as owner and permission data.

Reference: https://en.wikipedia.org/wiki/Inode


The inode number is like a finger-print, and usually is unique for each file on the Unix / Linux file system.
Referring to the diagram on the far right, issuing the ls command with the -i option
displays the inode number for each file. You can see that each file
(whether it is a directory or regular file) has its own unique inode number.


Hard Links:

(Image licensed under cc)
Image manipulated by author]
Hard links share the same inode number. Hard links are very good for backup purposes.

A Hard link is a reference to the physical data on a file system.

Advantages of hard links are that if one hard link remains (even if original file has been removed), the data in that hard linked file is NOT lost, a hard links will automatically change when a change to that original file or hard links occur since they share the same i-node number
and the Unix/Linux OS treats them all as the same file.

Disadvantages of hard links are that they take-up extra space,
you cannot hard link directory files, and you cannot hard link files from other Unix/Linux servers
(since the inode number may already be used by the other Unix/Linux server).


Examples:

touch myfile.txt
ln myfile.txt myfile1.hard.lnk
ln myfile.txt myfile2.hard.lnk
ln myfile.txt ~/backups/myfile.hard.lnk


Symbolic Links:

(Image licensed under cc)
Symbolic links do NOT share the same i-node number. Symbolic links are very good for short-cuts to other files (including directories) but NOT for backup purposes.

A Symbolic Link is an indirect pointer to a file and are also known as soft link or symlink.

Advantages of symbolic links are that they are shortcuts to other files, where the symbolic link only contains the pathname to the original file, you can create symbolic links
on different Unix/Linux servers, and that you can create symbolic links for directories.

Disadvantages of symbolic links are that they are NOT good for backup purposes
since a symbolic link can point to a nonexistent file (referred to as a "broken link").


Examples:

touch otherfile.txt
ln -s otherfile.txt otherfile1.sym.lnk
ln -s otherfile.txt otherfile2.sym.lnk
ln -s otherfile.txt ~/backups/otherfile.sym.lnk


Managing Processes

All programs that are executing on a Unix/Linux computer system are referred to as processes:

  • Each process has an owner
  • Each process has a unique ID (PID) Processes in UNIX can run in the foreground or background
  • Programs / Commands can be run in the background by placing an ampersand & after the command (eg. program-name &)
(Image licensed under cc)]

UNIX processes are hierarchical:

  • The process structure has a root, parents, and children
  • Creation of a new process is called forking or spawning
  • The Parent process can spawn a child and children can spawn their own children
  • Processes keep their PID for their entire life
  • Usually a parent sleeps when a child is executing
    (the exception is when the child process is executing in the background)

Users can learn to manage processes to become more productive while working in the Unix / Linux Command-line environment.


Below are common Linux commands / keyboard shortcuts to manage processes:

Linux Command / Key CombinationPurpose
psThe ps (process status) command displays snapshot information about processes. By default, the ps command displays information only about the current terminal (ps -l provides a detailed listing, ps -U username shows all)

Examples: ps , ps -l , ps -ef , ps -u , ps aux
topThe top command provides a continuous update including resource usage
NOTE: You can press ctrl-c to exit
fgThe fg (foreground) command moves a background job from the current environment into the foreground. The fg command issued without arguments will place the most recent process in the background to the foreground. Example: fg %job-number
<ctrl><c>Terminates a process running in the foreground
<ctrl><z>Sends a process running in the foreground into the background.
bgThe bg utility shall resume suspended jobs from the current environment. The bg command issued without arguments will run the most recent process that was placed into the background.
Example: bg %job-number
jobsThe jobs utility shall display the status of jobs that were started in the current shell environment
Examples:
jobs
[1]+ Stopped vim a   <-- Job #1 (+ means most recent process sent to background)
[2]  Running sleep 200 &  <-- Job #2
[3]  Running sleep 300 &  <-- Job #3
[4]- Running sleep 400 &  <-- Job #4 (- means second recent process sent to background)

killThe kill command sends the specified signal to the specified processes or process groups. If no signal is specified, the TERM signal is sent. The default action for this signal is to terminate the process.
Examples:
kill PID , kill -9 PID , kill %job-number ,
kill -9 %job-number

INVESTIGATION 1: LINKING FILES


In this section, you will learn how to create hard links and symbolic links on your Matrix account.


Perform the Following Steps:

  1. Login your matrix account.

  2. Issue a command to confirm you are located in your home directory.

  3. Issue the following Linux command to create a directory called ~/links:
    mkdir ~/links

  4. Issue the ls command to confirm that the directory called ~/links exists.

  5. Use a text editor to create a file called ~/links/data-file.txt

  6. Enter the following text displayed below:

    This is line 1
    This is line 2
    This is line 3

  7. Issue the following Linux command:
    ls -li ~/links/data-file.txt

    Note the i-node number for this file. What does this i-node number represent?

  8. Issue the following Linux command to create the following hard link in the same directory:
    ln ~/links/data-file.txt ~/links/data-file.hard.lnk

  9. Issue the ls -li command for the ~/links directory.

    What do you notice about both of these file's i-node numbers?

  10. Use a text editor to edit the file ~/links/data-file.txt and add some additional lines to this file.

  11. Save your editing session and exit your text editor.

  12. Issue the following Linux command:
    cat ~/links/data-file.hard.lnk

    What do you noticed what happened to this linked file?

  13. Use a text editor to edit the hard-linked file ~/links/data-file.txt.hard.lnk and add some additional lines to this file.

  14. Save your editing session and exit your text editor.

  15. Issue the following Linux command:
    cat ~/links/data-file.hard.lnk

    What do you noticed what happened to this original file?

  16. Issue the following Linux command to create a hard-linked file in your home directory:
    ln ~/links/data-file.txt ~/data-file.hard.lnk

  17. Issue the ls -i command to determine the i-node number for the file called ~/data-file.hard.lnk

    What do you notice about this file's i-node number?
  18. Make certain you are currently located in your home directory.

  19. Issue the following Linux command to remove the ~/links directory and its contents:
    rm -rf ~/links

  20. Issue a Linux command to confirm that the ~/links directory has been removed.

  21. Issue the following Linux command to view the contents of your linked file in your home directory:
    cat ~/data-file.hard.lnk

    What do you notice? What does this tell you about hard links?

  22. Issue the following Linux command to create a directory called ~/links2:
    mkdir ~/links

  23. Issue the ls command to confirm that the directory called ~/links2 exists.

  24. Use a text editor to create a file called ~/links2/text-file.txt

  25. Enter the following text displayed below:

    This is line one
    This is line two
    This is line three

  26. Issue the following Linux command to create the following symbolic link in the same directory:
    ln ~/links2/text-file.txt ~/links2/text-file.sym.lnk

  27. Issue the ls -li command for the ~/links2 directory.

    What do you notice about both of these file's i-node numbers?
    What do you notice about the size of the file ~/links2/text-file.sym.lnk?
    What pathname do you think it represents?
  28. Change to your home directory.

  29. Issue the following Linux command to create the following symbolic link in your home directory:
    ln ~/links2/text-file.txt ~/text-file.sym.lnk

  30. x



In the next investigation, you will ...

INVESTIGATION 2: MANAGING PROCESSES

In this section, you will learn how to ...


Perform the Following Steps:

  1. x

In the next investigation, you will ...


LINUX PRACTICE QUESTIONS

The purpose of this section is to obtain extra practice to help with quizzes, your midterm, and your final exam.

Here is a link to the MS Word Document of ALL of the questions displayed below but with extra room to answer on the document to simulate a quiz:

https://ict.senecacollege.ca/~murray.saul/uli101/uli101_week8_practice.docx

Your instructor may take-up these questions during class. It is up to the student to attend classes in order to obtain the answers to the following questions. Your instructor will NOT provide these answers in any other form (eg. e-mail, etc).


Review Questions:

  1. Write a single Linux command to create a hard link called ~/backup/myfile.txt.lnk to the existing file called ~/myfile.txt
    Write a single Linux command to display detailed information for those files above displaying their i-node numbers.
    In this case, will the inode numbers for those files above be the same or different?

  2. Write a single Linux command to create a symbolic link called ~/shortcuts/murray.saul.lnk to the existing directory called ~murray.saul
    Write a single Linux command to display detailed information for those files above displaying their i-node numbers.
    In this case, will the inode numbers for those files above be the same or different?

    What data is contained in the file called ~/shortcuts/murray.saul.lnk?
    What would be the size of the file called ~/shortcuts/murray.saul.lnk?

  3. Write a single Linux command to run the program called ~/clean.sh in the background.
    What command would you issue to place the previously issued program in the foreground?
    What command would you issue to confirm that this program is running in the background?
    What key-combination would you issue to send that program again into the background?

  4. Write a single Linux command to display running processes in “real-time”.
  5. Write a single Linux command to terminal a process that has the following PID: 22384
  6. Use the following diagram to answer the accompanying questions.
    Each of the following questions will use the diagram below and are treated as independent situations.

    [1] Stopped vim a
    [2]- Stopped vim b
    [3]+ Stopped vim c


    Write a single Linux command to bring the second-recently process placed in the background into the foreground.
    Write a single Linux command to terminate the job #3.

  7. Create a table listing each Linux command, useful options and command purpose for the following Linux commands: ln , ps , top , fg , bg , jobs , kill