CS-211 Fall 2017 Lab 1 Introduction to UNIX

Notice... because of some administrative problems, we do not yet have full Course Assistant coverage for the labs. Therefore, this first lab will be kept VERY SIMPLE, but I am going to ask you to do this lab on your own. Please e-mail the professor or come to office hours if you have any questions.

Since this lab will be unsupervised, we will not take attendance, and do not require you to work on this lab during your assigned lab time. You may use the Linux Lab (LNG-103) at any time except when other classes or sections have assigned lab periods. There is a schedule on the front door of the lab that identifies when assigned lab periods occur. Check the schedule, and please refrain from using the lab when others labs are in session.

The Lab Environment

The lab at LNG-103 is a room with UNIX computers available to each student. You can log on to one of these machines using your normal PODS userid and password. When you do so, you can open a web browser and navigate to this page (www.cs.binghamton.edu/~tbartens/CS211_Fall_2017/labs/Lab_01.html) to work on the lab.

If you are not able to log on to a Linux Lab machine with your normal userid and password, follow the instructions in the Linux Lab web page at https://www.binghamton.edu/its/facilities/linux-lab.html

When you are done, please log off the machine. To log off, find the person icon in the lower right hand corner of the screen. If you left click on that icon, it will bring up a pop-up menu that has, as one option, log out.

Introduction to Linux

Linux is a UNIX based operating system, widely used in the industry, and often preferred by professionals to more user oriented operating systems. For the last few decades, most engineering appliation software, especially Computer Aided Design (CAD) software has been written and used in a UNIX environment.

Unix supports a complete Graphical User Interface (you are using that now), with all of the tools like those you are used to using in Microsoft Windows or Apple OS X, such as web browsers, text editors, and file/directory browsers. However, the full graphic user interface tools are not as standardized on UNIX as they are in other places, and UNIX started out as a command line user interface. Therefore, it is worthwhile to get a basic understanding of the UNIX command line interface. To start this process, open a terminal window to enable command line processing.

Open a Terminal Window

In the lower left corner of the Graphical User Interface (GUI), there is a Menu button, similar to the Windows start button. Click on that button to bring up the list of programs, and choose Accessories. This will add sub-menu to your display the list all the accessory programs installed on this computer. Scroll down in the Accessories list and click on the icon labeled Terminal. (Note... avoid the icon labeled "Root Terminal" for now. That one opens a terminal window too, but has some minor differences that may be confusing at this point.) When you click on the terminal icon, a terminal window will open up on your desktop.

A terminal window is surrounded by a border. Within the border is a window title. The window title starts off as Terminal. The terminal window has its own menu line, with labels like File Edit View Search Terminal Help.

The main body of the terminal window is a scrollable window that runs a UNIX command line. The command line in the UNIX window consists of things that you type, represented here by this font, and things the computer types back to you, represented here by this font.

Command Line Prompts

The computer has typed a "prompt" string in your terminal window to indicate that it is waiting for you to type something. This prompt string starts with the name of the machine you are running on followed by a colon. All of the machines in the lab are named linuxlabxx, where xx is a number. For instance, you may see linuxlab08:.

The second part of the prompt string is the "current directory". More about directories and the current directory shortly, but for now, your current directory is represented by a single tilde character, "~".

The last part of the prompt is a greater than sign, ">", to indicate that the terminal window is waiting for you to type a UNIX command. When you type a command and hit the enter key, that command is passed to the command interpreter. The command interpreter executes the command and possibly types its results back to the terminal window. When it is done, control returns to the terminal window, which prints a prompt, and then waits for your next command. So the entire prompt string will look something like :


UNIX Files and Directories

Note - all the commands described here (plus a few more) are summarized here.

In UNIX, the file system is organized into files and directories, similar to Mac/OS and Windows. (Actually. Mac/OS and Windows copied the directory concept from UNIX.)

There are two special directories that UNIX keeps track of. The first is the "current directory", sometimes also called the "working directory". When you type a command that needs to know the directory, and you don't specify a directory, the "current directory" is the directory that will be used by default. The "pwd" command stands for "Print Working Directory". At the command prompt in your terminal window, type pwd to see your current directory.

To change your current directory, use the "cd" (Change Directory) command, where the argument to the command is the directory that you want to become the current directory. UNIX uses the short-hand ".." to indicate "parent of the current directory". Type the command cd ... Notice how the prompt changes. Then type pwd to find out the current directory. Then type the command to change your current directory back to the directory you came from. (If you get lost, remember, you can always type cd with no arguments to get back to your home directory.)

The second important directory for UNIX is your "home" directory. Your home directory is the the starting place for your own personal disk space in UNIX. You have the authority to create files and sub-directories from your home directory, but others usually cannot read or write files in your home directory. The "cd" command with no arguments makes your home directory the current directory. In UNIX, there is a shorthand for your home directory - a single tilde. Thus "cd ~" and "cd" do the same thing. The tilde shorthand notation is used in the command prompt.

To list the contents of a directory, use the "ls" command, specifying the name of the directory as an argument. To list the contents of the current directory (which should now be your home directory), type ls. To list the contents of the top level directory, type ls /.

Commands can have special options to control how they operate. In UNIX, the convention is to indicate parameters which change how a command operates through the use of "flags". A flag starts with a dash, "-", followed usually by a single letter (but sometimes a full word.) For instance, the -l flag to the ls command tells ls to list more information about each file or subdirectory. Try typing ls -l to get a "long" list of what is in your home directory. You will see one line for each entry. The beginning of each line has a bunch of characters... rwxd, etc. If the very first character is a "d", the line refers to a sub-directory. If the first character is a dash, "-", the line refers to a file. Do you have any files in your home directory?

Managing Files and Directories

With your home directory as current directory, type mkdir junk. Then try an ls -l. Notice that your have created a new sub-directory of your home directory called "junk". Now, type ls -l junk. Notice that the "junk" sub-directory is empty. Now type cd junk to make the junk sub-directory your current diretory.

Type gedit test.txt. This opens a "gedit" editor widow on your desktop, and you are currently editing file "test.txt" in directory "~/junk". Look back at your terminal window, and notice that there is no prompt. The "gedit" command has not finished yet, so you are not allowed to type any new commmands on your command window. Exit out of the "gedit" editor window, and notice that the command line prompt appears after the "gedit" window closed. Normally, the UNIX command line processor runs on only one command at a time - but when you have an editor window open, it would be nice to be able to edit and run commmands at the same time. To fix this problem, type gedit test.txt&. In UNIX, an ampersand (&) at the end of a command tells UNIX to start the command in a new process, but as soon as the commmand is started, don't wait for it to stop... keep doing what you were doing. In this case, that means print the command prompt, and wait for the next command. Check your terminal window, and notice that there is a new prompt. In UNIX, when a command ends with an ampersand, we say it is "running in the background".

In the terminal window, do an ls -l. Notice that the directory is still empty, even though the editor is working on "test.txt". In the gedit window, do a Save. Then go back to the terminal window and do another ls -l. Notice that the test.txt file has been created in the junk subdirectory. Close the gedit window, and look at the terminal window again. Notice that when a background command ends, a message appears on the terminal window.

Now, do a cd to get back to your home directory, and type rmdir junk. What happens? Next, type rm junk/test.txt. Then do ls -l junk. Try removing the junk subdirectory again. Did it work this time?

If you would like to learn more about UNIX, there is a good tutorial here

Create your first C program

Starting at your home directory, type mkdir cs211. This will create a new directory for this class. From now on, everything that we do in this class will be done in the ~/cs211 directory. That way you can keep work for this class separate from work on other classes. Once you have created the cs211 directory, type cd cs211 to make the cs211 directory your current directory.

Next, create a sub-directory for this lab by typing mkdir lab01 followed by a cd lab01. Now your prompt should have a suffix of ~/cs211/lab01>.

Edit a file called helloWorld.c. In this file, add the following text:

#include <stdio.h>

int main(int argc,char **argv) {
  printf("Hello world!\n");
  return 0;

Once you have saved the helloWorld.c file you created and edited, then compile this C code by typing the command gcc -g -Wall -o hello helloWorld.c. If you made any mistakes typing the code, you will get a compiler error that says something is wrong. Fix the problem, and re-run the compiler.

Once you get a clean compile (no compiler errors), run the command ls. Notice that there is a new file called "hello" in the lab01 directory. The "hello" file is a file that contains the machine code (binary instructions) created from your helloWorld.c input file.

Finally, run your program by typing the command ./hello. What shows up on the screen?

Can you edit your helloWorld.c program and make it do more than just say "hello world"? For instance, you might want to say hello from <your name>, or make it say "hello world - take me to your leader". See what happens if you edit the helloWorld.c file, recompile, and rerun.

Lab Submission

Go to MyCourses, and open the page associated with this course. We will be using the Lecture section on MyCourses labeled Fall 2017 - Programming I Engineers(LEC)(CS-211-A 0). (Note... there are lab sections for this course as well, but we will not be using the lab sections in MyCourses. You can ignore those.)

Once you have opened your lab section page, choose the "Content" hyperlink on the left list of options. That will get you to the lab section content page. You will see a "Lab 01 Submission" icon. Click on the lab 01 submission hyperlink to get instructions on submitting your helloWorld.c file. Make this submission before the due date of Friday, September 1 at 11:59 PM. You may submit as many times as you want before the due date, but only the latest submission will be graded.