Professional Documents
Culture Documents
-1الثريدات تحقق توازي حقيقي في بيئة متعددة المعالجات ،هذا يعني أن الثريدات يمكن
أن تعمل أكثر من عملية بشكل متوازي.
-2زيادة استجابة واجهة المستخدم ،وذلك الن الثريد الرئيسي يستمر في معالجة احداث
لوحة المفاتيح والماوس والثريدات الفرعية تقوم بعمليات أخرى.
-3تحقق الثريدات استخدام أفضل لوحدة المعالجة المركزية ،أي أنه عندما ينتظر أحد
الثريدات لالستجابة من العتاد المادي او أي حدث يقوم الثريد االخر باالستمرار في
التنفيذ.
-4تمييز المهام ذات األولويات المختلفة ،أي أن الثريد األعلى أولوية ينفذ المهمة األكثر
أهمية والثريد األقل أولوية ينفذ المهمة األقل أهمية.
العيوب-:
?12\What are the priority value used for scheduling a thread in c#
السؤال :ماهي قيم األولويات المستخدمة في جدولة الثريدات؟
الجواب :هنالك خمس قيم او خمس أولويات للقيام بعملية الجدولة للثريدات وهي:
1- Highest:- take the value 4
2- Above Normal:- take the value 3
3- Normal:- take the value 2
4- Below Normal:- take the value 1
5- Lowest:- tack the value 0
773133872 أكـــرم عبدالكريم لقــمان/ أ:تم مراجعته من قبل
ويمكن تغيير أولوية الثريد عن طريقNormal القيمة االفتراضية لثريد عند إنشاءه هي
-: بالطريقة التاليةPriority استخدام الخاصية
T1.Priority = ThreadPriority.Highest;
. هو أعلى أولوية من باقي الثريداتHighest والثريد الذي يمتلك القيمة
:ولطباعة قيمة أولوية الثريد نستخدم الطريقة التالي
Console.WriteLine(T1.Priority(;
.Highest ناتج السطر البرمجي هي طباعة القيمة
.Priority والمثال التالي يوضح طريقة استخدام الخاصية
.Normal لم نعطي له أولوية لذلك سوف يحتفظ بالقيمة االفتراضية وهيt5 الحظ ان الثريد
using System;
using System.Threading;
namespace priorityThread
{
class Program
{
public static void fun1()
{
Console.WriteLine(Thread.CurrentThread.Name+" is started");
Console.WriteLine(Thread.CurrentThread.Name+" has Priority
" +Thread.CurrentThread.Priority+"\n");
Thread.Sleep(200);
}
static void Main(string[] args)
{
Console.WriteLine("Main Thread Started");
Thread t1 = new Thread(fun1);
Thread t2 = new Thread(fun1);
Thread t3 = new Thread(fun1);
Thread t4 = new Thread(fun1);
Thread t5 = new Thread(fun1);
t1.Name = "Thread 1";
t2.Name = "Thread 2";
t3.Name = "Thread 3";
773133872 أكـــرم عبدالكريم لقــمان/ أ:تم مراجعته من قبل
t4.Name = "Thread 4";
t5.Name = "Thread 5";
t1.Priority = ThreadPriority.Highest;
t2.Priority = ThreadPriority.AboveNormal;
t3.Priority = ThreadPriority.BelowNormal;
t4.Priority = ThreadPriority.Lowest;
t1.Start();
t2.Start();
t3.Start();
t4.Start();
t5.Start();
t5.Join();
السؤال :من خالل دروة حياة الثريد ،ما هي الحاالت الممكنة للثريد؟ الجواب:
1- Unstarted
2- Running
3- WaitSleepJoin
4- Suspended
5- Aborted
6- Stopped
?30\ What is time slicing
السؤال :ماذا يكون تقطيع الوقت؟
الجواب :هي تقنية يستخدمها نظام التشغيل لجدولة الثريدات حيث يقوم بتوفير مدة قصيرة
قد تكون ثالث ثواني مثال لكل ثريد ،ويجعل الثريدات تعمل بالتناوب على ذلك الوقت ،أي
مثال لدينا اثنان من الثريدات رقم 1واخر رقم 2يقوم األول بتنفيذ جزء من المهمة في
تم مراجعته من قبل :أ /أكـــرم عبدالكريم لقــمان 773133872
ظرف ال 3ثواني ثم عندما ينقضي الوقت يتوقف ليقوم الثاني بالجزء الخاص به في نفس
المدة التي حددها المجدول وهكذا كل ثريد يتوفر على وقت قصير يقوم بجزء من المهمة
فقط حتى ينهي االثنان مهمتهما كاملة.
اكتب برنامج يحتوي على أربعة ثريدات لعرض مشكلة اإلقفال الميت
؟mutex وmonitor باستخدام
using System;
using System.Threading;
namespace FirstThread
{
class Program
{
public static object ob1 = new object();
public static object ob2 = new object();
public static object ob3 = new object();
public static object ob4 = new object();
public static Mutex mutex = new Mutex();
public static void fun1()
{
Monitor.Enter(ob1);
Thread t = Thread.CurrentThread;
Console.WriteLine(t.Name + " got lock ob1 ");
Thread.Sleep(500);
Monitor.Enter(ob2);
Console.WriteLine(t.Name + " got lock ob2 ");
mutex.ReleaseMutex();
Monitor.Exit(ob1);
}
public static void fun2()
{
Monitor.Enter(ob2);
Thread t = Thread.CurrentThread;
Console.WriteLine(t.Name + " got lock ob2 ");
Thread.Sleep(500);
Monitor.Enter(ob1);
Console.WriteLine(t.Name+" got lock again ob1 ");
// Monitor.Exit(ob1);
mutex.ReleaseMutex();
Monitor.Exit(ob2);
}
public static void fun3()
{
Monitor.Enter(ob3);
Thread t = Thread.CurrentThread;
Console.WriteLine(t.Name + " got lock ob3 ");
Thread.Sleep(500);
Monitor.Enter(ob4);
Console.WriteLine(t.Name + " got lock ob4 ");
// Monitor.Exit(ob4);
mutex.ReleaseMutex();
Monitor.Exit(ob3);
}
773133872 أكـــرم عبدالكريم لقــمان/ أ:تم مراجعته من قبل
public static void fun4()
{
Monitor.Enter(ob4);
Thread t = Thread.CurrentThread;
Console.WriteLine(t.Name + " got lock ob4 ");
Thread.Sleep(500);
Monitor.Enter(ob3);
Console.WriteLine(t.Name + " got lock ob3 ");
mutex.ReleaseMutex();
// Monitor.Exit(ob4);
mutex.ReleaseMutex();
}
static void Main(string[] args)
{
Thread th = Thread.CurrentThread;
Console.WriteLine("Main Thread Started\n");
Thread t1 = new Thread(fun1);
Thread t2 = new Thread(fun2);
Thread t3 = new Thread(fun3);
Thread t4 = new Thread(fun4);
t1.Name = "Thread 1";
t2.Name = "Thread 2";
t3.Name = "Thread 3";
t4.Name = "Thread 4";
t1.Start(); t2.Start();
t3.Start(); t4.Start();
using System;
using System.Threading;
namespace FirstThread
{
class Program
{
public static object ob1 = new object();
public static object ob2 = new object();
public static object ob3 = new object();
public static object ob4 = new object();
using System;
using System.Threading;
namespace FirstThread
{
class Program
{
public static object ob1 = new object();
public static object ob2 = new object();
public static object ob3 = new object();
public static object ob4 = new object();
public static void fun1()
{
Monitor.Enter(ob1);
773133872 أكـــرم عبدالكريم لقــمان/ أ:تم مراجعته من قبل
Thread t = Thread.CurrentThread;
Console.WriteLine(t.Name +" got lock ob1 ");
Thread.Sleep(500);
Monitor.Enter(ob2);
Console.WriteLine(t.Name + " got lock ob2 ");
Monitor.Exit(ob2);
Monitor.Exit(ob1);
}
public static void fun2()
{
Monitor.Enter(ob2);
Thread t = Thread.CurrentThread;
Console.WriteLine(t.Name + " got lock ob2 ");
Thread.Sleep(500);
Monitor.Enter(ob1);
Console.WriteLine(t.Name + " got lock ob1 ");
Monitor.Exit(ob1);
Monitor.Exit(ob2);
}
public static void fun3()
{
Monitor.Enter(ob3);
Thread t = Thread.CurrentThread;
Console.WriteLine(t.Name + " got lock ob3 ");
Thread.Sleep(500);
Monitor.Enter(ob4);
Console.WriteLine(t.Name + " got lock ob4 ");
Monitor.Exit(ob4);
Monitor.Exit(ob3);
}
public static void fun4()
{
Monitor.Enter(ob4);
Thread t = Thread.CurrentThread;
773133872 أكـــرم عبدالكريم لقــمان/ أ:تم مراجعته من قبل
Console.WriteLine(t.Name + " got lock ob4 ");
Thread.Sleep(500);
Monitor.Enter(ob3);
Console.WriteLine(t.Name + " got lock ob3 ");
Monitor.Exit(ob3);
Monitor.Exit(ob4);
}
static void Main(string[] args)
{
Thread th = Thread.CurrentThread;
Console.WriteLine("Main Thread Started\n");
Thread t1 = new Thread(fun1);
Thread t2 = new Thread(fun2);
Thread t3 = new Thread(fun3);
Thread t4 = new Thread(fun4);
t1.Name = "Thread 1"; t2.Name = "Thread 2";
t3.Name = "Thread 3"; t4.Name = "Thread 4";
t1.Start();
t2.Start();
t3.Start();
t4.Start();
// Console.WriteLine("Main Thread finshed");
Console.ReadKey();
}
}
}
t1.Start(); t2.Start();
t3.Start(); t4.Start(); //
Console.WriteLine("Main Thread finshed");
Console.ReadKey();
}
}
}
اكتب برنامج يحتوي على ثريدين ليعرض مشكلة اإلقفال الميت باستخدام
؟monitor ال
using System;
using System.Threading;
namespace FirstThread
{
class Program
{
public static object ob1 = new object();
public static object ob2 = new object();
public static Mutex mutex = new Mutex();
public static void fun1()
{
// mutex.WaitOne();
Monitor.Enter(ob1);
773133872 أكـــرم عبدالكريم لقــمان/ أ:تم مراجعته من قبل
Thread t = Thread.CurrentThread;
Console.WriteLine(t.Name + " got lock ob1 ");
Thread.Sleep(500);
// mutex.WaitOne();
Monitor.Enter(ob2);
// mutex.ReleaseMutex();
Console.WriteLine(t.Name + " got lock agine ob2 ");
// mutex.ReleaseMutex();
Monitor.Exit(ob2);
Monitor.Exit(ob1);
}
public static void fun2()
{
// mutex.WaitOne();
Monitor.Enter(ob2);
Thread t = Thread.CurrentThread;
Console.WriteLine(t.Name + " got lock ob1 ");
Thread.Sleep(500);
// mutex.WaitOne();
Monitor.Enter(ob1);
// mutex.ReleaseMutex();
Console.WriteLine(t.Name + " got lock agine ob2 ");
// mutex.ReleaseMutex();
Monitor.Exit(ob1);
Monitor.Exit(ob2);
}
static void Main(string[] args)
{
Thread th = Thread.CurrentThread;
Console.WriteLine("Main Thread Started\n");
Thread t1 = new Thread(fun1);
Thread t2 = new Thread(fun2);
t1.Name = "Thread 1"; t2.Name = "Thread 2";
t1.Start(); t2.Start();
// Console.WriteLine("Main Thread finshed");
773133872 أكـــرم عبدالكريم لقــمان/ أ:تم مراجعته من قبل
Console.ReadKey();
}
} }
5\ Write a program include two threads to show the
problem Of the deadlock using mutex?
اكتب برنامج يحتوي على ثريدين ليعرض مشكلة اإلقفال الميت باستخدام:السؤال
؟mutex ال-: Momitor وMutex باستخدام ال
using System;
using System.Threading;
namespace FirstThread
{
class Program
{
public static object ob1 = new object();
public static object ob2 = new object();
public static Mutex mutex = new Mutex();
public static void fun1()
{
Monitor.Enter(ob1);
Thread t = Thread.CurrentThread;
Console.WriteLine(t.Name +" got lock ob1 ");
Thread.Sleep(500);
Monitor.Enter(ob2);
mutex.ReleaseMutex();
Console.WriteLine(t.Name + " got lock ob2 ");
mutex.ReleaseMutex();
}
public static void fun2()
{
Monitor.Enter(ob2);
Thread t = Thread.CurrentThread;
Console.WriteLine(t.Name + " got lock ob2 ");
773133872 أكـــرم عبدالكريم لقــمان/ أ:تم مراجعته من قبل
Thread.Sleep(500);
Monitor.Enter(ob1);
mutex.ReleaseMutex();
Console.WriteLine(t.Name + " got lock ob1 ");
mutex.ReleaseMutex();
}
static void Main(string[] args)
{
Thread th = Thread.CurrentThread;
Console.WriteLine("Main Thread Started\n");
Thread t1 = new Thread(fun1);
Thread t2 = new Thread(fun2);
t1.Name = "Thread 1";
t2.Name = "Thread 2";
t1.Start();
t2.Start();
t2.Join();
Console.WriteLine("Main Thread finshed");
Console.ReadKey();
}
}
}
lock باستخدام
using System;
using System.Threading;
namespace FirstThread
{
class Program
{
public static object ob1 = new object();
public static object ob2 = new object();
public static Mutex mutex = new Mutex();
773133872 أكـــرم عبدالكريم لقــمان/ أ:تم مراجعته من قبل
public static void fun1()
{
// mutex.WaitOne();
lock (ob1)
{
{
Thread t = Thread.CurrentThread;
using System;
using System.Threading;
namespace FirstThread
{
773133872 أكـــرم عبدالكريم لقــمان/ أ:تم مراجعته من قبل
class Program
{
static void show ()
{
Thread t = Thread.CurrentThread;
for (int i = 0; i <5; i++)
{
Console.WriteLine(t.Name + " Started");
Console.WriteLine(t.Name+" Sleep for one Second");
Thread.Sleep(1000);
}
Console.WriteLine("\n");
}
static void Main(string[] args)
{
Thread th = Thread.CurrentThread;
Console.WriteLine("Main Thread Started\n");
Thread T1 = new Thread(show);
Thread T2 = new Thread(show);
using System;
using System.Threading;
namespace FirstThread
{
class Program
{
static void show ()
{
Thread t = Thread.CurrentThread;
for (int i = 0; i <5; i++)
{
Console.WriteLine(t.Name + " Started");
Console.WriteLine(t.Name+" Sleep for one Second");
Thread.Sleep(1000);
}
Console.WriteLine("\n");
}
static void Main(string[] args)
{
Thread th = Thread.CurrentThread;
Console.WriteLine("Main Thread Started\n");
Thread T1 = new Thread(show);
Thread T2 = new Thread(show);
T1.IsBackground = true;
T2.IsBackground = true;
T1.Start();
T2.Start();
T1.IsBackground = true;
//Background Thread
T2.IsBackground = false;
//foreground Thread
T1.Start();
T2.Start();
T1.IsBackground = true;
T2.IsBackground = true;
T1.Start();
T2.Start();
Console.WriteLine("Main Thread finshed");
Console.ReadKey();
}
}
}
using System;
773133872 أكـــرم عبدالكريم لقــمان/ أ:تم مراجعته من قبل
using System.Threading;
namespace FirstThread
{
class Program
{
static object ob = new object();
public static void lock1()
{
lock (ob)
{
Thread t = Thread.CurrentThread;
for (int i = 0; i < 5; i++)
{
Console.WriteLine(t.Name + " Started running");
Console.WriteLine(t.Name + " wait for second");
Thread.Sleep(1000);
}
Console.WriteLine("\n");
}
}
static void Main(string[] args)
{
Thread th = Thread.CurrentThread;
Console.WriteLine("Main Thread Started\n");
Thread t1 = new Thread(lock1);
Thread t2 = new Thread(lock1);
Thread t3 = new Thread(lock1);
mutex?
؟mutex اكتب برنامج ليعرض عملية المزامنة باستخدام الكلمة
using System;
using System.Threading;
namespace FirstThread
{
773133872 أكـــرم عبدالكريم لقــمان/ أ:تم مراجعته من قبل
class Program
{
static object ob1 = new object();
static Mutex mutex = new Mutex();
using System;
using System.Threading;
namespace FirstThread
{
class Program
{
static object ob1 = new object();
public static void locked ()
{
Monitor.Enter(ob1);
{
Thread t = Thread.CurrentThread;
for (int i = 0; i < 5; i++)
{
Console.WriteLine(t.Name + " Started lock");
Console.WriteLine(t.Name + " lock ob1");
Thread.Sleep(1000);
}
Console.WriteLine(t.Name + " unlock ob1");
Console.WriteLine("\n");
Monitor.Exit(ob1);
}
}
static void Main(string[] args)
773133872 أكـــرم عبدالكريم لقــمان/ أ:تم مراجعته من قبل
{
Thread th = Thread.CurrentThread;
Console.WriteLine("Main Thread Started\n");
Thread t1 = new Thread(locked);
Thread t2 = new Thread(locked);
t1.Name = "Thread 1";
t2.Name = "Thread 2";
t1.Start(); t2.Start();
t2.Join();
using System;
using System.Threading;
namespace secondThread
{
class Program
{
static void print ()
773133872 أكـــرم عبدالكريم لقــمان/ أ:تم مراجعته من قبل
{
Console.WriteLine("child Thread Started");
for(int i = 1; i <= 5; i++)
{
Console.WriteLine(" the child Thread Running");
}
}
static void Main(string[] args)
{
Thread th = Thread.CurrentThread;
Console.WriteLine("Main Thread Started");
Thread T = new Thread(print);
Console.WriteLine("ChildThread state is :" +T.ThreadState);
T.Start();
Console.WriteLine("ChildThread state is :" +T.ThreadState);
T.Join();
Console.WriteLine("ChildThread state is :" +T.ThreadState);