reference]
https://sepang2.tistory.com/39
I/O Systems and Operations
이번에는 입출력 시스템&입출력 작업에 대해 알아볼 것이다. 다음과 같은 순서로 구성된다. An Overview of I/O Subsystem and Operations Stream Model: I/O functions, Standard streams Buffering: Block buffering vs. Line bufferin
sepang2.tistory.com
Contents
• An Overview of I/O Subsystem and Operations
• Stream Model – I/O functions – Standard streams
• Buffering – Block buffering vs. Line buffering vs. Unbuffered
• Pipes • File I/O – File pointer – File Attributes
• Device I/O – Device Drivers – Block devices vs. Character devices vs. Network devices
Reminder: A View of OS Services
I/O (Input/Output)
• The main job of a computer is I/O
– Its another (major) job, Computing or Processing, is merely secondary
• The role of OS in computer I/O is to control devices and to manage I/O operations
=> I/O 디바이스 컨트롤, I/O operation들을 manage
The Role of OS: I/O Device Control
• I/O devices vary so widely in their function and speed
-> Varied methods are needed to control them
– These methods form the “I/O subsystem of the OS kernel”, which separates the rest of the kernel from the complexities of managing I/O devices
• The basic I/O hardware elements, such as ports, buses, and device controllers, accommodate a wide variety of I/O devices
– Port: A connection point between a device and a computer (e.g., a serial port)
– Bus: A common set of wires, shared by devices, and a rigidly defined protocol that specifies a set of messages that can be sent on the wires (I/O 디바이스와 CPU와 연결)
=> I/O 디바이스는 매우 다양하므로 method 또한 매우 다양
• To encapsulate the details and oddities of different devices, the OS kernel is structured to use device-driver modules
– Device Driver: A uniform device-access interface to the I/O subsystem, much as system calls provide a standard interface between the application and the OS
=> I/O 컨트롤을 하는 매개체
A Typical PC Bus Structure
A Kernel I/O Structure
The Role of OS: I/O Operation Management
• I/O operations or transactions occur when a program receives bytes from a source (e.g., a keyboard, mouse, file, and sensor) or sends bytes to a destination (e.g., a display, file, printer, and actuator)
-> Most modern OS, including Unix/Linux, use the “stream model” to control I/O
Stream Model
• Any input or output transaction can be viewed as a flow of bytes (=stream) from a source to a destination
• The operating system creates and manages streams based upon the function calls made by the program – An I/O stream connection can be established using the fopen() function, and broken using the fclose() function
=> source에서 dest로 byte sequence(stream)을 전송, fopen으로 stream 열기, fclose으로 stream 닫기
The Steps in Creating, Using, and Closing a Stream
• When a stream is created, one can think of it as having an address from which bytes are sent and an address at which bytes are received.
– Each address is at a memory location controlled by the OS
=> OS은 이미 source의 address와 destination의 address를 알기에 data transfer를 해준다.
Transporting Bytes on Streams
• There are several functions that can be used to transport bytes on streams:
– These include the familiar fprintf() and fscanf() functions, which are specialized to work only with ASCII formatted text data (byte tranfer + byte manipulation)
– The generic functions for this purpose are fread() and fwrite() (byte tranfer에만 신경씀)
->At the heart of both fprintf() and fscanf() functions are calls to fread() and fwrite() to actually move the bytes after the desired byte manipulations have been completed
– There are a number of other functions as well, such as fgetc(), fputc(), gets(), and puts()
fprintf()
• Bytes are being sent down a stream using the fprintf() function
fscanf()
• Bytes are being transported on a stream using the fscanf() function
- 하나의 string을 공백 문자나 개행문자로 구분!
- 데이터 가공: string 타입으로 써야하므로 끝에 \0을 붙여줌!
fscanf()
• fscanf() and fprintf() functions perform byte manipulations(fprintf도 수행) besides the byte transfers.
fread() & fwrite()
• Synopsis
– size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
– size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
• Description
– The function fread() reads nmemb elements of data, each size bytes long, from the stream pointed to by stream, storing them at the location given by ptr.
– The function fwrite() writes nmemb elements of data, each size bytes long, to the stream pointed to by stream, obtaining them from the location given by ptr.
• Return value
– On success, fread() and fwrite() return the number of items read or written (=fread(),fwrite() : element의 개수가 리턴됨)
-> An indicator of the number of bytes actually moved along the stream
– If an error occurs, the return value is a short item count (or zero). (=전송된 element의 개수가 리턴됨)
fread()
• Bytes are read from a source into the program regardless of what the bytes represent.
fwrite()
• Bytes are moved from the program into a destination
– The parameters of the fwrite() are identical to those in the fread() function
– Executing this code will produce a file data2.txt including “Fortytwo 42 …”
– Note that this example uses the strlen() function to determine the number of cells in the array to send on the stream, which in this case is 40.
Short Question…
• What would you expect here?
System I/O Functions
• Another set of functions that can be used for I/O
– These functions include open(), close(), read(), and write() (<= C libraryX Lib by OS O)
– They look very similar to the f-versions of the I/O functions, i.e., fopen(), fclose(), fread(), and fwrite() functions, which are standardized in the C library and therefore expected to behave similarly regardless of the underlying operating system
*** open(){ sys-open()} =wrapper function
• The non-f I/O functions are “system calls” (open(),close()..등은 시스템콜으로 취급,OS type에 따라 동작이 달라질 수 O)
– Depending on which OS they are called from, they may behave differently
– A general rule of thumbs is that the non-f versions are NOT buffered, while the f-versions are buffered
Standard Streams
• Most programs get user input from a keyboard and display text output to a screen.
=> 모든 application이 keyboard로 user input을 받을 수 있어야 하고, 스크린으로 output을 나타낼 수 있어야 함.
Therefore, every time a program is started, the OS automatically creates three streams:
– Standard in (stdin): It connects keyboard to program
– Standard out (stdout): It connects program to display
– Standard error (stderr)
It connects program to a secondary display that is intended only for displaying errors 일반적으로는 stdout과 동일한 display
stderr stream is intended as a backup output stream for programs
• The standard streams are most commonly used by calling the scanf() and printf() functions
– While fscanf() can receive bytes from any stream, scanf() is “hardwired” to the stdin stream. The same is true with regards to printf() and fprintf()
Example
• In the following example, the pair of scanf() & fscanf() and printf() & fprintf() function calls perform the exact same operation; => scanf와 fscanf 동일하게 동작, printf()와 fprintf() 동일하게 동작
– The definitions for the standard streams can be found in the include file stdio.h
Buffering
• A buffer is a temporary storage or the “middlemen” between sender and receiver of bytes on a stream (i.e., between two devices or between a device and an application)
=> sender -> BUFFER -> receiver
– An additional piece of memory that is used to moderate the flow of bytes from the source to the destination
• Buffering is done for 3 reasons: (Buffering 이유)
– To cope with device speed mismatch
What if the sender puts bytes into the stream faster than the receiver can handle?
What if a program is in the middle of a calculation and is not prepared to receive any bytes?
– To cope with device data-transfer size mismatch
Common in computer networking, where buffers are used widely for fragmentation and reassembly of messages
– To support “copy seman tics” for application I/O (=> write()호출했을 때의 data가 그대로 copy되어야하기 때문)
Three Types of Buffering
• Based on how the temporary storage is “flushed”
– Block buffering
The buffer is flushed when it receives a certain amount of data (e.g. 1KB, 16KB, 64KB, etc…)
Commonly used for large data transfers, such as file I/O
– Line buffering
The buffer is flushed when it receives a newline character(‘\n’)
Typically used for text-based I/O, such as when interacting with a user
– Unbuffered
The buffer doesn’t act as a buffer and is flushed every bytes
Buffering may be completely turned off when responsiveness is critical
• The type of buffering on an existing stream can be changed by the setvbuf() or setbuf() function
Example code
'시스템프로그래밍' 카테고리의 다른 글
Process/Thread Management (0) | 2022.12.01 |
---|---|
I/O Systems and Operations_2 (0) | 2022.11.24 |
OS Structures & Linux Overview (0) | 2022.11.10 |
OS Structures & Linux Overview (0) | 2022.11.07 |
Operating Systems: An Overview_2 (2) | 2022.11.06 |