Planifikimi i proceseve (angl. process scheduling) eshte aktiviteti i planifikimit, i vendosjes ne ekzekutim te proceseve, i kalimit te tyre ne gjendje pritje per tia dhene CPU nje procesi tjeter bazuar ne disa rregulla.
"Process scheduling" eshte nje pjese thelbesore e sistemeve operative Multiprogramming. Keto sistemie lejojne qe te ekzekutohen disa procese ne te njejten kohe. Proceset ngarkohen ne memorjen kryesore dhe me pas ato kalohen me rradhe ne ekzekutim ne CPU duke ndare kohen e CPU midis tyre.
Kategorite e teknikave te skedulimit
Teknikat e skedulimit te proceseve ndahen ne dy kategori :
Non-preemptive: Nese perdoret kjo teknike skedulimi, burimi nuk mund te merret nga procesi deri sa ai te perfundoje ekzekutimin. Kalimi i burimeve tek nje proces tjeter ndodh vetem kur procesi perfundon ose eshte ne gjendje pritje.
Preemptive: Kjo teknike parshikon qe busimet i vihen ne dispozicion nje procesi per nje sasi kohe te paracaktuar. Gjate venies ne dispozicion te burimeve procesi kalon nga gjendja ne 🏃ekzekutim ose 🏁gati ne gjendjen ne pritje 🫷. Kalimi i burimit nga nje proces tek tjetri mund te ndodhe per shkak te prioritetit me te larte qe mund te kene proceset e tjere. Ne kete rast sistemi operativ i le per me shume kohe ne dispozicion burimin proceseve me prioritare.
SIstemi operativ perdor politika te ndryshme per te menaxhuar rradhet, der te cilat:
FIFO,
Round Robin,
Priority,
etj
Skedulatori i proceseve percakton si ti levize proceset midis rradheve. Diagrama me lart ilustron kete proces pune.
Sistemi operativ perdor rradhet per te ruajtur bloqet e kontrollit te proceseve (PCB - Process Control Blocks).
Proceset qe jane ne te njejtin status mbahen ne te njejten rradhe.
Kur ndryshon statusi i nje procesi PCB e tij hiqet nga rradha ku ndodhet dhe kalohet ne rradhen e re sipas statusit te ri te procesit.
Sistemi operativ mban rradhet e meposhtme:
Job queue − Kjo rradhe mban te gjithe proceset e sistemit
Ready queue − Kjo rradhe mban proceset qe jane ne memorje ne gjendjen gati (ready) dhe ne pritje (waiting ). Proceset e reja vendosen fillimisht ne kete rradhe.
Device queues − Ne kete rradhe vendosen proceset qe jane blokuar per shkak te pritjes per nje veprim hyrje/dalje nga nje pajisje.
//using Processes;
using System;
using System.Diagnostics;
using System.Threading;
class Program
{
static void Main(string[] args)
{
string s;
Console.WriteLine("Current Process Id" + Process.GetCurrentProcess().Id +"Starting the program...");
Process process = new Process();
process.StartInfo.FileName = "notepad.exe";
process.StartInfo.Arguments = "test.txt";
process.Start();
Console.WriteLine("Process started...");
Console.WriteLine("ProcessId: " + process.Id.ToString());
Console.WriteLine("ProcessName: "+process.ProcessName);
Console.WriteLine("Filename: " + process.MainModule.FileName);
Console.WriteLine("MachineName: " + process.MachineName);
Console.WriteLine("MainWindowHandle: " + process.MainWindowHandle);
Console.WriteLine("MainWindowTitle: " + process.MainWindowTitle);
Console.WriteLine("Threads in the process: " + process.Threads.Count.ToString());
Console.ReadLine();
}
}
using System;
using System.Diagnostics;
using System.Threading;
class Program
{
static void Main()
{
string s;
Process.GetCurrentProcess().Id); gets the id of the running process
Console.WriteLine("Current process ID: " + Process.GetCurrentProcess().Id);
Console.WriteLine("Current process ID: " + Process.GetCurrentProcess().MainModule.FileName);
Console.Write("Do you want to start a new instance? (yes/no): ");
// instansiate a new process with the current running process
string input = Console.ReadLine();
Console.WriteLine(input + " at process " + Process.GetCurrentProcess().Id);
while (input?.ToLower() == "yes")
{
// Process.GetCurrentProcess().MainModule.FileName; get the current exe path
string exePath = Process.GetCurrentProcess().MainModule.FileName; // Get the current exe path
Process.Start(exePath); // Start a new instance
Console.WriteLine("New instance started from:" + Process.GetCurrentProcess().Id);
input = Console.ReadLine();
}
s = "continue";
while (s != "close")
{
Console.WriteLine("Msg from Process :" + Process.GetCurrentProcess().Id + ": Press close to exit.");
s = Console.ReadLine();
}
Console.WriteLine("Closing process..." + Process.GetCurrentProcess().Id);
}
}
Vini re nevojen per te ridrejtuar I/O e proceseve.
using Processes;
using System;
using System.Diagnostics;
using System.Threading;
class Program
{
static void Main()
{
Thread myThread = new Thread(DoWork);
myThread.Start();
}
static void DoWork()
{
// Your code goes here
string msg;
Console.WriteLine("Hello, World!");
msg = Console.ReadLine();
Console.WriteLine(msg);
}
}
Shembulli qe mbledh 10 numra ne sekuencial dhe ne paralel, duke krijuar thread per secilin numer
using System;
using System.Diagnostics;
using System.Threading;
class Program
{
static void Main()
{
MultiThreads multiThreads = new MultiThreads();
}
}
using System;
using System.Diagnostics;
using System.Threading;
class MultiThreads
{
public MultiThreads()
{
string s;
Console.WriteLine("MultiThreads");
int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int sum = 0;
Console.WriteLine("summing numbers sequentialy");
Stopwatch stopwatch = Stopwatch.StartNew();
foreach (var number in numbers)
{
sum += ProcessNumber(number);
}
stopwatch.Stop();
Console.WriteLine($"Sequential Sum: {sum}, Time taken: {stopwatch.ElapsedMilliseconds} ms");
s=Console.ReadLine();
sum = 0;
stopwatch.Reset();
stopwatch.Start();
Console.WriteLine("summing numbers in parallel");
// Parallel Calculation
Parallel.ForEach(numbers, number =>
{
sum += ProcessNumber(number);
});
stopwatch.Stop();
Console.WriteLine($"Parallel Sum: {sum}, Time taken: {stopwatch.ElapsedMilliseconds} ms");
s = Console.ReadLine();
}
static int ProcessNumber(int number)
{
Thread.Sleep(TimeSpan.FromMilliseconds(100));
Console.WriteLine(number.ToString());
return number;
}
}
Exekutojeni dhe vereni kohen e ekzekutimit per mbkedhjen sekuenciale dhe per mbledhjen ne paralel.