CS 101: Introduction to Computer Science
James Madison University, Fall 2019 Semester

Lab04: Unix commands and files

Background

The PCs in ISAT/CS 143 and ISAT/CS 250 run Linux Mint, and the iMacs in ISAT/CS 248 used to run macOS. Both operating systems are based on Unix and come with a powerful suite of general purpose computing tools. Many of these tools are only available via the command-line interface (also known as the "Terminal"). You will learn some of these commands in this course and others throughout your career. In today's lab, you will use the command line to gain insight into how operating systems work.

Objectives

Remember: It's okay (and encouraged) for you to work with other students during the lab. Two heads are better than one when figuring out this type of material. The only requirement is that each of you complete your own lab worksheet, in your own words.

Part 1: Creating New Processes

If you have not viewed the Command Line Basics tutorials, please do so before you begin the lab.

  1. Download a copy of Lab04-Worksheet.txt to your Desktop directory (right-click and "Save link as..."). Then open a terminal, and change to your Desktop directory.

    Note: Do NOT change the name of the worksheet file.
  2. You can run most programs directly from the command line. For example, enter the command xed Lab04-Worksheet.txt to open today's worksheet. Don't type the entire command yourself; press the tab key after typing the capital L. Answer the first four questions and save the file. You may find the Wikipedia article on Unix permissions useful for Question 4.

  3. When you run a program from the terminal, the operating system creates a new process for it. By default, the terminal waits until that process has completed before allowing you to enter another command. At this point you should have xed running and the terminal waiting. If not, please raise your hand and ask for help.

  4. Close your xed window. You should now be able to type another command in the terminal. Press the up and down arrows a few times (in the terminal) and see what happens. If you ever need to type the same command twice, don't forget you can use the up arrow to find it.

  5. Run xed again to open your worksheet -- remember to use the up arrow! Now press Ctrl-Z in the terminal. Try to edit your worksheet. What just happened? The Ctrl-Z command "suspends" a process, i.e., it tells the OS not to schedule the process until further notice. To resume the process, enter the fg ("foreground") command in the terminal. If you typed anything in your worksheet while xed was suspended, it may have just appeared.

  6. Suspend your process one more time. Now type the bg ("background") command. Notice that you can now use the terminal and xed at the same time. Answer question #5 in the worksheet.

  7. You can launch processes in the background without having to suspend them first. Simply type the & character after a command. For example, run the calculator program by typing the command gnome-calculator & in the terminal. Then type the jobs command and answer question #6 in the worksheet.

  8. Finally, you can terminate processes from the command line using the kill command. (I know that sounds rather violent, but it's just the name that was chosen back in the day.) Assuming you have xed as your first job and gnome-calculator as your second job, type kill %2 in the terminal and see what happens.

Part 2: What Multiuser Means

  1. Let's take a look at what all processes are running on your computer. Enter the top command on the terminal. The top program lists the top-running processes, including the process id (PID), what user owns them (USER), how much memory they're using (VIRT=virtual, RES=reserved physical, SHR=shared), how much CPU they're using, and so forth.

  2. Within top, press the 1 key (i.e., the number one). What changed? Press 1 again. You can press 1 as many times as you like to toggle the view. Answer question #7 in the worksheet.

  3. Press the 'q' key (for "quit") to exit the top program. Now let's connect to a CS lab machine (down the hall in ISAT/CS) and see who's logged in. Enter the command ssh L143**.cs.jmu.edu, but replacing the ** with a number between 40 and 70.

  4. The ssh program stands for "secure shell" and allows you to open a terminal on another computer. After logging in, type the who command to see who else is logged in. Run top and take a look at what processes are running.

  5. Notice how other users (besides you and the person next to you) are running processes on this machine. The "root" user is the operating system itself. To view a list of your own processes, press the 'u' key and enter your username. Answer the question #8 on the lab worksheet. Then press 'q' to exit the top program.

  6. The command line prompt (e.g., mayfiecs@14357) indicates what machine you are currently on. To log out of your ssh session, type exit in the terminal. Your prompt should now return to how it looked before you ran ssh.

Part 3: File Scavenger Hunt

  1. When you run a program, the operating system automatically knows where to find it. You can use the which command to see where a program is installed. For example, type which firefox in the terminal. Answer the worksheet question #9.

  2. Go to your Desktop directory in a terminal. (NOTE: You can simply right-click the desktop background and select Open in Terminal.) Then download files.zip to your Desktop directory.

    • Normal way: Right-click the link and "Save File As..." to your Desktop.

    • Better way: wget https://w3.cs.jmu.edu/cs101/unit04/files.zip
  3. Type unzip files.zip in the terminal. This will create three new files in the current directory. Let's take a look at each file. Type less worksheet.old in the terminal. The less command allows you to scroll through a plain text file using the up/down arrows, the page up/down keys, and so forth. To exit less, press the 'q' key (for "quit").

  4. Try running less on one of the other two files. What do you see? Answer the question #10 in the worksheet.

  5. The file command looks at the binary representation of a file and guesses what type of file it is. Enter file hidden.jmu and file secret.txt in the terminal (one at a time). Figure out how to open these files and see what's inside. Then answer the final question in the worksheet. (Hint: You may have to rename a file in order to open it.)

Submission Instructions