csci 3753: operating systems spring 2021
TRANSCRIPT
CSCI 3753: Operating Systems
Spring 2021Anh Nguyen
Department of Computer Science University of Colorado Boulder
Week 4: Programming Assignment 2fork() vs exec() System Calls
2CSCI 3753 Spring 2021
Programming Assignment 2
3CSCI 3753 Spring 2021
An Overview
4CSCI 3753 Spring 2021
Kernel SpaceUser Space
Physical Device
Programs Device Driver
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
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
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
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
PA2 – Requirements
2. NO over seeking/reading/writing in buffer
è Write(“hello”)?
10CSCI 3753 Spring 2021
…
1KB kernel buffer
Current pointer position
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
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
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
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
fork() and exec() System Calls
15CSCI 3753 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
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
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