Download presentation
Presentation is loading. Please wait.
Published byPhilippa Marsh Modified over 9 years ago
1
Project 3. “System Call and Synchronization” By Dongjin Kim 2015. 10. 29
2
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”
3
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
4
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
5
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
6
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
7
Semaphore in Linux (3) Example source code Task 1
8
Semaphore in Linux (4) Example source code Task 1
9
Semaphore in Linux (5) Compile Need “-lpthread” option Ex> # gcc -o sematest sematest.c -lpthread Execution result Task 1
10
Solve Synchronization Problems Solve problems using semaphore P1. Dining philosopher P2. Traffic control Task 1
11
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
12
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.
13
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
14
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 http://www.cplusplus.com/reference/cstdlib/rand/http://www.cplusplus.com/reference/cstdlib/rand/ 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
15
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
16
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)
17
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
18
How to Add System Call (3) kernel/mysyscall.c Create file and implement system call handler Use function name as previous step Task 2
19
How to Add System Call (4) kernel/Makefile Add entry to “obj-y” Use name from the created file name Task 2
20
How to Add System Call (5) Compile and install # make -j4 bzImage # make install Reboot Task 2
21
How to Use New System Call (1) Write test program in the user level Use same number as before Task 2
22
How to Use New System Call (2) Compile and run Task 2
23
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
24
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.
25
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
26
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
27
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
28
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
29
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
30
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
31
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
32
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) E-mail: djkim@core.kaist.ac.krdjkim@core.kaist.ac.kr Be careful! Do not send to other TAs Title: [EE516 Project 3] student_number Attached file: student_number.zip Various compression format is allowed
33
FYI If you have some questions, FEEL FREE to ask questions to TA Try to search web sites Use web board http://core.kaist.ac.kr/~EE516/WebBoard/ http://core.kaist.ac.kr/~EE516/WebBoard/ You can ask in English or Korean Send an e-mail djkim@core.kaist.ac.kr djkim@core.kaist.ac.kr
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.