csci 3753: operating systems spring 2021

17
CSCI 3753: Operating Systems Spring 2021 Anh Nguyen Department of Computer Science University of Colorado Boulder

Upload: others

Post on 15-Jun-2022

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSCI 3753: Operating Systems Spring 2021

CSCI 3753: Operating Systems

Spring 2021Anh Nguyen

Department of Computer Science University of Colorado Boulder

Page 2: CSCI 3753: Operating Systems Spring 2021

Week 4: Programming Assignment 2fork() vs exec() System Calls

2CSCI 3753 Spring 2021

Page 3: CSCI 3753: Operating Systems Spring 2021

Programming Assignment 2

3CSCI 3753 Spring 2021

Page 4: CSCI 3753: Operating Systems Spring 2021

An Overview

4CSCI 3753 Spring 2021

Kernel SpaceUser Space

Physical Device

Programs Device Driver

Page 5: CSCI 3753: Operating Systems Spring 2021

Device Driver

• Transfers data to and from a user process•When a calling program invokes a routine in the driver, the

driver issues commands to the device. •Once the device sends data back to the driver, the driver may

invoke routines in the original calling program.• A program cannot access the driver in the kernel directly.

6

Programs Device Driver

Kernel SpaceUser Space

Physical Device

CSCI 3753 Spring 2021

Devi

ce F

ile

Page 6: CSCI 3753: Operating Systems Spring 2021

PA2 – Character Device File

7CSCI 3753 Spring 2021

Programs

Kernel Space

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>Open, Read, Write, Close, Seek

Page 7: CSCI 3753: Operating Systems Spring 2021

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 (= 0, 1, or 2)•Output (recommended)• If error, return -1• If successful, return 0 or positive value

8CSCI 3753 Spring 2021

pa2_char_driver.c

Page 8: CSCI 3753: Operating Systems Spring 2021

PA2 – Requirements

1. Dynamically allocate constant-size 1KB 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

9CSCI 3753 Spring 2021

Page 9: CSCI 3753: Operating Systems Spring 2021

PA2 – Requirements

2. NO over seeking/reading/writing in buffer

è Write(“hello”)?

10CSCI 3753 Spring 2021

1KB kernel buffer

Current pointer position

Page 10: CSCI 3753: Operating Systems Spring 2021

PA2 – Requirements

2. NO over seeking/reading/writing in buffer

è Write(“hello”)?

11CSCI 3753 Spring 2021

h e l…

1KB kernel buffer

Current pointer position

Page 11: CSCI 3753: Operating Systems Spring 2021

PA2 – Requirements

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

è Write(“hello”)

12CSCI 3753 Spring 2021

1KB kernel buffer

Current pointer position

Page 12: CSCI 3753: Operating Systems Spring 2021

PA2 – Requirements

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

è Write(“hello”)è Write(“world”)

13CSCI 3753 Spring 2021

h e l l …

1KB kernel buffer

Current pointer position

o

Page 13: CSCI 3753: Operating Systems Spring 2021

PA2 – Requirements

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

è Write(“hello”)è Write(“world”)

14CSCI 3753 Spring 2021

h e l l …

1KB kernel buffer

Current pointer position

o w o r l d

Page 14: CSCI 3753: Operating Systems Spring 2021

fork() and exec() System Calls

15CSCI 3753 Spring 2021

Page 15: CSCI 3753: Operating Systems Spring 2021

fork() vs exec()

19CSCI 3753 Spring 2021

fork() exec()Usage

Create a new process by duplicating the calling process

Replace the current process with the new process

Properties of The New Process

The new (child) process + Has its own unique process id (pid)+ Does not inherit the parent’s memory lock & semaphores

The new process + Take over the replaced process’s pid+ Inherit the replaced process’s memory lock & semaphores

Start Running Point

Run the new (child) process from the calling point

Run the new process from the entry point

Page 16: CSCI 3753: Operating Systems Spring 2021

fork() System Call

•Both parent and child processes are executed simultaneously.

•Differentiate from the parent process by looking at the returned value• Returns the process id of the child process in the parent• Returns 0 in the child

è Demo

20CSCI 3753 Spring 2021

Page 17: CSCI 3753: Operating Systems Spring 2021

exec() System Call

•Control never returns to the original program unless there is an exec() error.

•There is a family of exec() functions, including

è Demo

21CSCI 3753 Spring 2021