Today’s agenda Lab 1 Module 4: Process Management

Slides:



Advertisements
Similar presentations
19/03/2006 SSST CS260 F. Hadziomerovic 1 Kernel Queues ^ MININT MAXINT ^ 4 2 Head = 32 Tail = 33 NPROC - 1 NPROC
Advertisements

Operating Systems Semaphores II
Inserting a Node into a Specified Position of a Linked List To create a node for the new item newNode = new Node(item); To insert a node between two nodes.
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
Global Environment Model. MUTUAL EXCLUSION PROBLEM The operations used by processes to access to common resources (critical sections) must be mutually.
Ch 7 B.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Module R2 Overview. Process queues As processes enter the system and transition from state to state, they are stored queues. There may be many different.
Module R2 CS450. Next Week R1 is due next Friday ▫Bring manuals in a binder - make sure to have a cover page with group number, module, and date. You.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Queues CS-212 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed in their.
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
1 CSC 211 Data Structures Lecture 22 Dr. Iftikhar Azim Niaz 1.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed.
Midterm 1 – Wednesday, June 4  Chapters 1-3: understand material as it relates to concepts covered  Chapter 4 - Processes: 4.1 Process Concept 4.2 Process.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Module R3 Process Scheduling. Module R3 involves the creation of a simple “Round Robin” dispatcher. The successful completion of this module will require.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Review Array Array Elements Accessing array elements
Data Structures Using C, 2e
Queues Chapter 4.
Chapter 12 – Data Structures
Top 50 Data Structures Interview Questions
Week 4 - Friday CS221.
Lectures linked lists Chapter 6 of textbook
Design & Analysis of Algorithm Priority Queue
Doubly Linked List Review - We are writing this code
Lectures Queues Chapter 8 of textbook 1. Concepts of queue
Queue data structure.
Queues Queues Queues.
Week 15 – Monday CS221.
Stack and Queue APURBO DATTA.
Queues Chapter 4.
Queues Chapter 8 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
Chapter 5: Process Synchronization
Prof. Neary Adapted from slides by Dr. Katherine Gibson
CMSC 341 Lecture 5 Stacks, Queues
תרגול 4 – ניהול תהליכים, מבני נתונים למימוש תהליכים
Queues.
Arrays and Linked Lists
Chapter 18: Linked Lists.
Realtime System Fundamentals : Scheduling and Priority-based scheduling B. Ramamurthy 11/22/2018.
Xinu Semaphores.
Lesson Objectives Aims
Memory Management Chapter 10 11/24/2018 Crowley OS Chap. 10.
Topic 6 (Textbook - Chapter 5) Process Synchronization
Semaphore Originally called P() and V() wait (S) { while S <= 0
Queues: Implemented using Arrays
Programming Abstractions
Today’s agenda Hardware architecture and runtime system
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
Linked Lists Chapter 4.
Today’s agenda Lab3 assigned (Due: Oct 28) Midterm Review
Xinu Semaphores.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Today’s agenda Announcements:
Data Structures & Algorithms
Chapter 7: Synchronization Examples
CSE 153 Design of Operating Systems Winter 2019
Using a Queue Chapter 8 introduces the queue data type.
Chapter 6: Synchronization Tools
Using a Queue Chapter 8 introduces the queue data type.
Presentation transcript:

Today’s agenda Lab 1 Module 4: Process Management Read Chapter 2 (2.8, 2.9) Semaphores: wait(), signal(),semcreate(),semdelete(), … Module 4: Process Management Chap 4: List and Queue Manipulation CS354-Fall2018

Lab 1: Producer-Consumer Read Chapter 2.8, 2.9 ex4.c, ex5.c and ex6.c two concurrent processes that each increments a shared integer n Race condition (how to handle it?) CS354-Fall2018

What if producer-consumer? write integer n read consumer CS354-Fall2018

How to synchronize independent processes? Semaphore abstraction wait() signal() semcreate() semdelete() Two semaphores: One on which the consumer waits One on which the producer waits CS354-Fall2018

Ex5.c /* ex5.c - main, prod2, cons2 */ # include <xinu.h> void prod2(sid32,sid32), cons2(sid32,sid32);   int32 n = 0; /* variable n has initial value zero */ /* --------------- * main - producer and consumer processes sychronizaed with semaphores * --------------- */ void main(void) { sid32 produced, consumed; consumed = semcreate(0); produced = semcreate(1); resume (create (cons2, 1024, 20, "cons", 2, consumed, produced)); resume (create (prod2, 1024, 20, "prod", 2, consumed, produced)); } CS354-Fall2018

* prod2 - Increment n 2000 times, waiting for it to be consumed. /* --------------- * prod2 - Increment n 2000 times, waiting for it to be consumed. * --------------- */   void prod2( sid32 consumed, sid32 produced) { int32 i; for (i=1; i<=2000; i++){ wait(consumed); n++; signal(produced); } /* --------------- * cons2 - print n 2000 times, waiting for it to be produced * --------------- */ void cons2( sid32 consumed, sid32 produced) { int32 i; for (i=1; i<=2000; i++){ wait(produced); printf("n is %d \n", n); signal(consumed); } CS354-Fall2018

Semaphores: Mutual Exclusion Only one of two executing processes obtains access to a shared resource what if not n? but a shared list? Accessed by many processes shared[n++] = item; /* n is updated over time */ CS354-Fall2018

# include <xinu.h> /* ex6.c - additem */   # include <xinu.h> sid32 mutex; /* assume initialized with semcreate */ int32 shared[100]; /* An array shared by many processes */ int32 n = 0; /* Count of items in the array */ int32 n = 0; /* variable n has initial value zero */ /* --------------- * addiotem - obtain exclusive use of array shared and add an item to it. * --------------- */ void additem (int32 item) { wait(mutex); shared[n++] = item; signal(mutex); } CS354-Fall2018

Lab1 https://www.cs.purdue.edu/homes/c s354/lab1/ Mutual exclusion Circled buffer Producer: insert into the buffer Consumer: extract from the buffer Multiple producers and consumers CS354-Fall2018

Switch to Module 4 CS354-Fall2018

Chapter 4: List and Queue Manipulation A linked list A single data structure A single set of nodes used by all levels of OS to maintain list of processes CS354-Fall2018

Doubly-linked List previous process key next Head Tail x - 4 2 - x 25 14 … greater than max key Smaller than max key CS354-Fall2018

In Xinu a fixed upper bound on # of processes: NPROC Relative pointer: each pointer occupies 4 bytes (32-bit system), address will be list[n] (continuous memory address) Implicit data structure: queue table queue.h, getitem.c, queue.c (see the codes) CS354-Fall2018

/* queue.h - firstid, firstkey, isempty, lastkey, nonempty */ /* Queue structure declarations, constants, and inline functions */ /* Default # of queue entries: 1 per process plus 2 for ready list plus */ /* 2 for sleep list plus 2 per semaphore */ #ifndef NQENT #define NQENT (NPROC + 4 + NSEM + NSEM) #endif #define EMPTY (-1) /* Null value for qnext or qprev index */ #define MAXKEY 0x7FFFFFFF /* Max key that can be stored in queue */ #define MINKEY 0x80000000 /* Min key that can be stored in queue */ struct qentry { /* One per process plus two per list */ int32 qkey; /* Key on which the queue is ordered */ qid16 qnext; /* Index of next process or tail */ qid16 qprev; /* Index of previous process or head */ }; extern struct qentry queuetab[]; /* Inline queue manipulation functions */ #define queuehead(q) (q) #define queuetail(q) ((q) + 1) #define firstid(q) (queuetab[queuehead(q)].qnext) #define lastid(q) (queuetab[queuetail(q)].qprev) #define isempty(q) (firstid(q) >= NPROC) #define nonempty(q) (firstid(q) < NPROC) #define firstkey(q) (queuetab[firstid(q)].qkey) #define lastkey(q) (queuetab[ lastid(q)].qkey) /* Inline to check queue id assumes interrupts are disabled */ #define isbadqid(x) (((int32)(x) < 0) || (int32)(x) >= NQENT-1) CS354-Fall2018

/* getitem.c - getfirst, getlast, getitem */ #include <xinu.h>   #include <xinu.h> /*------------------------------------------------------------------------ * getfirst - Remove a process from the front of a queue *------------------------------------------------------------------------ */ pid32 getfirst( qid16 q /* ID of queue from which to */ ) /* Remove a process (assumed */ /* valid with no check) */ { pid32 head; if (isempty(q)) { return EMPTY; } head = queuehead(q); return getitem(queuetab[head].qnext);   /*------------------------------------------------------------------------ * getlast - Remove a process from end of queue *------------------------------------------------------------------------ */ pid32 getlast( qid16 q /* ID of queue from which to */ ) /* Remove a process (assumed */ /* valid with no check) */ { pid32 tail; if (isempty(q)) { return EMPTY; } tail = queuetail(q); return getitem(queuetab[tail].qprev);  /*------------------------------------------------------------------------ * getitem - Remove a process from an arbitrary point in a queue *------------------------------------------------------------------------ */ pid32 getitem( pid32 pid /* ID of process to remove */ ) { pid32 prev, next;   next = queuetab[pid].qnext; /* Following node in list */ prev = queuetab[pid].qprev; /* Previous node in list */ queuetab[prev].qnext = next; queuetab[next].qprev = prev; return pid; } CS354-Fall2018

FIFO Queue Manipulation First-In-First-Out (FIFO): queue.c enqueue() and dequeue() CS354-Fall2018

#include <xinu.h> struct qentry queuetab[NQENT]; /* Table of process queues */ /*------------------------------------------------------------------------ * enqueue - Insert a process at the tail of a queue *------------------------------------------------------------------------ */ pid32 enqueue( pid32 pid, /* ID of process to insert */ qid16 q /* ID of queue to use */ ) { int tail, prev; /* Tail & previous node indexes */   if (isbadqid(q) || isbadpid(pid)) { return SYSERR; } tail = queuetail(q); prev = queuetab[tail].qprev; queuetab[pid].qnext = tail; /* Insert just before tail node */ queuetab[pid].qprev = prev; queuetab[prev].qnext = pid; queuetab[tail].qprev = pid; return pid; /*------------------------------------------------------------------------ * dequeue - Remove and return the first process on a list *------------------------------------------------------------------------ */ pid32 dequeue( qid16 q /* ID queue to use */ ) { pid32 pid; /* ID of process removed */   if (isbadqid(q)) { return SYSERR; } else if (isempty(q)) { return EMPTY; } pid = getfirst(q); queuetab[pid].qprev = EMPTY; queuetab[pid].qnext = EMPTY; return pid; CS354-Fall2018

How about Priority Queue? FIFO is not the sole way Process manager may select from a set of processes the one with highest priority Priority: as a qkey in the list Queue sorted by qkey Delete(): easy (just delete it) Insert(): searches for the one that is just below qkey See insert.c CS354-Fall2018

insert.c /*------------------------------------------------------------------------ * insert - Insert a process into a queue in descending key order *------------------------------------------------------------------------ */ status insert( pid32 pid, /* ID of process to insert */ qid16 q, /* ID of queue to use */ int32 key /* Key for the inserted process */ ) { int16 curr; /* Runs through items in a queue*/ int16 prev; /* Holds previous node index */   …. /* ignored */ curr = firstid(q); while (queuetab[curr].qkey >= key) { curr = queuetab[curr].qnext; } /* Insert process between curr node and previous node */ prev = queuetab[curr].qprev; /* Get index of previous node */ queuetab[pid].qnext = curr; queuetab[pid].qprev = prev; queuetab[pid].qkey = key; queuetab[prev].qnext = pid; queuetab[curr].qprev = pid; return OK; /* insert.c - insert */   #include <xinu.h> /*------------------------------------------------------------------------ * insert - Insert a process into a queue in descending key order *------------------------------------------------------------------------ */ status insert( pid32 pid, /* ID of process to insert */ qid16 q, /* ID of queue to use */ int32 key /* Key for the inserted process */ ) { int16 curr; /* Runs through items in a queue*/ int16 prev; /* Holds previous node index */ if (isbadqid(q) || isbadpid(pid)) { return SYSERR; } curr = firstid(q); while (queuetab[curr].qkey >= key) { curr = queuetab[curr].qnext; /* Insert process between curr node and previous node */ prev = queuetab[curr].qprev; /* Get index of previous node */ queuetab[pid].qnext = curr; queuetab[pid].qprev = prev; queuetab[pid].qkey = key; queuetab[prev].qnext = pid; queuetab[curr].qprev = pid; return OK; CS354-Fall2018

List Initialization? newqueue.c /*------------------------------------------------------------------------ * newqueue - Allocate and initialize a queue in the global queue table *------------------------------------------------------------------------ */ qid16 newqueue(void) { static qid16 nextqid=NPROC; /* Next list in queuetab to use */ qid16 q; /* ID of allocated queue */   q = nextqid; if (q > NQENT) { /* Check for table overflow */ return SYSERR; } nextqid += 2; /* Increment index for next call*/ /* Initialize head and tail nodes to form an empty queue */ queuetab[queuehead(q)].qnext = queuetail(q); queuetab[queuehead(q)].qprev = EMPTY; queuetab[queuehead(q)].qkey = MAXKEY; queuetab[queuetail(q)].qnext = EMPTY; queuetab[queuetail(q)].qprev = queuehead(q); queuetab[queuetail(q)].qkey = MINKEY; return q; List Initialization? newqueue.c CS354-Fall2018