Docsity

CS 326 Operating Systems: Lists, User Memory, and Threads at University of San Francisco -, Study Guides, Projects, Research of Operating Systems

Toggle sidebar
bg1
usf CSCS
UNIVERSITY of SAN FRANCISCO
department of computer science
Lists and User Memory
CS 326 Operating Systems
Greg Benson
1
Today’s Topics
!Review of linked lists in C
!Pintos linked lists
!User level memory
!Quick intro to threads
!First cut, more details later
!Intro to Pthreads and mutex locks
!Needed for Project 0
2
Linked Lists and Structures
!Pointers and linked structures are used extensively inside an
OS kernel
!Basic idea: allocate memory, create “links” between allocated
memory blocks
!Used for lists, trees, hash tables, etc.
!Can be tricky to debug, so we need to have a good
understanding to avoid common mistakes
!See lec03/src:
!Makefile, namelist.h namelist.c, nametest.c
3
Simple Linked Lists
!Basic Idea: link struct
instance to another
struct instance via a
pointer in the struct:
!Single or double links
(also more...)
struct node {
struct node *next;
int x;
int y;
}
struct node *head;
next
x
y
next
x
y
NULL
x
y
4
Linked Lists in Pintos
!Use lib/kernel/list.{h,c}
!Basic idea:
!Embed list_elem struct into existing struct
!List operations work on list_elem struct
!You can determine enclosing struct pointer via
list_entry() macro
!Other points: doubly linked, sentinels, walking,
ordering
!See prj0/src/listtest.c
5
C Struct Offsets
!What is offsetof(struct point, y)?
!What is offsetof(struct point, o)?
#define offsetof(TYPE, MEMBER)\
(size_t) &((TYPE *) 0)->MEMBER)
struct point {
int x;
int y;
int z;
char c;
float o;
};
0: x (4 byte int)
4: y (4 byte int)
8: z (4 byte int)
12: c (1 byte char)
13: 3 byte padding
16: o (4 byte float)
6
pf3

Partial preview of the text

Download CS 326 Operating Systems: Lists, User Memory, and Threads at University of San Francisco - and more Study Guides, Projects, Research Operating Systems in PDF only on Docsity!

usfCSCS

UNIVERSITY department of computer science of SAN FRANCISCO

Lists and User Memory

CS 326 Operating Systems Greg Benson 1

Today’s Topics

! Review of linked lists in C

! Pintos linked lists

! User level memory

! Quick intro to threads

! First cut, more details later

! Intro to Pthreads and mutex locks

! Needed for Project 0

2

Linked Lists and Structures

! Pointers and linked structures are used extensively inside an

OS kernel

! Basic idea: allocate memory, create “links” between allocated

memory blocks

! Used for lists, trees, hash tables, etc.

! Can be tricky to debug, so we need to have a good

understanding to avoid common mistakes

! See lec03/src:

! Makefile, namelist.h namelist.c, nametest.c

3

Simple Linked Lists

! Basic Idea: link struct

instance to another struct instance via a pointer in the struct:

! Single or double links

(also more...) struct node { struct node *next; int x; int y; } struct node *head; next x y next x y

NULL

x y Head 4

Linked Lists in Pintos

! Use lib/kernel/list.{h,c}

! Basic idea:

! Embed list_elem struct into existing struct

! List operations work on list_elem struct

! You can determine enclosing struct pointer via

list_entry() macro

! Other points: doubly linked, sentinels, walking,

ordering

5! See prj0/src/listtest.c

C Struct Offsets

! What is offsetof(struct point, y)?

! What is offsetof(struct point, o)?

#define offsetof(TYPE, MEMBER)
(size_t) &((TYPE *) 0)->MEMBER) struct point { int x; int y; int z; char c; float o; }; 0: x (4 byte int) 4: y (4 byte int) 8: z (4 byte int) 12: c (1 byte char) 13: 3 byte padding 16: o (4 byte float) 6

Project 0 ! Addressed ordered first-fit ! Coalescing free blocks Free block Used block Free list User Objects Continuous Memory Region 7 Process Definition Review ! A program in execution ! An instance of a program running on a computer ! A process has ! User-level state: memory + register values ! Memory: stack, heap, code ! Kernel-level state: stack, process info, memory maps, file descriptors, network connections... 8 Threads ! A thread is “smaller than” a process ! A thread represents an execution sequence: ! registers + stack ! Threads run in the context of a process ! A process provides memory and kernel state ! Alternate process definition: ! n threads + shared resources (heap, global vars, kernel state) 9!^ Note: each thread needs it’s own stack Scheduling ! An OS kernel schedules threads ! Basic idea: ! Run a thread for a fixed period of time or until it blocks (i.e., wait for date from network) ! Pick a different thread and switch to it ! If no threads ready to run, then twiddle (idle) 10 Using Threads ! Basic ideas ! Thread creation ! Create a new execution sequence ! Need: a new stack and a start function ! Thread interaction ! Need to^ synchronize^ access to shared resources (e.g., global and heap memory) 11 Pthreads ! POSIX Threads ! A standard API available on Linux, Mac OS X, and Windows (not common) ! We will look two sets of functions ! pthread_create() and pthread_join() ! pthread_mutex_lock() and pthread_mutex_unlock() 12