For these questions you'll need the mini course and "Linux-In-TheBrowser" virtual machine (yes it really does run in a web page on your machine using javascript!) at -
https://cs-education.github.io/sys/
Let's take a look at some C code (with apologies to a well known song)-
// An array to hold the following bytes. "q" will hold the address of where those bytes are.
// The [] mean set aside some space and copy these bytes into teh array array
char q[] = "Do you wanna build a C99 program?";
// This will be fun if our code has the word 'or' in later...
#define or "go debugging with gdb?"
// sizeof is not the same as strlen. You need to know how to use these correctly, including why you probably want strlen+1
static unsigned int i = sizeof(or) != strlen(or);
// Reading backwards, ptr is a pointer to a character. (It holds the address of the first byte of that string constant)
char* ptr = "lathe";
// Print something out
size_t come = fprintf(stdout,"%s door", ptr+2);
// Challenge: Why is the value of away equal to 1?
int away = ! (int) * "";
// Some system programming - ask for some virtual memory
int* shared = mmap(NULL, sizeof(int*), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
munmap(shared,sizeof(int*));
// Now clone our process and run other programs
if(!fork()) { execlp("man","man","-3","ftell", (char*)0); perror("failed"); }
if(!fork()) { execlp("make","make", "snowman", (char*)0); execlp("make","make", (char*)0)); }
// Let's get out of it?
exit(0);
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. For now just save them in a text document - we'll explain how to hand them later.
They will be due in the second week of class.
Important!
The virtual machine-in-your-browser and the videos you need for HW0 are here:
https://cs-education.github.io/sys/
The coursebook:
https://cs341.cs.illinois.edu/coursebook/index.html
Questions? Comments? Use Ed: (you'll need to accept the sign up link I sent you) https://edstem.org/
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 (which the course staff will grade later) because you'll need to submit them later in the course. More information will be in the first lab.
In which our intrepid hero battles standard out, standard error, file descriptors and writing to files.
- Write a program that uses
write()
to print out "Hi! My name is<Your Name>
".
- 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 usewrite()
. - The triangle should look like this, for n = 3:
* ** ***
- Your function should have the signature
- 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).
- Make sure to to use correct flags and a correct mode for
- Take your program from "Writing to files" and replace
write()
withprintf()
.- Make sure to print to the file instead of standard out!
- What are some differences between
write()
andprintf()
?
Sizing up C types and their limits, int
and char
arrays, and incrementing pointers
- How many bits are there in a byte?
- How many bytes are there in a
char
? - How many bytes the following are on your machine?
int
,double
,float
,long
, andlong long
- 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
?
- What is
data[3]
equivalent to in C?- Hint: what does C convert
data[3]
to before dereferencing the address?
- Hint: what does C convert
Remember, the type of a string constant "abc"
is an array.
- Why does this segfault?
char *ptr = "hello";
*ptr = 'J';
- What does
sizeof("Hello\0World")
return? - What does
strlen("Hello\0World")
return? - Give an example of X such that
sizeof(X)
is 3. - Give an example of Y such that
sizeof(Y)
might be 4 or 8 depending on the machine.
Program arguments, environment variables, and working with character arrays (strings)
- What are two ways to find the length of
argv
? - What does
argv[0]
represent?
- Where are the pointers to environment variables stored (on the stack, the heap, somewhere else)?
- 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?
- What data structure manages the lifetime of automatic variables?
Heap and stack memory, and working with structs
- 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?
- What are the differences between heap and stack memory?
- Are there other kinds of memory in a process?
- Fill in the blank: "In a good C program, for every malloc, there is a ___".
- What is one reason
malloc
can fail? - What are some differences between
time()
andctime()
? - What is wrong with this code snippet?
free(ptr);
free(ptr);
- What is wrong with this code snippet?
free(ptr);
printf("%s\n", ptr);
- How can one avoid the previous two mistakes?
- Create a
struct
that represents aPerson
. Then make atypedef
, so thatstruct 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 toPerson
s). - 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.
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.
Text input and output and parsing using getchar
, gets
, and getline
.
- What functions can be used for getting characters from
stdin
and writing them tostdout
? - Name one issue with
gets()
.
- Write code that parses the string "Hello 5 World" and initializes 3 variables to "Hello", 5, and "World".
- What does one need to define before including
getline()
? - Write a C program to print out the content of a file line-by-line using
getline()
.
These are general tips for compiling and developing using a compiler and git. Some web searches will be useful here
- What compiler flag is used to generate a debug build?
- You modify the Makefile to generate debug builds and type
make
again. Explain why this is insufficient to generate a new build. - Are tabs or spaces used to indent the commands after the rule in a Makefile?
- What does
git commit
do? What's asha
in the context of git? - What does
git log
show you? - What does
git status
tell you and how would the contents of.gitignore
change its output? - What does
git push
do? Why is it not just sufficient to commit withgit commit -m 'fixed all bugs'
? - What does a non-fast-forward error
git push
reject mean? What is the most common way of dealing with this?
- Convert your a song lyrics into System Programming and C code and share on Ed.
- Find, in your opinion, the best and worst C code on the web and post the link to Ed.
- Write a short C program with a deliberate subtle C bug and post it on Ed 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 Ed.