0% found this document useful (0 votes)
22 views21 pages

OS Ch2

The document provides an overview of operating systems, focusing on three main features: virtualization, concurrency, and persistence. It explains how CPU and memory virtualization create the illusion of multiple processors and private memory spaces for processes, respectively. Additionally, it discusses the importance of concurrency in multi-threaded programs and the need for protection mechanisms, along with the role of file systems in ensuring data persistence.

Uploaded by

서연
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views21 pages

OS Ch2

The document provides an overview of operating systems, focusing on three main features: virtualization, concurrency, and persistence. It explains how CPU and memory virtualization create the illusion of multiple processors and private memory spaces for processes, respectively. Additionally, it discusses the importance of concurrency in multi-threaded programs and the need for protection mechanisms, along with the role of file systems in ensuring data persistence.

Uploaded by

서연
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

COMP312 운영체제 Operating Systems

2. Introduction to
Operating Systems
Myungsuk Kim

School of Computer Science and


Engineering
Kyungpook National University
COMP312 운영체제 Operating Systems

Three Main Features of Operating Systems


▪ Concept based on top-down approach

Virtualization
CPU Virtualization (Scheduling, Process…)
Memory Virtualization (Paging, Address Translation…)

Concurrency

Threads, Locks and semaphores, Deadlocks…

Persistence

File system, I/O Devices, Journaling…


COMP312 운영체제 Operating Systems

Virtualizing the CPU


▪ Gives illusion that the system has large number (infinite) of
CPUs
• Allows many programs (or many users) to seemingly run at once
• Various policies (e.g. scheduling) and resource managements are needed
▪ Example: running many programs at once (assume single CPU)
A
AAAA

B
BBBB

C
CCCC

D
DDDD

A B D C A B D C A C B D….
COMP312 운영체제 Operating Systems

Virtualizing the CPU


1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <sys/time.h>
4 #include <assert.h>
5 #include "common.h"
6
7 int
8 main(int argc, char *argv[])
9 {
10 if (argc != 2) {
11 fprintf(stderr, "usage: cpu <string>\n");
12 exit(1);
13 }
14 char *str = argv[1];
15 while (1) {
16 Spin(1); // Repeatedly checks the time and
returns once it has run for a second
17 printf("%s\n", str);
18 }
19 return 0;
20 }

Simple Example (cpu.c): Code That Loops and Prints


COMP312 운영체제 Operating Systems

Virtualizing the CPU


(Result of execution)
prompt> ./cpu A & ; ./cpu B & ; ./cpu C & ; ./cpu D &
[1] 7353
[2] 7354
[3] 7355
[4] 7356
A
B
D
C
A
B
D
C
A
C
B
D
...

Even though we have only one processor, all four of


programs seem to be running at the same time!!!
COMP312 운영체제 Operating Systems

Virtualizing Memory
▪ Gives illusion that each process has its own private virtual
address space
• The physical memory is an array of bytes (or byte-addressable).
▫ In general computing systems, the physical memory is DRAM, and has
limited capacity
▫ Allows each user (or process) to think of physical memory as a contiguous
address space (or collection of contiguous segments)
• A program keeps all of its data structures in memory
▫ Read memory (load) : Specify an address to be able to access the data
▫ Write memory (store) : Specify the data to be written to the given address

How large a memory capacity can be visible to the user


(or application program) in your computer systems?
COMP312 운영체제 Operating Systems

Appendix: Virtual Memory in 32-bit Processors


▪ 4GB block of memory address
• A 32-bit CPU is able to generate up to different 232 numbers (0 ~ 232-1), i.e.,
addresses each one pointing to a specific byte in memory
• These virtual addresses (generated by the CPU) are then mapped to real,
physical memory addresses by page tables, which are kept by the OS kernel

(Linux OS in 32-bit CPU)


0xFFFFFFFF
OS kernel
0xC0000000
space (1 GB)
0xC0000000
0xBFFFFFFF

User mode
. . .
space (3 GB)

0x00000000
COMP312 운영체제 Operating Systems

Virtual Memory Layout


High
2N-1
0x0000 7fff Intel x86-64 &
Addresses ffff ffff AMD 64 (128TiB)
RISC-V Kernel (OS) 0x0000 007f ARM64
(128GiB)
ffff ffff (512GiB or 128TiB)
0x0000 003f
ffff fff0
Stack local variables; procedure context
Memory Addresses

(Managed by compiler automatically) (writable; not executable)


Data Memory

Dynamic Data: Heap variables allocated with new() or malloc()


(Managed by programmer) (writable; not executable)

Data static variables, global variables


(writable; not executable)
(Initialized when process starts)
Literals (Initialized when process starts) large constants
0x1000 0000 (read-only, non-executable)
Text or Code (.txt) - instructions program code Program
Low (Initialized when process starts) (read-only, executable)
Memory
Addresses 0x0040 0000
0x0000 0000 System Reserved
COMP312 운영체제 Operating Systems

Appendix: Unit Of Data


▪ “KB” is 210 byte ? or 103 byte?
• A kibibyte (KiB), derived from kilo-binary-byte, is a unit of digital
information storage equal to 1024 bytes
• The unit was established by the International Electrotechnical Commission
(IEC) in 1998 to differentiate units in base 10 from units in base 2

SI - decimal IEC - binary


KB 103 kilobyte KiB 210 kibibyte
MB 106 megabyte MiB 220 mebibyte
GB 109 gigabyte GiB 230 gibibyte
TB 1012 terabyte TiB 240 tebibyte
PB 1015 petabyte PiB 250 pebibyte
EB 1018 exabyte EiB 260 exbibyte
ZB 1021 zetabyte ZiB 270 zebibyte
YB 1024 yotabyte YiB 280 yobibyte
COMP312 운영체제 Operating Systems

Virtualizing Memory
1 #include <unistd.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include "common.h"
5
6 int
7 main(int argc, char *argv[])
8 {
9 int *p = malloc(sizeof(int)); // a1: allocate some
memory
10 assert(p != NULL);
11 printf("(%d) address of p: %08x\n",
12 getpid(), (unsigned) p); // a2: print out the
address of the memory
13 *p = 0; // a3: put zero into the first slot of the memory
14 while (1) {
15 Spin(1);
16 *p = *p + 1;
17 printf("(%d) p: %d\n", getpid(), *p); // a4
18 }
19 return 0;
20 }
A program that Accesses Memory (mem.c)
COMP312 운영체제 Operating Systems

Virtualizing Memory
(The output of the program mem.c)
prompt> ./mem
(2134) memory address of p: 00200000
(2134) p: 1
(2134) p: 2
(2134) p: 3
(2134) p: 4
(2134) p: 5
ˆC

♦ The newly allocated memory is at address 00200000


It updates the value and prints out the result
COMP312 운영체제 Operating Systems

Virtualizing Memory
(Running mem.c multiple times)
prompt> ./mem &; ./mem &
[1] 24113
[2] 24114
(24113) memory address of p: 00200000
(24114) memory address of p: 00200000
(24113) p: 1
(24114) p: 1
(24114) p: 2
(24113) p: 2
(24113) p: 3
(24114) p: 3
...

♦ It is as if each running program has its own private memory


○ Each running program has allocated memory at the same address
○ Each seems to be updating the value at 00200000 independently
COMP312 운영체제 Operating Systems

Virtualizing Memory
▪ The OS maps address space onto the physical memory.
• A memory reference within one running program does not affect the
address space of other processes → Protection
• Physical memory is a shared and limited resource, managed by the OS
▫ (c.f.) cache memory

Process A p=malloc(sizeof(int)) Process B p’=malloc(sizeof(int))

00200000 Address of p Address of p’ 00200000


Physical memory
Address of p

Address of p’
COMP312 운영체제 Operating Systems

Concurrency
▪ Concurrent access of shared resources is subtle and error-
prone
• The OS is juggling many things at once, first running one process, then
another, and so forth
• Modern multi-threaded programs also exhibit the concurrency problem
• Need protection, lock or other synchronization methods

Process A Counter Process B


Load counter 6 6
6 Load counter 6
Update counter 7 6
Store counter 7 7
7 Update counter 7
7 Store counter 7
7
7
COMP312 운영체제 Operating Systems

Concurrency
A Multi-threaded Program (thread.c)

1 #include <stdio.h>
2 #include <stdlib.h>
3 #include "common.h"
4
5 volatile int counter = 0;
6 int loops;
7
8 void *worker(void *arg) {
9 int i;
10 for (i = 0; i < loops; i++) {
11 counter++;
12 }
13 return NULL;
14 }
15
16 int
17 main(int argc, char *argv[])
18 {
19 if (argc != 2) {
20 fprintf(stderr, "usage: threads <value>\n");
21 exit(1);
22 }
COMP312 운영체제 Operating Systems

Concurrency
23 loops = atoi(argv[1]);
24 pthread_t p1, p2;
25 printf("Initial value : %d\n", counter);
26
27 Pthread_create(&p1, NULL, worker, NULL);
28 Pthread_create(&p2, NULL, worker, NULL);
29 Pthread_join(p1, NULL);
30 Pthread_join(p2, NULL);
31 printf("Final value : %d\n", counter);
32 return 0;
33 }

♦ The main program creates two threads.


○ Thread: a function running within the same memory space
Each thread start running in a routine called worker()
○ worker(): increments a counter
COMP312 운영체제 Operating Systems

Concurrency
▪ loops determines how many times each of the two
workers will increment the shared counter in a loop

✓ Loop = 1,000
prompt> gcc -o thread thread.c -Wall -pthread
prompt> ./thread 1000
Initial value : 0
Final value : 2000

✓ Loop = 100,000
prompt> ./thread 100000
Initial value : 0
Final value : 143012 // huh??
prompt> ./thread 100000
Initial value : 0
Final value : 137298 // what the??
COMP312 운영체제 Operating Systems

Concurrency – Why is this happening?


▪ Increment a shared counter → take three instructions
• Load the value of the counter from memory into register
• Increment it
• Store it back into memory

▪ These three instructions do not execute atomically


→ Problem of concurrency happen !!!
COMP312 운영체제 Operating Systems

Persistence
▪ Devices such as DRAM store values in a volatile
▪ Hardware and software are needed to store data persistently
• Hardware: I/O device such as a hard drive (HDDs) & solid-state
drives(SSDs)
• Software:
▫ File system manages the disk.
▫ File system is responsible for storing any files the user creates.

▪ (Sudden) Power off or system crash …

▪ I/O device, disk, RAIDs, file systems, …


COMP312 운영체제 Operating Systems

Persistence
▪ Example : journaling file system (JFS)

Source: http://www.howtogeek.com/howto/33552/htg-explains-which-linux-file-system-should-you-choose,
COMP312 운영체제 Operating Systems

Design Goals
▪ Build up abstractions
• Make the system convenient and easy to use
▪ Provide high performance
• Minimize the overhead of the OS
• Provide virtualization without excessive overhead
▪ Protection between applications
• Does not harm other and the OS itself via Isolation
▪ High degree of reliability
• OS must run non-stop; when it fails, all apps on the system fail as well
▪ Other goals
• Energy-efficiency, Security, Mobility

You might also like