Linux Process Scheduling Simulation

Source: Internet
Author: User

/* Simulate the Static Priority Algorithm for LINUX Process Scheduling and the time slice Rotation Algorithm to introduce the LINUX Scheduling
*/
# Include
# Include
# Include
# Include
# Include
# Define RUN 1
# Define SLEEP 0
# Define READY 2
# Define DEG_SCHEDULE
# Define NUM 6
Struct OSPCB
{
Int worker name;/* process name */
Int ReqCount;/* process execution count */
Int RunTime;/* Number of process execution times */
Int Prority;/* process priority */
Int ready status;/* Process status */
Int timeout time;/* process time slice */
Struct OSPCB * prev;
Struct OSPCB * next;
};
Struct ProcessQueue/* simulate CPU scheduling queue */
{
Struct OSPCB * PointerHead;/* point to the process linked list header */
Int Worker Number;/* CPU scheduling counter */
};
// Static struct CriticalResource
//{
// Int flag;
// Char BufferVoice [2000];
//}
Static int flag;
Void * Function (int * arg );
Void InitPcb (struct OSPCB * pcb );
Int Schedule (struct ProcessQueue * queue );
Void InheritSchedule (struct OSPCB * pcb );
Int main (void)
{
Int I, ret;
Struct OSPCB * pNewPcb, * pNew;
Struct ProcessQueue * pNewQueue;
Int a [4] [4] = }};
PNewQueue = (struct ProcessQueue *) malloc (sizeof (struct ProcessQueue ));
PNewQueue-> PointerHead = NULL;
PNewQueue-> queue number = 0;
For (I = 0; I <4; I ++)/* process initialization */
{
PNewPcb = (struct OSPCB *) malloc (sizeof (struct OSPCB ));
PNewPcb-> connector name = a [I] [0];
PNewPcb-> ReqCount = a [I] [1];
PNewPcb-> RunTime = a [I] [2];
PNewPcb-> Prority = a [I] [3];
PNewPcb-> prepare status = READY;
PNewPcb-> runtime time = 3;
InitPcb (pNewPcb );
If (pNewQueue-> PointerHead = NULL)
{
PNewQueue-> PointerHead = pNewPcb;
} Else {
PNew-> next = pNewPcb;
PNewPcb-> prev = pNew;
}
PNew = pNewPcb;
PNewQueue-> queue number ++;
}
# If 0
For (p = pNewQueue-> PointerHead; p! = NULL; p = p-> next)
{
Printf ("process name = % d \ n", p-> process name );
}
# Endif
Schedule (pNewQueue);/* enter process scheduling */
Return 0;
}
Void InitPcb (struct OSPCB * pcb)
{
Pcb-> prev = NULL;
Pcb-> next = NULL;
}
Int Schedule (struct ProcessQueue * queue)/* process scheduling */
{
Struct OSPCB * pcb, * CurrRun;
Int value, SechNumber = 8;
Pthread_t pthread_id [NUM];
Int I = 0;
// Printf ("% s \ n" ,__ FUNCTION __);
For (pcb = queue-> PointerHead; pcb! = NULL; pcb = pcb-> next)
{
If (pcb-> memory time = 0)
{
Pcb-> Prority + = 4;
}
Pcb-> runtime time = 3;
}
While (queue-> PointerHead! = NULL)
{
For (pcb = queue-> PointerHead; pcb! = NULL; pcb = pcb-> next)
{
If (pcb = queue-> PointerHead)
{
CurrRun = pcb;
} Else {
If (CurrRun-> Prority <pcb-> Prority)
CurrRun = pcb;
}
CurrRun-> running status = RUN;
}
SechNumber --;
CurrRun-> ReqCount --;
CurrRun-> runtime time --;
If (I! = (CurrRun-> your name ))
{
I = CurrRun-> your name;
Pthread_create (& pthread_id [I], NULL, (void *) Function, & (CurrRun-> role name ));
}
# Ifdef DEG_SCHEDULE
Printf ("present process = % d CurrRun-> ReqCount = % d \ n", CurrRun-> role name, CurrRun-> ReqCount );
# Endif
If (CurrRun-> runtime time = 0)
{
CurrRun-> Prority-= 4;/* process punitive priority reduction processing */
}
If (CurrRun-> ReqCount = 0)
{
If (CurrRun = queue-> PointerHead)
{
Queue-> PointerHead = CurrRun-> next;
} Else if (CurrRun-> next! = NULL ){
CurrRun-> prev-> next = CurrRun-> next;
CurrRun-> next-> prev = CurrRun-> prev;
} Else {
CurrRun-> prev-> next = NULL;
}
// Printf ("Run process name = % d Reqcount = % d Sechedule count = % d \ n", CurrRun-> role name, CurrRun-> ReqCount, SechNumber );
}
If (SechNumber = 0)/* reschedule when time slice is used up */
{
Schedule (queue );
}
}
Return 0;
}
Void SleepProcess (void)
{
}
Void DeleteProcess (void)
{
// Return 0;
}
Void * Function (int * arg)/* process execution Function */
{
Int I, count = 0;
Int PthreadName;
Struct OSPCB * pNew;
PNew = (struct OSPCB *) arg;
PthreadName = * (int *) arg;
# Ifdef DEG_SCHEDULE
Printf ("Enter the function process % d \ n", PthreadName );
# Endif
While (1)
{
If (flag = 0)/* access critical zone resources */
{
Flag = 1;
# Ifdef DEG_SCHEDULE
Printf ("get lcok in process % d \ n", PthreadName );
# Endif
For (I = 0; I <10000; I ++)
{
PNew-> running status = SLEEP;
Sleep (1 );
}
Flag = 0;/* release critical zone resources */
Break;
} Else {/* spin until an accessible critical zone resource is obtained */
Count ++;
If (count = 5000)
{
# Ifdef DEG_SCHEDULE
Printf ("flag = % d can not get lock in process name % d \ n", flag, PthreadName );
# Endif
}
}
}
# Ifdef DEG_SCHEDULE
Printf ("flag = % d out process % d \ n", flag, PthreadName );
# Endif
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.