Project 3. “System Call and Synchronization” By Dongjin Kim 2015. 10. 29.

Slides:



Advertisements
Similar presentations
Jump to Contents Instructor Tutorial essignments.com Paperless assignment submission system.
Advertisements

Operating Systems Semaphores II
Operating Systems: Monitors 1 Monitors (C.A.R. Hoare) higher level construct than semaphores a package of grouped procedures, variables and data i.e. object.
More on Processes Chapter 3. Process image _the physical representation of a process in the OS _an address space consisting of code, data and stack segments.
1 Chapter 5 Concurrency: Mutual Exclusion and Synchronization Principals of Concurrency Mutual Exclusion: Hardware Support Semaphores Readers/Writers Problem.
Ch 7 B.
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.
CSCC69: Operating Systems
Chapter 6: Process Synchronization
I/O Multiplexing The role of the ‘poll()’ method in Linux device-driver operations.
SPL/2010 Liveness And Performance 1. SPL/2010 Performance ● Throughput - How much work can your program complete in a given time unit? ● Example: HTTP.
Monitors Chapter 7. The semaphore is a low-level primitive because it is unstructured. If we were to build a large system using semaphores alone, the.
28.2 Functionality Application Software Provides Applications supply the high-level services that user access, and determine how users perceive the capabilities.
UC Santa Barbara Project 2 Discussion Bryce Boe 2011/04/26 and 2011/04/29.
Concurrency, Race Conditions, Mutual Exclusion, Semaphores, Monitors, Deadlocks Chapters 2 and 6 Tanenbaum’s Modern OS.
Critical Sections and Semaphores A critical section is code that contains access to shared resources that can accessed by multiple processes. Critical.
Avishai Wool lecture Introduction to Systems Programming Lecture 4 Inter-Process / Inter-Thread Communication.
Concurrent Processes Lecture 5. Introduction Modern operating systems can handle more than one process at a time System scheduler manages processes and.
Concurrency: Deadlock and Starvation Chapter 6. Revision Describe three necessary conditions for deadlock Which condition is the result of the three necessary.
Inter Process Communication:  It is an essential aspect of process management. By allowing processes to communicate with each other: 1.We can synchronize.
Concurrency: Mutual Exclusion, Synchronization, Deadlock, and Starvation in Representative Operating Systems.
Monitors CSCI 444/544 Operating Systems Fall 2008.
1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?
Threads CS 416: Operating Systems Design, Spring 2001 Department of Computer Science Rutgers University
Fundamentals of Python: From First Programs Through Data Structures
Operating System Program 5 I/O System DMA Device Driver.
Nachos Phase 1 Code -Hints and Comments
Moodle (Course Management Systems). Assignments 1 Assignments are a refreshingly simple method for collecting student work. They are a simple and flexible.
Copyright © 2007, Oracle. All rights reserved. Managing Concurrent Requests.
PHP meets MySQL.
9 Chapter Nine Compiled Web Server Programs. 9 Chapter Objectives Learn about Common Gateway Interface (CGI) Create CGI programs that generate dynamic.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Thread Synchronization with Semaphores
Programming With C.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Classical problems.
Java Threads. What is a Thread? A thread can be loosely defined as a separate stream of execution that takes place simultaneously with and independently.
Homework Assignment #1 J. H. Wang Oct. 2, 2015.
Thread Synchronization Tutorial #8 CPSC 261. A thread is a virtual processor Each thread is provided the illusion that it owns a core – Copy of the registers.
Nachos Project 4 Lecturer: Hao-Hua Chu TA: Chun-Po Wang (Artoo) Date: 2008/10/25.
1 Chapter 2.3 : Interprocess Communication Process concept  Process concept  Process scheduling  Process scheduling  Interprocess communication Interprocess.
Homework Assignment #1 J. H. Wang Oct. 13, Homework #1 Chap.1: 1.24 Chap.2: 2.13 Chap.3: 3.5, 3.13* (or 3.14*) Chap.4: 4.6, 4.12* –(*: optional.
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Homework Assignment #1 J. H. Wang Oct. 6, 2011.
Consider the program fragment below left. Assume that the program containing this fragment executes t1() and t2() on separate threads running on separate.
CS140 Project 1: Threads Slides by Kiyoshi Shikuma.
1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.
Project 4. “File System Implementation”
Nachos Project 3 Lecturer: Hao-Hua Chu TA: Chun-Po Wang (Artoo) Date: 2008/10/25.
Processes Creation and Threads. Shared Resource Example The console is just a place where text can be printed by the application currently running. Program.
Homework Assignment #1 J. H. Wang Oct. 11, 2013.
Operating Systems CMPSC 473 Signals, Introduction to mutual exclusion September 28, Lecture 9 Instructor: Bhuvan Urgaonkar.
Introduction Contain two or more CPU share common memory and peripherals. Provide greater system throughput. Multiple processor executing simultaneous.
1 Project 5: Leap Years. 222 Leap Years Write a program that reads an integer value from the user representing a year and determines if the year is a.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
Activity diagrams Practice 7. Task 1  The first action in the activity is the adding the item to the order. If the order if formed (“ready”), then it.
Lecture 7 Interrupt ,Trap and System Call
Web Server Architecture Client Main Thread for(j=0;j
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores.
1 Project 12: Cars from File. This is an extension of Project 11, Car Class You may use the posted solution for Project 11 as a starting point for this.
Project 4. “File System Implementation”
CS 6560: Operating Systems Design
Chapter 5: Process Synchronization – Part 3
Chapter 5: Process Synchronization – Part II
Project 3. “System Call and Synchronization”
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Liveness And Performance
Synchronization and Semaphores
Mr. M. D. Jamadar Assistant Professor
Lab #9 Semaphores Operating System Lab.
Presentation transcript:

Project 3. “System Call and Synchronization” By Dongjin Kim

Introduction to Projects  Project 1: Setting up Environment for Project  Project 2: Linux Fundamental  Project 3: System Call and Synchronization  Project 4: File System  Project 5: Device Driver for Embedded H/W  Especially, including “BeagleBoard Development”

Contents of Project 3  Task 1. Solve synchronization problems using Linux semaphore (25%)  Dining philosopher  Traffic control  Task 2. Make own system call (20%)  Follow guideline  Task 3. Make own semaphore using system call (25%)  Make semaphore with referring Linux source code  Add own rule to wake up processes  Report (30%)  Explanation about your work  Answer to questions

Semaphore  Non-negative integer value  “How many resources are available?”  0: no more resource available  Positive integer: more than one resource available  DOWN and UP  DOWN (resource request)  Value>0  decrease number and continue  Value==0  sleep  UP (resource release)  Value++  If something sleep on DOWN  choose one of them and wakes it up Task 1

Semaphore in Linux (1)  int sem_init (sem_t *sem, int pshared, unsigned int value)  sem  Semaphore structure  pshared  == 0: semaphore only for threads in single process  != 0: semaphore for multiple processes  value  Initial value of semaphore  Return value  0 for success, -1 for error Task 1

Semaphore in Linux (2)  int sem_wait (sem_t *sem)  DOWN of semaphore  If resource available, take it and go on  If not, sleep  Return value  0 for success, -1 for error  int sem_post (sem_t *sem)  UP of semaphore  Release resource  Wake up one of sleeping processes  Return value  0 for success, -1 for error Task 1

Semaphore in Linux (3)  Example source code Task 1

Semaphore in Linux (4)  Example source code Task 1

Semaphore in Linux (5)  Compile  Need “-lpthread” option  Ex> # gcc -o sematest sematest.c -lpthread  Execution result Task 1

Solve Synchronization Problems  Solve problems using semaphore  P1. Dining philosopher  P2. Traffic control Task 1

P1. Dining Philosopher  Description  Five philosophers are seated around a circular table  Each philosophers has a plate of spaghetti  Because the spaghetti is too slippery, a philosopher needs two forks to eat it  There is one fork between each pair of plates  The life of a philosopher consists of alternate period of eating and thinking  When a philosopher gets hungry, he tries to acquire his left and right fork, one at a time, in either order  If successful in acquiring two forks, he eats for a while, then puts down the forks and continues to think  Write a program for each philosopher that does what it is supposed to do and never gets stuck  Print a message when a philosopher eats, thinks, and takes left or right fork  Test for various number of philosophers  Ex. 4, 5, 6 philosophers Task 1

P2. Traffic Control (1)  Description  Design and implement a mechanism for controlling traffic through an intersection  If a car comes from the south  Go straight  Approach  SE  NE  leave  Turn left  Approach  SE  NE  NW  leave  Turn right  Approach  SE  leave  All cars must complete their journey  The default number of the cars are 30  But, the program should work with various numbers Task 1 A skeleton program will be given on Oct.29, 2015 through Web site.

P2. Traffic Control (2)  After half of cars leave, “Presidential limousine” comes  Cars already in the intersection try to finish their journey  Waiting cars cannot enter to the intersection until “Presidential limousine” crossing the intersection  “Presidential limousine” is not included in the total number of cars  After “Presidential limousine” leaves, “police car” patrols in the intersection  NE  NW  SW  SE  NE  …  Until all cars complete their journey Task 1

P2. Traffic Control (3)  No more than one car can be in the same portion of the intersection at the same time  Cars going the same way do not pass each other  If two cars both approach from the same direction and head in the same direction, the first car to reach the intersection should be the first to reach the destination  Your solution should maximize traffic flow without allowing traffic from any direction to starve traffic from any other direction  In other words, you should permit as many cars as possible into the intersection  Approach direction and turn direction are determined randomly (using rand() function)  See  Approach direction (east, west, south, north)  Turn direction (straight, left, right)  Each car should print a message as it approaches, enters, and leaves the intersection indicating the car number, approach direction and destination direction  Examples  “Car 5 (from south, go straight): approaches to south”  “Car 7 (from north, turn left): enters into SW” Task 1

System Call  Interface for user applications  To access kernel or hardware devices  We will implement it in the Linux source code  Which you’ve already used in Project 2  Only for 64-bit VM Task 2 USER Application Hardware Device Kernel System Call System Call Interface Access Hardware

How to Add System Call (1)  arch/x86/syscalls/syscall_64.tbl  Add entry Task 2 Remember the number (it should be less then 512, and should not be same as others) Remember the number (it should be less then 512, and should not be same as others)

How to Add System Call (2)  include/linux/syscalls.h  Add entry at the last part of the file Task 2 Use function name as previous step

How to Add System Call (3)  kernel/mysyscall.c  Create file and implement system call handler Use function name as previous step Task 2

How to Add System Call (4)  kernel/Makefile  Add entry to “obj-y” Use name from the created file name Task 2

How to Add System Call (5)  Compile and install  # make -j4 bzImage  # make install  Reboot Task 2

How to Use New System Call (1)  Write test program in the user level Use same number as before Task 2

How to Use New System Call (2)  Compile and run Task 2

Make Your Own System Call  Make a system call  User gives two integer values  System call handler  Prints information including your student ID  Returns results of addition of two integer values given by user  User prints returned value  Example result Task 2

Make Your Own Semaphore  Support 10 semaphore values  Each value is identified by an integer value from 0 to 9  Each semaphore can have two states  Active / inactive  Each semaphore can have two modes  FIFO mode / alternative mode  Implement 4 system calls in “kernel/mysemaphore.c”  int mysema_init (int sema_id, int start_value, int mode)  int mysema_down (int sema_id)  int mysema_up (int sema_id)  int mysema_release (int sema_id) Task 3 A skeleton program will be given on Oct.29, 2015 through Web site.

Semaphore Initialization  int mysema_init (int sema_id, int start_value, int mode)  Initializes a semaphore (i.e. change the state of the semaphore from inactive to active) specified by sema_id  Return error if the corresponding semaphore is already active  sema_id  The index of the semaphore that should be initialized  Integer between 0 and 9  start_value  The initial value for the semaphore  Non-negative integer value  mode  == 0: FIFO mode  != 0: alternative mode  Return value  0 for success, -1 for error Task 3

Semaphore Down  int mysema_down (int sema_id)  Should be invoked for an active semaphore  Calling it on an inactive semaphore leads to an error  If value == 0, go to sleep  Waiting in the queue  If value > 0, reduce the semaphore value by one  Return value  0 for success, -1 for error Task 3

Semaphore Up  int mysema_up (int sema_id)  Should be invoked for an active semaphore  Calling it on an inactive semaphore leads to an error  Increase the semaphore value by one  If there is at least one process sleeping in the queue …  FIFO mode  First process or thread in the queue will wake up  Alternative mode  Make your own rule to wake up a process or thread in the queue  Largest average waiting time, sleeping frequency, …  Any rule is okay if it is reasonable  Return value  0 for success, -1 for error Task 3

Semaphore Release  int mysema_release (int sema_id)  Should be invoked for an active semaphore  Calling it on an inactive semaphore leads to an error  Change the state of corresponding semaphore to inactive  If there is at least one process or thread in the sleeping queue, return error  Return value  0 for success, -1 for error Task 3

Test Your Semaphore  Write a simple program  To check the correct operation of 4 system calls  To test difference of FIFO mode and alternative mode Task 3

Hint for Task 3  You can see semaphore implemented in Linux kernel  include/linux/semaphore.h  kernel/locking/semaphore.c  Analyze them carefully  struct semaphore, down(), down_common(), up(), …  You can copy them, but do not call them directly  Roles of copied part should be explained in the report Task 3

Questions for the Report  Find answers from your brain, web site, book, …  Q1. There are POSIX semaphore and non-POSIX semaphore (ex. System V semaphore). What is the difference? Pros and cons?  Q2. Classify semaphores in task 1 and task 3 into POSIX and non-POSIX semaphore. What is the reason?  Q3. An user-level process may access to kernel or hardware directly. What are pros and cons of using system call?  Q4. To make a new system call, we compiled entire kernel. What happen if we try to do it with adding a module?  Q5. Your own semaphore has two modes, FIFO and alternative. What is pros and cons of two modes?  Specify the references

Submission  Contents  Source codes  Implemented and modified codes  Include comments in the source codes  Report  Key points of your source code  Do not attach entire source codes in the report  Result screenshots  Answers of questions  Submission  Due date: Nov. 13, PM 23:59  Delay penalty  10% per day (AM 00:00)   Be careful! Do not send to other TAs  Title: [EE516 Project 3] student_number  Attached file: student_number.zip  Various compression format is allowed

FYI  If you have some questions, FEEL FREE to ask questions to TA  Try to search web sites  Use web board   You can ask in English or Korean  Send an 