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

Lab04: Unix commands and files

Background

The PCs in the CS Department labs King CS249, King 250 and EnGeo 2204 run Linux. If you have an iMac or Apple laptop, then you are running 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.

You will need to use either a Linux OS or a Mac OS to complete this lab. The lab machines are available for your use when classes are not scheduled in the lab. You use your eid and password to log onto these machines. You may also use the laptop in the classroom while in class to work on the lab. If you have a Mac, you can use that too.

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 using cd . Use ls to see what all files you have on your desktop

    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 nano Lab04-Worksheet.txt. 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. Experiment with command line programs like ls -a, ls -l, cd, pwd that are part of the command line basics.
  4. 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 nano running and the terminal prompt is not visible, which means it is waiting for this process to complete.

  5. Close your nano 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. Answer question 5 on the worksheep. If you ever need to type the same command twice, don't forget you can use the up arrow to find it.

  6. Run nano again to open your worksheet -- remember to use the up arrow! with the & at the end of the command, e.g., nano Lab04-Worksheet.txt&. What just happened? The & puts a process in the background mode, e.g., in this case it tells the OS not to schedule the process until further notice. To resume the process, enter the fg ("foreground") command in the terminal. You should now be able to edit your file.

  7. You can launch multiple processes in the background. Again, edit your worksheet using nano and place in the background. Then edit a new file temp.txt with nano and also place it in the background. Now type the jobs command. You can use the fg command to select which process to bring back to the foreground. Experiment with using fg. and answer question #7 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 nano as your first job and another nano 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 a computer. First, log onto one of the Computer Science Department Lab machines, classroom machine or a Mac that is connected to the univeristy wifi.

    1. Once on a machine, open a terminal
    2. Enter ssh uid@student.cs.jmu.edu at command prompt, uid is your jmu eid, e.g., simmonsj. You will be prompted for a password, use your JMU password. You may be asked a question about continuing, select yes. Now you are logged into the student Linux server, a different machine from the one you are physically sitting. The name of this server machine is student.cs.jmu.edu The command prompt in your terminal should now be eid@stu:~$>
    3. If not, you need to stop and get this to work.
    4. From the Linux server, in the same terminal, you need to log onto one of the numerous Linux lab machines. Enter the command ssh L250**.cs.jmu.edu, but replacing the ** with a number between 01 and 33.
  2. Congratulations! You have logged onto one the Linux machines in King 250. 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. Then type whoami, this should be you.

  3. At the prompt, enter hostname This should contain the name of the lab machine you selected. This machine is uniquely identified by this name. We will be learning about hostnames
  4. Let's take a look at all processes running on this Linux 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.

  5. 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 #10 in the worksheet.

  6. Notice how other users (a.k.a, JMU students) 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 #11 on the lab worksheet. Then press 'q' to exit the top program.

  7. Press the 'q' key (for "quit") to exit the top program.

Part 3: Create your own web page

All students with a CS account (i.e., those who can log into our lab machines) have space on http://w3stu.cs.jmu.edu to host their own websites. Open a terminal to run ssh.
  1. For this part of the lab, you'll need to connect to the student server. Go to your terminal and enter the command ssh username@student.cs.jmu.edu (replace username with your JMU login). You'll be connected when you see a prompt like simmonsj@student:~$.

  2. Navigate to the www directory in your home directory (i.e., cd www This directory is linked to /cs/www/stu/username which is accessible over the Internet via the URL http://w3stu.cs.jmu.edu/username. Notice how the path in the URL does not include /cs/www/stu

    NOTE: If you don't have a www directory, run the command ln -s /cs/www/stu/username www wher e ``username'' is your JMU login.
  3. From the www directory, type nano index.html , which will create a new file. Type the words "Hello, World! and save the file (press Ctrl+O). In a web browser, navigate to your directory on w3stu to make sure you can view the file. Congratulations, you now have a CS home page!

  4. Now add at least two some HTML links to your index.html. See W3Schools HTML Examples for how to add links. For full credit on today's lab, your index.html file should include an appropriate title tag and two or more hypertext links (i.e., that you can actually click on and go to another web page). Just remember that this page is publicly available over the Internet. It's up to you what else you'd like to put on your home page. Some students put their real name, a picture, and/or a few favorite links. Others use this space to post their resume when looking for an internship or full-time job. Be creative and have fun, but don't spend too much time on it. :-)

Submission Instructions