CS010 Practice 2

Unix & C Basics 

Getting Help

One of the most important things to learn this semester is how to get help online so that you can find things yourself. There are two tools of particular interest: xman and the info package within Emacs.

xman is already started when you log in. xman provides a graphical user interface to online Unix manuals. The manual is divided into several sections. The most useful section for users is section 1 on user commands. C programmers will be interested in sections 2 and 3 which describe library routines provided by Unix that can be called from C programs. If you click on the Sections menu, a list of sections will appear. Select Section 1, user commands, and you will see a complete table of contents of all Unix commands. If you click on one entry in the window, you will now see the manual page for that command. This is very useful when you want to know exactly how to use a command or exactly what arguments it takes. You will be reading a lot of man pages over the month so you will become more familiar with doing this as the month progresses.

Using xman in this way is only useful if you know exactly what command you are looking for. xman also supplies a keyword-based search. To use it, pulldown the Options menu. Select Search. Type in a keyword and then click on the apropos button. You will now see a list of every entry in the manual with that keyword in its title. Remember that you will find section 1 most interesting so it is safe to ignore those entries that do not have (1) after the command name since those are not in section 1 and thus are not user commands.

xman is most useful for the simplest Unix commands. More complex tools are usually better explained with the info package within Emacs. To start info, enter Emacs and then use "C-h i" to begin info. (Remember C- means "hold down the control key while typing the following character"). The first screen shows a list of programs for which help is available. Move the cursor to the program you want more help on using the arrows and then use the carriage return to select the entry. This will now give you help in a hypertext style for the program. You may find underlined links that you can move the cursor to and then hit carriage return to select them. You can also navigate the hypertext selection using the following keys:

l

Last item visited (like Back in Netscape)

u

Up to parent

n

Next sibling

p

Previous sibling

Process Control

To run a program, you normally just type the name of the program. For example,

->xcalc

starts a calculator program. If you look at your shell, you'll see that you do not have a new shell prompt. To get a new shell prompt, you should have added an & to the end of the command line:

->xcalc &

What is happening? With the & the job is running in the background. Without the & the job is running in the foreground. When a program runs in the foreground, the shell waits for the command to complete before giving you a new command prompt. When it runs in the background, the shell continues without waiting for the new program to complete.

So, what can you do if you forget the &? You can pause the program and then tell it to run in the background. To do this, type C-z at the shell. You should then see a line like:

[3]+   Stopped    xcalc

The number is the job number and is likely to be different from the one listed above. Now you should have a shell prompt. Use the bg command to run the stopped job in the background. Now the calculator should continue running and you should get a shell prompt back:

-> bg
[3]+ xcalc &
->

Your other option is to kill the program. From the shell you can type C-c. This will work for most, but not all, commands. This will remove xcalc and give you back a shell prompt.

If C-c does not work, create a new shell window. (Point the mouse at the root, click the left mouse button, and select xterm.) Now use "ps" to find the process number. ps will show you all the programs you are running. The first column, labelled PID, shows process numbers. The last column, labelled COMMAND, shows the program that is running. Search the last column for xcalc then look in the first column of that row for its process number. Then use "kill -9 <process-number>" to kill the program. This should give you the shell prompt back in your original window.

-> ps
  PID  TT  STAT      TIME COMMAND
25358  p0  Is+    0:00.02 bash
25372  p1  Ss     0:00.03 bash
25382  p1  S      0:00:03 xcalc
25383  p1  R+     0:00.00 ps
25328  v0  Is+    0:00.04 -bash (bash)
25347  v0  I+     0:00.01 xinit fvwm2
25351  v0  I      0:00.02 /usr/local/bin/bash /home/cs-students/jcool/.xinitrc
25354  v0  I      0:00.28 emacs -geometry 80x40+10+150
25355  v0  S      0:00.02 xbiff -geometry 67x64-70+100 -bg lightskyblue
25356  v0  S      0:00.02 xclock -geometry 69x64-0+100 -bg lightskyblue
25367  v0  I      0:00.12 xman -geometry 139x66-1+1
25369  v0  I      0:00.05 fvwm2
25370  v0  I      0:00.00 /bin/sh -c xterm
-> kill -9 25382
->

Another way to kill the program is to point at the left icon in xcalc's ttiel bar and click the left mouse button. Then select Destroy from the menu.

Try these out!

Filename Completion

Unix provides filename completion so that it is often unnecessary to type entire filenames in Unix prompts. To use it, type the beginning of a filename and then hit the tab key. Unix will expand the filename as much as possible. If more than one filename has the same prefix, it will only expand it until the characters differ. For example, if your directory contains foo and foo.c and you type "ls f<tab>", Unix will replace the tab with "oo" and then beep at you. If it can complete the filename uniquely, it will do so but will not beep.

Another related feature is the '*' wildcard character. If you put '*' in a filename, it will match 0 or more characters. Thus, "ls f*" will list all the filenames that begin with f.

Within Emacs, filename completion is done with the space key. So, if you are trying to visit a file, you can type a prefix of the filename followed by the space key. Emacs will complete the filename as much as possible and show you all the matching filenames in a buffer.

Again, give these a try so you become comfortable with them.

Cancelling commands in Emacs

Sometimes you may want to stop a command that you have started in Emacs. For example, you might have used a command to read a file iuto Emacs but then decide you don't want to read in any files. The command to use is C-g. This will cancel whatever command is currently running. If no command is running, it will just beep.

Copy-and-paste in X windows

A useful way to move information from one X window to another is using X's copy-and-paste commands. Most applications support these, but not all of them. To cut point the mouse at the beginning of the text to copy and push down the left mouse button. Drag to the end of the text to copy and release the mouse button. Now move to the position where you want to insert the copy and click the middle mouse button.

Exercises

The following exercises are primarily short C programs but will also give you the opportunity to become more comfortable with Unix and Emacs.

  1. Use Alt+Tab to toggle between the open windows on your desktop a few times. This is a handy shortcut to switch between Emacs and a shell window when you are editing and testing a program at the same time.
  2. xeyes is a program that displays 2 eyes. The eyes move to follow the cursor as you move your mouse. It's helpful if you have trouble finding the cursor on the screen. Run this program by typing xeyes at a shell prompt. Notice that this starts the program for you but does not give you a new prompt in your shell. Kill the program using C-c. Now start the program again, use C-z to pause it, then use "bg" to resume it in the background. Finally, use ps to find the process id of the program and use the kill command to quit the program.
  3. Use the x copy-and-paste commands to copy face.c from the lecture notes and paste it into an Emacs buffer. Save the buffer to a file and compile and execute it.
  4. Write a C function that returns the minimum value in an array of integers. Write a main program that calls this function with arrays of different sizes and prints out the minimum value found.
  5. Draw a picture of memory right before the following function returns. Assume that x is at address 1000, y is at 1004, iptr is at 1008, and so on.
    int main() {
      int x,y;
      int *iptr;
      int *wombat;
      char *cptr;
      char c;
      iptr = &x;
      *iptr = 100;
      x++;
      y = *iptr + x;
      wombat = iptr;
      y = y + *iptr + *wombat;
      cptr = &c;
      c = 'a';
      (*cptr)++;
      return 0;
    }
    
    Try running the program and printing the values of x, y, and c to see if your answers are right.
  6. Write a function void swap(int *a, int *b) that takes pointers to two integers and changes swaps the values of what they are pointing at.
  7. What does the following program do and why? Try to reason it through first, then try running it to see what happens. You might want to add some printf statements to help understand its behavior.
    int main () {
        int a[4], i;
       
        for (i = 1; i <= 5; i++) {
            a[i] = 0;
        }
    }
       

    (Remember how to use C-z, kill and C-c to stop processes!)


Return to CS 010 home page