시스템프로그래밍

I/O Systems and Operations_2

아무 말하는 감자 2022. 11. 24. 14:42

Three Types of Buffering

• Based on how the temporary storage is “flushed” (=buffer를 비우는 방법에 따라 buffering 타입 다름)

   – 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

      => new line character를 받았을 때 buffer 비움

   – 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

fflush(): force buffer flusing / 첫번째는 출력 나오는데 5초 걸림 (buffer에 계속해서 채워지다가 프로그램 종료시 OS가 flush하기 때문)<->두번째는 하나의 출력시 1초 (stdout이 내부적으로 line buffering을 사용하기 때문), 세 번째도 1초

Pipes

• The term “PIPE” (stream의 source나 destination은 다른 곳으로 redirection 할 수 있는 것)

   – Used in contexts where streams are reconnected to alternate sources or destinations

   – The process of connecting and reconnecting streams is referred to as piping, or pipelining

   – An example of piping is to reconnect the standard out stream so that it simultaneously sends the same bytes to a file and a display

   – pipe() and dup() are the file I/O functions (i.e., system calls) that can be used to manipulate stream connections

Piping symbols

• 3 pipelining symbols

  – OS automatically creates three streams for every running program.

  – Most shells provide the ability to redirect those streams at startup

입 벌린 쪽이 받는 쪽!

Piping Example (1/3)

Piping Example (2/3)

symbol을 써서 piping 한 것 , source,des 연결을 바꿈

Piping Example (3/3)

• Connect the stdout stream of one program to the stdin stream of a second program

 

두 프로그램 사이에 pipe같은 경로가 생기고 한 프로그램의 output이 다음 프로그램의 input으로 들어감

Pipe chaining

• Pipeline chaining (pipeline 심볼들이 연속적으로 나옴)

   – Multiple piping redirections can be done simultaneously

• Example

  – Both the stdin and stdout of the summer are being redirected

  – bingo also has its stdout stream redirected to a file

 

Files

• What is it?

– Contiguous logical address space

– An image, movie, or database can be stored as a one-dimensional array of bytes by writing all the elements out in a long list

=> secondary storage에 있음

File Pointer

• A marker used to keep track of the location for reading or writing on a stream.

• When opening a file, the file pointer points to the 1st byte in the file. Each time a byte is read, the file pointer automatically advances to the next byte.

   – When reading multiple bytes, the file pointer advances beyond all the read bytes

• Functions for manipulating the file pointer:

  – fseek(): moves it to a new value without reading or writing any data on the stream

  – ftell(): obtain its current value

=> 처음에는 제일 첫번째 byte를 가리킴, 계속 이동하는 것

 

Example

• Consider the following data in a file: abcdef

fseek()

• Synopsis

  – int fseek(FILE *stream, long offset, int whence);  // file pointer 위치 이동 명시해줄 수 있음

• Description

  – The fseek() function sets the file position indicator for the stream pointed to by stream

  – The new position, measured in bytes, is obtained by adding offset bytes to the position specified by whence

  – If whence is set to SEEK_SET, SEEK_CUR, or SEEK_END, the offset is relative to the start of the file, the current position indicator, or end-of-file, respectively

• Return value

  – Upon successful completion, fseek() returns 0 – If an error occurs, the return value is -1

 

Example: fileseek.c

File Attributes (file 정보)

• They are metadata associated with computer files

  – Name, size, date, time, and file system permissions, …

owner,group가 어떤 permission을 가지고 있는지 알 수 있음

• Permission indicates who is allowed to access the file, and what types of access are allowed

r: read, w:write, x:excute, -: permission denied

– On a UNIX system, it can be changed using the “chmod” system program

 

stat()

• Synopsis

– int stat(const char *pathname, struct stat *buf);

 

• Description (참고)

– It returns information about a file, in the buffer pointed to by buf

– No permissions are required on the file itself, but—in the case of stat() — execute (search) permission is required on all of the directories in pathname that lead to the file

 

• Return value

– On success, zero(0) is returned

– If an error occurs, the return value is -1

 

stat data structure

stat example code

Directory

• Directory

– A directory is an organizational tool for files

– List of filenames and auxiliary information for each file

– OS manages directories in much the same manner as it manages files

=> file들을 구조화하기 위한 도구 (=directory=folder)

 

• These tables can be accessed using the opendir(), readdir(), and closedir() functions

Example of directory code

 

 

Devices

• One of the neat aspects of UNIX is that any peripheral, device, or piece of hardware connected to the computing system can be accessed as if it was a file

• This concept is often referred to as “file-based I/O”

  – An I/O transaction with a device is handled similarly to an I/O transaction with a file. They both make use of streams and buffers, and use the same I/O functions

=> 대부분의 UNIX 기반 시스템에서는 device I/O를 file I/O와 동일하게 취급 , 다른 interface 적응할 필요 X

appication이 file I/O interface를 이용해 Device file에다 요청

 

Device Driver

• A device driver is a set of functions used to access a device

  – open(), close(), read(), write(), lseek(), dup(), …

• The functions associated with a particular device are executed when the device is accessed through its device filename 44 44 • A device driver layer within a kernel I/O structure

– Making the kernel I/O subsystem independent of the hardware 

   -> simplify the job of the OS developer

=> 이런 디자인의 Device Driver가 HW independent하게 만들어줌

 

 

 

 

Device Driver

• Most operating systems have an escape (or back door) that transparently passes arbitrary commands from an application to a device driver

– In UNIX, this system call is ioctl(), which enables an application to access any functionality that can be implemented by any device driver, without the need to invent a new system call

 

ioctl()

=> application이 제한된 방식으로 HW device를 제어

• Synopsis – #include int ioctl(int fd, unsigned long request, …); : fd: file discripter, request: request 코드

• Description – The ioctl() system call manipulates the underlying device parameters of special files. In particular, many operating characteristics of character special files (e.g., terminals) may be controlled with ioctl() requests

– The 1st argument fd must be an open file descriptor (= a device identifier that connects the application to the driver)

– The 2nd argument is a device-dependent request code (= an integer that selects one of the commands implemented in the driver) 

– The 3rd argument is a pointer to an arbitrary data structure in memory that enables the application and driver to communicate any necessary control information or data (request에 대한 추가적 요청)

 

 

Major/minor number

• The device identifier in UNIX/Linux is a tuple of “major and minor” device numbers

Major number is the device type, and it is used by the OS to route I/O requests to the appropriate device driver

Minor number is the instance of that device, and it is used to provide a unique ID for multiple device filenames that use the same device driver functions.

The Overall Structure of Device-Driver System

• Linux splits all devices into three classes:

– Block devices – Character devices – Network devices (data를 전송하는 방법의 차이)

Block and Character Devices

• Block device – Include a disk driver ,byte block단위 전송

  – Transfer a block of bytes as a unit

  – Expected to understand commands such as read(), write(), seek()

• Character-stream device , keyboard(),mouse() 1byte씩 전송

  – Include a keyboard, mouse, serial port, printer, audio board

  – Transfer bytes one by one – Commands include get(), put()

  – Libraries layered on top allow line-at-a-time access, with buffering and editing services (e.g., when a user types a backspace, the preceding character is removed from the input stream)

 

Network Devices

• Because the performance and addressing characteristics of network I/O differ significantly from those of disk I/O, most OS provide a network I/O interface that is different from the read()-write()-seek() interface used for disks

– One interface available in many OSs, including UNIX/Linux and Windows, is the network socket interface

 

• Just like a wall socket for electricity ( -> any electrical appliance can be plugged in), the system calls in the socket interface enable an application to

– create a socket

– connect a local socket to a remote address

– listen for any remote application to plug into the local socket

– send and receive packets over the connection

 

Summary

• Streams

– Any input or output transaction can be viewed as a flow of bytes

• Buffers

– It is a temporary storage between the sender and receiver

• Pipes

– The process of connecting and reconnecting streams

• Files

– Files stored as a one-dimensional array of bytes

• Devices

– Devices can be accessed as though it were a file

 

'시스템프로그래밍' 카테고리의 다른 글

Process/Thread Management_2  (0) 2022.12.05
Process/Thread Management  (0) 2022.12.01
I/O Systems and Operations  (1) 2022.11.21
OS Structures & Linux Overview  (0) 2022.11.10
OS Structures & Linux Overview  (0) 2022.11.07