Week 3.b CS3650 01/24 2024 https://naizhengtan.github.io/24spring/ 1. main function 2. intro to processes 3. Process's view of memory (and registers) 4. process birth ---- Recap: C, tightly coupled with memory - pointers == memory addresses - array == a chunk of memory, with multiple bytes represent a unit - string == a chunk of memory, with each byte represents a char with a tailing '\0' - local var == memory that will be recycle after func return - global var == memory that will never be recycled (until program finishes) - malloc memory == memory that will only be recycled when calling free() 1. main function - full signature: int main ( int argc, char *argv[] ) - arguments: -- argc: number of arguments -- argv: a list of arguments in string - return value: -- 0: okay -- non-0: error [an example: int main(int argc, char *argv[]) { for (int i=0; i--->|wait for input|---->|wait for input| gcc----------------> [this is called "overlapping I/O and computation"] --example #2: reduce latency: A goes for 80 s, B goes for 20 s A-----------> B --> : takes B 100 s run A and B concurrently, say for 10s each, makes B finish faster. --process from scratch, a recall of "a process's life cycle" [DRAW PICTURE: loader HUMAN --> SOURCE CODE --> EXECUTABLE -----------> PROCESS vi gcc as ld loader HUMAN ---> foo.c ---> foo.s ----> foo.o ---> a.out ----> process NOTE: 'ld' is the name of the linker. it stands for 'linkage editor'.] --classical definition of a process: instance of a running program --examples are browser, text editor, word processor, PDF viewer, image processor, photo editor, messaging app --a process can be understood in two ways: a. from the **process's** point of view high-level: process sees an abstract machine will discuss details in a second b. from the **OS's** point of view meaning how does the OS implement, or arrange to create, the abstraction of a process? we will deprioritize this for now, and come back later in the course. 3. Process view of memory (and registers) Background, before we even get to processes, recall some basic elements in a machine (a computer) are: Q: basic elements in a machine? a. CPU core (a processor), which consists of * Some execution units (like ALUs) that can perform computation, in response to _instructions_ (addq, subq, xorq ...). Arithmetic and logical instructions typically take one _processor cycle_, or less. * A small number of registers which execution units can read from very quickly (in under a cycle). There are many types of registers. For today, we only need to think about two kinds: ** _General-purpose_ registers. There are 16 of these on the types of machines we are considering (known as the x86-64 architecture). ** Special purpose registers. The only one we consider here is: RIP This is the instruction pointer (also known as program counter). It points to the *next* instruction that the processor will execute, assuming there is no branch or jump to another location. b. Memory, which takes more time to access than registers (usually 2 to several 100 cycles). [In reality, there are "hierarchies of memory", built from caches, but for today, we will just think of memory as a homogeneous resource.] c. peripherals (disks, GPUs, ...) At a high level, a process abstracts - CPU => scheduling (multiplexing CPUs) - memory => virtual memory - file => something that is readable and writeable Today, our focus is on registers and memory. Three aspects to a process: (i). Each process "has its own registers." What does that mean? It means that while the process is executing (and from the perspective of the programmer who wrote the program that became the process), the process is using those registers mentioned above. This is part of what we mean when we say that a process has the impression that it's executing on an abstract machine. (The reason that this should not be taken for granted is that a given physical machine will generally have multiple processes that are sharing the CPU, and each has "its own" registers, yet each process has no knowledge of the others. The mechanisms by which the hardware and OS conspire to give each process this isolated view are outside of our current scope; we'll come back to this when we study context switches. It relates to process/OS control transfers, discussed at the end.) (What your book calls "direct execution" is the fact that, while the process is "executing", it's using the actual CPU's true registers.) (ii). Each process has its own view of memory, which contains: * the ".text" segment: memory used to store the program itself * the ".data" segment: memory used to store global variables * The memory used by the heap, from which programmer allocates using `malloc()` * The memory used for the stack, which we will talk about in more detail below. process (really, the developer of the program) thinks of memory as a contiguous array: [text/code | data | heap --> <-- stack] memory addr: Low High (iii). For a process, very little else is actually needed, but a modern process does have a lot of associated information: --- signal state, signal mask, priority, whether being debugged, etc., etc. 4. Process birth How does a process come into being? --answer: a system call! Q: what are system calls? A: interfaces between application and OS kernel [will cover the topic later] --in Unix, it is fork() --fork() creates an almost exact copy, except that the return value is different --QUESTION: what's wrong with an exact copy? [think as a process] --parent/child example if (fork() == 0) { // child printf("I'm child\n"); } else { // parent printf("I'm parent\n"); wait(NULL); } --pid_t wait(int *wstatus) --will block until one child terminates --"wstatus" collects the status (e.g., exit code) of the child --what if a child process finishes but the parent process never call wait()? [answer: child process becomes zombie process] --what if a parent process finishes before a child process? [answer: child process becomes orphan process] --QUESTION: zombie and orphan, which do you think is more problematic? [answer: zombie is more annoying because they will consume system resources, the process table.] --Q: Can we create a process by "create_process" (a syscall)? [start from here next time] ----- a note: - lab1: 2%; all others 10% - late assignments: drop the lowest two - distinguish two things: what you've learned (self improvement) vs. lab/final grades [they not necessarily align. In fact, in many cases, they conflict.] - this is an optimization problem max SOMETHING s.t. time --know your optimization object --know your constraints -----