Download presentation
Presentation is loading. Please wait.
1
Project 3. “System Call and Synchronization”
Prepared by Dong Jin Kim PhD. Student
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 (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
4
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
5
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
6
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
7
Task 1 Semaphore in Linux (3) Example source code
8
Task 1 Semaphore in Linux (4) Example source code
9
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
10
Solve Synchronization Problems
Task 1 Solve Synchronization Problems Solve problems using semaphore P1. Dining philosopher P2. Training Monkey
11
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
12
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
13
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 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”
14
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
15
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)
16
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
17
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
18
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
19
How to Add System Call (5)
Task 2 How to Add System Call (5) Compile and install # make -j4 bzImage # make install Reboot
20
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
21
How to Use New System Call (2)
Task 2 How to Use New System Call (2) Compile and run
22
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
23
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)
24
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
25
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
26
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
27
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
28
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
29
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
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
31
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
32
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) (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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.