-
Notifications
You must be signed in to change notification settings - Fork 770
// 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);
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;
}
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.
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 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.
Legal and Licensing information: Unless otherwise specified, submitted content to the wiki must be original work (including text, java code, and media) and you provide this material under a Creative Commons License. If you are not the copyright holder, please give proper attribution and credit to existing content and ensure that you have license to include the materials.