Thursday 12 April 2018

Implementing lottery scheduling on XV6

About

In this blog, I will show you how to implement lottery scheduling for XV6 operating system. XV6 by default uses Round Robin algorithm. Please note that in this implementation, the process sets its own tickets which is not ideal as it may take a huge number and the other processes would have very less chance to run.

What is XV6?

xv6 is a re-implementation of Dennis Ritchie's and Ken Thompson's Unix Version 6 (v6).  xv6 loosely follows the structure and style of v6, but is implemented for a modern x86-based multiprocessor using ANSI C.
source

What is lottery scheduling?

Lottery scheduling is a probabilistic scheduling algorithm for processes in an operating system. Processes are each assigned some number of lottery tickets, and the scheduler draws a random ticket to select the next process. The distribution of tickets need not be uniform; granting a process more tickets provides it a relatively higher chance of selection. This technique can be used to approximate other scheduling algorithms, such as Shortest job next and Fair-share scheduling.
Lottery scheduling solves the problem of starvation. Giving each process at least one lottery ticket guarantees that it has a non-zero probability of being selected at each scheduling operation.

How lottery scheduling works?

  1. We have two processes A and B. A has 60 tickets (ticket number 1 to 60) and B have 40 tickets (ticket no. 61 to 100).
  2. Scheduler picks a random number from 1 to 100. If the picked no. is from 1 to 60 then A is executed otherwise B is executed. (source)


Steps

We need to do the following things to implement lottery scheduling in XV6
  1. Make a system call which allows you to set the tickets for a process.
  2. Code to generate a random number.
  3. In the scheduler function count the total number of tickets for all processes that are runnable.
  4. Generate a random number between 0 and the total tickets calculated above.
  5. When looping through the processes keep the counter of the total number of tickets passed.
  6. Just when the counter becomes greater the random value we got, run the process.
  7. Put a break at the end of for loop so that we don't execute the processes following the process we just run.

Making a system call

Each process has a ticket value assigned to it. So to implement this we need to change the proc structure which holds the information about a process and add int tickets which will store its ticket value. 
Follow this blog to know how to add system call. In the part where you have to write the actual implementation use the following code which sets the number of tickets for a process.

 int sys_settickets(void)  
 {  
   int n;  
  if(argint(0, &n) < 0)  
   return -1;  
  proc->tickets = n;  
  return n;  
 }  


Allocate default value of tickets when a process is created

In the proc.c file, enter the following line of code in "allocproc" function just below "found".

 p->tickets = 10;  

This sets the default ticket value to 10.


Creating Random number

  1. Include rand.o\ in OBJS in Makefile
  2. Copy files rand.c and rand.h in the XV6 directory.

After doing these two steps you can include rand.h header file in your proc.c file which is responsible for scheduling all processes.

Scheduler Function

The scheduler function has two for loops, one inside the other. The outer for loop just keeps on running forever. The inside for loop loops over all the processes and selects the first process that it finds in a RUNNABLE state. It then runs this process till its time quanta expire or the process yields voluntarily. It then selects the next process in RUNNABLE state and so on. This is the way it implements Round Robin.
For lottery scheduling, we need the total number of tickets of the processes that are in the RUNNABLE state. To calculate this we put a for loop inside the outer for loop just before the inside for loop executes. This calculates the total number of tickets.
Now we generate a random number between 0 and the total number of tickets. After we have the random number we execute the for loop that runs processes. When this for loop loops over processes, we keep counting the total number of tickets passed of processed that are in a RUNNABLE state. As soon as the total tickets passed get higher than the random number we got we run that process. 
Now, after the process runs, we need to put a break at the end of for loop which executes all processes. This is because:
If we don't break the value of total tickets passed will keep on increasing and will always be higher than the random number.
Also, after we run a winning process we need to recompute the total number of tickets of all RUNNABLE processes as that value might have changed.












71 comments:

  1. Good stuff. rand.o needs to be added in kernel/makefile.mk. Copy rand.c and rand.h into kernel folder. You need to add tickets value to the proc struct in kernel/proc.h (line 63ish)

    ReplyDelete
  2. Gracefully written information on this blog are going to support me for my coming assignments. Every point was very clear and taught me few new parameters. I would like to use this information in coming future.
    หวย เด็ด งวด นี้

    ReplyDelete
  3. This blog is truly useful to convey overhauled instructive undertakings over web which is truly examination. I discovered one fruitful case of this truth through this blog. I will utilize such data now.
    ผล หวย หุ้น

    ReplyDelete
  4. Lottery live Lottery Sambad lottery sambad live Sikkim state lottery live Nagaland state lottery live Westbengal Lottery Live লটারি রেজাল্ট। লটারি সংবাদ
    West Bengal Lottery

    ReplyDelete
  5. An author must have a vast knowledge of vocabulary. The dictionary of a writer must be full of new english vocabulary to make their work more attractive. Use of new words makes their work more valuable and graceful.
    หวย เด็ด งวด นี้

    ReplyDelete
  6. Stay in touch with us to get the Thai lottery tips at
    Thai Lottery Tips 16th July 2019

    ReplyDelete
  7. I was very pleased to find this site.I wanted to thank you for this great read!! I definitely enjoying every little bit of it and I have you bookmarked to check out new stuff you post. แทงบอล

    ReplyDelete
  8. Hello, I have browsed most of your posts. This post is probably where I got the most useful information for my research. Thanks for posting, maybe we can see more on this. Are you aware of any other websites on this subject. xosohanoi

    ReplyDelete
  9. Thanks for writing this. I really feel as though I know so much more about this than I did before. Your blog really brought some things to light that I never would have thought about before reading it. You should continue this, Im sure most people would agree youve got a gift. see this

    ReplyDelete
  10. Considerably, the post is really the greatest on this worthy topic. I agree with your conclusions and also can eagerly look forward to your future updates. Simply just saying thanks definitely will not simply just be enough, for the fantasti c clarity in your writing. I will right away grab your rss feed to stay abreast of any kind of updates. Genuine work and also much success in your business dealings! Read More Here

    ReplyDelete
  11. Wonderful article. Fascinating to read. I love to read such an excellent article. Thanks! It has made my task more and extra easy. Keep rocking. KBC WhatsApp Lottery

    ReplyDelete
  12. Hello I am so delighted I located your blog, I really located you by mistake, while I was watching on google for something else, Anyways I am here now and could just like to say thank for a tremendous post and a all round entertaining website. Please do keep up the great work. หวยรัฐบาลไทย

    ReplyDelete
  13. I wanted to thank you for this excellent read!! I definitely loved every little bit of it. I have you bookmarked your site to check out the new stuff you post. RUAY

    ReplyDelete
  14. Superior post, keep up with this exceptional work. It's nice to know that this topic is being also covered on this web site so cheers for taking the time to discuss this! Thanks again and again! หวยออนไลน์ 888

    ReplyDelete
  15. Satta Matka Mega provides Live Satta Matka Results, Indian Satta, Live Matka India Result, Jodi Charts, Weekly Panel Charts, and 2020 Live Record Chart Results. Indian satta matka

    ReplyDelete
  16. Rags to riches stories are always fun to hear. The best stories involve hard-luck cases, where significant winnings can be used to help out those truly in need. Though most of us have not won the Mega millions Jackpot, everyone has their own lottery success story. 파워볼사이트

    ReplyDelete
  17. The house edge is one of the most important factors that every player should consider whenever he/she is engaged in any type of gambling. It tells us how much of our money goes to the gambling agent's pocket and how much is redistributed to the winners. The purpose of this article is to compare the house edges of three representative games of a casino, a sports betting, and a lottery, so that a casual gambler, and even a veteran one for that matter, would be aware of what lies behind the curtain. 실시간 파워볼

    ReplyDelete
  18. I truly like your interpretation of the issue. I now have an unmistakable thought on what this matter is about.. Schedule Employee Check

    ReplyDelete
  19. Not so long ago I used to spend a small fortune playing the Euro Millions lottery and the UK National lottery, only to end up losing money hand over fist. These days however, I make good money every time the Euro Millions lottery draw takes place. But the remarkable thing is - I don't even have to buy a single ticket. อนิเมะ

    ReplyDelete
  20. An intriguing discussion will probably be worth comment. There’s no doubt that that you simply write regarding this topic, it will not certainly be a taboo subject but usually consumers are not enough to communicate on such topics. An additional. Cheers visit

    ReplyDelete
  21. Super Moolah additionally furnishes players with more modest online jackpots. The Major jackpot offers a base prize of $10 000 while the base prize offered by the Minor jackpot is $100. The littlest jackpot accessible is the Mini jackpot, which seeds at $10. judi slot microgaming

    ReplyDelete
  22. Have you tried twitterfeed on your blog, i think it would be cool.,-:`. pp slot

    ReplyDelete
  23. Basically to follow up on the up-date of this topic on your web site and wish to let you know simply how much I loved the time you took to generate this valuable post. Within the post, you really spoke of how to actually handle this matter with all ease. It would be my pleasure to collect some more ideas from your web page and come as much as offer other people what I learned from you. Many thanks for your usual terrific effort. Levinsohn profile

    ReplyDelete
  24. To know wisdom and instruction, to perceive the words of understanding medium blog

    ReplyDelete
  25. Lotteries are everywhere. This is a given fact, and it means that their are literally thousands of lotteries out there in the world. Not only are there a vast number of lotteries available to play, but the vast majority of people playing these lotteries are paying for them, and why not? ตรวจหวย

    ReplyDelete
  26. INTRODUCTION - Perhaps I should start by asking the question "What is a Home Based Business? This is any business operated from the comfort of the residential home of the owner. Ebay Google SEO expert

    ReplyDelete
  27. You made some decent points there. I looked on the net for your problem and located most people goes together with along with your internet site. an interview with Ross Levinsohn

    ReplyDelete
  28. You made some decent points there. I looked on the net for your problem and located most people goes together with along with your internet site. what does Ross Levinsohn do? Ross Levinsohn LinkedIn

    ReplyDelete
  29. I respect what you have done here. I like the part where you say you are doing this to give back however I would expect by every one of the remarks this is working for you also. catchy store names

    ReplyDelete
  30. Chicken had been with for hundreds of years. It has nourished him and gave him food to make him last this long. Let's discover how man's ability to cook chicken has evolved through the years by knowing the different ways he uses to make chicken palatable. https://prometeolibros.com

    ReplyDelete
  31. You, my pal, ROCK! I found just the information I already searched all over the place and just could not locate it. What a perfect web site. buy HGH

    ReplyDelete
  32. Why video game tester job programs which make some people thousands of dollars leave others with a bad experience. And what determines when these programs are, and are not, right for you. mega888 ios

    ReplyDelete
  33. Can I just now say that of a relief to get somebody who actually knows what theyre speaking about on the web. You actually realize how to bring an issue to light and produce it important. Workout . need to look at this and fully grasp this side on the story. I cant think youre no more common because you definitely contain the gift. double sided tape

    ReplyDelete
  34. you can always tell the quality of USB cables by looking at the thickness of the cable. thicker usb cables have higher quality., check this out

    ReplyDelete
  35. When your website or blog goes live for the first time, it is exciting. That is until you realize no one but you and your. Black Magic Spell Caster

    ReplyDelete
  36. Why video game tester job programs which make some people thousands of dollars leave others with a bad experience. And what determines when these programs are, and are not, right for you. bet188

    ReplyDelete
  37. Thanks for sharing nice information with us. i like your post and all you share with us is uptodate and quite informative, i would like to bookmark the page so i can come here again to read you, as you have done a wonderful job.ythub.cc

    ReplyDelete
  38. प्ले बाजार, सत्ता किंग, दिसावर सत्ता , फरीदाबाद सत्ता, श्री गणेश , ताज , आगरा प्ले बाजार , सत्ता मटका मार्किट इस ओने ऑफ़ थे world'स मोस्ट फेमस सत्ता मटका एंड सत्ता मटका गेम्स. मान्य पीपल अराउंड थे वर्ल्ड हैवे प्लाएड थिस गेम. Satta King

    ReplyDelete
  39. Positive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work. Prediksi Sydney

    ReplyDelete
  40. I have read your article, it is very informative and helpful for me.I admire the valuable information you offer in your articles. Thanks for posting it.. Bocoran HK

    ReplyDelete
  41. Online casino games have become tremendously popular thanks to the spread of the internet. Online casinos can be divided into three groups based on their interface: web-based casinos, download-based casinos, and more recently live casinos. Read the article to know more about online casinos 스포츠토토

    ReplyDelete
  42. Strong Blog Sharing you.Best Casino Sites in 2021 | Korea No.1 Casino Site - Yesn2.온라인바카라

    ReplyDelete
  43. Thanks for your article it very useful article.Private Toto Site Major Playground Sports Toto Private Toto Safety Playground We select and recommend the best companies. This is a list of reliable sports toto sites that are 100% guaranteed by Choknara.토토

    ReplyDelete
  44. Thank you for your information sharing it is very useful Article.we are provide Online money making Services so please click here.
    Online Business Idea

    ReplyDelete
  45. Thanks For your sharing Article.we provides a unique and different experience unique to online casinos and provides fun and relaxation.
    온라인바카라사이트

    ReplyDelete
  46. หวยลาว Wow, cool post. I'd like to write like this too - taking time and real hard work to make a great article... but I put things off too much and never seem to get started. Thanks though.

    ReplyDelete
  47. Excellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking. k8loto

    ReplyDelete
  48. Technically flash games are interactive games created using the Adobe Flash software. Macromedia originally owned and developed the Flash software for use in animation programming and was being used to create movies and TV shows and complex business presentations. Soon after, Flash was picked up by programmers to create web-based interactive games. qq online

    ReplyDelete
  49. Ever since the advent of video games, there have been debates on the pros and cons of gaming. While there is no denial that gaming sharpens a number of skills of the child, it also stands for a fact that the addiction of gaming can have dire health consequences. Apart from health risks, an addiction to gaming also brings along a non-social temperament that leads to stalled emotional growth. toptotosite.com

    ReplyDelete
  50. In this article you will read about the best games which can be played on high quality gaming laptops. Give your time to this post and increase your knowledge related to the best games and quality gaming laptops. 안전놀이터

    ReplyDelete
  51. I never thought that I would be a millionaire by winning a lottery but Lord Bubuza made it possible for me, I have played the lottery game for years without winning so I spoke to my friend who won massively and he told how lord Bubuza gave him the winning number. I contacted lord Bubuza immediately seeking help and he responded. He said a reading has to be done after which a spell will be casted and he will give me the lottery winning number. I did as he instructed and the spell was casted, lord Bubuza gave me some digits and told me when and where to play the lottery which I did and behold I won $562,600,000 Million. I am a millionaire today with the help of lord Bubuza spell and winning number. Let's eradicate poverty, I am making this testimony because Lord Bubuza is a great PSYCHICS. Contact him for help to win a lottery via WhatsApp/call/text: +1 984 240 7211 or email: lordbubuzamiraclework@hotmail.com

    ReplyDelete
  52. I really thank you for the valuable info on this great subject and look forward to more great posts. Thanks a lot for enjoying this beauty article with me. I am appreciating it very much! Looking forward to another great article. Good luck to the author! All the best! ruay

    ReplyDelete
  53. it was a wonderful chance to visit this kind of site and I am happy to know. thank you so much for giving us a chance to have this opportunity..
    ซื้อหวย

    ReplyDelete
  54. Good suggestion.Play Online Lottery And Get Draw within 2 hours to see their results.
    goodluckindia lottery

    ReplyDelete
  55. Are you looking for ways to hit the lottery jackpot? Search no more for Dr Amber can help you win the lottery you want with his powerful lottery spell. Visit: amberlottotemple.com or WhatsApp +1 318 306 5044 or email: amberlottotemple@yahoo.com for his spells are real & genuine.

    ReplyDelete
  56. I was paid $30 per hour at work and it was a stressful job, I was researching online for a better job when I saw testimonies of how lord Bubuza helped people with his spell to help a lottery. I have not played the lottery game before so I wondered how it works, I spoke to lord Bubuza via text: +1 507 400-0072 asking for help to win the lottery game too. He responded and requested for my name and picture, he said it was needed to cast a spell so that his gods will reveal to him the site where i was destined to win. I did as he instructed and the items were bought to cast the spell, after the casting of the spell he gave me a lottery number and a website which was revealed to him by the gods when he was casting the spell. I played the lottery numbers and two days later I got an email that I won 139 million dollars. I thought it was a joke but behold I got paid without stress. Lord Bubuza is a great Psychic, join me thank him via Text/call: +1 507 400-0072 or email: lordbubuzamiraclework @ hotmail . com

    ReplyDelete
  57. Do you need help to get the right lottery winner numbers ? There is a spell caster called lord Bubuza, he reveal lottery winning numbers with the help of his spell. His spell is super active. My name is Nang T and I won $18.7 million with the help of his spell. Contact Lord Bubuza via WhatsApp: +1 951 442 2214 or email: lordbubuzamiraclework @ hotmail . com

    ReplyDelete
  58. I’m excited to uncover this page. I need to to thank you for ones time for this particularly fantastic read!! I definitely really liked every part of it and i also have you saved to fav to look at new information in your site. I’ve read some good stuff here. Definitely worth bookmarking for revisiting. I surprise how much effort you put to create such a great informative website. Thumbs up guys your doing a really good job. 카지노사이트핫

    ReplyDelete
  59. Pleasant article.Think so new type of elements have incorporated into your article. Sitting tight for your next article . I am happy to find this post Very useful for me, as it contains lot of information. I Always prefer to read The Quality and glad I found this thing in you post. Thanks . This surely helps me in my work. Lol, thanks for your comment! wink Glad you found it helpful . These are just a couple of things to think about when looking for sports betting books and systems. Good Day! The way you have written your post is so nice. Very professional . Keep writing this amazing post. 바둑이사이트넷

    ReplyDelete
  60. 바카라사이트인포We place a great deal of attention on the values, principles, knowledge, relationships and abilities that allow for wealth creation. Our values-based model for wealth transition influences human, intellectual, social and structural capital to support and promote growing financial capital.

    ReplyDelete
  61. Help commonly key fantastic materials -- you could fully grasp the many dinners with: gacha club outfits | Hablamos de Gamers

    ReplyDelete
  62. Thank you so much for providing the information on the lottery result to check What's The Powerball For Tonight, The Lottery Results For Saturday, Numbers For The Powerball Last Night. The other information can be found using the following words at your online lotto store.

    The Lottery Result For Saturday
    Check The Lotto Results
    How To Win The Lotto
    The Winning Lotto Numbers
    The Lotto Numbers For Last Night
    Check The Powerball Numbers

    ReplyDelete
  63. download Truecaller MOD APK is a promotion free App for Android clients with north of 50 million . With this application. You can play melodies behind the scenes.

    ReplyDelete
  64. The anticipation filled the air as the crowd eagerly awaited the announcement of the kerala state lottery result. Hearts raced and fingers crossed, hoping for the life-changing numbers that would unlock a world of unimaginable possibilities.

    ReplyDelete
  65. vegasgroup 5 situs bandar online paling terpercaya di asia.
    vegasgroup toto link alternatif situs resmi vegasgroup

    ReplyDelete
  66. I am really excited to read your blogs. You are doing really good.
    If you are interested in trading then you must visit and get great advice from the best stock broker in India.

    ReplyDelete
  67. the relationship between lottery scheduling and a millionaire. However, the question lottery in saudi arabia is a bit unclear. Lottery scheduling is a computer science concept used in operating systems to allocate resources fairly among competing processes. On the other hand, being a millionaire typically refers to a person who has a net worth of at least one million currency units.

    ReplyDelete
  68. HOW I WON $8.500.000 MEGA MILLION LOTTERY WITH THE HELP OF PROPHET ISAAC SURE AND WINNING LOTTERY NUMBERS EMAIL URGENTSPELLCAST01@GMAIL.COM

    Read my review and contact him now for your own lottery winning numbers. It is a very hard situation when playing the lottery and never won, or keep winning low fund not up to 500 bucks, i have been a victim of such a tough life, the biggest fund i have ever won was 350 buck, and i have been playing the lottery for 8 years, things suddenly change the moment i came across a testimony of a voodoo doctor who help people in any type of lottery numbers, i was not easily convinced, but i decided to take try, after all done, i am proud to be a lottery winner with the help of Prophet Isaac, i won $8.500.000 and i am making this known to every one of you who have been trying to win the lottery, believe me this is the only way to win the lottery, this is the real secret we all have been looking for.

    You can contact PROFESSOR ISAAC on his WhatsApp: +27 60 630 7392

    Email: urgentspellcast01@gmail.com

    ReplyDelete