ラベル Report の投稿を表示しています。 すべての投稿を表示
ラベル Report の投稿を表示しています。 すべての投稿を表示

2011-11-04

第1回 fluxflex meetup in Tokyoで発表して思ったこととかアレやコレや



はい、今さらなお話です。勢いで "imasara"というタグを付けてしまったぐらいに今さらなお話です。でも、数百人の前で発表する機会なんてあんまり経験して来なかったので、せっかくなので当日までのアレコレや、言い訳や、その後の後日談などを思い出しながら書いてみます。

約1ヶ月前、第1回 fluxflex meetup in Tokyo というイベントがありまして、そこで「GitHub Importを使った fluxflex へのデプロイ例」という内容を発表してきました。元々は、一般参加者として参加するつもりでした。だったんですが、Simple Timekeeper という、fluxflex上で動いているサービスが、幸運にも fluxflex の中の人の目に止まったようです。それで、DMで僕の方に発表の誘いが来て、そして、発表に至ったという感じです。

言い訳をさせて下さい。僕はアイデアが思いついたら可能な限り早く作るのが好きな人間です。だって良いアイデアを思いついたら、早く動いてる姿が見たいじゃないですか。Simple Timekeeper も例に漏れず5時間ぐらいで作ったサービスです。なので、中身を見ると「body bgcolor="black"」とか書いてあるぐらいのウ◯コードとなっています。しかしまぁ、「まずは発表資料を用意するのが優先だ」と考え、というか今さら書き直す時間もなく、気力も無く、クソ◯スのまま発表に至りました。本当はもっとキレイに書けるよ!だからそんな目で見ないで!

ところで、僕はずっと fluxflex のことを「auto-scaling」を推している PaaS だと思っていましたが、いつの間にか軽量なウェブサービス開発者をターゲットにした PaaS にPivot(?)したみたいですね。僕の思い込みかもしれないので、実際のところは今度会った時にでも聞こうかなーと思っていますが、少なくとも僕からしてみたら最高のpivotです。というのも、僕は軽量なウェブサービスをたくさん作ってたくさん失敗するタイプの人間なので、もうアレですね。最高です。「ずっとこの路線で行ってくれたら‥」と密かに願っています。「Pivotしたのかなー」と思った背景は色々とありますが、詳細は後日 Okinawa.rb で発表した fluxflex meetup survey にて。地域Rubyの会で最もRubyの話をしない人間とは僕のことです。



僕はシンプルなウェブサービスやアプリがもっともっと増えたら良いなーと思っています。それには、僕がZen信者だからとか、そもそも個人でスゴいウェブサービスを作る技術が無いからとか、色々な理由があります。ですが、最大の理由は、スゴいモノじゃなくても、数万ラインのモノじゃなくても、多くのユーザに使ってもらえるイイモノはたくさんあると信じているからです。ホイッスル on Androidを実際に作ってみて、「あ、やっぱいけるじゃん」と確信しました。

でも、僕自身そうでしたが、頭ではそう思っていても、実際にそうするのは、若干の度胸が必要です。だって、まず、失敗することが多い。作っても誰も見てくれないとかザラです。公に自分の失敗を晒すのは、やはり若干の覚悟が必要です。

しかも、ウ◯コードになりやすい。まぁこれは僕だけの特別な傾向かもしれませんが、少なくとも僕ぐらいのスキルでは、ウ◯コード量産機になる度胸が必要だと思います。ウン◯ードでしかも誰も使ってくれないときなんかは、もう完全に涙目ですよ。



「もっとキレイに書け」とか「汚いコードを書くヤツは(ry」みたいな風潮を僕は感じます。企業で、大勢で書くモノを作るときは全く持ってその通りだと思います。でも、個人や小さいチームでモノを作るとき、本当にそうなんでしょうか。あのGoogleですら初期バージョンのコードは酷いモノだったそうです。それなら、凡人の僕が書くコードなんて酷くて当たり前な気がします。なんていうんですかね、フリーミアムが「Freeと1円以上のときは法則が違う」と主張したように、個人や少人数でモノを作るときと、企業や大勢でモノを作るときの法則は違うような気がします。気がするだけです。証拠も何もありません。

「証拠が無いなら作っちゃおう」。そういうのは実験して確かめればいいですよね。ということで、週末ものづくり講座ってのを実際に開いてみました。どうなんですかね。モノ作りと同じで、一般的には失敗する確率の方が多そうです。ですがまぁ、そこらへん色々試してみて、結果を見てみて、その後じっくり考えることにします。

まさか fluxflex の話からモノ作りの話に飛ぶとは僕自身思いませんでしたが、とりあえずここらへんに着地して終わりにしようと思います。

そんじゃーn(ry





追記:

最近「自分のアタマで考えよう」という本を読んだので、久々に自分のアタマで考えて文章にしてみたんですが、本書でも述べられているように、最初はろくなもんにならないっすね。コード書くのに失敗したり文章書くのに失敗したり、失敗から離れられない今日この頃です。でもまぁ、「失敗しろ」って主張しているようなもんなので、主張してる僕が失敗しないことには始まらないっすね。

2011-10-14

Nico Rank Inverter




1ヶ月前ぐらいにtwitterでもつぶやきましたが、Gitoriousをサーバに設置している途中で、「ちょっと息抜きに何か作ろうかなー」と妄想に耽っていたら、気付いたらこんなのが出来上がっていました。

もしよければどうぞ。


Description

Chrome Web Store
https://chrome.google.com/webstore/detail/bkmhgfacjlbplglbimpdbbeligolfadl

ソースコード
https://github.com/yasulab/nico-rank-inverter

2011-08-30

eXtreme HAGO 2 LT 大会アレコレ



先週末に eXtreme HAGO 2 LT 大会 ( #xHago2 )に参加したので、そのときにやったことや得た情報、感想などのアレコレを忘れないうちにまとめておきます。

やったこと:

  1. xHagoのロゴを作成しました。
  2. 発表しました。というかOkinawa.rbの宣伝を思う存分やってきました。
    今週末の土曜日(9/3)はOJAG + Okinawa.rbの勉強会があります。是非!
  3. タイムキープ用のWebサービスを作りました。わりと好評だった模様。
    ただ、MacBook以外のディスプレイだとレイアウトが壊れるかも。
知ったこと:

  1. 琉球大学ではMacを強制的に買わされ&Emacs+tcshを使わされるらしい。
  2. 次回沖縄iPhone勉強会でjQuery Mobileの話をするんだとか。面白そう!
    ABC2011sのデザイントラックでも耳にしましたが、まだまだ不具合が多いっぽい。
  3. 学生さん達がやる気一杯!ASAPで海外留学or就労して欲しいなー。

気になること:
  1. Tythonの行く末。
  2. 自作PCの行く末。
  3. ショートコーダーの末路。
次回は2月頃にあるそうです。参加出来るかどうかは分からないけれども、またフラっと寄れたらいいなーって考えています。

2011-08-28

Android Bazaar and Conference 2011 Survey: 10年に1度の変革期を遊びたおすために



先日、琉球大学で開催された eXtreme HAGO 2 LT 大会 ( #xhago2 )に参加し、表題の内容を発表してきました。内容は、以前登壇したABC2011sというカンファレンスで見聞きしたことに、僕個人の意見を加えたものとなっています。

発表動画:Xhago2nd, Ustream

※始めの部分は録画出来てないようです。ネタを仕込んだのに...残念(´・ω・`) スライドと当日の反応からネタの雰囲気を感じ取って下さい。

スライド:

当日の反応:eXtreme HAGO 2 LT 大会 第2セッション, togetter

おまけ:もしもエンジニアが本気でダイエットしたら

※夜の部でネタ発表をしてきました。反応が良かったようなので、後日、目標まで到達したらブログ記事でまとめるつもりです。

2011-08-15

OAuth on Simple Twitter Bot (2)




ちょっと @yasulabot というネタbotをつくるためだけに、SimpleTwitterBotを弄り直しました。基本的にはREADMEに書かれている通りにregister_pin.pyを実行すれば、OAuth周りの設定は勝手にやってくれるはずですが、個人的につまずいたポイントをちょろっとリストにしてみます。


- 1. botのtwitterアカウントでログインしたブラウザで、OAuthに登録する事。ChromeならCtrl+Shift+Nで開いたブラウザでbotアカウントにログインして、OAuthに登録すると楽。

- 2. デフォルトのAPIのpermissionは"Read only"なので、APIを通してtweetとかdeleteとかしたいならOAuthの設定画面から、Permissionを"Read & Write"に変更すること。

- 3. たぶん、Windowsだとうまく動かない(Issue 1)。Windows非対応。

基本的にこれらの点に注意して、あとはREADME通りにやれば、自作BotがGAE上で動くような気がします。

参考:OAuth on Simple Twitter Bot

2011-08-12

SimpleTimeKeeper



I created a simple time keeper that helps you timekeeping. It can be used for presentations and lightning talks. No software needs to be installed; just visit the website below :)

http://timekeeper.fluxflex.com/

諸事情で今月の #ojag Workshopの仕切りをやる事になったので、当日必要になるであろうタイムキーパーをサクッと作ってみました。よければ使ってあげてください。

2011-07-21

How to Escape from Mixi

「mixi退会」という脱出ゲームをプレイしてみました。ググって答えを見つけるなんて邪道です。でもどうしても分からない人のために、下記に攻略法を載せておきました。ネタばれ注意!!!











1. ヘルプ検索で"退会"を検索。シロウトにはまずこの発想が思い浮かばない。しかも、上位のトピックにはまず表示されないので、どこかに埋もれている退会トピックを見つける必要がある。





2. 見つけたら、今後、同じ轍を踏む人が出ないように、とりあえず"役に立った"をクリック。その後、「mixiを退会する」をクリック。




3. 足を引っ張られるが、しかしそこは歯を食いしばり、「退会手続きへ」をクリック。



4. mixiさんはとても優しいので、もう一回確認してくれます。しかし、挫けずに「次へ」をクリック。思い立ったが吉日。




5. パスワード入力画面と、メールのお知らせの可否についての画面。ここは単純な事務作業なので、無心でPasswordを打ち込み、お知らせメールは拒否しましょう。別れた彼女から来るメールほど、虚しいものはありません。




6. 確かここら辺で、退会理由を聞かれます。が、SS撮るの忘れてしまいました。とりあえず字数制限(400字)いっぱいに、思いの丈をぶちまけてください。



7. コミュニティを運営してると、さらにここで確認が来る。しかし、もう活動していないコミュニティを存続させる価値はありません。潔く潰してしまいましょう。



8. 退会確認画面。mixiでしか繋がっていない人には大変申し訳ないですが、これが僕の正直な気持ちです。ただまぁ、よく考えてみると、mixiでしか繋がっていない人が、このブログを見る術があるのか、って感じですが。




9. Goodbye Mixi.




とまぁ、こんな具合で、脱出ゲームがクリア出来ます。



僕は面白いものが好きなので、面白い機能やAPIがmixiに実装されたら、きっとまたmixiに登録すると思います。なので、それまでの間、ちょっとだけさよならです。

mixiさん、今までありがとうございました。

2011-06-06

Whistle App: A Case Study of Smart-phone Development and Comparison

ホイッスル on Androidがそろそろ50,000ダウンロードに到達しそうなので、記念に(?)ホイッスル on Androidを題材としたレポートをアップしときます。本当はブログに本文をべたーっと貼りたいのですが(検索に引っかかるので)、うまい方法が見つからないので、とりあえずアブストラクトとインデックスだけ貼っておいて、中身はリンク先へ、という形でアップします。興味があればどうぞ(*)。

* 諸事情でノー添削ver.です。文章はかなりroughなので注意。

Whistle App: A Case Study of Smart-phone  Development and Comparison


Abstract

In order to clarify the insight of differences between Android and iPhone applications, this paper first introduces Whistle App, which is a smart-phone application that runs on both devices. And then, using it as a case study, this paper attempts to compare both de-vices in terms of UI components, resource managements, and markets. The other works not directly related to this paper yet helpful to write it are all attached as an appendix.


Index:
1. Introduction
  a. Background
  b. Why comparing with a case study
  c. What's Whistle App
2. Evaluation Environment, Resource, and Collaborators
  a. Evaluation Environment
  b. Resources
  c. Collaborators
3. Comparison
  a. UI Components
    i. Whistle on Android
    ii. Whistle on iPhone
  b. Resource Management
    i. Whistle on Android
    ii. Whistle on iPhone
  c. Market
4. Future Work
5. Conclusion
6. Acknowledgement
7. Reference

Appendix
  Multi-media Productions of Whistle App
  Reports on Whistle App
  Whistle on Titanium Studio
  Whistle Widget

Body:
https://docs.google.com/viewer?a=v&pid=explorer&chrome=true&srcid=0B2FvmcJSggmCZjhmMmUzM2UtZTRlNy00NTY0LWEzOGUtNmNkNGMyN2QyYmU0&hl=en_US

Whistle App in Android Market:
https://market.android.com/details?id=org.sorarier.whistle

2011-05-19

DoCoMo Smartphone Lounge

ホイッスル on AndroidがDoCoMo Smartphone Lounge(DSL)で紹介されている」という噂を検証するべく、先日、実際にそこに行ってきました。ただ、一人で行くのは寂しかったので、研究室にいた留学生達にも無理矢理こころよく同行してもらう事に。以下、現場の証拠写真+レポート。

DSLへのアクセスマップ

入り口

Touch & Tryコーナーの一角

おや...

おぉ!

本当に紹介されてる!あれ、でもなんかロゴが違うような...。まぁいいか。

紹介文によると、「自分の居場所を教えるために」ホイッスル on Androidを使うそうです。なるほど。例えば、渋谷のハチ公前で待ち合わせしているときなんかに、これを使ってお互いの居場所を教え合えばいいわけですね、分かります。そんな待ち合わせ方が流行ったら、それはもうなんかカオスです楽しい世の中になりそうですね!ということで、是非「ここにいるよーー!と伝えたときに」ホイッスル on Androidをどうぞ。

以上、現場レポートでした。

yasu

2011-04-16

Xv6: Modern OS-like Case Study

PDF ver.


Yohei Yasukawa
Operating System
Dec. 15th, 2010
Case Study - Xv6


1. Introduction


Like case study sections in Modern Operating System, this paper introduces about Xv6, an teaching operating system, and shows how it runs in order to deeply understand a operating system concept.


A. What’s Xv6?


Xv6 is a teaching operating system that was developed by MIT in 2002, and being used as a material of operating system courses for graduate students since then.  And it is becoming famous as a well-developed teaching operating system. In fact, in 2010, it is used as a teaching material of operating system courses in not only MIT, but also Rutgers University, Yale University, Johns Hopkins University, and Stanford University, because of its simplicity.

For many years, operating systems had been taught as one of essential courses for understanding computer science. However, there were few operating systems that can be covered in within a semester because the great operating systems, which are well-developed as a teaching material such as BSD and Linux, are too large to cover in a course. Also, the existing operating systems, such as Unix, were so obsolete that many students were struggled to understand the differences between old and new architectures, and between old and new programming languages. These obstacles made it difficult to teach operating system concepts with actual readable source codes. 

To attack this problem, xv6 operating system was developed as a very compacted operating system that implements important operating system concepts which are mainly inspired from Unix version 6, but it is written in ANSI C programming language and runs on x86. So, Xv6 is becoming famous for a good teaching material that is written in readable source codes, and follows current architectures, but its line of codes keeps less than 10,000 and its commentary consists of less than 100 pages, which can be covered in a semester.


B. Related works

To understand perspective of xv6, knowing related works are helpful, so this section briefly introduces two other teaching operating systems, MINIX3 and Haribote OS.

  • MINIX 3


MINIX 3, which is an operating system developed since 1987, is one of other well-known operating systems that is intended for teaching. This operating system is thoroughly explained in the textbook, Operating Systems: Design and Implementation. 

There are two key different features from xv6. One main difference is that MINIX 3 uses micro kernel structure, which is one of concepts on operating system that implementing less features in a kernel results in increasing performances rather than implementing many features in a kernel. In other words, it needs to have well abstracted organizations, although UNIX tends to put all key functions in a kernel. In fact, the kernel in MINIX 3 operating system consists of less than 4,000 lines. Thereby, MINIX 3 operating system is potentially easier to porting other architectures.

 The other key difference is the concept of MINIX 3 operating system. MINIX 3 was developed not only for teaching, but also for embedded systems. Former versions of MINIX, such as MINIX 1 and MINIX 2, were intended only for teaching, but the modern organization, micro kernel, was fit to the purpose in embedded technology fields. So, from MINIX 3, MINIX expands its features to fit to the needs from embedded systems. Thereby, MINIX 3 users are increased from MINIX 3; on the other hand, the size of MINIX 3 is becoming larger, and now the line of codes keeps more than 30,000 lines.

  • Haribote OS


The other teaching operating system is Haribote OS, which was released with the book published in 2006 in Japan. This operating system contributes to understand operating system with actual source codes, because it does not fully cover general design and concepts that other operating systems do, but is intended for learning how operating systems are implemented. So, the book starts from implementing bootstrap from disks, and ends with implementing applications running on the Haribote OS. 

Also, unlike MINIX 3 and xv6, this operating system does not need any previous knowledge. According to the author, this operating system and book was structured for being understood by not only university students, but also junior high school students. In fact, the book kindly explains computer architectures when the readers needs to understand to implement. So, this teaching operating system is friendly for operating system learners, especially at the very beginning level.


C. Overview of Xv6

Operating systems have different aspects in order to adapt a particular situation or to achieve a particular purpose. But most operating systems, especially unix and unix-like operating systems, have very similar designs. The designs should be slightly different one by one, but its essential concept is totally same.

Fundamental Concepts

Xv6 is, as it is inspired from Unix V6, designed based on such shared, essential concepts. Thereby, xv6 can be a concrete example of other general operating systems. So, understanding of xv6 can be useful for any kind of operating systems. On the other hand, because it is an aggregation of general operating systems, most organizations are so simple that it is very understandable but not effective. For example, many operating systems separately use read locks and write locks in order to decrease performance degradation. However, xv6 does not separately use locks but use just a lock, which increases readability and simplicity of source codes. But it decreases the performance.

Interfaces

From this reason, xv6 kernel provides just following system calls:


Obviously, these system calls are familiar with general operating systems, and they run in the almost same way actually. But as you see that the arguments of functions are equal to or less than general ones, each function is simplified.

Like other operating systems, these system calls serve as an interface between kernel space and user space. Processes in user space cannot use the resources held by kernel, but they can use them by calling system calls. For example, to achieve an xv6’s memory allocation organization that can be dynamic, kalloc() and kfree() system calls are used to implement.

In xv6, any kind of organizations follow this concepts. So, when seeing xv6 codes, you can easily understand essential concepts of process, scheduler, locks, traps, etc. in implementation level, but you need to consider how those concepts can be adapted to real worlds for advancing your knowledge.



2. Process in Xv6

Xv6’s process contains instructions, data, and stack in user-space memory. Also, the process information is stored at data structure in a kernel-space. For example, if a process wants to get its process identifier, it can get it by calling pid() system call. So, processes in xv6 implements several organizations by controlling the data with system calls. This section introduces fundamental functions and a few distinct organizations in xv6.

Fundamental Functions

Xv6’s process can create a new process with fork() system call. It lets a process create new child process. Each process has same memory contents but separately managed.


Also, process can stop with exit() and can wait for one of the calling process’s children to exit with wait().


As the program and sequence diagram above illustrates, fork() returns child process id to the parent process, and returns 0 to child process. So, the fork() behaves in the same way as general operating systems. However, exit() and wait() runs in a slightly different way. In general, they need status code in their arguments, but in xv6 they are not needed when calling exit() and wait(). So, when a process calls wait(), it just waits for one of the caller’s children to call exit().

Similarly, xv6’s process has exec() system call to replace the calling process’s memory with a new memory image. The different colors of data in user space illustrates, exec() system call replaces instructions, data, stack data in user space with new ones. So, in xv6,  the memory image except data structure in kernel space are all replaced with a new program when calling exec().


Scheduling

As most operating systems implement multiple process concept, xv6 implements it, too. But the way of scheduling processes works in a different, simpler way. When scheduling processes, red colored members in the following data structures are used.


The colored members are basically same as general designs except ZOMBIE state of process. In xv6, when a process died, it turns into a ZOMBIE process, and it is not removed automatically. The processes in ZOMBIE states do nothing, and wait for being detected from scheduler. If the scheduler notices that there is a ZOMBIE process in a process table, it removes from the table then. Implementing the automatically process-detected system should be smart, but it requires to have some kind of interrupts, which will reduce the simplicity of scheduler. So, ZOMBIE process state functions as just waiting for being removed.

With the colored data members, xv6’s scheduler runs in the following way which corresponds to the program below.
1. If a hardware interrupt is waiting to be handled, the scheduler's CPU will handle it before continuing. 
2. Check the process table looking for a runnable process. 
3. Set up CPU's segment descriptors and current process task state. 
4. Mark the process as RUNNING.
5. Call swtch() to start running new process.
(* swtch() function causes context switching; specifically, it saves the current context and switches to the chosen process’s context.)



These steps are basically same as the ones in general operating systems. The distinct point of xv6’s scheduler is that the scheduler seeks process table entries incrementally. It is so simple that the line of code are within just about 15 lines. However, in this scheduler, there is no way to assign process intentionally, such as priority-based scheduling. So, if there is a process that needs to be done earlier than other processes, it, however, has to wait for being assigned by xv6’s scheduler.


3. Memory Management in Xv6

As most operating systems have memory managers, xv6 operating system also has a memory manager in its kernel. If a process wants to get more memory spaces or returns its memory spaces to the kernel, they need to contact a memory manager. So, to accept a request from processes, a memory manager always manages used and free memory spaces.

In xv6, the memory manager manages its memory space with a linked list called freelist that contains list of memory area. The unit of its page size is 4KB because x86 segment size granularity is 4KB. So, for example, a page in the freelist is able to have huge size amount of memory spaces in one page, but the least size of a page should be 4KB. The actual structure is as follows:
struct run {  struct run *next;int len; // bytes;} struct run *freelist;
So, when xv6‘s user processes want to request more memory space for doing something, they can get it by calling kalloc() function. Like the scheduling algorithm in xv6, the memory manager seeks the free list incrementally, and if it detects the page that is large enough to accept the request, it returns a kernel-segment pointer. If not, it return 0. 

Similarly, if a process needed to release memory space, they can return it by calling kfree() function. But the kfree() runs a little more complex than that the kalloc() runs. When kfree() is called, not only the len bytes of memory spaces are freed, but also the memory manager sorts freelist, meaning that if there are free, adjacent pages, they are combined into one long page.

Obviously, this memory-allocating algorithm is too simple to adapt real world. But it matches xv6’s concept, an operating system as teaching material.


4. File System

As almost all of operating systems have file systems to maintain data consistently,  xv6 also has its simple file system. So, this chapter introduces how the file system in xv6 works  in terms of two sections: file system data, and file system call.

File System Data

In xv6, the file system composes of the following four elements:

1. Block Allocator 
2. I-nodes 
3. Directories 
4. Path names

First, the block allocator is responsible for managing disk blocks, and keeping track of which blocks are in use. It functions as a memory allocator does. So, if balloc() function is called, the allocator finds and returns free blocks, and then, some data is allocated at the returned new disk block. Also, it can release blocks by calling bfree(). One of the signature has a block to be freed, so if it is called, the given block will be released. So, the block allocator in xv6 can manage disk blocks.

Second element is an i-node, which refer to am unnamed file in the file system. Precisely, it is managed by several functions. For example, when allocating, getting or putting data,  the function of ialloc(), iget(), or iput() is used respectively. To allocate a new i-node, such as creating a file, xv6 will call ialloc() function. The ialloc() runs like balloc(), allocating a new i-node with the given type on device. In iget(), it finds the i-node by a given i-node number and returns its copy. And the xv6 can release a reference to an i-node by calling iput(). With such functions, xv6 can manage i-nodes.

The third is directories, which are special kind of i-nodes whose content is a sequence of directory entries. Each entry is a structure in which there are data of name and i-node number. So, for searching a directory, we can find it by calling dirlookup() with the name. Also, for writing, dirlink() function is provided. It enables us to write a new entry with the given name and i-node number into a directory. With those functions, the concept of directory is implemented in xv6.

Finally, there is a path name element, which serves as providing convenient syntax to identify particular files or directories. For example, you can directly refer to a file with the path name, “/xv6/fs.c”, by using the path name. In the case, xv6 first looks up root directory because it begins with a slash, and then, look up “xv6” directory. If the directory exits,  then starts to refer to “fs.c” file. If the path name does not begin with a slash, xv6 assumes that the path is a relative path, not absolute path.

With those elements, xv6 implements the file system. So, the xv6 file system runs in almost same way that the current unix and unix-like operating system runs. But the algorithms are totally naive. The functions to seek something such as balloc() and dirlookup(), xv6 uses a linear seeking, in order to fit the concept of xv6. So, the file system behaves almost same as other operating systems, but it is not efficient.

File System Call

We learned how the data structure in the xv6 file system works in the previous section, but not how the system calls in xv6 are related. So, this section briefly introduces which system calls are used to implement the file system. 

The following list shows some of the system calls and its brief behavior mainly used in xv6 file system.


In addition, there are some helper functions for system calls, which include argint(), argstr(), argptr(), and argfd(). They function as making a good abstraction and serve as connectors between lower level functions to upper level one. And they run in the same way as modern systems have, except that its algorithm is much simpler than the modern systems.



5. Input / Output


We described how the file system is structured and which system calls are used to access data in the previous chapter. Since this chapter, we will focus on how input and output works; specifically, how a buffer cache works in xv6.

Overview

Xv6 has a buffer cache organization to read data from and to write data into disks. This buffer cache functions as managing data temporarily before reading or writing in order to control data exclusively and increase performance. So, if two processes access the same data simultaneously, the organization can serve as serializing it. Also, it is beneficial for reducing the number of accessing physical devices, which is considered as one of the highest cost operations.

Data Structure

How does xv6 implement the buffer cache? Xv6 implements it by using a structure data called “buf”. The buf has three fields: dev, sector, and data. Dev specifies which disk device is referred, sector specifies which sector in a disk is referred, and data is a memory space for copying data from the sector. So, each buf corresponds to a sector in a disk. 

The point of this data structure is that how it collaborates with other processes for exclusive control. For collaborating, the data field in the structure data has flags to maintain a status of read and write by B_VALID, B_DIRTY, and B_BUSY. B_VALID flag determines if data is already read, B_DIRTY determines if the data need to write out, and B_BUSY is used for exclusive control. For example, if a process is using a buf, the B_BUSY flag turns on in order to avoid the situation that the other process uses the buf.

Disk Driver

To access disks, an interface between operating systems and hardwares is required. Currently, there are several kinds of interface to access disks, such as SCSI and SATA, but xv6 uses IDE controller as an interface, because it is a little older but very simple. The actual steps to boot IDE device is as follows:
1. Control interrupts:
      • Initializes locks and forbids hardware interrupts.
      • Allows uni- and multi-processor to interrupt.
2. Boot and wait:
      • Boot IDE controller
      • Do polling status bits until IDE controller becomes ready
3. Check disk status:
      • Choose each disk incrementally
      • Check if status bits are changed
      • If not changed, recognize that the disk is absent

So, through these steps above, xv6 boots the IDE and control disks.

Now we understand how to boot the disk interface, IDE controller. So, let us start understand how it controls disk accesses with buffers. In xv6, the requests of reading and writing data to disks are managed by a queue. If buffers in a queue all finish, the controller notifies it with an hardware interrupt. Specifically, the buffer is added into a queue by calling iderw(), and the first buffer in the queue is started to read or write by calling idestart(), while other buffers in the queue wait for finishing the previous buffer. 

The idestart() changes its behavior by the status of flags; if the write bit is on, it writes data, and then notify that it finished by calling ideintr(). If the read bit is on, with the sleep() system call, it waits for the notification that the disks are ready for being read, and then read data. 

Interrupts and Locks

What happens if an interrupt occurs when reading or writing data? Obviously, the consistency of disk file system will be broken because the disk access cannot be stopped immediately. To avoid such situations, xv6 has a special lock called idklock; xv6 acquires the lock by calling acquire(), and releases it by release(). So, with the lock, xv6’s writing and reading operations can be synchronized.

Like general operating systems, because the idelock is used, we should avoid race condition when acquiring and releasing the lock. So, xv6 creates critical regions by adding pushcli() or popcli() operation to forbid interrupts. Specifically, pushcli() is inserted before acquire(), and popcli() is inserted after release(). Of course, the interrupts can be nested, so the puchcli() and popcli() operations can be stacked in xv6.

Buffer Cache

Now we grasp the idea about how buffer cache works to read and write data. But why does the buffer cache serve as serializing accesses? It is obvious that we need to establish the situation that only one kernel process can edit file system’s data, because if not, the file system will not be consistent and the data will be corrupted. To allow only one process to read and write, bread() is used when using buffer caches to block processes. For example, if two processes are about to call bread(), one process gets the buffer successfully, and the other process will wait for that the first process finished its operation.

The bread() function calls two helper functions; first, call bget() to obtain a buffer for a given sector, and then, call iderw() to add the buffer into a queue in order to read or write disks. When bget() function is called, it starts to scan buffers incrementally, and if the buffer for a given sector is found, it tries to acquire a lock. If a lock is acquired successfully, bget() sets B_BUSY flag true, and return the buffer. For exceptions, if cannot acquire the lock, sleep until the lock is released. Also, if not found the targeted buffer, create a new buffer for that section, and if there is a buffer never used recently, reuse the buffer as a new buffer. In other words, there are no buffers in the list at the beginning, but each buffer is created based on needs. Once created, it is remained in the list, or reused as a new buffer.

It is common that general operating systems have the list of buffers for input and output, but in xv6, the list is structured based on LRU algorithm. The reason why xv6 uses LRU algorithm is that it is simple to scan for two meanings. If the obtained buffer is used successfully, the buffer moves its entry in the list to the first entry, because xv6 assumes that recently used buffer will be used again in the near future. Also, if looking for buffers never used recently, xv6 can scan the list by the reverse order, because the reverse order of the list means the order of buffer recently not used. So, the LRU algorithm fits to the concept of xv6.


6. Summary

In this case study, we investigated the background and concept of xv6 first, and then showed how xv6 runs. Overall, because of its concept, xv6 would be too simple as an operating system; however, it is implemented by the understandable codes and algorithms and they are essences modern operating systems. Also, the lack of functions to be an real operating system could be good assignments for learners to implement. For those reasons, xv6 is becoming famous as a new teaching operation system, and becoming widely used in operating system classes.



7. References


1. Xv6: a teaching operating system (revision 3)

2. The MINIX 3 Operating System

3. Haribote OS

2011-04-14

Titanium Studio:3日で出来るiPhoneアプリ開発環境

噂のTitanium Studioを使って、iPhoneアプリ開発経験の無い僕が3日(金、土、日曜)で出来るところまでやってみました。作った物はImage-based Word Listといって、英語初心者および初級者向けの英単語学習アプリです。



このアプリは、与えられた英単語から、その英単語に関連する画像(flickr API)と定義(Dictionary.com API)、および例文をWeb上から取って来ます(Google Search API)。英単語を学習するモード(Learn)と、復習するモード(Review)、テストするモード(Quiz)の3つがあり、これらのモードを使い分けて、英単語を学習していくことが出来ます。

似たようなコンセプトのアプリとして、LinkedWordという辞書アプリがありますが、僕の作った物は辞書ではなく単語帳アプリです。なので、APIは同じか、もしくは似ている物を使っているはずですが、表示の仕方が異なります。

具体的な動作については、DEMO動画を取ったので、それを見て下さい。バグやら何やらも写っていますが、良いところだけ写すのもどうかと思ったので、取り直しはしていません。



Titanium Studioを使ってみた感想ですが、付属のKichenSinkというサンプルアプリ集がかなり秀逸で、それを参考にして作っていけば、かなりサクサクとアプリを作っていくことができます。また、デフォルトで背景が黒色、文字が白色なのも素晴らしいです。それと、未だ試していまませんが、CUIでも動くようです。これでオープンソースというのだから、驚きです。

TitaniumではAndroidアプリも作れるようなので、今度はアンドロイドアプリをTitanium Studioで作ってみようかと思います。今のところ、ホイッスル on Androidのウィジェット版が欲しいという声をちらほら聞くので、今週末は、その実装をTitaniumでやろうかと考えてます。

Image-based Word Listのソースコード
https://github.com/yasulab/Image-based-Word-List

なお、自作API用に使っている自サーバのメンテナンスコストが大きいので、Image-based Word Listアプリを公開する予定はありません。また、自サーバが動いていないと、WebView(画面下の表示領域)に何も表示されなくなります。

// もし自作APIをGoogle App Engineなどにうまく移植出来たら、公開するかもしれません。

2011-03-20

User Review Notifier for Android Market



1つ前の投稿で紹介したUser Review Getter from Android Marketを利用して、Android Marketにある任意のAppのユーザレビューを取得し、新しいものがあればメールで知らせるスクリプト(の組み合わせ)です。

Source Code:
https://github.com/yasulab/user-review-notifier-for-android-market

以下、READMEから引用
====================

Periodically check user reviews on your android app
in Android Market, and email you if there is new reviews.

What You Need
-------------

- Unix Server
- sendmail (command)
- lxml (python)
- cron

Setup
-----
1. Replace upper-case strings in user-review-notifier.sh with your own.

   #!/bin/sh
   dir="PATH_TO_THIS_DIR"
   package="PACKAGE_NAME"
   mail="YOUR_ADDR@YOUR.DOMAIN.COM"

   python ${dir}user-review-getter.py ${package} > ${dir}latest.data
   diff ${dir}latest.data ${dir}last.data > ${dir}diff.data
   mv ${dir}latest.data ${dir}last.data
   python ${dir}sendmail.py ${dir}diff.data ${mail}

Example:

   #!/bin/sh
   dir="/home/yasulab/user-review-notifier/"
   package="org.sorarier.whistle"
   mail="yasulab@gmail.com"

   python ${dir}user-review-getter.py ${package} > ${dir}latest.data
   diff ${dir}latest.data ${dir}last.data > ${dir}diff.data
   mv ${dir}latest.data ${dir}last.data
   python ${dir}sendmail.py ${dir}diff.data ${mail}

2. Make sure that your server can type the following commands.

- $ sendmail
- $ python
    >  import lxml

3. Check if python scripts run.

- $ python user-review-getter.py PACKAGE_NAME
- $ python sendmail.py FILENAME TO_ADDR

4. Test to run initial shell script.

- $ sh user-review-notifier.sh

5. Check your e-mail box if you got an e-mail.

6. Setup your cron to run the shell script periodically.

- $ sudo crontab -e

Example:
# m h  dom mon dow   command
0,10,20,30,40,50 * * * * /bin/sh /PATH_TO_DIR/user-review-notifier.sh >/dev/null 2>&1

7. Done! You will be able to get an e-mail if there is new reviews.


Reference:
User Review Getter from Android Market

Usage of user-review-getter.py (GitHub)
--------------------------------------------

     $ python user-review-getter.py PACKAGE_NAME

Example
-------

     $ python user-review-getter.py org.sorarier.whistle

Result
------


非常に素晴らしいアプリだと思います。 そして迅速な改善に頭が下がります。 製作者樣、ありがとうございます。by あっきー–2011/03/19
こまめな更新に、感謝感激by Gaz–2011/03/19
音量自動最大はいいんですが、元々の音量設定に戻りません。 これだと困ります。改善おねがいします。 Xperia 2.1by 陸–2011/03/19
強制終了問題解決!対応の早さに感謝!by 環境IS04–2011/03/18
ちゃんと意見を汲み上げ判断したのち反映する誠実さと、その迅速な行動力に感服しました…。 災害時のみならず、防犯上でも役に立つ。 ...by aki–2011/03/18ちゃんと意見を汲み上げ判断したのち反映する誠実さと、その迅速な行動力に感服しました…。 災害時のみならず、防犯上でも役に立つ。 できうるなら、音声(例えば自分で録音しておいたものとか)の方がより分かりやすいのだろうが。
速やかな改良、対応に頭が下がります。by Gen–2011/03/17
使用時に端末の音量設定を最大まで上げるようには出来ないのですか?by まーさん–2011/03/16
音が小さいby 沙弥香–2011/03/15
音が小さいよねby 綾子–2011/03/15
シンプルで良いと思うけど、もっと音が大きくないと…by 五月女–2011/03/14
Works on droidx. No permissions needed.by Leonard–March 13, 2011





2011-03-19

User Review Getter from Android Market



Android Marketの中の、指定されたAppのユーザレビューを取り出すプログラムを書きました。いずれRSSが登録出来るようになる(もしくは既にあるけど知らないだけ?)でしょうが、待てなかったので自分で作りました。

Usage:

     $ python user-review-getter.py PACKAGE_NAME

Ex:

     $ python user-review-getter.py org.sorarier.whistle

Result:

非常に素晴らしいアプリだと思います。 そして迅速な改善に頭が下がります。 製作者樣、ありがとうございます。by あっきー–2011/03/19
こまめな更新に、感謝感激by Gaz–2011/03/19
音量自動最大はいいんですが、元々の音量設定に戻りません。 これだと困ります。改善おねがいします。 Xperia 2.1by 陸–2011/03/19
強制終了問題解決!対応の早さに感謝!by 環境IS04–2011/03/18
ちゃんと意見を汲み上げ判断したのち反映する誠実さと、その迅速な行動力に感服しました…。 災害時のみならず、防犯上でも役に立つ。 ...by aki–2011/03/18ちゃんと意見を汲み上げ判断したのち反映する誠実さと、その迅速な行動力に感服しました…。 災害時のみならず、防犯上でも役に立つ。 できうるなら、音声(例えば自分で録音しておいたものとか)の方がより分かりやすいのだろうが。
速やかな改良、対応に頭が下がります。by Gen–2011/03/17
使用時に端末の音量設定を最大まで上げるようには出来ないのですか?by まーさん–2011/03/16
音が小さいby 沙弥香–2011/03/15
音が小さいよねby 綾子–2011/03/15
シンプルで良いと思うけど、もっと音が大きくないと…by 五月女–2011/03/14
Works on droidx. No permissions needed.by Leonard–March 13, 2011

Requirements:
- lxml

Source Code:
https://github.com/yasulab/user-review-getter-for-android-market


User Review Getterは、ただ英語と日本語のユーザレビューを取り出すだけなので、単品ではあんまり役に立たないと思いますが、他の物と組み合わせると便利になると思います。僕の場合は、User Review Getterとdiff、smtp、cronを組み合わせて、最新のユーザレビューを見つけたらメールで知らせてくれる、Notifierみたいな感じに仕上げました。もしかしたら他にも使い道があるかもしれません。

とにかく、これでいちいちAndroid Marketでユーザレビュー確認する必要が無くなり、少し効率的な生活が送れるようになりました。あとはAndroid MarketのRSS対応を待つばかりです。

追記:
User Review Notifierを作りました。

2010-12-03

How to Select a Course




留学センターから近況報告書の期限が迫っているとの連絡があり、今つらつらと近況報告書を書いています。

この報告書の中には、自由記入欄なる箇所があって、そこで大学の授業選択の仕方について書いていたら、少しエッセイっぽくなってしまいました。そこそこ一般的なことをせっかく書いたのに、留学センターの人達と未来の交換留学生達にしか読まれないと思うと癪だったので、その一部をブログの方にも転載します。何かの参考になれば幸い。

----- 以下、転載 -----

Illinois州の中では、Knox CollegeとMonmouth Collegeでリベラルアーツカレッジの雌雄を競っているらしいが、いずれにせよトップランクの大学ではないので注意すべき。もし留学する理由が勉学に励むことであれば、下記に記すTIPS(授業、学生、生活)を考慮しておくと役に立つと思う。

まず、授業の質が教授によって異なることを留意する必要がある。上述の「履修科目と授業について」の説明欄でも述べているとおり、全ての教授が熱心に授業に取り組んでいるわけではない。また、一回の交換留学では、たかだか2学期分の講義(約8講義)しか履修できない。数少ない履修授業が、質の低い教授による質の低い授業だったのでは、実のある留学生活になるわけがない。そのため、留学先で意義のある授業を取るためには、質の高い教授を自分で見定める必要がある。

教授を見定める方法として、シラバス、ウェブ、対談の3つの方法がある。1つはシラバスから判断することである。熱心な教授であるほど、シラバスに授業方針、授業内容、今後出される全ての課題・プロジェクトなどの情報を詳細に記す傾向がある。シラバスの内容が手抜きであったり、今後どのように授業が進められるかイメージしづらいシラバスであったりした場合は、教授および授業の質を疑ったほうが良い。

一方で、シラバスだけでなく、Publicationやウェブ上の情報も、教授の質を判断する良い材料となる。理工学系の教授であれば、Microsoft Academic Searchが役に立つ。教授の名前を入力することで、その教授が過去に発表した論文と、そのCitationの数が分かる。もし教授の論文のCitationの数が少なかった場合は、少なくともトップレベルの教育は受けていないと考えてよい。また、RateMyProfessorというサイトで、各大学の教授を任意の学生が評価しているので、学生の視点から見た教授像を把握することができる。今期の教授について言えば、事実にかなり即している情報が載っていたので、全く信用できない情報ではないと思う。また、大学のHPから教授のページに探したりGoogleで検索したりして、その教授の詳細な経歴(学歴と職歴)を調べても良い。いずれの方法でも、教授の素性が全く掴めなかった場合、教授の質を強く疑ったほうが良い。(言い換えれば、全く話題に上がらないほど実績の無い、名ばかりの教授である可能性が高い。)

ここまでの全てのプロセスを経ると、だいぶ情報が集まっていると思うが、しかし、意思決定に必要な最後の判断材料は、自分と教授とのフィーリングである。どんな教授でも、メールで問い合わせれば、大抵の場合、授業の内容について対談の場を設けてくれる。それまでに授業に関する情報が集め終わっていることに越したことは無いが、そうでなくとも、その教授による授業を学びたいかどうか、個人的な価値観で判断することは可能なはずである。例えば、授業に関する内容のOpen-ended Questionや、履修を考えている動機を投げかけ、教授のスタンスや主張の立て方を見ることで、その教授の能力を知ることができる。そして、その教授の能力や話し方を観察し、自分自身が納得できるのであれば、その教授の授業を是非履修するべきだろう。

個人的な仮説だが、トップレベルの大学から離れれば離れるほど、その大学内にいる優秀な教授の割合が少なくなっているのではないかと思う。しかし、トップレベルの大学であろうとなかろうと、授業はたった1人の教授によって行われる。したがって、教授が十分に優秀であれば、大学によらず、十分に質の良い授業を履修できるはずだ。例えば、Vrije Universiteit Amsterdamは世界中で有名な大学とは言えないが、しかし、その大学にいるAndrew S. Tanenbaum教授の教え子達は、コンピュータサイエンスの分野で十分な成果を出している。せっかくの留学の機会を、少なくとも自分自身が納得のできる形で終わらせたいのであれば、熟慮を重ねた授業選択が不可欠ではないかと思う。

(以下は、留学先の大学における具体的な内容なので省略。)

----- ここまで -----

2010-11-03

Virtual Zoo

以前の記事で少し述べてましたが、今週はRacket(Scheme)を使ったVirtual Zoo Projectもなんとか完成したので、連投します。

Virtual Zooとは、簡単なReal-timeペット飼育ゲームです。死に行く運命の亀さんと犬さん、そして無意味に画面を縦横無尽に飛び回るホタルを、Virtual Zooの中でどれだけ生かすことが出来るかを競います。「どれだけ生かすことができるか」と釘打ったものの、スコア機能を付け忘れたので記録することは出来ませんし、競うことも出来ません。また、DrRacketというRacket専用IDEが無いとゲームを動かせません。

これでただプログラムを公開しただけだと流石につまらないので、実際に動いてる画面をYouTubeにアップしました。参考程度にドゾ。


そんなゲームで大丈夫か?
大丈夫だ、問題ない。

以上、一発ネタでした。ごめんなさい。

- Project Description
- Racket Program (pdf)
- Virtual Zoo on GitHub




Design Portfolio




先週のProcess Simulatorに引き続き、今週はLayout & Designという授業のDesign Portfolio Projectの締め切りでした。Presentation ZenおよびZen Designを通してDesignに興味が湧いていたのがきっかけで、留学先でこの授業を取っています。学部時代にはDesignの授業なんて一切取ってなかったので、かなり新鮮な内容が盛りだくさんで面白いです。とりあえず分かったことは、プログラムを書くのと同様に、よりよいDesignについて考え始めると結構な量の脳内乳酸が発生するということです。


さて、Projectの話に戻すと、Design Portfolio Projectでは次の3つの作品を作りました。

- Flyer Ads
- Logo
- CD Package

使用したソフトはAdobe PhotoShop / InDesign / Illustrator / DreamWeaverの4つです。PhotoShopやInDesign, DreamWeaverは今までの経験と知識でなんとか使えそうではありますが、Illustratorだけは、自称絵の才能の無い人間としては四苦八苦の連続です。しかも未だに手応えが全くありません。そして上達する気配もありません。今までイラストから逃げ続けてきたツケが帰ってきた感じです。ただ、イラストが出来るようになると、個人のプロジェクトで出来る幅が結構広がるので、この機になんとか、少なくとも平均レベルぐらいまでには上げておきたいです。


なお、Design Portfolioで作った作品は次のページにて公開しています。
興味があれば覗いてみてください。

http://www.dcl.info.waseda.ac.jp/~y_yasukawa/design/index.htm

2010-10-22

Process Simulator



どうも、最近Blogの更新がご無沙汰でしたが、ちゃんと生きてます。最近、こっちの大学のOSの授業で、Simulation Exercises for Operating Systemなる面白い課題が出されたので、Process Simulatorなる作品を作りました。今回は、その公開を兼ねて投稿しています。

Process Simulatorとは、OS内のプロセスマネジメントをシミュレートするシステムです。このシステムでは、複数のプロセスが1つのCPUをどのように共有しているかを、1 clockずつ追うことができます。

各プロセスは、簡易プログラムを読み込み、そのプログラムの指示通りの計算を行います。例えば、あるプロセスが、簡易プログラム内の"B"というinstructionを実行すると、そのプロセスはRunning状態からBlocked状態に遷移します。Blocked状態に遷移したプロセスは、任意のタイミングで、Blocked状態からReady状態に遷移させることができます。

各プロセスの状態はScheduling時に影響を受けます。例えば、実行中のプロセスがPreemptedされたり、Quantumを使いきったりした場合、Ready状態のプロセスリストの中にある1つのプロセスをRunning状態に遷移させます。なお、このときのリストのアルゴリズムは、FIFO Queueです。

... (中略) ...

とまぁ、触りの部分だけちょっと説明しましたが、詳細はREADMEにばっちり書いてあるので、続きに興味のある方は下記のREADMEを読んでください。

例によって、今回もプログラム一式丸ごとGitHubの下記URLにアップロードしています。

          http://github.com/yasulab/process-simulator

GitでCloneする場合は、こんな感じでcloneできます。

          $ clone git://github.com/yasulab/process-simulator.git

なお、Mainプログラムはココから参照できます。


‥‥リファクタリング?
大丈夫だ、問題ない。


以上です。

ちなみに、来週までにVirtual Zooなる作品をSchemeで作る予定なので、ちゃんと期日までに完成していれば、来週はVirtual Zooを投稿しようと思ってます。

ではでは。




README
=======


*************************
***Process Simulator***
*************************
Author: Yohei Yasukawa
Date: 10/20/2010


Index
1. What's Process Simulator?
1.1 Command Format
1.2 Instruction Format
1.3 Scheduling Policy
1.4 Synchronizing Organization
1.5 Reference
2. How to run Process Simulator
3. Existing Problems
4. How to get latest codes
5. Sample Output


1. What's Process Simulator?
============================
This program simulates how proceses run on a CPU,
in order to understand how processes share the CPU.
It includes essentail organizations in operating systems,
such as Process and Scheduling.


1.1 Command Format
------------------
To use this simulator, you need to know some commands for handling a world of ProcessSimulator.
After starting the simulator, the input shell are appeared.
When the simulator needs inputs, you can type the following commands:

    Q: End of one unit of time
       - CPU consumes 1 instruction from programs, and execute it.
    U: Unblock the first simulated process in blocked queue
       - If there is a blocked process, move its state from Blocked to Ready.
    P: Print the current state of the system.
       - The state include PC, PID, PPID, Priority, Value, Time, etc.
    T: Terminate the system after printing the current state.
       - The printing is same as 'P' command.

*1 The capital or not does not matter (q, u, p, and t are also accepted).
*2 You can read the command description above when you type 'help'.


1.2  Instruction Format
-----------------------
When you type Q command, 1 instruction in a special program is executed.
The instruction has several types which include:
  
    S n: Set the value of integer variable to n, where n is an integer.
    A n: Add n to the value of the integer variable, where n is an integer.
    D n: Substract n from the value of the integer variable, where n is an integer.
    B: Block this simulated process.
    E: Terminate this simulated process.
    F n: Create a new simulated process. The new simulated process is an exact copy of
       the parent simulated process. The new simulated process executes from the instruction
immediately after this instruction, while the parent simulated process continues its
execution n instructions after the next instruction.
    R filename: Replace the program of the simulated process with the program in the file
       filename, and set program counter to the first instruction of this new program.

An example of a program for a simulated is as follows:
  
       S 1000
       A 19
       A 20
       D 53
       A 55
       F 1
       R file_a
       F 1
       R file_b
       F 1
       R file_c
       F 1
       R file_d
       F 1
       R file_e
       E    


1.3 Scheduling Policy
---------------------
When the simulator has more than 1 process,
processes share a CPU by the scheduling policy.

The scheduling policy used in the simulator is FIFO Queue with priority.
When context swiching happens, the process in the Ready State queue is dequeued.
Then, the process is assigned to the CPU, and it can use CPU for some ticks.
The tick the process can use is determined by the priority. The relationship
between priority and ticks are:

Priority | Quantum(ticks)
  0 |    1
  1 |    2
  2 |    4
  3 |    8

If the process are preemted by B instruction, or uses all its quantum,
the context switching happens again, and the next process in the queue will be assigned.


1.4 Synchronizing Organization
------------------------------
Process Simulation issues 2 main processes, Commander and Process Manager process.
Commander process waits a command from users, and if received it,
the process immediately sends it to Process Manager process.
Similarly, Process Manager process reports the state of processes by issueing
Reporter process. So, to communicate between processes properly,
we need synchronizing organization.

Although sleep() is famous for synchronizing, Process Simulater
synchronizes by using pipe(), which is one of the experimental challenges in this program.
The pipe synchronization is that one process sleeps on a piped file descriptor,
and another process executes particular instructions, and then, closethe piped
file descriptor. So, the sleeping process notices that the file descriptor are
no longer used, and then it stops sleeping and executes the next instruction.
As a result, pipe serves as a synchronizing organization.

You can read the specific code of the organization in main.c program.
The codes have a comment like "Pipe Synchronization".



1.5 Reference
-------------
For futher information, please read the "spec.pdf" file.



2. How to run Process Simulator?
=============================
To run the simulator, you need the following envrionment.
   - GCC version 4.2.1 (GCC version 4.* will be also satisfied.)
   - Linux (Debian is recommended, but any distritbutions should be okay.)

If you are satisfied with the environment,
you can run the simulator by typing following commands in your terminal.

   $ make clean
   $ make
   $ make run

In default configuration, 'init.prog' are read as an initial program to start.
If you want to read other program instead, please type the following command in your terminal.

   $ ./ProcessSimulator INIT_PROGRAM_NAME

Then, the targeted program determined by the command line arguemnt is read as an initial program.
The program name does not need to have ".prog" extention.



3. Existing Problems
-------------------------
In Process Simulator ver 1.0, the following problems were discovered.
Part of or all of them will be fixed in the next update.

- 1. Problem occurs when the program does not end with E instruction.
- 2. Over maximum number of process(256) stops the system.
- 3. Over maximum number of instructions in a program(64) stops the system.
- 4. Over maximum number of inputs(256) stops the system.


4. How to get latest code
-------------------------
If you would like to use latest version of Process Simulater,
please visit the following website.

    http://github.com/yasulab/process-simulator

Or, please clone the code by typing the following command.

    $ git clone git://github.com/yasulab/process-simulator.git

To clone the code, you need to install Git in advance.

Git - Fast Version Control System
http://git-scm.com/


5. Sample Output
----------------
This section explains how Process Simulator runs with an example.
We use init.prog and calc.prog as example programs.

NOTE:
init.prog and calc.prog are as follows:

init.prog:
 1: F 1
 2: R calc.prog
 3: E

calc.prog:
 1: S 1000
 2: A 20
 3: D 25
 4: A 35
 5: D 30
 6: E

To make a long behavior short,
the following is the process from start to end of the execution.

Tick | Process ID | Instruction | Memo
1.     pid=0:      F 1  pid=1 is created (Start from 2nd tick).
2.     pid=1:       R calc.prog
3.     pid=0:    E  pid=0's turn around time is 3 ticks. (3-0=3)
4.     pid=1:    S 1000
5.     pid=1:    A 20
6.     pid=1:    D 25
7.     pid=1:    A 35
8.     pid=1:    D 30
9.     pid=1:    E  pid=1's turn around time is 7 ticks. (9-2=7)

So, Average Turn Around time is 5 ticks.

The following is the output that
you can see when you actually run.

OUTPUT:
/Users/yohei/os-project% make
gcc -c main.c
gcc -o ProcessSimulator main.o
/Users/yohei/os-project% make run
make
gcc -o ProcessSimulator main.o
./ProcessSimulator init.prog
> q
> Command = q
End of one unit of time.
Instruction = 'F 1'
Create 1 new simulated process(es).
Created a process(pid=1).
Quantum was expired, so assign the first process in the que to CPU.
Pid(0)'s priority class was raised to 1.
New process was assigned to CPU.
Swithed: cpu(0) <--> pid(1)

> q
Command = q
End of one unit of time.
Instruction = 'R calc.prog'
Replace the program of the simulated process with the program in the file 'calc.prog'.
Quantum was expired, so assign the first process in the que to CPU.
Pid(1)'s priority class was raised to 1.
New process was assigned to CPU.
Swithed: cpu(1) <--> pid(0)

> q
Command = q
End of one unit of time.
Instruction = 'E'
Terminate this simulated process.
pid=0 is Terminated.
There are no process running, so assign the first process in the queue to CPU.
New process was assigned to CPU.
Assigned: cpu <--- pcbTable[1]

> q
Command = q
End of one unit of time.
Instruction = 'S 1000'
Set the value of the integer variable to 1000.
CPU value: 0 -> 1000
No ready processes, so continue to run the current process.

> q
Command = q
End of one unit of time.
Instruction = 'A 20'
Add 20 to the value of the integer variable.
CPU value: 1000 -> 1020
No ready processes, so continue to run the current process.

> q
Command = q
End of one unit of time.
Instruction = 'D 25'
Substract 25 from the value of the integer variable.
CPU value: 1020 -> 995
No ready processes, so continue to run the current process.

> q
Command = q
End of one unit of time.
Instruction = 'A 35'
Add 35 to the value of the integer variable.
CPU value: 995 -> 1030
No ready processes, so continue to run the current process.

> q
Command = q
End of one unit of time.
Instruction = 'D 30'
Substract 30 from the value of the integer variable.
CPU value: 1030 -> 1000
No ready processes, so continue to run the current process.

> q
Command = q
End of one unit of time.
Instruction = 'E'
Terminate this simulated process.
pid=1 is Terminated.
Program was successfully executed.

=== RESULT ===
*********************************************
The current system state is as follows:
*********************************************
CURRENT TIME: 9
AVERAGE TURN AROUND TIME: 5.000000.

RUNNING PROCESS:
queue is empty

BLOCKED PROCESSES:
Queue of blocked processes:
queue is empty

PROCESSES READY TO EXECUTE:
Queue of processes with priority 0:
queue is empty
Queue of processes with priority 1:
queue is empty
Queue of processes with priority 2:
queue is empty
Queue of processes with priority 3:
queue is empty
=== END OF SYSTEM ===