YouTube2Text

[CS61C FA20] Lecture 28.3 - OS & Virtual Memory Intro: Operating System Functions — Transcript

by CS 61C Departmental · 3,317 words · 625 segments · language en · Watch on YouTube

Full transcript

  1. 0:00[Music]
  2. 0:09hello
  3. 0:10welcome back to the virtual memory and
  4. 0:12operating system module
  5. 0:15so far we have seen what are some of the
  6. 0:17basic principles
  7. 0:19of operation of the operating system
  8. 0:24we have also gotten up to a point where
  9. 0:26we
  10. 0:27booted an operating system from
  11. 0:30an external flash rom device
  12. 0:33into the main memory and loaded
  13. 0:37the operating system into our processor
  14. 0:40now what we would like to go ahead
  15. 0:44and start executing some
  16. 0:47programs okay one thing to keep in mind
  17. 0:51this module is a bit narrative
  18. 0:52um it and it may be worth
  19. 0:56while rewatching it after you go through
  20. 0:58the entire virtual memory
  21. 1:00and io segments because it may
  22. 1:04more sense make more sense at that point
  23. 1:07but for now i'm laying out a bunch of
  24. 1:09principles
  25. 1:10that need to be done many of the
  26. 1:14functions that need to be performed by
  27. 1:15the operating system
  28. 1:16so hang in with me a lot of things are
  29. 1:19going to get clear
  30. 1:20a bit later so as we said
  31. 1:24we booted the operating system and we
  32. 1:26would like to launch
  33. 1:27applications so how do we launch an
  34. 1:30application
  35. 1:30remember where we left off we loaded
  36. 1:33some kind of a
  37. 1:34shell and that shell is just waiting for
  38. 1:36the user input
  39. 1:38that input may be a you know a set of
  40. 1:41keyboard strokes that are going to
  41. 1:43launch the application
  42. 1:45first thing applications are generally
  43. 1:48pro called processes
  44. 1:50uh in most of operating systems
  45. 1:54processes have separate memory work in
  46. 1:57separate memory locations as opposed to
  47. 1:59threads
  48. 2:00threads are always in the shared memory
  49. 2:04both threads and processes
  50. 2:07run pseudo simultaneously what do i mean
  51. 2:10by that
  52. 2:13from a user perspective from a user's
  53. 2:16point of view
  54. 2:17threads and processes look like they're
  55. 2:18running simultaneously but underneath
  56. 2:21we may we generally will have much
  57. 2:24less much lower number of processor
  58. 2:26cores that than the number of processes
  59. 2:28or threads that we are running
  60. 2:30so these processes
  61. 2:34are going to be sharing the same core
  62. 2:36how is that going to happen well the
  63. 2:38operating system is going to manage that
  64. 2:40it is going to run a process
  65. 2:43for a little bit typically 10
  66. 2:46milliseconds or so
  67. 2:47on a processor core and then
  68. 2:50we'll swap it out and put in a new
  69. 2:54process
  70. 2:55to run on it
  71. 2:58from a user's perspective you know
  72. 3:00really
  73. 3:01humans often cannot see that things like
  74. 3:04these were happening underneath
  75. 3:05as long as the application is running
  76. 3:08smoothly
  77. 3:08for us from a user's point of view
  78. 3:12so applications are started
  79. 3:15by generally a shell that calls an os
  80. 3:20routine
  81. 3:21by using a assist call this is called in
  82. 3:23linux
  83. 3:25this process really depends on the os
  84. 3:28if it is a linux linux uses something
  85. 3:32that is called a fork to create a new
  86. 3:34process and exec
  87. 3:35v execute file command loads the
  88. 3:38application
  89. 3:40so what happens then we load the
  90. 3:42executable file the binary file
  91. 3:45from the disk by using some of the file
  92. 3:48system
  93. 3:49service functions and puts
  94. 3:52the instructions and the data in their
  95. 3:55appropriate locations in the memory
  96. 3:58so those will be our text and data
  97. 4:00sections
  98. 4:01and then prepares the stack and the heap
  99. 4:04that sets then sets uh uh
  100. 4:08rxc and arc b and then jumps
  101. 4:11to the main from there the program
  102. 4:15counter picks up
  103. 4:16from the main executes the instructions
  104. 4:18and shells
  105. 4:19shell just waits for the program to
  106. 4:21complete
  107. 4:23so it waits for this main to return
  108. 4:26or in linux language
  109. 4:29waits for it to join
  110. 4:32another important thing here is there
  111. 4:35are many
  112. 4:36processes running simultaneously and
  113. 4:40it's really important to manage them
  114. 4:41we need to make sure that they don't run
  115. 4:43on top of each other so that's why we
  116. 4:45have a
  117. 4:46supervisor mode so if something goes
  118. 4:49wrong with the
  119. 4:50the application we really don't want it
  120. 4:52to crash the entire machine and start
  121. 4:54scribbling over the disk
  122. 4:56uh it would not be good it's terrible
  123. 5:00we also have to have ways to support to
  124. 5:03to stop
  125. 5:04the malware from doing the same things
  126. 5:07to us
  127. 5:10so there is os that manages
  128. 5:14uh a bunch of applications from doing
  129. 5:16something wrong
  130. 5:18prevents them from doing something wrong
  131. 5:20um so it
  132. 5:21puts constraints on the applications on
  133. 5:23where they can run
  134. 5:25right into which part of the memory they
  135. 5:28can access
  136. 5:29and which devices can they
  137. 5:33access in order to do that
  138. 5:38operating system has a higher
  139. 5:42mode or more privileged mode operation
  140. 5:44it is called the supervisor mode
  141. 5:47and the supervisor mode is you know
  142. 5:50something relatively straightforward
  143. 5:52you know that is supported by the
  144. 5:53hardware in the hardware
  145. 5:55will find out that supervisor mode is
  146. 5:57entered by just
  147. 5:58flipping a few bits in the status
  148. 6:00register
  149. 6:02so a general process accesses all uh
  150. 6:06it can act has access only to a subset
  151. 6:08of instructions in a processor
  152. 6:12and those instructions are the ones that
  153. 6:14we have seen so far
  154. 6:16supervisor mode has access to a few more
  155. 6:19instructions
  156. 6:20these instructions are nothing
  157. 6:21particularly fancy they're typically
  158. 6:24setting some csrs some instructions that
  159. 6:26are associated with manipulating
  160. 6:28csrs and then
  161. 6:33these is the the the supervisor mode
  162. 6:36also
  163. 6:37has access to the special set of csr
  164. 6:40registers
  165. 6:43so the this
  166. 6:47supervisory mode is kind of a super user
  167. 6:50mode or seo mode
  168. 6:52in in unix now keep in mind that
  169. 6:55most of the os applications are running
  170. 6:57in a regular user
  171. 6:59mode very rarely we exercise
  172. 7:02uh enter the supervisor mode
  173. 7:05um if there is an error in the
  174. 7:07supervisor mode
  175. 7:10it is often catastrophic that is what
  176. 7:13causes the
  177. 7:14blue screen of that in windows or
  178. 7:18scribbling over our are this drives
  179. 7:22so it's a good idea not
  180. 7:25to too often use a
  181. 7:28supervisor mode because it you know and
  182. 7:31errors there
  183. 7:32are fatal generally
  184. 7:36now uh the next thing that is worth
  185. 7:39mentioning mentioning are the syscalls
  186. 7:41we've encountered
  187. 7:42cisco's before but now they start making
  188. 7:44a little bit more sense
  189. 7:48so when we want os to do something
  190. 7:51for example to read a file launch a new
  191. 7:53process
  192. 7:55malloc send data to the network or
  193. 7:58to you know the the wired network or to
  194. 8:01wi-fi
  195. 8:02um what do we do it's really
  196. 8:05inconvenient to write our own
  197. 8:06low-level routine that is going to take
  198. 8:08a frame of data
  199. 8:10and pass it on to the wi-fi modem
  200. 8:13i mean it's totally doable but we
  201. 8:16generally don't want to do that
  202. 8:19also it is not practical to do that
  203. 8:21because multiple processes may end up
  204. 8:24contending for the same shared resource
  205. 8:27what we do instead
  206. 8:28we make a syscall to
  207. 8:31the operating system an operating system
  208. 8:35is going to
  209. 8:36manage that for us so it's much easier
  210. 8:39to
  211. 8:39make a syscall than to write this
  212. 8:42individual routine
  213. 8:43that accesses a device
  214. 8:48the way how we make a syscall system
  215. 8:51call
  216. 8:51is by placing some arguments in the
  217. 8:53registers
  218. 8:54generally where the the information can
  219. 8:56be found in place
  220. 8:58and then raise something that is called
  221. 9:00the software interrupt
  222. 9:02there is a special instruction that does
  223. 9:05this interrupt and at that point the
  224. 9:08operating system
  225. 9:09takes over and
  226. 9:13performs the required operation whether
  227. 9:15it's a printing on a printer
  228. 9:17or sending a a block of data via wi-fi
  229. 9:21and then returns to the user mode
  230. 9:26so this way the operating system can
  231. 9:28manage
  232. 9:29multiple processes for trying to do
  233. 9:32similar things
  234. 9:33or the same things multiple processes
  235. 9:36can print or access network and so on
  236. 9:40operating system make sure that there
  237. 9:41are no collisions between them
  238. 9:43and that they're ordered properly
  239. 9:47one really important mechanism that we
  240. 9:49have hinted to
  241. 9:50is the mechanism of interrupts and
  242. 9:54exceptions we
  243. 9:57generally need to transition to the
  244. 10:01supervisor mode when something important
  245. 10:03happens
  246. 10:04these important things can be either
  247. 10:07external
  248. 10:08or internal external ones are so called
  249. 10:11interrupts
  250. 10:12that there is something that happened in
  251. 10:14the outside world that we should know
  252. 10:16somebody rang you know while we are
  253. 10:18doing this somebody might have
  254. 10:19rang a doorbell we should check what is
  255. 10:22going on in there
  256. 10:26the other ones um are ins exceptions the
  257. 10:29other class of
  258. 10:31events that we should care about are
  259. 10:32exceptions exceptions are something
  260. 10:35internal uh something done by running
  261. 10:37the program by executing a wrong
  262. 10:39instruction trying to divide by zero for
  263. 10:41example or
  264. 10:42read through from uh uh memory location
  265. 10:46that should not exist
  266. 10:52so those are two differences between
  267. 10:56interrupts and exceptions
  268. 11:01also this puts equals and e-brakes
  269. 11:04in their perspective space
  270. 11:08equal triggers an exception to the
  271. 11:11higher
  272. 11:11privilege an break triggers an exception
  273. 11:15within the current privilege so equal
  274. 11:18is the way how we communicate to the
  275. 11:20operating system
  276. 11:21this is what we basically use to
  277. 11:23influence this call so
  278. 11:25we will set a a set of arguments
  279. 11:28to recall and that is what is going to
  280. 11:33cause a system call in linux on the
  281. 11:36other hand
  282. 11:38e-brake transfers control
  283. 11:42within the current level of privilege
  284. 11:45okay a little bit more about the
  285. 11:47terminology that is used in 61c
  286. 11:49different places or different courses
  287. 11:53may use a different terminology
  288. 11:56so one more time interrupt is something
  289. 11:59that is caused by an external event
  290. 12:02that event that is completely external
  291. 12:05to the
  292. 12:05running of the current program it may be
  293. 12:10a key press or disk io event
  294. 12:13or arrival of a of a wi-fi
  295. 12:17frame that is completely asynchronous to
  296. 12:19the current program
  297. 12:21and we generally don't have
  298. 12:24terrific urgency um in
  299. 12:28handling an interrupt like that
  300. 12:32there are some higher priority
  301. 12:33interrupts but generally
  302. 12:35in linux context we don't have to
  303. 12:38immediately
  304. 12:39handle that interrupt we can wait to
  305. 12:41finish what we are doing like if
  306. 12:43you know there is a doorbell ring i can
  307. 12:45finish
  308. 12:46this current sentence and then i can
  309. 12:48pause it and then i can go
  310. 12:50answer the door only if i perhaps if i
  311. 12:52were not recording this
  312. 12:55on the other hand exceptions are usually
  313. 13:00internal events something happened to
  314. 13:02the program and usually something pretty
  315. 13:04terrible
  316. 13:06there is a memory or a bus error illegal
  317. 13:08instruction
  318. 13:11and that is going to raise the exception
  319. 13:14that usually has to be treated
  320. 13:17immediately so we need to drop
  321. 13:20immediately everything we are doing like
  322. 13:21there is a fire alarm or something like
  323. 13:23that
  324. 13:25and go figure out what happened
  325. 13:29in both cases we are handling this
  326. 13:35in a very similar way the way the action
  327. 13:39of servicing an interrupt
  328. 13:40or an exception is called a trap so
  329. 13:44we generally what what we do in order to
  330. 13:46service either an interrupt
  331. 13:48or the exception we jump to an interrupt
  332. 13:52or trap handler
  333. 13:53that's a piece of code that resides in
  334. 13:55memory that is
  335. 13:57designated for handling particular
  336. 13:59interrupts or traps
  337. 14:00if you don't have an interrupt handler
  338. 14:03or a trap handler
  339. 14:04we will either ignore that event
  340. 14:08or will crash
  341. 14:11trap handling is
  342. 14:14very similar to what we have seen in say
  343. 14:17function calls
  344. 14:18but generally needs to be more precise
  345. 14:23so trap handling involves altering the
  346. 14:26regular execution flow that is our main
  347. 14:29program
  348. 14:30that is running instructions here
  349. 14:32instruction i minus one instruction i
  350. 14:34and instruction i plus one
  351. 14:38interrupt or exception happens
  352. 14:42we generally would like to
  353. 14:45finish all the instructions that have
  354. 14:47been that precede
  355. 14:49this trap and then we jump
  356. 14:52over to the handler we execute the
  357. 14:55handler
  358. 14:56and return back to the point where we
  359. 14:59stopped
  360. 15:02so this is something really important
  361. 15:04and we are going to see examples of some
  362. 15:06of these handlers later when we
  363. 15:08introduce io devices these
  364. 15:12are going to be essentially pieces of
  365. 15:14code
  366. 15:15that are responding to unknown external
  367. 15:18event
  368. 15:21so there is another important concept
  369. 15:23here that
  370. 15:24goes along with all of this this is
  371. 15:27something that
  372. 15:28is related to precise traps
  373. 15:33traps often um raised by the exceptions
  374. 15:37have to be dealt with immediately so
  375. 15:40trap handler's
  376. 15:41view of machine code state is that every
  377. 15:44instruction that precedes that
  378. 15:48trap has been completed has been
  379. 15:50executed
  380. 15:51and none of the instructions that follow
  381. 15:56have have started their execution so
  382. 15:58that sounds relatively
  383. 15:59straightforward although it's not that
  384. 16:01straightforward in super scalar
  385. 16:02processors where we have multiple
  386. 16:04instructions in flight
  387. 16:08um in general here
  388. 16:11the handler can return from an interrupt
  389. 16:13by restoring user registers
  390. 16:15and jumping back to the on to the
  391. 16:18interrupted instruction
  392. 16:19what does that mean every time we have
  393. 16:21an interrupt handler we have to
  394. 16:23store the complete state of the machine
  395. 16:26all the registers
  396. 16:28like we do in functional calls but in
  397. 16:29this case we generally will
  398. 16:31will save them all both saved and
  399. 16:34temporary ones
  400. 16:36we'll handle the um
  401. 16:39the the trap and then we'll restore the
  402. 16:42state of the machine
  403. 16:43like the interrupt never happened and
  404. 16:46our
  405. 16:46our program is going to continue
  406. 16:48executing
  407. 16:50this is a bit tricky in superscalar
  408. 16:53processors because remember we have to
  409. 16:55precisely
  410. 16:56insert convert some of these
  411. 16:58instructions into knobs
  412. 17:00we know how to do that we have done that
  413. 17:03when
  414. 17:04we were dealing with hazards so
  415. 17:07essentially at the point
  416. 17:08if there is an instruction that raises
  417. 17:11an exception
  418. 17:12all the instructions that follow that
  419. 17:15instruction
  420. 17:17are going to be converted to knobs we
  421. 17:20are going to at that point
  422. 17:21transfer the control to the trap handler
  423. 17:25trap handler is going to save the state
  424. 17:27of the registers
  425. 17:29do whatever it needs to do restore the
  426. 17:32registers
  427. 17:32and return and re-execute the extraction
  428. 17:35instruction that we have converted
  429. 17:37to knob okay
  430. 17:40a little bit more of a view how these
  431. 17:42things can happen and they can happen in
  432. 17:44different stages for example
  433. 17:47you know here are a few exceptions that
  434. 17:49may happen in
  435. 17:50a five-stage pipeline perhaps
  436. 17:54we may end up addressing um
  437. 17:57wrong instruction space so we can have a
  438. 18:01pc address exception
  439. 18:03or we may be trying to execute
  440. 18:07an illegal instruction by having a wrong
  441. 18:09op code
  442. 18:11we will decode that and raise an
  443. 18:13exception by the way this is a way how
  444. 18:15we can execute
  445. 18:16some instructions that we don't have
  446. 18:18supported by our hardware
  447. 18:19so for example if we don't have uh an
  448. 18:22instruction for multiplication we can
  449. 18:24handle it
  450. 18:26with a trap handler for that we all
  451. 18:27detect that we have
  452. 18:29um that we would like to do the
  453. 18:30multiplication and we do it in softer
  454. 18:33it's slower but it doesn't crash the
  455. 18:35machine
  456. 18:37we can divide by zero and have a uh
  457. 18:40we can raise an exception with that or
  458. 18:42we may address the wrong part
  459. 18:44of the memory space by trying um
  460. 18:48uh but by addressing some part of a
  461. 18:50memory where we
  462. 18:51should not have our data that all is
  463. 18:53going to cause exceptions
  464. 18:56all right so uh
  465. 18:59in in relatively brief um trap handling
  466. 19:03is very similar to to pipeline hazards
  467. 19:05followed by function calls
  468. 19:08so we are completing when we are
  469. 19:10handling
  470. 19:11a trap we complete the execution of the
  471. 19:14instruction
  472. 19:15that is um that
  473. 19:18precedes the uh the exception
  474. 19:23we flush all the instructions that we
  475. 19:25have currently in the pipeline
  476. 19:26by converting them to knobs and
  477. 19:30there is an optional step if we would
  478. 19:33like to know
  479. 19:34why the exception has been raised we
  480. 19:36usually um
  481. 19:38store a cause in uh in a controlling
  482. 19:41status register
  483. 19:43so we know what has happened there then
  484. 19:46we transfer
  485. 19:46execution to the trap handler trap
  486. 19:49handlers
  487. 19:50saves all the registers when it finishes
  488. 19:53restores the registers and
  489. 19:57if appropriate if it is if it has not
  490. 20:00decided to terminate the program
  491. 20:02returns to the original program and
  492. 20:04re-executes the next instruction that we
  493. 20:07had in flight
  494. 20:09by the way since we understand a lot of
  495. 20:13this stuff now
  496. 20:14this is also the way how we support
  497. 20:16multiple
  498. 20:17processes or multi-programming how do we
  499. 20:20concurrently
  500. 20:21execute multiple programs
  501. 20:25so os is going to help us run multiple
  502. 20:28applications at the same time
  503. 20:32and does that but by
  504. 20:35either scheduling them to different
  505. 20:36cores if you have them but in general
  506. 20:38in a single core machine or where
  507. 20:42there is a number of course the number
  508. 20:43of courses smaller the number of
  509. 20:45processes
  510. 20:45it is going to schedule them to be
  511. 20:48executed
  512. 20:49on the same core so
  513. 20:54and this is something that happens very
  514. 20:56quickly so the way
  515. 20:59how this is done it runs a piece of a
  516. 21:02code
  517. 21:05few millions of instructions generally
  518. 21:09of the order of millions of instructions
  519. 21:12and then
  520. 21:13switches context what does that context
  521. 21:16switch mean
  522. 21:16it goes to service the next program runs
  523. 21:19a few
  524. 21:20millions of instructions of the next
  525. 21:21program and switches back
  526. 21:24so every time we jump into a program
  527. 21:28in this multi-programming mode the
  528. 21:31operating system sets a timer
  529. 21:34and timers are again accessed through
  530. 21:37control and status registers
  531. 21:38so it's a timer like a kitchen timer for
  532. 21:4110 milliseconds
  533. 21:43and goes into executing one program in a
  534. 21:46kitchen
  535. 21:46you know that well we would probably not
  536. 21:48doing that at 10 millisecond intervals
  537. 21:51but we'll put something in the oven we
  538. 21:52stake
  539. 21:54the if the oven is the shared resource
  540. 21:56we would put keep something in the oven
  541. 21:58for 10 minutes and then we'll take it
  542. 22:01out of the oven
  543. 22:02after our kitchen timer tells us to do
  544. 22:05that and put something else into the
  545. 22:07in the oven we will set the kitchen
  546. 22:10timer again
  547. 22:11and take it out and keep moving multiple
  548. 22:15things through that oven this works
  549. 22:18in a very similar way in the processors
  550. 22:21except in a very tiny
  551. 22:22time scale generally 10 milliseconds
  552. 22:27so every time the timer goes off what we
  553. 22:30do
  554. 22:30we save the state
  555. 22:34of the program that we were running so
  556. 22:36we
  557. 22:37save all of its state we
  558. 22:40load the state of a new program and
  559. 22:42start running it
  560. 22:46of course we set the timer such that we
  561. 22:48know when
  562. 22:49we should take it out from the processor
  563. 22:54deciding how to do this and how much
  564. 22:57time to allocate to
  565. 22:59to these processes is the job of uh
  566. 23:03of the operating system which is called
  567. 23:06scheduling
  568. 23:08another really important thing here
  569. 23:10since we have so many things in flight
  570. 23:12here there are things that are going
  571. 23:13into the oven
  572. 23:14into the processor all the time are
  573. 23:15being taken out of the time it is
  574. 23:17important that these things don't run
  575. 23:19on top of each other so that's the
  576. 23:20reason why
  577. 23:23the supervisor mode is not alone we need
  578. 23:26to
  579. 23:26provide protection translation and
  580. 23:29paging
  581. 23:30our goal of that is not to allow one
  582. 23:34instruct
  583. 23:34one program to run over another
  584. 23:37one application should never corrupt
  585. 23:40other applications data
  586. 23:42so generally programs all programs are
  587. 23:44going to start from some fixed address
  588. 23:46say
  589. 23:49eight ffff in hicks and
  590. 23:53all of them are going to start from that
  591. 23:54instruction now we have to
  592. 23:56have a way to translate these addresses
  593. 24:00these virtual addresses into physical
  594. 24:02addresses
  595. 24:04and some of these programs or multitude
  596. 24:07of programs
  597. 24:08may be using more memory than we
  598. 24:10physically have
  599. 24:12the solution to that is a concept of a
  600. 24:15virtual memory
  601. 24:18the concept of virtual memory we're
  602. 24:20going to dive into
  603. 24:22great detail in the next set of modules
  604. 24:25in the next set of video segments but in
  605. 24:28general
  606. 24:30the concept of virtual memory is there
  607. 24:32to provide this
  608. 24:33illusion of the memory higher
  609. 24:36memory uh pyramid which says
  610. 24:40that our memory is
  611. 24:43infinite and as fast
  612. 24:47as our caches
  613. 24:50so each program is going to think that
  614. 24:53it has
  615. 24:53all the memory available to itself
  616. 24:57while all the other programs are going
  617. 24:58to think the same
  618. 25:00so how does that happen is uh
  619. 25:03is done through the hardware with
  620. 25:05support of a software and we're going to
  621. 25:07see that
  622. 25:08in a bit time to perhaps take a bit
  623. 25:12longer break because we are going to
  624. 25:15continue with a set of modules that deal
  625. 25:17with virtual memory see you then

About this transcript

This page contains the full transcript of [CS61C FA20] Lecture 28.3 - OS & Virtual Memory Intro: Operating System Functions by CS 61C Departmental, generated from the public captions YouTube serves with the video. The transcript has 3,317 words across 625 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.

What you can do with it

Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.

Free YouTube transcript tool

YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.