Ejemplo n.º 1
0
        override public void DrawArrowTo(Graphics g, Pen p, ProcessNodes Too)
        {
            Point To = Too.Location;

            if (Location.X == To.X)    //Ve duoc thang doc
            {
                if (Location.Y > To.Y) //tu duoi di len
                {
                    Location.X += 15;
                    To.X       += 15;
                    To.Y       += 30;
                    g.DrawLine(p, Location, To);
                }
                else //ve tu tren xuong
                {
                    Location.X += 15; Location.Y += 30; To.X += 15;
                    g.DrawLine(p, Location, To);
                }
            }
            else if (Location.Y == To.Y) //Ve duoc ngang
            {
                if (Location.X < To.X)   //Ve tu trai sang phai
                {
                    Location.X += 30;
                    Location.Y += 15;
                    To.Y       += 15;
                    g.DrawLine(p, Location, To);
                }
                else
                {
                    Location.Y += 15;
                    To.X       += 30;
                    To.Y       += 15;
                    g.DrawLine(p, Location, To);
                }
            }
        }
Ejemplo n.º 2
0
 public ResourceNodes(string name, ProcessNodes of)
 {
     Name   = name;
     OF     = of;
     WantMe = new List <ProcessNodes>();
 }
Ejemplo n.º 3
0
 virtual public void DrawArrowTo(Graphics g, Pen p, ProcessNodes Too)
 {
 }
Ejemplo n.º 4
0
 //Them moi tien trinh
 private void btnAddProcess_Click(object sender, EventArgs e)
 {
     if (checkTxtHaveText(txtProcess.Text.Trim().ToString()))
     {
         if (Contain(txtProcess.Text.Trim().ToString(), P))
         {
             MessageBox.Show("Tien trinh nay da ton tai");
         }
         else
         {
             bool         existed = false;
             ProcessNodes Pi      = new ProcessNodes(txtProcess.Text.Trim().ToString());
             if (checkTxtHaveText(txtResouceHave.Text.Trim().ToString()))
             {
                 string[] resHave = txtResouceHave.Text.Trim().ToString().Split(' ');
                 for (int i = 0; i < resHave.Length; i++)
                 {
                     if (Contain(resHave[i], RofP))
                     {
                         MessageBox.Show("Tai nguyen " + RofP[IndexOf(resHave[i], RofP)].Name
                                         + " da thuoc ve tien trinh " + RofP[IndexOf(resHave[i], RofP)].OF.Name);
                         RofP.RemoveRange(RofP.Count - i - 1, i);
                         existed = true;
                         break;
                     }
                     else
                     {
                         if (Contain(resHave[i], AllRes))
                         {
                             ResourceNodes Ri = AllRes[IndexOf(resHave[i], AllRes)];
                             Ri.OF = Pi;
                             Pi.Have.Add(Ri);
                             RofP.Add(Ri);
                         }
                         else
                         {
                             ResourceNodes Ri = new ResourceNodes(resHave[i], Pi);
                             Ri.OF = Pi;
                             Pi.Have.Add(Ri);
                             AllRes.Add(Ri);
                             RofP.Add(Ri);
                         }
                     }
                 }
             }
             if (!existed)
             {
                 if (checkTxtHaveText(txtResourceWant.Text.Trim().ToString()))
                 {
                     string[] resWant = txtResourceWant.Text.Trim().ToString().Split(' ');
                     for (int i = 0; i < resWant.Length; i++)
                     {
                         if (Contain(resWant[i], AllRes))
                         {
                             ResourceNodes Ri = AllRes[IndexOf(resWant[i], AllRes)];
                             Ri.WantMe.Add(Pi);
                             Pi.Want.Add(Ri);
                         }
                         else
                         {
                             ResourceNodes Ri = new ResourceNodes(resWant[i]);
                             Ri.OF = Pi;
                             Pi.Want.Add(Ri);
                             AllRes.Add(Ri);
                         }
                     }
                 }
                 P.Add(Pi);
                 MessageBox.Show("Success!");
                 txtProcess.Text      = "";
                 txtResouceHave.Text  = "";
                 txtResourceWant.Text = "";
                 txtProcess.Focus();
             }
         }
     }
     else
     {
         MessageBox.Show("Insert Process Name");
     }
 }
Ejemplo n.º 5
0
        //Tim deadlock
        private void btnFindDeadlock_Click(object sender, EventArgs e)
        {
            if (!haveDeadlock) // bien xuatHienDeadlock = null/false
            {
                haveDeadlock = false;
            }
            else
            {
                MessageBox.Show(KQ);
            }
            currentPointOfStartPeakInP = 0;

            while (!haveDeadlock && currentPointOfStartPeakInP < P.Count) //Khi chua xuat hien deadlock hoac chua duyet het cac tien trinh
            {
                MessageBox.Show("Chon dinh " + P[currentPointOfStartPeakInP].Name + " la dinh xuat phat");

                listProcessPass.Clear();
                pathResouce.Clear();
                pathProcess.Clear();
                step = 0;
                pathProcess.Add(P[currentPointOfStartPeakInP]);
                while (!haveDeadlock && step >= 0)                        //Khi chua xuat hien deadlock hoac chua quay ve vi tri node dau tien va het duong di,
                {
                    currentPointInP = IndexOf(pathProcess[step].Name, P); //
                    // Khi chua xuat hien deadlock hoac tai Mot tien tien trinh nao do van con tai nguyen ma no di toi ( Mong muon )
                    while (!haveDeadlock && currentPointInP >= 0 && P[currentPointInP].CurrentPoint < P[currentPointInP].Want.Count)
                    {
                        resWantNow = P[currentPointInP].ResWantNow();                     //lay ten cua tai nguyen ma tien trinh mong muon
                        P[currentPointInP].CurrentPoint++;                                //Tang con tro toi tai nguyen mong muon le 1
                        pathResouce.Add(resWantNow);                                      //luu vet tai nguyen
                        if (Contain(resWantNow.Name, RofP))                               //Neu tai nguyen do thuoc tien trinh nao do thi di tiep
                        {
                            nextPeak = resWantNow.OF;                                     //Lay ten cua tien trinh tiep theo
                            if (!Contain(nextPeak.Name, pathProcess))                     //Kiem tra xem tien trinh tiep theo da di qua truoc do hay chua
                            {
                                if (!Contain(nextPeak.Name, listProcessPass))             //Neu nut nay da tung di qua va bi loai thi khong di qua nua
                                {
                                    pathProcess.Add(nextPeak);                            //Them vao vet duong di : Khi dinh do chua duoc di qua, luu dinh do vao vet duong di, va tiep tuc tu dinh do di tiep toi cac tai nguyen
                                    step++;
                                    currentPointInP = IndexOf(pathProcess[step].Name, P); //Lay vi tri cua dinh tiep theo do o trong mang P, de tiep tuc xet
                                }
                            }
                            else
                            {
                                viTriDinhGayBatDauDeadlock = IndexOf(nextPeak.Name, pathProcess); //lay vi tri bat dau xuat hien deadlock
                                for (int i = viTriDinhGayBatDauDeadlock; i < pathProcess.Count; i++)
                                {
                                    KQ = KQ + pathProcess[i].Name + " ";
                                }
                                KQ = "Cac tien trinh gay ra deadlock la: " + KQ;
                                MessageBox.Show(KQ);
                                haveDeadlock = true;
                            }
                        }
                        else //Tai nguyen khong thuoc tien trinh nao
                        {
                            pathResouce.RemoveAt(pathResouce.Count - 1);
                        }
                    }
                    if (!haveDeadlock)
                    {
                        listProcessPass.Add(pathProcess[step].Name); //Them nut bi lui lai vao blackList, de loai bo cho luot sau
                        pathProcess.RemoveAt(step);                  //Lui lai mot buoc, xoa buoc lui lai khoi vet duong di
                        if (step > 0)
                        {
                            pathResouce.RemoveAt(step - 1);
                        }
                        step--;
                    }
                }
                currentPointOfStartPeakInP++;
            }
            if (!haveDeadlock)
            {
                MessageBox.Show("Khong co deadlock");
            }
        }
Ejemplo n.º 6
0
        private void btnTakeDataFromFile_Click(object sender, EventArgs e)
        {
            Refresh();
            bool           existed = false;
            OpenFileDialog ofd     = new OpenFileDialog();

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                txtFileName.Text = ofd.FileName;
                string[] lines = File.ReadAllLines(ofd.FileName);
                //lines[0] dung de khai bao cac thu
                for (int i = 1; i < lines.Length; i = i + 2)
                {
                    string[] have = lines[i].Split(' ');
                    string[] want = lines[i + 1].Split(' ');
                    if (Contain(have[0], P))
                    {
                        MessageBox.Show("Tien trinh " + have[0] + " tai dong " + (i) + " trung voi dong " + (IndexOf(have[0], P) * 2 + 2));
                        existed = true;
                        break;
                    }
                    else if (Contain(want[0], P))
                    {
                        MessageBox.Show("Tien trinh " + want[0] + " tai dong " + (i + 1) + " trung voi dong " + (IndexOf(want[0], P) * 2 + 2));
                        existed = true;
                        break;
                    }
                    else
                    {
                        existed = false;
                        ProcessNodes Pi = new ProcessNodes(have[0]);
                        if (have.Length > 1) //Khong tinh have[0] la ten tien trinh
                        {
                            for (int j = 1; j < have.Length; j++)
                            {
                                if (Contain(have[j], RofP))
                                {
                                    MessageBox.Show("Tai nguyen " + have[j] + " da thuoc ve tien trinh "
                                                    + RofP[IndexOf(have[j], RofP)].OF.Name + " tai dong "
                                                    + (IndexOf(have[0], P) * 2 - 1));
                                    existed = true;
                                    break;
                                }
                                else
                                {
                                    if (Contain(have[j], AllRes))
                                    {
                                        ResourceNodes Ri = AllRes[IndexOf(have[j], AllRes)];
                                        Ri.OF = Pi;
                                        Pi.Have.Add(Ri);
                                        RofP.Add(Ri);
                                    }
                                    else
                                    {
                                        ResourceNodes Ri = new ResourceNodes(have[j], Pi);
                                        Ri.OF = Pi;
                                        Pi.Have.Add(Ri);
                                        AllRes.Add(Ri);
                                        RofP.Add(Ri);
                                    }
                                }
                            }
                        }
                        if (!existed)
                        {
                            if (want.Length > 1)
                            {
                                for (int j = 1; j < want.Length; j++)
                                {
                                    if (Contain(want[j], AllRes))
                                    {
                                        ResourceNodes Ri = AllRes[IndexOf(want[j], AllRes)];
                                        Ri.WantMe.Add(Pi);
                                        Pi.Want.Add(Ri);
                                    }
                                    else
                                    {
                                        ResourceNodes Ri = new ResourceNodes(want[j]);
                                        Ri.OF = Pi;
                                        Pi.Want.Add(Ri);
                                        AllRes.Add(Ri);
                                    }
                                }
                            }
                            P.Add(Pi);
                        }
                        else
                        {
                            MessageBox.Show("Nap du lieu khong thanh cong!");
                            break;
                        }
                    }
                }
                if (!existed)
                {
                    MessageBox.Show("Doc du lieu thanh cong!");
                }
            }
        }