CS F-MID Midterm October 25, 2001

Similar documents
20-EECE-4029 Operating Systems Spring, 2015 John Franco

PRINCIPLES OF OPERATING SYSTEMS

Global variables and resources are shared between threads within the same process. Each thread has its own stack and program counter (see Fig 2-7).

Overview. Lab advice. Lab advice. Readers-writers problem. Readers-writers solution. Real-time Systems D0003E 3/10/2009

Concurrent Programming. Concurrent Programming with Processes and Threads

Condition Variables. Dongkun Shin, SKKU

Introduc)on to pthreads. Shared memory Parallel Programming

Final Examination (Fall 2016) (Section 1)

Multithreading Programming II

CS 471 Operating Systems. Yue Cheng. George Mason University Fall 2017

Condition Variables. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

But first... Re: generic parking. A cyclic POSIX thread (2) Real-time Systems SMD138. Lecture 14: Real-time languages (Burns & Wellings ch.

CS 220: Introduction to Parallel Computing. Condition Variables. Lecture 24

Condition Variables. Dongkun Shin, SKKU

Shared Memory Programming Models III

There are 8 total numbered pages, 6 Questions. You have 60 minutes. Budget your time carefully!

CS-537: Midterm Exam (Fall 2013) Professor McFlub

20-EECE-4029 Operating Systems Fall, 2015 John Franco

HPCSE - I. «Introduction to multithreading» Panos Hadjidoukas

CS 351 Sample Final Exam, Spring 2015

Recitation 14: Proxy Lab Part 2

CS Operating system

Lecture #24 Synchronization

Process Synchronization(2)

Synchronization and Semaphores. Copyright : University of Illinois CS 241 Staff 1

CS 3214 Midterm. Here is the distribution of midterm scores for both sections (combined). Midterm Scores 20. # Problem Points Score

Carnegie Mellon Synchronization: Advanced

EECE.4810/EECE.5730: Operating Systems Spring Midterm Exam March 8, Name: Section: EECE.4810 (undergraduate) EECE.

Operating Systems. Spring Dongkun Shin, SKKU

POSIX Threads. HUJI Spring 2011

Operating Systems. Fall Dongkun Shin, SKKU

Process Synchronization(2)

Process Synchronization(2)

CS510 Operating System Foundations. Jonathan Walpole

CSE 4/521 Introduction to Operating Systems

Problem 1: Concepts (Please be concise)

Process Synchronization

Synchronization II. Today. ! Condition Variables! Semaphores! Monitors! and some classical problems Next time. ! Deadlocks

Recitation 14: Proxy Lab Part 2

Condition Variables. Figure 29.1: A Parent Waiting For Its Child

Note: The following (with modifications) is adapted from Silberschatz (our course textbook), Project: Producer-Consumer Problem.

Data Races and Deadlocks! (or The Dangers of Threading) CS449 Fall 2017

Condition Variables. parent: begin child parent: end

Computer Systems Assignment 2: Fork and Threads Package

Carnegie Mellon. Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition

FAT (32 KB) 216 * 215 = 231 = 2GB

CS533 Concepts of Operating Systems. Jonathan Walpole

[537] Semaphores. Tyler Harter

CS/CoE 1541 Final exam (Fall 2017). This is the cumulative final exam given in the Fall of Question 1 (12 points): was on Chapter 4

CS-537: Midterm Exam (Fall 2013) Professor McFlub: The Solutions Edition

CROWDMARK. Examination Midterm. Spring 2017 CS 350. Closed Book. Page 1 of 30. University of Waterloo CS350 Midterm Examination.

CS333 Intro to Operating Systems. Jonathan Walpole

CS 370 Operating Systems

CS 326 Operating Systems Synchronization. Greg Benson Department of Computer Science University of San Francisco

Synchronization and Semaphores. Copyright : University of Illinois CS 241 Staff 1

Condition Variables CS 241. Prof. Brighten Godfrey. March 16, University of Illinois

COSC 6374 Parallel Computation. Shared memory programming with POSIX Threads. Edgar Gabriel. Fall References

Carnegie Mellon. Synchroniza+on : Introduc+on to Computer Systems Recita+on 14: November 25, Pra+k Shah (pcshah) Sec+on C

Thread Coordination -Managing Concurrency

Midterm - Winter SE 350

CS 33. Architecture and the OS. CS33 Intro to Computer Systems XIX 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

CPS 310 first midterm exam, 10/6/2014

Introduction to PThreads and Basic Synchronization

Final review. From threads to file systems

Final Exam. Fall Semester 2016 KAIST EE209 Programming Structures for Electrical Engineering. Name: Student ID:

Midterm - Winter SE 350

Overview of Time Related Data Structures and Functions in Unix/C. It s About Time

Summary Semaphores. Passing the Baton any await statement. Synchronisation code not linked to the data

CS 3305 Intro to Threads. Lecture 6

Synchronization II. q Condition Variables q Semaphores and monitors q Some classical problems q Next time: Deadlocks

Programming with Shared Memory PART I. HPC Fall 2010 Prof. Robert van Engelen

Locks and semaphores. Johan Montelius KTH

CS 33. Architecture and the OS. CS33 Intro to Computer Systems XIX 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Reading compiler errors

First Exam name: John Franco

Assignment #2. Problem 2.1: airplane synchronization

Third Midterm Exam April 24, 2017 CS162 Operating Systems

CS 6400 Lecture 11 Name:

CS-345 Operating Systems. Tutorial 2: Grocer-Client Threads, Shared Memory, Synchronization

Semaphores INF4140. Lecture 3. 0 Book: Andrews - ch.04 ( ) INF4140 ( ) Semaphores Lecture 3 1 / 34

Processes & Threads. Today. Next Time. ! Process concept! Process model! Implementing processes! Multiprocessing once again. ! More of the same J

Computer Systems II. First Two Major Computer System Evolution Steps

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Pre-lab #2 tutorial. ECE 254 Operating Systems and Systems Programming. May 24, 2012

CSE 380: Homework 2: Synchronization

TDDB68. Lesson 1. Simon Ståhlberg

CS Operating system Fall Midterm I -- Oct 5, 2017 You have 75 min. Good Luck!

There are 10 total numbered pages, 5 Questions. You have 60 minutes. Budget your time carefully!

Sections 01 (11:30), 02 (16:00), 03 (8:30) Ashraf Aboulnaga & Borzoo Bonakdarpour

Operating Systems Comprehensive Exam. Spring Student ID # 3/16/2006

Threads. What is a thread? Motivation. Single and Multithreaded Processes. Benefits

Operating systems fundamentals - B06

Introduction to Computer Systems. Final Exam. May 3, Notes and calculators are permitted, but not computers. Caching. Signals.

Semaphores. Derived Inference Rules. (R g>0) Q g (g 1) {R} V (g) {Q} R Q g (g+1) V (s), hs := 1; i

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

More Shared Memory Programming

Semaphores. To avoid busy waiting: when a process has to wait, it will be put in a blocked queue of processes waiting for the same event

Semaphores. Semaphores. Semaphore s operations. Semaphores: observations

Chapter 2 Processes and Threads

POSIX threads CS 241. February 17, Copyright University of Illinois CS 241 Staff

Transcription:

Name (Last, First): This exam consists of 5 questions on 9 pages; be sure you have the entire exam before starting. The point value of each question is indicated at its beginning; the entire exam has 100 points. Individual parts of a multi-part question are generally assigned approximately the same point value: exceptions are noted. This exam is open text and notes. However, you may NOT share material with another student during the exam. Be concise and clearly indicate your answer. Presentation and simplicity of your answers may affect your grade. Answer each question in the space following the question. If you find it necessary to continue an answer elsewhere, clearly indicate the location of its continuation and label its continuation with the question number and subpart if appropriate. You should read through all the questions first, then pace yourself. The questions begin on the next page. Problem Possible Score 1 20 2 20 3 20 4 20 5 20 Total 100 1

1. ( /20 points ) (a) Why are the clock and hardware interrupts important to modern operation systems? What important operating system feature could not be implemented without them? Explain your answer. (b) Recall that a context switch occurs when the operating system switches from one process to another. Consider the following user-level code fragment: x = atoi(argv[1]); p = getpid(); In MINIX, what is the minimum number of context switches required to execute this code? Explain your answer. Think carefully!. 2

2. ( /20 points ) We want to measure how long it takes to send 1 KB (1024 bytes) of data from one process to another process using a pipe. Your job is to add code to the following skeleton program to measure as accurately as possible the amount of time it takes to send 1 KB of data from one process to another. That is, minimize the measurement of any processing activity not involved in sending and receiving data over a pipe (e.g., you should not include the time involved in creating a new process). However, you should measure the time for the write(), the read() and the context switch from the writing process to the reading process. Recall the times() system call: clock_t times(struct tms *buf); struct tms { clock_t tms_utime; /* user time */ clock_t tms_stime; /* system time */ clock_t tms_cutime; /* user time of children */ clock_t tms_cstime; /* system time of children */ ; Explain how your solution works. #include<stdio.h> void main(void) { int pfd[2]; char buf[1024]; int totaltime = 0; pipe(pfd); if (fork() == 0) { write(pfd[1], buf, 1024); read(pfd[0], buf, 1024); wait(null); print("time to send 1KB is %d\n", totaltime); 3

3. ( /20 points ) Consider the following semaphore solution to readers-writers. semaphore s = 1; semaphore w = 1; int rcnt = 0; cobegin (i = 1 to R) /* Reader thread i */ while(1) { /* obtain access */ P(s); rcnt++; if (rcnt == 1) P(w); V(s); /* read database */... /* release access */ P(s); rcnt--; if (rcnt == 0) V(w); V(s); // (i = 1 to W) /* Writer thread i */ while(1) { /* obtain access */ P(w); /* write database */... /* release database */ V(w); coend (a) Suppose W1 is writing to the database and R1, R2, and R3 attempt to obtain access for reading in that order. (i) Explain what happens to R1, R2, and R3. Be specific. (ii) When W1 finishes writing, do R1, R2, and R3 get to access the database concurrently? Explain. (b) Is it possible for a reader and a writer to be blocked on w at the same time? Explain. (c) Is starvation possible in this solution? Explain. 4

4. ( /20 points ) In the MINIX kernel, the pick_proc() function is used to choose the next process to run. Currently, MINIX uses three queues for scheduling: a USER_Q, a SERVER_Q, and a TASK_Q. Consider modifying MINIX so that it uses a single queue for all processes rather than three queues. Assume the new, global ready queue is declared as follows: struct proc *rdy_head; struct proc *rdy_tail; (15 points) Rewrite pick_proc() assuming that all processes are on the single ready queue. However, your new pick_proc() should behave just as the original pick_proc(). /*===========================================================================* * pick_proc * *===========================================================================*/ PRIVATE void pick_proc() Problem continued on the next page 5

(5 points) Now, rewrite ready() also assuming that all processes are on the single ready queue. /*===========================================================================* * ready * *===========================================================================*/ PRIVATE void ready(rp) 6

5. ( /20 points ) Recall the monitor solution to the producer and consumer problem. In Lab 3 you were required to implement a Pthreads version of the producer consumer problem. We want to add a new kind of thread, a high priority consumer (Hicon for short). A Hicon thread is like a regular consumer, but its requests for items are to be serviced before requests form regular consumers. That is, if both a Hicon and a regular consumer are waiting for an item, then when a producer deposits a new item, the Hicon, not the regular consumer, should get that item. Modify/complete the following Pthreads program to solve this problem. Do so by crossing out pieces of code, inserting new code, declarations, etc. Be neat. The code continues on the next page. #include<stdio.h> #include<pthread.h> #define P 4 #define C 2 #define H 2 #define Psteps 2 #define Csteps 2 #define Hsteps 2 #define BUFSIZE 100 int buf[bufsize]; int front = 0; int rear = 0; int count = 0; pthread_mutex_t mutex; pthread_cond_t not_full; pthread_cond_t not_empty; void *producer(void *arg) { int i; int item; for (i = 0; i < Psteps; i++) { /* produce item */ item = i; pthread_mutex_lock(&mutex); while (count == BUFSIZE) { pthread_cond_wait(&not_full, &mutex); buf[rear] = item; rear = (rear + 1) % BUFSIZE; count++; pthread_cond_signal(&not_empty); pthread_mutex_unlock(&mutex); void *consumer(void *arg) { int i; int item; for (i = 0; i < Csteps; i++) { pthread_mutex_lock(&mutex); while (count == 0) { pthread_cond_wait(&not_empty, &mutex); item = buf[front]; front = (front + 1) % BUFSIZE; count--; pthread_cond_signal(&not_full); pthread_mutex_unlock(&mutex); /* consume item */ 7

void *hicon(void *arg) { int i; int item; for (i = 0; i < Hsteps; i++) { item = buf[front]; front = (front + 1) % BUFSIZE; count--; /* consume item */ int main(int argc, char *argv[]) { int i; pthread_t producers[p]; pthread_t consumers[c]; pthread_t hicons[h]; pthread_mutex_init(&mutex, NULL); pthread_cond_init(&not_full, NULL); pthread_cond_init(&not_empty, NULL); for (i = 0; i < C; i++) { pthread_create(&consumers[i], NULL, consumer, (void *) i); for (i = 0; i < H; i++) { pthread_create(&hicons[i], NULL, hicon, (void *) i); for (i = 0; i < P; i++) { pthread_create(&producers[i], NULL, producer, (void *) i); for (i = 0; i < P; i++) { pthread_join(producers[i], NULL); for (i = 0; i < C; i++) { pthread_join(consumers[i], NULL); for (i = 0; i < H; i++) { pthread_join(hicons[i], NULL); exit(0); 8

Continue your answers here if necessary. 9