Modul 1 - Processer Flashcards

1
Q

Hur funkar heapen och vad allokeras här?

A

Heap är en region i data minnet som inte hanteras automatiskt. Utan du måste själv allokera minne genom kommandon malloc() eller calloc() som är inbyggda funktioner i C. När man allokerat minne i heapen, så ansvarar man själv för att frigöra minnet när man använt klart det. Används för det mesta för att allokera variabler som ska vara mer “långvariga” eller för större datastrukturer.

Unlike the stack, the heap does not have size restrictions on variable size (apart from the obvious physical limitations of your computer). Heap memory is slightly slower to be read from and written to, because one has to use pointers to access memory on the heap.

Unlike the stack, variables created on the heap are accessible by any function, anywhere in your program. Heap variables are essentially global in scope.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Hur funkar stacken och vad allokeras här?

A

Stacken är en datastruktur som används för att lagra temporära variabler som skapas av varje funktion. Lagrar enligt “LIFO” (last in, first out). Varje gång en ny variabel deklareras läggs den i stacken och när programmet kör ut funktionen så kommer de variablerna som skapades i funktion bli fria “freed” d.v.s. de tas bort från stacken.

To summarize the stack:

  • The stack grows and shrinks as functions push and pop local variables.
  • There is no need to manage the memory yourself, variables are allocated and freed automatically.
  • The stack has size limits.
  • Stack variables only exist while the function that created them, is running.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Stack vs heap?

A

Stack:

  • Very fast access
  • Don’t have to explicitly de-allocate variables
  • Space is managed efficiently by CPU, memory will not become fragmented
  • Local variables only
  • Limit on stack size (OS-dependent)
  • Variables cannot be resized

Heap:

  • Variables can be accessed globally
  • No limit on memory size
  • (Relatively) slower access
  • No guaranteed efficient use of space, memory may become fragmented over time as blocks of memory are allocated, then freed
  • You must manage memory (you’re in charge of allocating and freeing variables)
  • Variables can be resized using realloc()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Vilka minnesaddresser brukar stacken, heapen och koden finnas? (Tenta fråga 1.3) .

A

-

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Hur funkar den inbyggda funktionen free()? (Heap)

Tenta fråga 1.4

A

-

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Skillnad mellan user mode och kernel mode?

A

-

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Vad gör instruktionen INT?

A
  • The special instruction, INT 0x80, will set the execution in kernel mode and jump to the 0x80 position in the interrupt table (IDT).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Vad är skillnaden mellan systemanrop och biblioteksrutiner? Vilka tillhör operativsystemet?

A

-

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Hur fungerar automatisk minneshantering på ex Java?

A

-

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Varför virtualiserar man CPU:n?

A

För att ge illusionen om att det finns flera CPU:n än vad det egentligen finns. Detta gör man så att flera processer kan köras samtidigt.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Vad är en process?

A

En process är ett program som körs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Hur virtualiserar man CPU:n? (Basic)

A

Man virtualiserar genom att switcha mellan olika processer. Alltså köra en process en stund, sen stoppa den och byta till en annan process o.s.v.

Tekniken heter time sharing av CPU:n, låter användare köra många processer samtidigt. Den potentiella kostnaden är prestanda, då varje process kommer köra långsammare om CPU:n måste delas.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Vad är time sharing?

A

En teknik som används av OS:n för att dela på en resurs. Genom att låta att resursen används en liten stund av en entitet (entity) och sen en liten stund av en annan o.s.v.

På detta sätt kan exempelvis CPU:n eller en nätverksläng delas av många samtidigt.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Vad är en mekanism?

A

Det är låg level (low-level) metoder eller protokoll för att implementera nödvändig funktionalitet.

Mekanismer ger helt enkelt svaret på hur något ska ske. Exempelvis hur ska OS:n utföra en context switch?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Vad är en policy?

A

En algoritm som gör beslut inom OS:n.

Exempelvis kommer schemaläggnings policyn bestämma vilket program OS:n ska köra givet många möjligt körbara program.

Policyn ger svaret på vilka grejer som ska ske. Exemeplvis vilken process ska OS:n köra?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Vad är en context switch?

A

Det är en time sharing mekanism som ger OS:n förmågan att kunna stoppa ett program och köra ett annat program på en given CPU.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Vad är en processens machine state?

A

Machine state är vad ett program kan läsa eller uppdatera när den körs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Vilka kompontenter består processers “machine state” av?

A

Machinestate är vad ett program kan läsa eller uppdatera när den körs.

Består av:

Minnet - Då instruktioner och data som programmet läser och skriver till ligger här. Minnet som en process kan addressera till kallas för address space.

Register - Instruktioner läser eller uppdaterar register.

Program counter (PC) - Beskriver vilken instruktion i porgrammet som körs.

Stack pointer och frame pointer - Hanterar stacken för funktions parametrar, lokala variabler och retur addresser.

Lagringsenheter - Här finns exempelvis information om vilka filer som en process har för tillfället öppet.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Explain what the instruction pointer (EIP) also known as program counter (PC), stack pointer (ESP), base pointer (EBP) and general purpose registers are? The CPU can be described by these.

A
  • The instruction pointer (EIP): a reference to the next instruction to execute
  • The stack pointer (ESP): a reference to the top of the stack.
  • The base pointer (EBP): a reference to the current stack frame.
  • General purpose registers: used by a process to store data.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What is a stack frame?

A

The stack frame is the collection of all data on the stack associated with one subprogram call.

The stack frame generally includes the following components:

  • The return address
  • Argument variables passed on the stack
  • Local variables
  • Saved copies of any registers modified by the subprogram that need to be restored (e.g. $s0 - $s8).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

The heap API?

A
  • void *malloc(size_t size) : allocate size bytes on the heap, returns a pointer to the structure.
  • free(void *ptr) : deallocates a structure pointed to by ptr
  • void *calloc(size_t nmemb, size_t size) : allocate an array of nmemb element of size bytes initialize it to zero.
  • void *realloc(void *ptr, size_t size) : changes the size of the structure pointed to by ptr.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

How does the OS create a process from a program?

A
  • The program is found on some external data storage (ex. hard-drive)
  • Memory is allocated to hold the: code, static data, heap and stack.
  • A process context is created holding the necessary register values.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

How does the process execution work?

A
  • Direct execution:
    The operating system loads the code of the user process, sets the stack and heap pointers and jumps to the first instruction of the process.
  1. Creates an process entry on the process list.
  2. Allocates memory for the program.
  3. Loads the program to the memory from the hard-drive.
  4. Cleans the registers.
  5. Executes main().
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

Vad är en process lista?

A

En process lista används för att hålla reda på alla processer som är redo att köras och ytterliggare info för att se vilken process som körs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

Process API?

A

Functions:

fork(), exec(), wait().

26
Q

How does fork() work?

A
  • fork() creates a new process that is a copy of the process that called fork(). This new process is called the child and the calling process is the parent.
  • The child process dosen’t start running on main() instead it runs as it had called fork() itself.
  • fork() returns the process id PID of the child process to the parent and 0 to the child (returns twice).
  • Which process that runs first is determined by the scheduler.
  • The child process has its own address space (virtual memory)!!
  • The memory of the two processes are separated from each other (but use the same addresses).
  • Already open files are shared by the two processes.
  • Newly open files are not shared.
27
Q

How does wait() work?

A
  • wait() is used by a parent process to wait for the child process to finish executing.
  • the wait() system call also reads the finished child process (dead child process) exit status and other information. Allows the parent process to get information from the dead process. After wait() is called, the zombie process is completely removed from memory.

. This

28
Q

How does exec() work?

A
  • exec() is used to run a program that is different from the one calling exec(). While fork() is used to run copies of itself.
  • exec() dosen’t create any new process, instead it transforms the running program to another running program.
  • exec functions do not return when successful.
  • There is execl, execlp, execle, execv, execvp, execvpe:
  • e: pass an array of pointers to environment variables.
  • l: arguments are passed individually to the function.
  • v: arguments are passed as an array of strings.
  • p: uses PATH environment variable to find the file that is executed.
29
Q

What is a zombie process (UNIX)?

A

When a process dies on Linux, it isn’t all removed from memory immediately — its process descriptor stays in memory (the process descriptor only takes a tiny amount of memory). The process’s status becomes EXIT_ZOMBIE and the process’s parent is notified that its child process has died with the SIGCHLD signal. The parent process is then supposed to execute the wait() system call to read the dead process’s exit status and other information. Allows the parent process to get information from the dead process. After wait() is called, the zombie process is completely removed from memory.

  • It’s a child process that has finished executing but still exists in the process table.
  • Happens if parent dosen’t call wait().
30
Q

What is an orphan process?

A
  • Child process that is still running but the parent process has finished executing or terminated.
  • Will be “adopted” by another process, thus get a new parent process.
  • In UNIX the new parent process is the init or systemd process
    - which is the parent of all processes.
    - has the process id (PID) = 1 .
31
Q

What is a daemon process?

A
  • Process that runs automatically in the background instead of under direct control of a user.
  • Performs operations at predefined times or in response to event.
  • Runs most of the tasks in a system.
32
Q

What are pipes?

A
  • Sends the output of one program to another programs input.
  • Denoted by the symbol ‘|’
  • Piping in the shell: combine serveral commands.

Ex: cat countries.txt | grep a | sort
Displays all countries that start with an ‘a’ and sorts them in alphabetic order.

  • Pipes has built-in flow control. So for example if a consumer dosen’t read form the pipe then the producer will be suspended when it tries to write to the filled pipe.
33
Q

What does dup2() do?

A

dup2() is a system call similar to dup in that it duplicates one file descriptor, making them aliases, and then deleting the old file descriptor. This becomes very useful when attempting to redirect output, as it automatically takes care of closing the new file descriptor, performing the redirection in one elegant command. For example, if you wanted to redirect standard output to a file, then you would simply call dup2, providing the open file descriptor for the file as the first command and 1 (standard output) as the second command.

  • Can be used to redirect stdout to an opened file.
34
Q

What are signals?

A
  • Notifications sent to a process to inform that a certain event has occured.
  • Causes a process to stop executing and handle the signal that has been received.
  • SIGINT: interrupt a process from keyboard (Ctrel + C)
  • SIGTERM: terminate a process
  • SIGKILL: kill process immediately
  • SIGFPE: arithmetic exeception (division by 0)
  • SIGSEGV: invalid memory access (segmentation fault)
  • Signals can be sent from the keyboard, the shell or using system calls.
35
Q

What is an Interupt descriptor table (IDT)?

A
  • Used to determine the correct response to interrupts and exceptions.
  • It contains pointers to different procedures that should be executed on different interrupts, each signal has its default action to be taken.
  • The OS has one IDT
  • Every process has its own Signal Table (holds the different signals)
  • You can define your own signal handling procedures.
36
Q

Signal handlers?

A
  • Each signal has a default handler. Ex. SIGINT - terminate a process.
  • The action can be changed by using the sigaction structure.
  • Define a function that is invoked when the signal is received.
  • The default actions for SIGKILL and SIGSTOP cannot be changed.
37
Q

What does Sigaction do?

A
  • Adds a new handler to a specific row in the IDT (interrupt descriptor table) for a process.
  • sigaction(signal, action, oldaction)
  • signal: the signal we want to handle
  • action: the action that should be taken when reciving the signal
  • oldaction: needed if you want to see what the old action was, should be set to NULL if it isn’t needed.
38
Q

Context information in the handler?

A

The context can be received by the handler function. It can receive up to 3 arguments
handler_function(int signal [, siginfo_t info, void* u_context] )

  • siginfo_t - Signal information (pid of process etc.)
  • u_context - The execution context. Things such as program counter and other registers.
39
Q

A simple way to kill a program is to hit ctrl-c. If we write a program we might not want to die or we might want to do some last operations before terminating. What mechanisms should we use in our program to handle this?

A

Answer: We should create a signal handler, a procedure that we will register for a specific signal, in this case SIGINT. When Ctrl-c is pressed a SIGINT will be sent to the process and thus our signal handler will be executed.

40
Q

What does the IDT (Interrupt Descriptor Table) contain and what happens when a user process executes the instruction INT (x86 assembler). Give a short description.

A

Answer: The IDT is set up by the kernel and contains
pointers to procedures that should be executed by different interrupts. When a user process executes for example INT 80 the process enters kernel mode and jumps to the procedure indicated by position 80 (hex).

41
Q

In a x86-architecture we have an IDT that is used when we implement among other things system calls. What does the operating system have to add to the IDT in order to make a system call possible?

A

Answer: The OS stores a pointer at a specified position in the table(0x80) to a procedure that handles all
system-signals. When a user process executes INT 0x80 the stored procedure will be put in charge, and will be executed in Kernel Mode.

42
Q

In the processor 80286, that was launched in 1982, Intel had added a privileged instruction LIDT (Load Interrupt Descriptor Table). What does it mean that the instruction is privileged and why does this instruction need to be privileged?

A

Answer: A privileged instruction can only be executed in kernel mode. The instruction will set a pointer to a table (IDT) that describes what should be done for each exception. This is nothing that a user process should be allowed to do.

43
Q

How does limited direct execution work?

A

Runs the program directly on the hardware CPU but limits its access. Lets programs run as fast as one might expect.

  • Limited in that the user program isn’t able to execute all instructions.
  • It can only access its own memory
  • It is only allowed to execute for a limited time.
44
Q

Kernel mode?

A
  • Kernel mode is where the OS runs and hasn’t any restrictions. OS has access to full resources of the machine.
  • The kernel should not take for granted that it can trust memory references from user space - security and portability. It should use special procedures when
    reading or writing to user space.
45
Q

User mode?

A
  • User mode is restricted in the sense that programs running in this mode doesn’t have full access of the hardware resources.
  • User mode exists to ensure that a process dosen’t take complete control of the system. In user mode you can’t for example directly issue I/O calls.
  • Processes in user mode are only allowed to access its own code, stack and heap.
46
Q

System calls?

A

System calls gives user programs the ability to perform some kind of privileged operations. Allows the kernel to carefully expose certain key pieces of functionality to user programs, such as accessing the file system, creating and destroying processes, communicating with other processes, and allocating more memory.

System calls executes special trap instruction.

47
Q

What is a trap instruction?

A
  • A trap instruction jumps into the kernel and raises the privilege level to kernel mode, calls return-from-trap to go back to user mode.
  • Trap instructions saves register states, changes hardware status to kernel mode and jumps into the OS trap table.
48
Q

Life of an execution.

A
  1. Hardware - turn on power - start executing “BIOS”
    1. Kernel mode: load operating system - set up interrupt descriptor table (IDT)
    2. Kernel mode: load user program - set up stack, heap etc - start execution
  2. User mode: run program
  3. User mode: prepare system call - trap to kernel mode
  4. Kernel mode: jump to system call handler
  5. Kernel mode: handle system call - prepare result - return from trap
  6. User mode: continue execution
49
Q

The purpose of a timer interrupt?

A

Timer interrupts are used to ensure that a program dosen’t run forever.

50
Q

What is a context switch?

A
  • A context switch is performed to change which process is running.
  • Gives the ability to stop running one program and starting running another on a given CPU
  • These can happen during an interrupt or a system call.

When a context switch happens:
1. Save the current registers in the context (process table) of the current process.

  1. Decide which other process that should run,
  2. Load the stored registers of the selected process,
    reset the timer, switch to user mode and,
  3. Set the instruction pointer of the selected process.
51
Q

What 3 basic things does a process consist of?

A

Address space - instructions, data

CPU State - program counter, stack pointer, registers

OS Resources - open files, network connections..

52
Q

What are the 4 areas of a process’s address space?

A

Stack (dynamic mem)

Heap (dynamic mem)

Static data (data segment)

Code (text segment)

53
Q

What data structure does the OS maintain to keep track of a process’s state?

A

Process Control Block (PCB) or process descriptor

54
Q

What sort of information does a Process Control Block include?

A

Process ID, Parent ID, execution state, Program counter, Stack pointer, registers etc.

55
Q

How can a different process be started?

A

First fork then exec()

56
Q

What does the exit() syscall do?

A

releases all resources

gets rid of most OS structures supporting the process

checks if parent is alive

if os, enters “zombie” state

otherwise dies

57
Q

How do programs request services from the OS?

A

Traps or exceptions.

58
Q

How does the OS regain control after the kernel has allowed a user process to execute?

A
  • The user process executes a INT instruction.
  • The hardware generates an interrupt.
  • The kernel sets a timer interrupt before it allows the user process to execute.
  • When the interrupt is generate by the hardware, the kernel can make a decision
    to schedule another process.
59
Q

Different types of interrupts?

A
  • Asynchronous interrupts: raised by the hardware: keyboard, IO, etc, can be masked by the kernel.
  • Synchronous interrupts - exceptions: raised by the CPU:
  • faults: something strange happens, will be handled by the kernel and instruction is re-executed (if the kernel can fix it).
  • traps: something special happens, used mainly by debugging.
  • abort: severe errors that will not be fixed by the kernel.
  • programmed exceptions (software exceptions): raised by for example the INT instruction, used by system calls and debuggers.
60
Q

Difference between a trap and an interrupt?

A
  • A trap is an exception in a user process. It’s caused by division by zero or invalid memory access. It’s also the usual way to invoke a kernel routine (a system call) because those run with a higher priority than user code. Handling is synchronous (so the user code is suspended and continues afterwards). In a sense they are “active” - most of the time, the code expects the trap to happen and relies on this fact.
  • An interrupt is something generated by the hardware (devices like the hard disk, graphics card, I/O ports, etc). These are asynchronous (i.e. they don’t happen at predictable places in the user code) or “passive” since the interrupt handler has to wait for them to happen eventually.