組み込みシステムの実行をシミュレートする C コードを書いています。イベントは特定の数のデバイスから発生し、優先度 0 ~ 3 が与えられます。提供する必要があるイベントの優先度は、主にデバイス番号 (デバイス 0 > デバイス 3 > デバイス 7) に基づいており、デバイスごとに提供されるイベントの優先度は、提供された優先度に基づいています。
このシステムを実装するには、おそらく 2D キューが最適な方法であるという結論に達しました。for ループを含む配列は「無駄」と見なされ、ほとんどの Function Queue スケジューリングでは while(notEmpty) などのループが使用されます。
私の大まかな計画は、次のような 2 つの「ノード」構造体を作成することです。
struct events{
Event curEvent; //the "node"
struct events *next; //leads to event of lower priority
};
struct devices { //a linked list of Devices
int deviceNumber; //the "node"
struct devices *next;//leads to next device in order 0->max
struct events *headEevent; //a linked list of events per device
};
サービスを提供するデバイスの数と、デバイスごとのイベントの最大数がコマンド ラインから提供されます。
私の質問は二重だと思います。1つ目は、私は正しい軌道に乗っていますか? 2 番目の (そしておそらくもっと重要な) 質問は、「この 2D キューを初期化する最善の方法と、発生したイベントをキューに入れる最善の方法は何か?」です。
現在、私の初期化コードが間違っています。
curDevice = (struct device * ) malloc (sizeof (struct device));
deviceHead = curDevice;//sets head to first that's initialized
for (i = 0; i< Number_Devices; i++) {
curDevice -> deviceNumber = i;
curEvent = (struct event * ) malloc (sizeof (struct event));
curDevice -> headEvent = curEvent; //sets head event to the empty curEvent
for (j = 0; j<Q; j++) { //Q is max queue size
newEvent = (struct event* ) malloc (sizeof struct event));
curEvent -> next = newEvent;
curEvent = newEvent;
}
new_device = (struct device * ) malloc (sizeof (struct device));
curDevice -> next = newDevice;
curDevice = newDevice;
}
自分でエンキューする方法を発見できると思います。while ループを使用してリンクされたリストを繰り返し処理し、現在のイベントが個々のデバイスのスタックにあるものよりも優先度が高い場合は、それを一番上にプッシュします。
これは長くて複雑な質問ですので、不明な点があれば遠慮なくお尋ねください。前もって感謝します!