Skip to content
Lawrence Angrave edited this page Aug 23, 2018 · 27 revisions

Welcome!

// First, can you guess which lyrics have been transformed into this C-like system code?
char q[] = "Do you wanna build a C99 program?";
#define or "go debugging with gdb?"
static unsigned int i = sizeof(or) != strlen(or);
char* ptr = "lathe"; size_t come = fprintf(stdout,"%s door", ptr+2);
int away = ! (int) * "";

int* shared = mmap(NULL, sizeof(int*), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
munmap(shared,sizeof(int*));

if(!fork()) { execlp("man","man","-3","ftell", (char*)0); perror("failed"); }
if(!fork()) { execlp("make","make", "snowman", (char*)0); execlp("make","make", (char*)0)); }

exit(0);

So you want to master System Programming? And get a better grade than B?

int main(int argc, char** argv) {
	puts("Great! We have plenty of useful resources for you, but it's up to you to");
	puts(" be an active learner and learn how to solve problems and debug code.");
	puts("Bring your near-completed answers the problems below");
	puts(" to the first lab to show that you've been working on this.");
	printf("A few \"don't knows\" or \"unsure\" is fine for lab 1.\n"); 
	puts("Warning: you and your peers will work hard in this class.");
	puts("This is not CS225; you will be pushed much harder to");
	puts(" work things out on your own.");
	fprintf(stdout,"This homework is a stepping stone to all future assignments.\n");
	char p[] = "So, you will want to clear up any confusions or misconceptions.\n";
	write(1, p, strlen(p) );
	char buffer[1024];
	sprintf(buffer,"For grading purposes, this homework 0 will be graded as part of your lab %d work.\n", 1);
	write(1, buffer, strlen(buffer));
	printf("Press Return to continue\n");
	read(0, buffer, sizeof(buffer));
	return 0;
}

Watch the videos and write up your answers to the following questions

Important!

The virtual machine-in-your-browser and the videos you need for HW0 are here:

http://cs-education.github.io/sys/

The course wikibook:

https://github.com/angrave/SystemProgramming/wiki

Questions? Comments? Use Piazza: https://piazza.com/illinois/fall2018/cs241

The in-browser virtual machine runs entirely in Javascript and is fastest in Chrome. Note the VM and any code you write is reset when you reload the page, so copy your code to a separate document. The post-video challenges (e.g. Haiku poem) are not part of homework 0 but you learn the most by doing (rather than just passively watching) - so we suggest you have some fun with each end-of-video challenge.

HW0 questions are below. Copy your answers into a text document sd you'll need to submit them later in the course.

Chapter 1

In which our intrepid hero battles standard out, standard error, file descriptors and writing to files

Hello, World! (system call style)

  1. Write a program that uses write() to print out "Hi! My name is <Your Name>".

Hello, Standard Error Stream!

  1. Write a function to print out a triangle of height n to standard error.
    • Your function should have the signature void write_triangle(int n) and should use write().
    • The triangle should look like this, for n = 3:
    *
    **
    ***

Writing to files

  1. Take your program from "Hello, World!" modify it write to a file called hello_world.txt.
    • Make sure to to use correct flags and a correct mode for open() (man 2 open is your friend).

Not everything is a system call

  1. Take your program from "Writing to files" and replace write() with printf().
    • Make sure to print to the file instead of standard out!
  2. What are some differences between write() and printf()?

Chapter 2

Sizing up C types and their limits, int and char arrays, and incrementing pointers

Not all bytes are 8 bits?

  1. How many bits are there in a byte?
  2. How many bytes are there in a char?
  3. How many bytes the following are on your machine?
    • int, double, float, long, and long long

Follow the int pointer

  1. On a machine with 8 byte integers:
int main(){
    int data[8];
} 

If the address of data is 0x7fbd9d40, then what is the address of data+2?

  1. What is data[3] equivalent to in C?
    • Hint: what does C convert data[3] to before dereferencing the address?

sizeof character arrays, incrementing pointers

Remember, the type of a string constant "abc" is an array.

  1. Why does this segfault?
char *ptr = "hello";
*ptr = 'J';
  1. What does sizeof("Hello\0World") return?
  2. What does strlen("Hello\0World") return?
  3. Give an example of X such that sizeof(X) is 3.
  4. Give an example of Y such that sizeof(Y) might be 4 or 8 depending on the machine.

Chapter 3

Program arguments, environment variables, and working with character arrays (strings)

Program arguments, argc, argv

  1. What are two ways to find the length of argv?
  2. What does argv[0] represent?

Environment Variables

  1. Where are the pointers to environment variables stored (on the stack, the heap, somewhere else)?

String searching (strings are just char arrays)

  1. On a machine where pointers are 8 bytes, and with the following code:
char *ptr = "Hello";
char array[] = "Hello";

What are the values of sizeof(ptr) and sizeof(array)? Why?

Lifetime of automatic variables

  1. What data structure manages the lifetime of automatic variables?

Chapter 4

Heap and stack memory, and working with structs

Memory allocation using malloc, the heap, and time

  1. If I want to use data after the lifetime of the function it was created in ends, where should I put it? How do I put it there?
  2. What are the differences between heap and stack memory?
  3. Are there other kinds of memory in a process?
  4. Fill in the blank: "In a good C program, for every malloc, there is a ___".

Heap allocation gotchas

  1. What is one reason malloc can fail?
  2. What are some differences between time() and ctime()?
  3. What is wrong with this code snippet?
free(ptr);
free(ptr);
  1. What is wrong with this code snippet?
free(ptr);
printf("%s\n", ptr);
  1. How can one avoid the previous two mistakes?

struct, typedefs, and a linked list

  1. Create a struct that represents a Person. Then make a typedef, so that struct Person can be replaced with a single word. A person should contain the following information: their name (a string), their age (an integer), and a list of their friends (stored as a pointer to an array of pointers to Persons).
  2. Now, make two persons on the heap, "Agent Smith" and "Sonny Moore", who are 128 and 256 years old respectively and are friends with each other.

Duplicating strings, memory allocation and deallocation of structures

Create functions to create and destroy a Person (Person's and their names should live on the heap). 12. create() should take a name and age. The name should be copied onto the heap. Use malloc to reserve sufficient memory for everyone having up to ten friends. Be sure initialize all fields (why?). 13. destroy() should free up not only the memory of the person struct, but also free all of its attributes that are stored on the heap. Destroying one person should not destroy any others.

Chapter 5

Text input and output and parsing using getchar, gets, and getline.

Reading characters, trouble with gets

  1. What functions can be used for getting characters from stdin and writing them to stdout?
  2. Name one issue with gets().

Introducing sscanf and friends

  1. Write code that parses the string "Hello 5 World" and initializes 3 variables to "Hello", 5, and "World".

getline is useful

  1. What does one need to define before including getline()?
  2. Write a C program to print out the content of a file line-by-line using getline().

C Development

These are general tips for compiling and developing using a compiler and git. Some web searches will be useful here

  1. What compiler flag is used to generate a debug build?
  2. You modify the Makefile to generate debug builds and type make again. Explain why this is insufficient to generate a new build.
  3. Are tabs or spaces used to indent the commands after the rule in a Makefile?
  4. What does git commit do? What's a sha in the context of git?
  5. What does git log show you?
  6. What does git status tell you and how would the contents of .gitignore change its output?
  7. What does git push do? Why is it not just sufficient to commit with git commit -m 'fixed all bugs' ?
  8. What does a non-fast-forward error git push reject mean? What is the most common way of dealing with this?

Optional (Just for fun)

  • Convert your a song lyrics into System Programming and C code covered in this wiki book and share on Piazza.
  • Find, in your opinion, the best and worst C code on the web and post the link to Piazza.
  • Write a short C program with a deliberate subtle C bug and post it on Piazza to see if others can spot your bug.
  • Do you have any cool/disastrous system programming bugs you've heard about? Feel free to share with your peers and the course staff on piazza.
Clone this wiki locally