0
0
Operating Systemsknowledge~10 mins

Memory-mapped files in Operating Systems - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a memory-mapped file in Python.

Operating Systems
import mmap
with open('example.txt', 'r+b') as f:
    mm = mmap.mmap(f.fileno(), [1])
    print(mm.readline())
Drag options to blanks, or click blank then click option'
A1024
B-1
C0
DNone
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 or -1 as size causes errors or unexpected behavior.
2fill in blank
medium

Complete the code to open a memory-mapped file with read-only access.

Operating Systems
import mmap
with open('data.bin', 'rb') as f:
    mm = mmap.mmap(f.fileno(), 0, access=[1])
    print(mm.read(10))
Drag options to blanks, or click blank then click option'
Ammap.ACCESS_WRITE
Bmmap.ACCESS_DEFAULT
Cmmap.ACCESS_COPY
Dmmap.ACCESS_READ
Attempts:
3 left
💡 Hint
Common Mistakes
Using ACCESS_WRITE allows writing, which is not desired here.
3fill in blank
hard

Fix the error in the code to correctly map the entire file size.

Operating Systems
import os
import mmap
filename = 'logfile.log'
size = os.path.getsize(filename)
with open(filename, 'r+b') as f:
    mm = mmap.mmap(f.fileno(), [1])
    print(mm[:10])
Drag options to blanks, or click blank then click option'
A-1
B0
Csize
Dlen(filename)
Attempts:
3 left
💡 Hint
Common Mistakes
Passing 0 maps the whole file only on some systems, which is not portable.
4fill in blank
hard

Fill both blanks to create a memory-mapped file and write data to it.

Operating Systems
import mmap
with open('file.dat', 'r+b') as f:
    mm = mmap.mmap(f.fileno(), [1], access=[2])
    mm[0:5] = b'Hello'
    mm.flush()
Drag options to blanks, or click blank then click option'
A10
Bmmap.ACCESS_READ
Cmmap.ACCESS_WRITE
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using ACCESS_READ prevents writing and causes errors.
5fill in blank
hard

Fill all three blanks to create a memory-mapped file, read a slice, and close it properly.

Operating Systems
import mmap
with open('sample.txt', 'r+b') as f:
    mm = mmap.mmap(f.fileno(), [1], access=[2])
    data = mm[[3]:[3]+5]
    print(data)
    mm.close()
Drag options to blanks, or click blank then click option'
A20
Bmmap.ACCESS_READ
C10
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using ACCESS_WRITE when only reading is needed.