Top Banner
CSCI-3753: Operating Systems Fall 2018 Anh Nguyen Department of Computer Science University of Colorado Boulder
17

CSCI-3753: Operating Systems Fall 2018mnslab.org/.../CSCI3753_Materials/F18_3753_Anh_Recitation_Week4… · CSCI-3753: Operating Systems Fall 2018 Anh Nguyen Department of Computer

May 28, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CSCI-3753: Operating Systems Fall 2018mnslab.org/.../CSCI3753_Materials/F18_3753_Anh_Recitation_Week4… · CSCI-3753: Operating Systems Fall 2018 Anh Nguyen Department of Computer

CSCI-3753: Operating SystemsFall 2018

Anh NguyenDepartment of Computer Science University of Colorado Boulder

Page 2: CSCI-3753: Operating Systems Fall 2018mnslab.org/.../CSCI3753_Materials/F18_3753_Anh_Recitation_Week4… · CSCI-3753: Operating Systems Fall 2018 Anh Nguyen Department of Computer

Announcement

•PA2 is up on Moodle !!!

CSCI 3753 Fall 2018 2

Page 3: CSCI-3753: Operating Systems Fall 2018mnslab.org/.../CSCI3753_Materials/F18_3753_Anh_Recitation_Week4… · CSCI-3753: Operating Systems Fall 2018 Anh Nguyen Department of Computer

Programming Assignment Two

CSCI 3753 Fall 2018 3

Page 4: CSCI-3753: Operating Systems Fall 2018mnslab.org/.../CSCI3753_Materials/F18_3753_Anh_Recitation_Week4… · CSCI-3753: Operating Systems Fall 2018 Anh Nguyen Department of Computer

PA2 – LKM

•How to code a LKM?•module_init()•module_exit()

•How to install the module?•Makefile: obj-m:=xyz.o•make –C /lib/modules/$(uname -r)/build M=$PWD modules

•How to run the module?• sudo insmod xyz.ko

•How to remove the module?• sudo rmmod xyz

CSCI 3753 Fall 2018 4

Page 5: CSCI-3753: Operating Systems Fall 2018mnslab.org/.../CSCI3753_Materials/F18_3753_Anh_Recitation_Week4… · CSCI-3753: Operating Systems Fall 2018 Anh Nguyen Department of Computer

PA2 – Character Device Driver

• A device driver is a program that operates or controls a particular type of device that is attached to a computer.• A driver communicates with

the device through the computer bus or communications subsystem to which the hardware connects. •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.

CSCI 3753 Fall 2018 5

Page 6: CSCI-3753: Operating Systems Fall 2018mnslab.org/.../CSCI3753_Materials/F18_3753_Anh_Recitation_Week4… · CSCI-3753: Operating Systems Fall 2018 Anh Nguyen Department of Computer

PA2 – Character Device Driver

•To work with device drivers, we have to work with the corresponding device files.•These files are stored in /dev folder.•To create device file for a device driver

• sudo mknod –m 777 /dev/simple_character_device c 240 0• mknod was originally used to create the character and

block devices that populate /dev/.• Device files • a storage device (block): cd-roms, hard drives, etc.• a device use for other purpose (character): /dev/zero,

/dev/null, or any other device not used to store info • "b" means block, and "c" means character

CSCI 3753 Fall 2018 6

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

Page 7: CSCI-3753: Operating Systems Fall 2018mnslab.org/.../CSCI3753_Materials/F18_3753_Anh_Recitation_Week4… · CSCI-3753: Operating Systems Fall 2018 Anh Nguyen Department of Computer

Week 4: Inter-Process Communications(IPC) in Linux

CSCI 3753 Fall 2018 7

Page 8: CSCI-3753: Operating Systems Fall 2018mnslab.org/.../CSCI3753_Materials/F18_3753_Anh_Recitation_Week4… · CSCI-3753: Operating Systems Fall 2018 Anh Nguyen Department of Computer

Process

• A software program consist of a sequence of code instructions and data stored on disk.• A program is a passive entity

• A process is a program actively executing from main memory within its own address space

CSCI 3753 Fall 2018 8

Code

Data

Program P1

Other…

Page 9: CSCI-3753: Operating Systems Fall 2018mnslab.org/.../CSCI3753_Materials/F18_3753_Anh_Recitation_Week4… · CSCI-3753: Operating Systems Fall 2018 Anh Nguyen Department of Computer

Process State

•New: The process is being created. •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). •Ready: The process is waiting to be assigned to a

processor.•Terminated: The process has finished execution.

CSCI 3753 Fall 2018 9

Page 10: CSCI-3753: Operating Systems Fall 2018mnslab.org/.../CSCI3753_Materials/F18_3753_Anh_Recitation_Week4… · CSCI-3753: Operating Systems Fall 2018 Anh Nguyen Department of Computer

Process Cooperation

•Reasons for providing an environment that allows process cooperation: • Information sharing: providing an environment to allow

concurrent access to such information • Computation speedup: break a particular task into

subtasks executing in parallel with the others to run faster•Modularity: dividing the system functions into separate

processes or threads • Convenience: working on many tasks at the same time

•Cooperating processes needs IPC mechanism !!!

CSCI 3753 Fall 2018 11

Page 11: CSCI-3753: Operating Systems Fall 2018mnslab.org/.../CSCI3753_Materials/F18_3753_Anh_Recitation_Week4… · CSCI-3753: Operating Systems Fall 2018 Anh Nguyen Department of Computer

IPC in Linux

•There are two fundamental models of IPC: •Message passing• Communication takes place by means of messages

exchanged between the cooperating processes.• Advantages:• Useful for exchanging small amounts of data,

because no conflicts need be avoided • Easy to implement in a distributed system

• Disadvantages:• Time-consuming as typically implemented using

system calls

CSCI 3753 Fall 2018 12

Page 12: CSCI-3753: Operating Systems Fall 2018mnslab.org/.../CSCI3753_Materials/F18_3753_Anh_Recitation_Week4… · CSCI-3753: Operating Systems Fall 2018 Anh Nguyen Department of Computer

IPC in Linux

•There are two fundamental models of IPC: •Shared memory• A region of memory is shared by cooperating

processes is established.• Processes can then exchange information by reading

and writing data to the shared region.• Advantages:• Fast as system calls are required only to establish

shared-memory• Useful for exchanging large amounts of data

• Disadvantages:• Complicated to implement in a distributed system

CSCI 3753 Fall 2018 13

Page 13: CSCI-3753: Operating Systems Fall 2018mnslab.org/.../CSCI3753_Materials/F18_3753_Anh_Recitation_Week4… · CSCI-3753: Operating Systems Fall 2018 Anh Nguyen Department of Computer

Shared Memory

•One process creates a shared memory segment that other processes (if permitted) can access.

• If succeeds, it returns the shared memory segment ID. • It is also used to get the ID of an existing shared

segment (from a process requesting sharing of some existing memory portion).

CSCI 3753 Fall 2018 14

#include <sys/ipc.h>#include <sys/shm.h>int shmget(key_t key, size_t size, int shmflg);

Page 14: CSCI-3753: Operating Systems Fall 2018mnslab.org/.../CSCI3753_Materials/F18_3753_Anh_Recitation_Week4… · CSCI-3753: Operating Systems Fall 2018 Anh Nguyen Department of Computer

Shared Memory

•Once created, a shared segment can be attached to a process address space.

• If succeeds, it returns a pointer, shmaddr, to the head of the shared segment associated with a valid shmid. •Once attached, a process can read or write to the

segment, as allowed by the permission requested in the attach operation.

CSCI 3753 Fall 2018 15

#include <sys/types.h> #include <sys/shm.h>void *shmat(int shmid, const void *shmaddr, int shmflg);

Page 15: CSCI-3753: Operating Systems Fall 2018mnslab.org/.../CSCI3753_Materials/F18_3753_Anh_Recitation_Week4… · CSCI-3753: Operating Systems Fall 2018 Anh Nguyen Department of Computer

Shared Memory

•To detach the shared memory segment located at the address indicated by shmaddr

CSCI 3753 Fall 2018 16

int shmdt(const void *shmaddr);

Page 16: CSCI-3753: Operating Systems Fall 2018mnslab.org/.../CSCI3753_Materials/F18_3753_Anh_Recitation_Week4… · CSCI-3753: Operating Systems Fall 2018 Anh Nguyen Department of Computer

Shared Memory Example

•Goal: to illustrate the usage of shared memory between two processes in Linux. •The server process creates a shared memory

segment and writes some characters in it. It then waits for the first character in the shared memory to become '*', and then exits. •The client process reads characters from this shared

memory and prints them out on the terminal, writes '*' at the beginning of the shared memory segment, and then exits.

CSCI 3753 Fall 2018 17

Page 17: CSCI-3753: Operating Systems Fall 2018mnslab.org/.../CSCI3753_Materials/F18_3753_Anh_Recitation_Week4… · CSCI-3753: Operating Systems Fall 2018 Anh Nguyen Department of Computer

Week 4 – Checklist

q Discuss PA2q Discuss IPC – Shared memory & exampleq Read more about IPC

CSCI 3753 Fall 2018 20