Thinking in C and *nix (CSC 282 2015S) : Outlines

Outline 01: Introduction


Held: Thursday, 22 January 2015

On to Outline 02 - Some Basic Tasks and Corresponding Tools.

Summary

We begin the course with an overview of some important principles and a few basic exploratory exercises.

Related Pages

Overview

Administrivia

Motivation

This course is an extension of a seminar I ran in spring 2012 and that students asked me to repeat in spring 2013, that I repeated in spring 2014, and that we have now regularized. That seminar was an outgrowth of things I observed and was told in CSC 362 2011F. In particular, the students in that course had much less familiarity with Makefiles than I would have liked and they told me that they generally did not use gdb when debugging their programs. I also observed that many of them were less than competent at dealing with memory leaks in their programs.

I do not blame my students for these flaws in their background. Rather, I expect that it's a consequnce of our decision to teach these issues relatively early in their careers and of different faculty opinions on what is important to teach. (I'm not sure that students who took 161 from me have substantially different backgrounds from those who took it from someone else.)

A few of the students asked me to spend some additional time teaching the things that I consider important (and that they quickly found were important). But we didn't have class sessions to spare. This class is therefore my attempt to address some of the weaknesses that I or the students observed. I hope to offer it every year.

Policies and procedures

Principles and practices

Important programming practices

Since I'm teaching this course, I get to impose my favorite practices on you. (I'll note that most of our students who leave Grinnell and go on to careers in programming find that I'm right about most of these things.)

You can take all of these with a grain of salt (or many many grains of salt) when you're just writing quick-and-dirty code to get stuff done. Keep in mind that your typical goal is efficiency. You don't need to comprehensively test a few-line throwaway program. However, you should document the program, because you may want to do a similar thing later.

Some key *nix principles

Unix and its descendents have been around a long time. They must be doing something right. So, what are the principles that have made nix so successful? Here are a few particularly important principles that many C/nix programmers follow. (We will expand upon these as the seminar continues.)

That first principle may seem a bit daunting. Do you always write programs to do tasks? No, you can certainly use programs. Here are ways you better do things "by program".

Some important C practices

In addition to the general programming practices I've mentioned above, there are a few approaches that C programmers should master. (We'll add more as the semester goes on.)

There are also things one should learn about over the longer term, including threads, signals, and some common libraries (such as libraries for processing the command line). Due to limited time, we probably won't cover those in this course.

Thinking in C: A Simple Example

The following code comes from K&R (although it is reformatted). What does it do?

char *
fun (char *t, char *s)
{
  while (*t++ = *s++);
  return t;
} // fun

Detour: A C Memory Problem

Activity/Discussion

To illustrate my point that understanding memory in C is important, let's continue with a problem that a friend gave to me a while ago. He showed me the following fragment of C code.

  x = malloc (...);
  foo ();
  bar ();
  free (x);

The program was crashing on the call to free.

Here are some things he discovered. * If he removed the call to free, the program ran through to completion.
* If he moved the call to free before the call to bar, the program ran through to completion.
* He had no calls to free in bar.

What is likely to be wrong with his code?

Exercises: Some simple tasks

Activity

Here are some tasks that I find it useful to think about. They illustrate some kinds of tasks that people often find they have to do and give you the opportunity to explore different approaches. For each problem, we'll make a list of ways to approach the problem (e.g., "Write a C program") and estimate the time each would take.

Homework

This will appear online some time in the near future (probably over the weekend).

Repository: https://github.com/Grinnell-CSC282/hw1-2015S

  1. Read chapter 1 of Raymond to get a deeper understanding of the *nix Philosophy.

  2. Find good solutions to any of the tasks we did not solve in class. (That is, write a program or command to solve the task.) Put them in a folder with your name in the GitHub repository.

  3. Make a list of *nix tools and utilities that you've found useful (or even that you've just heard of). Try to categorize and add a short description. Add them to the tools.md file in the repository.