02.Introduction to Operating System
02.Introduction to Operating System
Disclaimer: The lecture slide set used in this course is mostly unchanged
from Prof Youjip Won’s same course at Hanyang University (KOR). This slide
set is for the OSTEP book.
1
What do you know about Operating Systems?
2
What do OSes have in common?
All of these hardware machines also have some common features, such
as processors, RAMs, storage devices (HDDs, SSDs, flash…), I/O devices
(mouse, keyboard, microphone, speakers, camera…)
They all are able to run programs
Powerpoint
Google Chrome
Antivirus
Shell
Task manager
3
What happens when a program runs?
4
What an Operating System (OS) does…
5
Virtualization
6
System call
7
The OS is a resource manager.
8
Virtualizing the CPU
9
Virtualizing the CPU (Cont.)
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 }
10
Virtualizing the CPU (Cont.)
Execution result 1.
prompt> gcc -o cpu cpu.c -Wall
prompt> ./cpu "A"
A
A
A
ˆC
prompt>
11
Virtualizing the CPU (Cont.)
Execution result 2.
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
...
12
Virtualizing Memory
13
Virtualizing Memory (Cont.)
14
Virtualizing Memory (Cont.)
15
Virtualizing Memory (Cont.)
16
Virtualizing Memory (Cont.)
A memory reference within one running program does not affect the
address space of other processes.
Physical memory is a shared resource, managed by the OS.
17
The problem of Concurrency
18
Concurrency Example
19
Concurrency Example
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 ...
20
Concurrency Example (Cont.)
16 int
17 main(int argc, char *argv[])
18 {
19 if (argc != 2) {
20 fprintf(stderr, "usage: threads <value>\
n");
21 exit(1);
22 }
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
21
Concurrency Example (Cont.)
loops determines how many times each of the two workers will
increment the shared counter in a loop.
loops: 1000.
prompt> gcc -o thread thread.c -Wall -pthread
prompt> ./thread 1000
Initial value : 0
Final value : 2000
loops: 100000.
22
Why is this happening?
2. Increment it
23
Persistence
Software:
File system manages the disk.
File system is responsible for storing any files the user creates.
24
Persistence (Cont.)
open(), write(), and close() system calls are routed to the part of
OS called the file system, which handles the requests
25
Persistence (Cont.)
26
Design Goals
Build up abstraction
Make the system convenient and easy to use.
27
Design Goals (Cont.)
Other issues
Energy-efficiency
Security
Mobility
28