Project 3. “System Call and Synchronization”

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

Priority Inversion BAE5030 Advanced Embedded Systems 9/13/04.
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.
Operating Systems Mehdi Naghavi Winter 1385.
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
SPL/2010 Liveness And Performance 1. SPL/2010 Performance ● Throughput - How much work can your program complete in a given time unit? ● Example: HTTP.
CS444/CS544 Operating Systems Synchronization 2/16/2006 Prof. Searleman
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.
Avishai Wool lecture Introduction to Systems Programming Lecture 4 Inter-Process / Inter-Thread Communication.
Concurrency: Deadlock and Starvation Chapter 6. Revision Describe three necessary conditions for deadlock Which condition is the result of the three necessary.
A Introduction to Computing II Lecture 18: Concurrency Issues Fall Session 2000.
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?
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
Operating System Program 5 I/O System DMA Device Driver.
Nachos Phase 1 Code -Hints and Comments
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Classical problems.
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.
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
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.
Processes, Threads, and Process States. Programs and Processes  Program: an executable file (before/after compilation)  Process: an instance of a program.
Project 4. “File System Implementation”
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.
Project 3. “System Call and Synchronization” By Dongjin Kim
Introduction Contain two or more CPU share common memory and peripherals. Provide greater system throughput. Multiple processor executing simultaneous.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
Lecture 7 Interrupt ,Trap and System Call
Web Server Architecture Client Main Thread for(j=0;j
Exceptional Control Flow
Project 4. “File System Implementation”
EE516: Embedded Software Project 1
Interprocess Communication Race Conditions
OPERATING SYSTEM CONCEPT AND PRACTISE
Chapter 2 Memory and process management
Lecture 12: Real-Time Scheduling
CS 6560: Operating Systems Design
Semaphores Reference text: Tanenbaum ch
Process Synchronization: Semaphores
Chapter 5: Process Synchronization – Part 3
Auburn University COMP 3500 Introduction to Operating Systems Synchronization: Part 4 Classical Synchronization Problems.
Chapter 5: Process Synchronization – Part II
The Buffer Cache.
2.4 Classic IPC Problems Dining philosophers Readers and writers
Process Description and Control
Monitors Chapter 7.
Chapter 6: CPU Scheduling
Chapter 5: CPU Scheduling
Liveness And Performance
Synchronization and Semaphores
Process Description and Control
Lecture 2 Part 2 Process Synchronization
Chapter 7: Synchronization Examples
Monitors Chapter 7.
Monitors Chapter 7.
Unemployment Insurance Agency Michigan Web Account Manager
Semaphores Reference text: Tanenbaum ch
“The Little Book on Semaphores” Allen B. Downey
Mr. M. D. Jamadar Assistant Professor
CSE 542: Operating Systems
Lab #9 Semaphores Operating System Lab.
Presentation transcript:

Project 3. “System Call and Synchronization” 2016.11.1. Prepared by Dong Jin Kim PhD. Student

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 (30%) Dining philosopher Training monkey Task 2. Make your own system call (10%) Follow guideline Task 3. Make your own semaphore using system call (30%) Make semaphore with referring Linux source codes Make several rules to wake up processes Report (30%) Explanation about your work Answer to questions

Semaphore Non-negative integer value DOWN and UP Task 1 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

Semaphore in Linux (2) int sem_wait (sem_t *sem) 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

Task 1 Semaphore in Linux (3) Example source code

Task 1 Semaphore in Linux (4) Example source code

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

Solve Synchronization Problems Task 1 Solve Synchronization Problems Solve problems using semaphore P1. Dining philosopher P2. Training Monkey

P1. Dining Philosopher Task 1 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

P2. Training Monkeys Task 1 There are 4 balls (red, green, blue, yellow) and 4 monkeys in the room Each monkey has to take 2 different balls in a given order After a monkey takes one of corresponding balls, it needs to think for a while Since monkeys are well-trained, they will not steal already taken balls If a monkey finishes collecting corresponding balls, it will release the balls and leave the room Monkey in the room: Take the first ball  Think  Take the second ball  Think  Release balls and leave Whenever a monkey leaves the room, another monkey will enter into the room The monkey leaving the room will face 2 feeding bowls If there is no free bowl, the monkey will eat apples and finish its training There is no limitation to eat apples. Many monkeys can eat apples at the same time If there is a free bowl, the monkey will take it and wait a trainer After the trainer puts bananas in the bowl, the monkey will eat the banana and finish its training The trainer puts bananas if there is a waiting monkey at the empty bowl, or goes to sleep Write a program that does what it is supposed to do and never gets stuck You need to train total 30 monkeys, but your solution should work with various numbers of monkeys

P2. Training Monkeys Task 1 No more than one monkey can take a ball with same color at the same time Your solution should maximize the number of monkeys leaving the room per time In other words, you should permit as many monkeys as possible to take the ball Colors and order of balls for each monkey are determined randomly (using rand() function) See http://www.cplusplus.com/reference/cstdlib/rand/ Each monkey should print a message as it enters into the room, takes balls, leaves the room, takes a bowl, and eats apples or bananas, with their ID and given ball information Examples “Monkey 5 (green, blue): takes the green ball” “Monkey 7 (blue, yellow): eats apples” The trainer should print a message as it goes to sleep and puts bananas “Trainer: goes to sleep” “Trainer: puts bananas”

System Call Interface for user applications Task 2 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 USER Application System Call Kernel System Call Interface Access Hardware Hardware Device

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

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

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

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

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

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

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

Make Your Own System Call 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

Make Your Own Semaphore Task 3 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 several modes Implement 5 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_down_userprio (int sema_id, int priority) int mysema_up (int sema_id) int mysema_release (int sema_id)

Semaphore Initialization Task 3 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 1: OS priority 2: User priority Return value 0 for success, -1 for error

Semaphore Down int mysema_down (int sema_id) Task 3 Semaphore Down int mysema_down (int sema_id) If mode == 2, call mysema_down_userprio with maximum priority value (=100) 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

Semaphore Down int mysema_down_userprio (int sema_id, int userprio) Task 3 Semaphore Down int mysema_down_userprio (int sema_id, int userprio) 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 With priority number from 0 to 100 If <0: apply priority number 0 If >100: apply priority number 100 If value > 0, reduce the semaphore value by one Return value 0 for success, -1 for error

Semaphore Up int mysema_up (int sema_id) 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 OS priority mode Based on the priority (prio) of task (task_struct) If this value is lower, the task has higher priority User priority mode Same as OS priority mode, but use the value given by user Return value 0 for success, -1 for error

Semaphore Release int mysema_release (int sema_id) 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

Test Your Semaphore Write a simple user-level program Task 3 Test Your Semaphore Write a simple user-level program Check the correct operation of system calls Test difference of modes

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

Questions for the Report Find answers Q1. For “Dining philosopher” problem, how can you prevent starvation? Q2. For “Training monkey” problem, if two male monkeys and two female monkeys can stay in the room, how should your solution be modified? Q3. For “Training monkey” problem, if monkeys can enter the room in the ascending order of their IDs, how should your solution be modified? Q4. There are POSIX semaphore and non-POSIX semaphore (ex. System V semaphore). What is the difference? Pros and cons? Q5. Classify semaphores in task 1 and task 3 into POSIX and non-POSIX semaphore. What is the reason? Q6. An user-level process may access to kernel or hardware directly. What are pros and cons of using system call? Q7. To make a new system call, we compiled entire kernel. What happen if we try to do it with adding a module? Q8. Your own semaphore has several modes. What is pros and cons of each mode? In what situations each mode can take a benefit? Specify the references From Q4 to Q8

Submission Contents Submission Source codes Report 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. 14, PM 23:59 Delay penalty 10% per day (AM 00:00) E-mail: w.j.kim@kaist.ac.kr (Kim, Woo Joong) Be careful! Do not send to other TAs Title: [EE516 Project 3] student_number Attached file: student_number.zip Various compression format is allowed