introduction to tizen platformnyx.skku.ac.kr/wp-content/uploads/2016/03/lecture11-week... · 2016....
TRANSCRIPT
19
1
Embedded Software Lab.
Sungkyunkwan University
Embedded Software Lab.
Dongkun Shin
전공핵심실습1:운영체제론Chapter 11. Accessing Files
19
2
Embedded Software Lab.
• Tizen Z3 기기 반납– 일시: 2016년 6월 14일 화요일 오후 6:15~ (15주차 수업 시간)
– 구성품
• Samsung Z3(SM-Z300HZDDINS) 1대
• Micro-USB 케이블 1개
• Micro-SD 카드 (Sandisk micro-SDHC Ultra 16GB Class10) 1개
• Micro-SD 카드 리더기 (Memorette M2 리더기) 1개
– 실습장비 사용 중에 관리 소홀로 인한 고장, 부속품 분실 등으로비용이 발생할 경우 대여자의 책임으로 처리함.
Announcement
19
3
Embedded Software Lab.
• Reading and Writing a File
• Memory Mapping
• Practice 1. Accessing File in Linux
• Memory Area Allocation in malloc
• Practice 2. malloc in Linux
Contents
19
4
Embedded Software Lab.
• File Accessing Modes– Canonical mode
• Read ops block the calling process, Write ops do not block the calling process. (Read : Sync, Write : Async by using page cache)
– Synchronous mode
• Write ops also block the calling process.
– Memory Mapping mode
• Map the file into memory that can be accessed directly by the applications. (User buffer has a role as page cache)
– Direct I/O mode
• Bypass the page cache.
– Asynchronous mode
• Requests for data transfers never block the calling process.
Accessing Files in Linux
19
5
Embedded Software Lab.
Linux I/O Stack
19
6
Embedded Software Lab.
• Reading from a file (Canonical/Synchronous mode)– Page-based
• If the data is not in RAM
• Kernel allocates a new page frame
• Fills the page with the data
• Adds the page to the page cache (Kernel Address Space)
• Copies the page into the process address space
• Writing to a file– Depends on the file system type.
• Usually do_generic_file_write()
Reading and Writing a File
19
7
Embedded Software Lab.
• do_generic_file_read()
• Descriptors
– iovec : address & length of user mode buffer
– kiocb : keep track of the completion status of I/O operation
Reading from a file
Corresponding file object pointerSemantic descriptor for asynchronous I/OMethod called in cancelling this operation
Current position of the file in this operationBytes to transfer
19
8
Embedded Software Lab.
Read()
19
9
Embedded Software Lab.
• Many disk accesses are sequential
• Enhance disk performance & Improve system responsiveness
• No use when performing random accesses to files
• Current Window– Consists of pages requested by process or read in advance in
kernel.
• Ahead Window– Consist of pages currently being read by kernel
– Not yet requested by process
Read-ahead
PG_READAHEAD
19
10
Embedded Software Lab.
• Read ahead Descriptor
Read-ahead (cont’)
19
11
Embedded Software Lab.
Write
19
12
Embedded Software Lab.
• Memory Mapping (File I/O by generic memory instruction)– Memory region can be associated with regular file or block
device file
– Accessing pages of the memory region = operation on the mapped file
– Shared memory paging (MAP_SHARED)
• Each write changes the file on disk
• Visible to all other processes that map the same file
– Private memory paging (MAP_PRIVATE)
• Just a read the file, not to write it
• Much more efficient than shared memory mapping
Memory Mapping
19
13
Embedded Software Lab.
Memory Mapping Data Structures
Metadata about memory area of a process
Metadata about page cache memory area of a file
19
14
Embedded Software Lab.
• mmap() system call– Parameters
• File descriptor
• Offset inside the file
• Length of the file
• Flag (Shared or Private)
• Permission (Read, Write and Execute)
– Procedure1. Check availability of mmap.
2. Find available virtual memory area with get_unmapped_area()
3. Check open mode
4. Initialize memory mapping descriptors
• Destroying a Memory Mapping– munmap()
• Flush all dirty data and remove vma
Creating a Memory Mapping
19
15
Embedded Software Lab.
• Write the code and compile by gcc. (in Linux PC)1. $ gedit fileaccess.c
2. $ gcc -o fileaccess fileaccess.c
Practice 1. Accessing File in Linux (1/2)
#include <stdio.h>
int main() {FILE *fp1, *fp2;int readdata;printf("1. fopen()\n");fp1 = fopen("./a.txt", "w");printf("2. fprintf()\n");fprintf(fp1, "12345");printf("3. fclose()\n");fclose(fp1);
printf("4. fopen()\n");fp2 = fopen("./a.txt", "r");printf("5. fscanf()\n");fscanf(fp2, "%d", &readdata);printf("6. fclose()\n");fclose(fp2);return 0;
}
19
16
Embedded Software Lab.
• Print system calls called on accessing file. – $ strace ./fileaccess
Practice 1. Accessing File in Linux (2/2)
19
17
Embedded Software Lab.
• Chunk– Memory allocation unit of malloc
• Bin– a doubly linked list aggregating
free chunks by size
• Small bin(16B≤|r|≤512B)– Allocated by brk() (data area)
– b : 1 bin per 8B unit = 62 bins
• Large bin(512B<|r|)– b : 64B, 512B, 4KB, 32KB, 256KB
– The bins includes chunks of which size is less that b.
– 128KB<r• Allocated by mmap() (heap area)
• Cannot be separated
Memory Area Allocation in malloc
r : requested memory sizeb : bin’s chunk unit criteria
b
19
18
Embedded Software Lab.
• Write the code and compile by gcc. (in Linux PC)1. $ gedit malloc.c
2. $ gcc -o malloc malloc.c
Practice 2. malloc in Linux (1/2)
#include <stdio.h>#include <stdlib.h>
int main() {void *mem1, *mem2;printf("1. malloc(100)\n"); // 100Bmem1 = malloc(100);printf("2. first free()\n");free(mem1);printf("3. malloc(1000000)\n"); // 1MBmem2 = malloc(1000000);printf("4. second free()\n");free(mem2);
}
19
19
Embedded Software Lab.
• Print system calls called on accessing file. – $ strace ./malloc
Practice 2. malloc in Linux (2/2)