-
Notifications
You must be signed in to change notification settings - Fork 1
/
chapter02.html
2198 lines (1560 loc) · 68.4 KB
/
chapter02.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<title>Operating System Structures</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<link href="css/slides.css" rel="stylesheet" type="text/css" />
</head>
<body>
<textarea id="source">
class: center, middle, title-slide
## CSCI 340 Operating Systems
<br>
## Chapter 2: Operating System Structures
.author[
Stewart Weiss<br>
]
.license[
Copyright 2020 Stewart Weiss. Unless noted otherwise all content is released under a
[Creative Commons Attribution-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-sa/4.0/).
Background image: roof of the EPIC Museum, Dublin, by Stewart Weiss.
]
---
name: cc-notice
template: default
layout: true
.bottom-left[© Stewart Weiss. CC-BY-SA.]
---
name: tinted-slide
template: cc-notice
layout: true
class: tinted
---
name:toc
### Table of Contents
[About This Chapter](#summary)<br>
[Chapter Objectives](#objectives)<br>
[Services Provided by the System](#system-services)<br>
[User Interfaces](#user-interfaces)<br>
[Services For Users](#user-services-2)<br>
[Schematic View of Services](#user-services-3)<br>
[Command-Line Interface](#cli)<br>
[Command-Line Interpreters and Terminals](#terminals)<br>
[Command Implementation](#command-implementation)<br>
[Graphical User Interface](#gui)<br>
[Touchscreen Interface](#touchscreen)<br>
[Batch System Monitor](#batch)<br>
[About System Calls](#system-calls-1)<br>
[Application Programming Interfaces](#apis)<br>
[A Bit About POSIX](#posix)<br>
[A Subtle Distinction](#technicality)<br>
[Example Using System Calls](#syscall-example)<br>
[The .fixed[read] System Call in UNIX](#read-syscall)<br>
[System Call Implementation](#syscall-implementation-1)<br>
[System Call Parameter Passing](#syscall-parameters)<br>
[Under the Hood: System Calls](#syscall-implementation)<br>
---
### Table of Contents
[Illustration of the Complete Flow](#syscall-complete-flow)<br>
[Flow of .fixed[read] System Call in Linux](#syscall-read)<br>
[Categories of System Calls](#syscall-types)<br>
[Process Control](#process-syscalls)<br>
[File Management](#file-syscalls)<br>
[Device Management](#device-syscalls)<br>
[Information Management](#info-syscalls)<br>
[User and Group Management](#user-syscalls)<br>
[Communication and Synchronization](#communication-syscalls)<br>
[Protection](#protection-syscalls)<br>
[About System Programs](#sys-programs)<br>
[System Programs versus Applications](#sysprograms-apps)<br>
[Categorizing System Programs](#sysprog-categories)<br>
[Categorizing System Programs (2)](#sysprog-categories-1)<br>
[Program Development](#prog-development)<br>
[Software Libraries](#libraries)<br>
[Steps in Building an Executable: Compilation](#compiling)<br>
[Steps in Building an Executable: Linking, Part 1](#linking)<br>
[Steps in Building an Executable: Linking, Part 2](#linking-2)<br>
[Steps in Building an Executable: Dynamic Linking](#dynamic-linking)<br>
[Dynamic versus Static Linking](#dynamic-vs-static)<br>
[Advantages of Static Linking](#dynamic-vs-static-2)<br>
[From Source to Executable, Visualized](#build-visualized)<br>
---
### Table of Contents
[Inquiring About Object Code](#link-exploration)<br>
[Displaying Information About Object Files](#readelf)<br>
[Using .fixedblue[ltrace] For Dynamic Information](#tracing)<br>
[Portability](#portability)<br>
[Solutions to Creating Portable Executables](#portability-solutions)<br>
[Operating System Design](#opsys-design)<br>
[Design Principles](#design-principles)<br>
[How Systems Evolve](#unix-derivatives)<br>
[Mechanisms and Policies](#mechanism-policy)<br>
[Programming Languages for Implementation](#implementation)<br>
[Operating System Structure](#opsys-structure)<br>
[Monolithic Structure: UNIX](#monolithic-1)<br>
[Monolithic Structure: Linux](#monolithic-2)<br>
[Layered or Ring Structure](#layered)<br>
[UNIX as a Ring Structure](#unix-rings)<br>
[Microkernel Foundations](#microkernel-foundation)<br>
[Benefits of Microkernels](#microkernel-advantages)<br>
[The Early Microkernels](#microkernels)<br>
[Modern Microkernels](#microkernels-modern)<br>
[Loadable Kernel Modules](#modules)<br>
[Hybrid Operating Systems](#hybrids)<br>
[macOS](#macos-x)<br>
[Bootstrapping the System: Loading the Kernel](#system-boot)<br>
---
### Table of Contents
[Bootstrapping the System: After Kernel Load](#system-boot-2)<br>
[References](#references)<br>
<!--TOC_END-->
---
name: summary
### About This Chapter
The chapter examines operating systems from three different perspectives:
- through the set of .greenbold[services] that it provides to .greenbold[users and programmers];
- through the set of .greenbold[interfaces] that it makes available to .greenbold[programmers];
- through its .greenbold[design and implementation], of interest to those who study and
design operating systems.
It covers the following topics:
- Operating system services
- User interfaces
- System calls
- System programs
- Operating system design and implementation structures
- System boot
---
name: objectives
### Chapter Objectives
You should be able to
- identify the services provided by an operating system and whether they are directed
at users, programmers, or both;
- explain in detail how system calls are used to provide operating system services
to programs;
- explain how operating systems and system software support program development and
execution, from source code to executable image;
- compare and contrast the different structural designs of operating systems, such as
monolithic, layered, microkernel, modular, and hybrid; and
- explain the process of booting an operating system.
---
name: services
layout: false
class: center, middle, inverse
## System Services for Users
We examine the ways in which users can obtain services from an operating system.
---
template: tinted-slide
layout: true
---
name: system-services
### Services Provided by the System
Recall from Chapter 1 that the operating system's services can be categorized, for the
most part, as being in one of the following groups:
- .greenbold[Program execution]
- .greenbold[I/O operations]
- .greenbold[File systems]
- .greenbold[Communication]
- .greenbold[Error detection and recovery]
- .greenbold[Protection and security]
- .greenbold[Accounting]
as illustrated below.
.center[
<img src="figures/services.png" width=100% alt="system services">
]
---
name: user-interfaces
### User Interfaces
For a user to access any of these services, some type of user interface to the
operating system is required. It can be any of:
- .greenbold[Command-Line Interface] (.greenbold[CLI])
- .greenbold[Graphical User Interface] (.greenbold[GUI])
- .greenbold[Touch Screen]
- .greenbold[Batch Monitor]
These will be explained shortly.
Regardless of which interface is used, users can access all types of services,
although some may be more useful to users than others.
---
name: user-services-2
### Services For Users
- .greenbold[Program execution]: The operating system gives users the ability
to run applications and programs. This is an important category of service.
- .greenbold[I/O operations]: The applications and programs run by users require input and produce
output. The operating system provides the environment in which this can happen.
- .greenbold[File systems]: Users and their software can create and delete files and directories,
read and modify them, rename them, search through them, list and modify their properties,
and share and protect them. The operating system provides support for all of this.
- .greenbold[Protection and security]: Users need control of who can access their data. The
operating system provides security and protection for both files and executing
programs.
--
Indirectly, users require services such as .greenbold[error detection and recovery] when their
files get corrupted, or their programs "crash", or worse, when the system itself
enters some error state.
--
They also require that the system perform .greenbold[accounting] so that they can monitor things
such as disk usage, memory usage, networking traffic and so on.
--
Lastly, sometimes users explicitly request that the system .greenbold[manage resources], such as by requesting
that certain programs run with lower priority, or by requesting that certain file
systems be mounted in restricted modes.
---
name: user-services-3
### Schematic View of Services
Users obtain services either directly through a user interface such as
by typing a command in a command-line interpreter, or indirectly, by running
an application that issues a request. In the first case, the command is executed by a
.greenbold[system program]. In the second case, it might be either a system program
or a .greenbold[system call] that provides the service.
.center[
<img src="figures/user_services.png" width=100% alt="system services">
]
---
name: cli
### Command-Line Interface
A .bluebold[command-line interface], (.bluebold[CLI]) is a type of user interface in which
a user enters a command by entering .redbold[plain text] followed by a .redbold[newline] character.
- Usually, there is a .redbold[prompt] of some kind to indicate that the user is supposed to enter the command.
- The text must conform to the syntax expected by the interface.
- When the newline is entered, the interface tries to run the command.
--
A .bluebold[command-line interpreter] .redbold[ is a special type of command line interface] -
it is a stand-alone program that can be used to interact with the operating system.
A command line interface is more general - application programs such as .greenbold[emacs], .greenbold[MatLab] and
.greenbold[GNU Octave] have command line interfaces, but they are not command line interpreters.
--
In UNIX, a command line interpreter is called a .bluebold[shell].
A shell is not just a command line interpreter,
but a .redbold[full-fledged programming language].
Examples of shells are
- the .greenbold[.fixed[Bourne-again Shell]] (.fixedblue[bash]),
- the .greenbold[.fixed[C Shell]] (.fixedblue[csh]), and
- the original .greenbold[.fixed[Bourne shell]] (.fixedblue[sh]).
--
.redbold[ A command-line interpreter can be run in a text terminal, which is a]
.redbold[terminal that transmits data one character at a time, and can only display characters].
---
name: cli-activity
count: false
class: breakout
### Command-Line Interface Activity
- What other shells are available in __Linux__ systems? Make a list of them.
- Of the shells that you found, which are .greenbold[free and open source] shells?
- What is the name of the person who wrote the very first shell and when was it written?
---
name: terminals
### Command-Line Interpreters and Terminals
To use a CLI, the user must open a .greenbold[terminal] or a .greenbold[pseudo-terminal] window.super[1].
.footnote[
1 You can also use a remote login program such as .fixedblue[ssh] to remotely login to a system, in which case you are automatically
given a shell session. ]
---
name: terminal-window1
template: terminals
count: false
.left-column-large[
<img src="figures/terminal_window.png" width=100% alt="terminal window">
]
.right-column-small[
In __Linux__, there is an application named .fixedblue[Terminal] that opens a window in which you can run
a shell such as .fixedblue[bash].
]
---
name: terminal-window2
template: terminals
count: false
.left-column-large[
<img src="figures/terminal_window_1.png" width=100% alt="terminal window">
]
.right-column-small[
In __Linux__, there is an application named .fixedblue[Terminal] that opens a window in which you can run
a shell such as .fixedblue[bash].
We can enter the .fixedblue[date] command by typing the word ".fixedblue[date]".
]
---
name: terminal-window3
template: terminals
count: false
.left-column-large[
<img src="figures/terminal_window_2.png" width=100% alt="terminal window">
]
.right-column-small[
In __Linux__, there is an application named .fixedblue[Terminal] that opens a window in which you can run
a shell such as .fixedblue[bash].
We can enter the .fixedblue[date] command by typing the word ".fixedblue[date]" followed by newline.
The current date and time appears.
]
--
.below-column[
Apple systems also have a .greenbold[Terminal] application, and __Windows__
has both the .greenbold[Powershell] and
the old standby, .fixedblue[command.exe], in which you can run DOS commands.
]
---
name: terminal-activity
count: false
class: breakout
### A Terminal Activity
- Login to .fixedblue[eniac.cs.hunter.cuny.edu] and then login to
any of the .fixedblue[cslab] hosts using .fixedblue[ssh]. For example:
```bash
ssh cslab12
```
- Try out these commands:
```bash
pwd
who
ls -a
cd /data/biocs/b/student.accounts/cs340_sw/demos
pushd ~
popd
```
---
name: command-implementation
### Command Implementation
How are the commands that you enter in a CLI, such as .fixedblue[bash], executed?
Where is their executable code?
--
In UNIX and UNIX-like systems, a command can be implemented in one of two ways:
- .greenbold[Within the shell itself], in which case it is called a .bluebold[built-in command];
- For example, in .fixedblue[bash], the .fixedblue[cd] command is a built-in command,
as is the .fixedblue[read] command.
- .greenbold[In a separate, executable file], in which case it is called a .bluebold[system program];
- For example, in __Linux__ systems, the .fixedblue[grep] and .fixedblue[date] commands
are in the files .fixedblue[/bin/grep] and .fixedblue[/bin/date] respectively.
--
There are a handful of commands that are implemented in both ways, such as
the .fixedblue[echo] , .fixedblue[kill], and .fixedblue[pwd] commands.
---
name: command-typing-activity
count: false
class: breakout
### Learning About Commands
- Login to .fixedblue[eniac.cs.hunter.cuny.edu] and then login to any
.fixedblue[cslab] host.
- Use the .fixedblue[type] command to determine the types of various
commands that you know.
- Use the .fixedblue[file] command to find the types of the non-built-in commands
that you discovered.
- Use the .fixedblue[help] command to see a list of the .fixedblue[bash] built-in commands.
---
name: gui
### Graphical User Interface
The .greenbold[graphical user interface], or .greenbold[GUI], is an alternative
user interface to the operating system. It requires a .greenbold[graphical terminal],
which can display not just characters, but images.
--
.left-column[
The GUI was invented at .greenbold[Xerox PARC],
appearing in the Xerox Alto computer in 1973. Previously
Ivan Sutherland developed a graphical program called .greenbold[Sketchpad] in 1963.
Apple Computers introduced the first GUI as we know it in their .greenbold[Apple Lisa] in 1983
and the .greenbold[Apple Macintosh] in 1984.
]
.right-column[
<img src="figures/macintosh_128k.png" width=45% alt="Apple Macintosh"> .sub[1]
]
.footnote[
1 Image from w:User:Grm wnr, CC BY-SA 3.0, https://commons.wikimedia.org/w/index.php?curid=519781
]
--
.below-column[
- A GUI emulates a desktop using a cursor-based window-and-menu system.
The contents of the screen are graphical elements such as an active cursor, icons, images, and
windows, which can represent files, directories, and applications.
- The .greenbold[cursor] is moved by the user with a pointing device such as a .greenbold[mouse]
or by voice commands.
The cursor is used to execute commands, select elements, draw, etc.
- Most systems provide a GUI, such as Microsoft's __Windows__, Apple's __macOS__, and
__Linux__'s __Gnome__, or __KDE__.
]
---
name: touchscreen
### Touchscreen Interface
Keyboards and mice are not feasible input devices for mobile computing devices
such as smartphones and tablets, making CLIs and GUIs impractical for them.
.left-column[
These devices typically have .greenbold[touchscreens], which are both input and
output devices.
]
.right-column[
<img src="figures/multitouch_screen.png" >
]
.footnote[
Image from User:Willtron, CC BY-SA 1.0, https://commons.wikimedia.org/wiki/File:Multitouch_screen.svg
]
.below-column[
Touchscreen devices require a different interface:
- actions and selections are based on .greenbold[gestures], which are not single point clicks, but
.greenbold[motions], which are represented by location, velocity, and acceleration data.
- keyboards are on-screen for text entry
- voice commands are supported.
]
---
name: batch
### Batch System Monitor
A .bluebold[batch processing system] is a type of computer system in which users
submit .greenbold[jobs] that can run without user interaction.
In modern systems, batch processing jobs are usually computationally intensive and require
a large amount resources not usually available on a desktop computer.
--
Users interact with a batch processing system through a .bluebold[monitor],
which is a program that reads a batch of jobs, schedules the first one on the computer, and runs it.
After each job is run, the monitor runs and selects the next job to run.
A monitor has a limited interface with a .greenbold[job control language] that allows
the user to specify the parameters of the job's run, such as how much disk space,
memory, and so on, is needed by the job.
A monitor is therefore another type of interface for interacting with the computer
system.
---
name: system-calls
layout: false
class: center, middle, inverse
## System Calls
We examine system calls, the means by which programs obtain services
directly from the kernel.
---
template: tinted-slide
layout: true
---
name: system-calls-1
### About System Calls
A .bluebold[system call] is a call to code inside the kernel of the operating system
made via a .greenbold[software interrupt] or .greenbold[trap].
.redbold[It is the means by which a user process explicitly requests a service from the kernel].super[1].
.footnote[
1 Kernel processes do not make system calls.
]
--
- Usually, system calls are not invoked directly from a user program. Instead, the
program calls a .greenbold[wrapper function] that invokes that system call.
- A .bluebold[wrapper function] is a function whose only purpose is to make a call to
another function.
It might perform some small amount of work before calling that
function and then a bit more work after the call.
- In the context of system calls, a .bluebold[wrapper function] is a function whose only
purpose is to make the system call.
- The wrapper function is part of a user level library and runs in user mode,
whereas the actual system call is inside the kernel and runs in kernel mode.
---
name: apis
### Application Programming Interfaces
An .bluebold[Application Programming Interface] (.bluebold[API]) is a definition or
collection of definitions that specify how a program is to obtain the services
provided by some other piece of software.
In the context of operating systems and the kernel, an API is the set of specifications
of functions for obtaining the services of the kernel.
--
A programming language needs to provide a means for programs written in that language
to obtain kernel services.
--
Therefore, programming language libraries usually provide wrapper functions in that language for
all system calls.
For example, the GNU C library, .fixedblue[glibc], contains wrapper functions for
all of the system calls in GNU __Linux__.
This is not to say that every function in that library is a wrapper function. It has other
functions too. And it might have functions that make several system calls.
But it is true that almost every system call in __Linux__ has a wrapper function in .fixedblue[glibc].
--
The three most common APIs are the
- .bluebold[WinAPI] for __Windows__ (.bluebold[Win64] for 64-bit __Windows__),
- .bluebold[POSIX API] for POSIX-based systems, which includes
virtually all versions of UNIX, __Linux__, and __macOS__, and
- .bluebold[Java API] for the Java virtual machine (JVM)
---
name: posix
### A Bit About POSIX
POSIX is an acronym for
the .bluebold[Portable Operating System Interface]. You will encounter references
to POSIX so often that it merits a brief description here.
POSIX is a family of standards specified by the .greenbold[IEEE Computer Society]
for maintaining compatibility between operating systems.
--
The IEEE official description of it.super[1] states that,
> POSIX defines a standard operating system interface and environment,
> including a command interpreter (or “shell”), and
> common utility programs to support applications portability at the source code level.
.footnote[
1 https://standards.ieee.org/standard/1003_1,2016Edition-0.html
]
It was intended originally to define standards for software compatibility
with variants of Unix and other operating systems.
--
When you write code that uses the POSIX API, it can be run on any POSIX-compliant system.
---
name: technicality
### A Subtle Distinction
Technically, the function that your program calls to obtain services from the kernel
.redbold[is not a system call], but the .redbold[wrapper function defined in the API].
.left-column[
Suppose that .fixedblue[foo()] is a system call. The library will have a wrapper with
the same name. Your program will call .fixedblue[foo()], but this will invoke the
wrapper, not the actual system call, which is invoked within the wrapper itself.
In the illustration to the right, the string .greenbold[.fixed[SYSCALL]] is just a placeholder
for the code that would switch to kernel mode and jump to the system call handler.
]
.right-column[
<img src="figures/syscall_api.png" width=100% alt="system call" >
]
--
.below-column[
When we say that a program calls some system call, we mean that it calls the
corresponding wrapper function in the user-level library.
]
---
name: syscall-example
### Example Using System Calls
In __Linux__, the system call to read from a file is .fixedblue[read()], to write to
a file is .fixedblue[write()], to open a file, whether for reading or writing,
is .fixedblue[open()], and to create a new file is .fixedblue[creat()].super[1].
.footnote[
1 This is not a typo; the .fixedblue[creat] call has no 'e' at the end.
]
.left-column-small[
The __Linux__ .fixedblue[cp] command has many uses, but the simplest use is to copy an existing
file to a new file:
.center[
```bash
cp oldfile newfile
```
]
A program that does this, with some pseudocode for error handling, is shown to the
right. It makes several system calls, which are highlighted in yellow.
]
.right-column-large[
.smaller[
```C
int main(int argc, char *argv[])
{
int in_fd, out_fd, n_chars;
char buf[256];
/* check args */
if ( argc != 3 ) /* not enough args - report error and exit */
/* open first file and create new one */
if ( (in_fd = `open(argv[1], O_RDONLY)`) == -1 )
/* handle error - cannot open first file */
if ( (out_fd = `creat( argv[2], 0644)`) == -1 )
/* handle error - cannot create new file */
/* copy files */
while ( (n_chars = `read(in_fd , buf, 256)`) > 0 )
if ( `write( out_fd, buf, n_chars )` != n_chars )
/* handle write error */
if ( -1 == n_chars )
/* handle error - Read error from file */
/* close files */
if ( `close(in_fd)` == -1 || `close(out_fd)` == -1 )
/* handle error closing files */
return 0;
}
```
]
]
---
name: read-syscall
### The .fixed[read] System Call in UNIX
The .fixedblue[read()] system call is a powerful call in UNIX, because it can be
used to read from anything that can be mapped to a file, which is to say all devices and files.
We use it to demonstrate how you can learn about the __Linux__ API from the manual pages.
Type ".fixedblue[man read]" in a terminal to see the beginning of the manual page for it,
which provides its API and also shows that the .fixedblue[unistd.h] header file must be included to use it.super[1]:
```C
#include <unistd.h>
ssize_t read(int fd, void *buf, size_t count);
```
.footnote[
1 In C, any program that makes a system call must include .fixedblue[unistd.h].
]
The page shows that .fixedblue[read] has three parameters:
.lightbluebox[
```C
int fd : an integer file descriptor that identifies the file from which to read
void * buf : the address of the first byte of memory into which to store the data
size_t count : the number of bytes to be read into the buffer
```
]
It returns a .fixedblue[ssize_t] result, which is the number of bytes actually read.
.fixedblue[ssize_t] is a .fixedblue[C] signed integer type.
---
name: read-syscall-activity
count: false
class: breakout
### A .fixedblue[read] System Call Activity
- Login to .fixedblue[eniac.cs.hunter.cuny.edu] and then .fixedblue[ssh] into
any .fixedblue[cslab] host.
- Navigate to the directory
.fixedblue[/data/biocs/b/student.accounts/cs340_sw/demos]
- There you will find the file .fixedblue[mycp.c], which is a full version of the
program from the previous slide that copies one file to another.
- Copy the .fixedblue[mycp.c] program into your home directory using the system
.fixedblue[cp] command.
- Using a simple editor, make the buffer size larger and smaller, recompiling
each time and running on some large file. How does buffer size affect running time?
You can find large, readable files in <br>.fixedblue[/var/cache/apt/archives].
---
name: syscall-implementation-1
### System Call Implementation
In the first chapter, you learned that when a trap occurs as a result of a system
call, the mode bit is switched to kernel mode and a system call handler runs.
This handler handles all system calls.
The system call handler knows which system call to invoke because each system call
has a number associated with it, that is read from a register. (In __Linux__ it is
in the .fixedblue[eax] register.)
--
The kernel maintains a .greenbold[system call dispatch table]
indexed according to these numbers.
The table contains the address of the start of each system call in the kernel.
The system-call handler invokes the actual kernel function, which runs in kernel mode,
and when it is finished, it returns control back to the handler, which
returns the exit status of the system call and any return values.
--
.redbold[Question:]
When a program makes a call such as
```C
read(fd, buffer, 256);
```
how are the arguments
of this function made available to the kernel?
---
name: syscall-parameters
### System Call Parameter Passing
There are three general methods for passing the arguments of calls to the kernel in a system call.
- .bluebold[Register Method]: The simplest method is to put them into known registers in a specific order.
When the number of parameters exceeds the number of available registers,
or if the lengths of these parameters exceed the size of the registers, this does not work.
- .bluebold[Block Method]: The parameters are stored in a block of consecutive bytes in memory, and the
address of the block is passed in a register.
- .bluebold[Stack Method]: The parameters are pushed onto the stack by the program and popped off the
stack by the kernel. The problem with this is that there are usually two separate
stacks - one for the user program and one for the kernel, and popping from one and
pushing to the other is painstakingly slow.
Neither the block method nor the stack method limits
the number of parameters.
.lightbluebox[
__Linux__ uses a combination of the register method and the block method. If the
parameters fit into the registers, it uses them, otherwise the block method is used.
In the current version, __Linux__ does not allow more than six parameters to a system call.
]
---
name: syscall-implementation
### Under the Hood: System Calls
Different operating systems implement system calls in different ways. We describe
how they are implemented in __Linux__, which is roughly how they are implemented in
__macOS__. The __Windows__ implementation is different.
--
The wrapper function for a system call in __Linux__ usually does little more
than the following:
- It copies the arguments and the unique system call number to the registers
where the kernel expects them;
- It traps to kernel mode, at which point the system call handler in the kernel
does some setup and then invokes system call itself;
- It puts an error value in a special variable named .fixed[errno]
if the system call returns an error number when the
kernel returns the CPU to user mode.
Depending on the call, it might do more than this, such as pre-processing the arguments
or post-processing results.
--
This shows why the wrapper function exists: these actions could not be performed
in the kernel itself. If nothing else, the trap to kernel mode must take place
outside the kernel!
---
name: syscall-complete-flow
### Illustration of the Complete Flow
The diagram below depicts the complete control flow, from user mode to kernel mode and
back, of a call by a main program to a fictitious system call named .fixedblue[foo].
<img src="figures/syscall_full_flow.png" width=95% alt="Control Flow of System Call">
- The code in the figure labeled .fixedblue[glibc] library is a mix of pseudocode and assembly.
The call .fixedblue[SYSCALL] does not really pass the number of the call; it would be in
a register.
- The system call handler is assembly code and it invokes the actual code in the
kernel. The handler has already determined which function it is and the call
.fixedblue[syscall_foo()] is just pseudocode - it would be assembly code to jump
to that function.
---
name: syscall-read
### Flow of .fixed[read] System Call in Linux
The following diagram.super[1] depicts the sequence of events that take place when a
.fixedblue[C] program calls the __Linux__ .fixedblue[read()] system call.
(The compiled call is to .fixedblue[_libc_read()]).
.center[
<img src="figures/ksys-figure1.png" width=80% alt="system call"/>
]
.footnote[
1. by Alessandro Rubini, at http://www.linux.it/~rubini/docs/ksys/
]
---
name: syscall-types
### Categories of System Calls
Most operating systems provide a large number of system calls.
For example, there are more than four hundred in a typical __Linux__ version.
To put some structure around them,
we can group most system calls into the following categories:
- Process control
- File management
- Device management
- Information maintenance
- Communication and synchronization
- Protection
--
The next few slides briefly describe the calls in each category.
---
name: process-syscalls
### Process Control
The most important types of services provided by system calls related to
process control fall into these subcategories:
- creating and terminating processes
- loading and executing processes
- getting and setting process attributes
- waiting for events
- signaling events
- registering signal actions, setting signal handling properties
- allocating and freeing memory
--
Processes and the kernel's support for them is the subject of
Chapters 3 and 4 (.greenbold[threads].)
---
name: file-syscalls
### File Management
The most important types of services provided by system calls related to
file management fall into these subcategories:
- creating and deleting files and directories
- linking and renaming files
- opening and closing files
- duplication of file descriptors
- reading and writing files
- repositioning read/write pointers
- getting and setting file attributes
--