4. Processes

SoftSys Lecture Notes, Wednesday 2/10/21 #

Head First C Recap #

Memory Layout #

Know the memory layout well - it may come in handy later in this course.

At the very least, remember that the stack grows down and the heap grows up.

Arrays vs. Pointers #

Remember the differences between arrays and pointers - it can be really easy to make mistakes on this.

There are a few things you can do - one is to always use pointers in function parameters. Always prefer void foo(char* s) over void foo(char s[]).

You can turn on compiler warnings, which can helpfully point out potential errors. I use gcc -Wall -Wextra -pedantic ....

Think OS Recap #

A process represents an instance of a running program. This includes:

  • Program code
  • Data
  • Memory
  • Statuses (of I/O and network requests, etc.)
  • Hardware state

There are a lot of abstractions at play.

  • Processes can be interrupted and resumed by the OS. This gives the illusion of running multiple things simultaneously.
  • Each process thinks that it has access to all of the computer’s memory, but this is of course not the case.
  • On UNIX systems, everything is a file. Reading and writing on an input device, network card, or file is all the same abstraction.

Remember that you can run ps -e to see all running processes on your system. What is process number 1 for you?

Linux Tutorial #

For the next 20 minutes:

  • Head to the UNIX Tutorial for Beginners site: http://www.ee.surrey.ac.uk/Teaching/Unix/
  • Do Tutorials 4-6. After doing Tutorial 4, read the mangpage for puts.
  • If you don’t finish, try to do so before our next meeting.

Stack Diagrams #

For the next 10 minutes:

Draw a stack diagram for the following code, at the point just before increment finishes running for the first time.

void increment(int *p) {
    *p = *p + 1;
}

int add_and_increment(int *p, int *q) {
    int sum = *p + *q;
    increment(p);
    increment(q);
    return sum;
}

int main ()
{
    int x = 3;
    int y = 4;
    int sum = add_and_increment(&x, &y);
    printf("%d %d %d\n", x, y, sum);
}

For Next Time #

  • Finish Homework 2.
  • Read Chapter 2.5 of Head First C.
  • Finish the Linux Tutorials we covered today/