16
CSCI-3753: Operating Systems Fall 2019 Anh Nguyen Department of Computer Science University of Colorado Boulder

CSCI-3753: Operating Systems Fall 2019mnslab.org/.../F19_3753_Anh_Recitation_Week4.pdf · F19_3753_Anh_Recitation_Week4 Created Date: 9/27/2019 1:44:58 PM

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CSCI-3753: Operating Systems Fall 2019mnslab.org/.../F19_3753_Anh_Recitation_Week4.pdf · F19_3753_Anh_Recitation_Week4 Created Date: 9/27/2019 1:44:58 PM

CSCI-3753: Operating SystemsFall 2019

Anh NguyenDepartment of Computer Science University of Colorado Boulder

Page 2: CSCI-3753: Operating Systems Fall 2019mnslab.org/.../F19_3753_Anh_Recitation_Week4.pdf · F19_3753_Anh_Recitation_Week4 Created Date: 9/27/2019 1:44:58 PM

Programming Assignment 2

2CSCI 3753 Fall 2019

Page 3: CSCI-3753: Operating Systems Fall 2019mnslab.org/.../F19_3753_Anh_Recitation_Week4.pdf · F19_3753_Anh_Recitation_Week4 Created Date: 9/27/2019 1:44:58 PM

LKM – Quick Recap

•What are two main functions required in a LKM code?

•How to compile the LKM code?

•How to load the module into the kernel?

•How to unload the module from the kernel?

3CSCI 3753 Fall 2019

Page 4: CSCI-3753: Operating Systems Fall 2019mnslab.org/.../F19_3753_Anh_Recitation_Week4.pdf · F19_3753_Anh_Recitation_Week4 Created Date: 9/27/2019 1:44:58 PM

PA2 – Character Device File

4CSCI 3753 Fall 2019

Programs

Kernel

User Space

Physical Device

Device File (/dev/<name>)

Open, Read, Write, Close, Seek

File Operations

mknod –m <permission> <device_file_location><type of driver><major number><minor number>

For example,sudo mknod –m 777

/dev/simple_character_devicec2400

<major>:<minor>

Device Driver

<minor><name>

Page 5: CSCI-3753: Operating Systems Fall 2019mnslab.org/.../F19_3753_Anh_Recitation_Week4.pdf · F19_3753_Anh_Recitation_Week4 Created Date: 9/27/2019 1:44:58 PM

PA2 – Requirements

1. Dynamically allocate constant-size kernel buffer to store the data written by the user • kmalloc() • Allocate memory for objects smaller than page size in the

kernel at initialization time

• kfree()• Free memory previously allocated using kmalloc() before

exiting

2. NO over seeking/reading/writing in buffer

3. Always remember the position of pointer in the device file after each input action

6CSCI 3753 Fall 2019

Page 6: CSCI-3753: Operating Systems Fall 2019mnslab.org/.../F19_3753_Anh_Recitation_Week4.pdf · F19_3753_Anh_Recitation_Week4 Created Date: 9/27/2019 1:44:58 PM

PA2 – Requirements

1. Read/write function• Input: device file, user-space buffer, offset•Output (recommended): the number of bytes you read or

write at the end of each function call• If error, return -1

2. Seek function• Input: device file, offset, whence•Output (recommended)• If error, return -1

• If successful, return 0 or positive value

7CSCI 3753 Fall 2019

Page 7: CSCI-3753: Operating Systems Fall 2019mnslab.org/.../F19_3753_Anh_Recitation_Week4.pdf · F19_3753_Anh_Recitation_Week4 Created Date: 9/27/2019 1:44:58 PM

PA2 – Requirements

1. Open the device file at the beginning of the test program

2. Use the "register_chrdev" function to attach the module to the device file when loading the module

3. Use the ”unregister_chrdev" function to detach the module from the device file when unloading the module

8CSCI 3753 Fall 2019

Page 8: CSCI-3753: Operating Systems Fall 2019mnslab.org/.../F19_3753_Anh_Recitation_Week4.pdf · F19_3753_Anh_Recitation_Week4 Created Date: 9/27/2019 1:44:58 PM

PA2 – Updates

•asm/uaccess.h à linux/uaccess.h

• file_operations structure à start from line 1695 in file

/lib/modules/4.15.0-33-generic/build/include/linux/fs.h

• .close à .release

• For seek operation, there is no pointer returned.

loff_t (*llseek) (struct file *, loff_t, int)

change to

loff_t simple_character_driver_seek (struct file *, loff_t, int)

9CSCI 3753 Fall 2019

Page 9: CSCI-3753: Operating Systems Fall 2019mnslab.org/.../F19_3753_Anh_Recitation_Week4.pdf · F19_3753_Anh_Recitation_Week4 Created Date: 9/27/2019 1:44:58 PM

Week 4: Processes

10CSCI 3753 Fall 2019

Page 10: CSCI-3753: Operating Systems Fall 2019mnslab.org/.../F19_3753_Anh_Recitation_Week4.pdf · F19_3753_Anh_Recitation_Week4 Created Date: 9/27/2019 1:44:58 PM

Processes

•Programs that are running on your machine

•Managed by the kernel

•Have an ID associated with it called the process ID (PID)

•ps command• PID: Process ID

• TTY: Controlling terminal

associated with the process

• STAT: Process status code• TIME: Total CPU usage time

• CMD: Name of executable/command

11CSCI 3753 Fall 2019

Page 11: CSCI-3753: Operating Systems Fall 2019mnslab.org/.../F19_3753_Anh_Recitation_Week4.pdf · F19_3753_Anh_Recitation_Week4 Created Date: 9/27/2019 1:44:58 PM

/proc directory

•Contain virtual files and numbered directories corresponding to each running process

•Directory names = process ids

•When a process ends, its directory in /proc disappears automatically.

•Files in a numbered directory• cmdline• cwd

• environ• fd•maps, statm, mem; stat, status

12CSCI 3753 Fall 2019

Page 12: CSCI-3753: Operating Systems Fall 2019mnslab.org/.../F19_3753_Anh_Recitation_Week4.pdf · F19_3753_Anh_Recitation_Week4 Created Date: 9/27/2019 1:44:58 PM

/proc directory

• Some typical virtual files that provide • Hardware information:• /proc/cpuinfo: identifies the type of processor used by your

system• /proc/iomem: shows you the current map of the system's

memory for each physical device • /proc/meminfo• /proc/interrupts

• File-related info:• /proc/filesystems: displays a list of the file system types currently

supported by the kernel • /proc/partitions

• Kernel configuration parameters:• /proc/cmdline: shows the parameters passed to the kernel at the

time it is started• /proc/sys

13CSCI 3753 Fall 2019

Page 13: CSCI-3753: Operating Systems Fall 2019mnslab.org/.../F19_3753_Anh_Recitation_Week4.pdf · F19_3753_Anh_Recitation_Week4 Created Date: 9/27/2019 1:44:58 PM

top command

•Display a list of processes with their resource usage.

14CSCI 3753 Fall 2019

Page 14: CSCI-3753: Operating Systems Fall 2019mnslab.org/.../F19_3753_Anh_Recitation_Week4.pdf · F19_3753_Anh_Recitation_Week4 Created Date: 9/27/2019 1:44:58 PM

Process State

•New: The process is being created.

•Ready: The process is waiting to be assigned to a processor.

•Running: Instructions are being executed.

•Waiting: The process is waiting for some event to occur (such as an I/O completion or reception of a signal).

•Terminated: The process has finished execution.

15CSCI 3753 Fall 2019

Page 15: CSCI-3753: Operating Systems Fall 2019mnslab.org/.../F19_3753_Anh_Recitation_Week4.pdf · F19_3753_Anh_Recitation_Week4 Created Date: 9/27/2019 1:44:58 PM

Program vs Application vs Process vs Thread

16CSCI 3753 Fall 2019

•Program: a passive entity

•Application: a program loaded into the memory

•Process: a program actively executing from main memory within its own address space

•Thread: a logical flow or unit of execution that runs within the context of a process• Thread-safety, race condition, reentrancy

Page 16: CSCI-3753: Operating Systems Fall 2019mnslab.org/.../F19_3753_Anh_Recitation_Week4.pdf · F19_3753_Anh_Recitation_Week4 Created Date: 9/27/2019 1:44:58 PM

Week 4 – Checklist

q Discuss PA2

q Discuss processes vs threads

19CSCI 3753 Fall 2019