Download presentation
Presentation is loading. Please wait.
1
Project 3 Threads and Synchronization
Due Thursday, September 25, 6:00 PM Assumptions: Graduate level Operating Systems Making Choices about operation systems Why a micro-century? …just about enough time for one concept CS-3013 A-term 2008 Project 3, Threads and Synchronization
2
Project 3, Threads and Synchronization
Purpose To introduce you to working with threads and synchronization operations To prepare for Project 4 CS-3013 A-term 2008 Project 3, Threads and Synchronization
3
Project 3, Threads and Synchronization
Approach Implement a simple, shared data structure I.e., a doubly-linked list, like the one in previous lecture notes Shared access and updating by multiple threads Implement a multi-threaded test program To exercise the shared data structure, accessing, and updating CS-3013 A-term 2008 Project 3, Threads and Synchronization
4
Project 3, Threads and Synchronization
Shared Data Structure struct Item { struct Item *next, *prev; pthread_t creating_thread; struct timespec time_added: }; extern struct Item *head, *tail; #define MAX_ITEMS 128 See project3.h CS-3013 A-term 2008 Project 3, Threads and Synchronization
5
Operations on Shared Data Structure
int AddItem(int block); Int RemoveItem(pthread_t *threadID, timespec *time_added, int block); #define BLOCK 1 #define NO_BLOCK 0 CS-3013 A-term 2008 Project 3, Threads and Synchronization
6
Project 3, Threads and Synchronization
Operational Details AddItem(int block) If list is not full Creates new Item using malloc() Adds ID of current thread and also current time Links onto end of list pointed to by *head, *tail Returns zero If list is full and block = BLOCK Blocks and waits until someone removes an item from list, and then proceeds as above. If list is full and block = NO_BLOCK Returns immediately with error code. No item added to list; any malloc() is undone CS-3013 A-term 2008 Project 3, Threads and Synchronization
7
Operational Details (continued)
Int RemoveItem(pthread_t *threadID, timespec *time_added, int block); If list is not empty Removes Item from list, extracts threadID and time_added Calls free() to release storage for Item Returns zero If list is empty and block = BLOCK Blocks and waits until someone adds an item to list, and then proceeds as above. If list is full and block = NO_BLOCK Returns immediately with error code. No item removed from list; no free() executed CS-3013 A-term 2008 Project 3, Threads and Synchronization
8
Project 3, Threads and Synchronization
Implementation AddItem, RemoveItem, head, and tail implemented in Project3.c Implementation must be thread-safe Need to provide synchronization among competing threads Monitor style Producer-Consumer style Any other style you can document and explain! CS-3013 A-term 2008 Project 3, Threads and Synchronization
9
Project 3, Threads and Synchronization
Test Program Multi-threaded Master (i.e. main()) creates worker threads Some execute AddItem() Some execute RemoveItem() # of worker threads and duration of test specified in command line After duration seconds have elapsed, master orders worker threads to stop CS-3013 A-term 2008 Project 3, Threads and Synchronization
10
Test Program (continued)
Each worker thread Checks for “stop” condition If none, waits random # of milliseconds Executes AddItem or RemoveItem (according to type of thread) block = BLOCK Loops If “stop” condition “Adding” threads exit immediately with # of items added in status “Removing” threads continue to loop with block = NO_BLOCK to clear out list, then exit with # of items removed in status CS-3013 A-term 2008 Project 3, Threads and Synchronization
11
Test Program (continued)
After ordering “stop”, master waits for each thread to exit via pthread_join() Collects total numbers of items added or deleted by each thread Prints summary message Exits CS-3013 A-term 2008 Project 3, Threads and Synchronization
12
Project 3, Threads and Synchronization
Issues Master must provide for threads that are blocked after stop is ordered E.g., add new items so threads can unblock Memory leaks Be alert for possibility that some items were malloc’d but not free’d Check your code; use printf() for debugging Deadlocks Incorrect synchronization typically causes test program threads to deadlock CS-3013 A-term 2008 Project 3, Threads and Synchronization
13
Project 3, Threads and Synchronization
Write-up Bigger, more comprehensive than most project write-ups Explain synchronization Specify program invariants sufficient to explain behavior of program Show how AddItem and RemoveItem preserve invariants Prove (or cogently explain) why items are removed in order added CS-3013 A-term 2008 Project 3, Threads and Synchronization
14
Project 3, Threads and Synchronization
Grading 1 pt:– make without warnings or errors 5 pts: correctly execute your test cases without memory leaks n adding threads, m removing threads, duration d seconds 5 pts: correctly execute grader’s test cases without memory leaks 4 pts: code review by grader 5 pts: write-up CS-3013 A-term 2008 Project 3, Threads and Synchronization
15
Project 3, Threads and Synchronization
Questions? CS-3013 A-term 2008 Project 3, Threads and Synchronization
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.