Click here to Skip to main content
15,844,498 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Is there any one can help provide the source code for RealTime scheduler in operating system, implemented in C++, Thanks. so appreciated.
Sergey Alexandrovich Kryukov 17-Nov-12 22:23pm    
Not really a question. If there is one, what this person should write to you? The implementation of the Scheduler, or what?
Kevin Stlip 17-Nov-12 22:34pm    
yes i want to have a look at the source code of implementing of the RealTime Scheduler.
Sergey Alexandrovich Kryukov 17-Nov-12 22:39pm    
It does not make it a question...
I used to write something like that, but it does not mean you can see that source code, sorry...

1 solution


Perfectly valid question.

There are many RTOS-es available with source code in C

The problem is that if you are looking for bare bones one, it is hard to find as most will have TCP/IP implemented in the OS.

Here is a classic RTOS - XINU, the advantage being it has a great book explaining it step by step.[^]

Here is the reschedule code from it as an example of what you get when you download it from the above link:
#include <conf.h>
#include <kernel.h>
#include <proc.h>
#include <q.h>
#include <inttypes.h>

extern void panic();
extern int insert();
extern int getlast();
extern void ctxsw();
char * getSP(void);

 * resched  --  reschedule processor to highest priority ready process
 * Notes:	Upon entry, currpid gives current process id.
 *		Proctab[currpid].pstate gives correct NEXT state for
 *			current process if other than PRREADY.

int resched(void)
	register struct	pentry volatile *optr;	/* pointer to old process entry */
	register struct	pentry volatile *nptr;	/* pointer to new process entry */
	int newpid;

	preempt = QUANTUM;		/* reset preemption counter	*/

	/* no switch needed if current process priority higher than next */
	if ( ( (optr= &proctab[currpid])->pstate == PRCURR) &&
             ( lastkey(rdytail) < optr->pprio) )	{
//		kprintf("resched: No Switch currpid=%d\n", currpid);
	/* force context switch */
	if (optr->pstate == PRCURR) {
		optr->pstate = PRREADY;

	/* remove highest priority process at end of ready list */
	if ( (newpid = getlast(rdytail)) == EMPTY )
	nptr = &proctab[ ( currpid = newpid ) ];
	nptr->pstate = PRCURR;		/* mark it currently running	*/
//	kprintf("resched: Yes Switch currpid=%d\n", currpid);

	ctxsw(&optr->pregs[0],&nptr->pregs[0]);	/* switch context from old to new */


void dump_Stack(int pid)
    struct pentry *p;
    char *saddr;
    int j;

    p = &proctab[pid];
    if (pid == currpid)
	saddr = getSP()+8;
	saddr = p->pSP;
    kprintf("\nProcess %d stack %p\n",pid,saddr);
    while ((unsigned)saddr < (unsigned)p->pbase)
	j = *++saddr;

char *getSP(void)
    char * mySP;

    asm volatile(
		 "in %A0, __SP_L__" "\n\t"
		 "in %B0, __SP_H__" "\n\t"
		 : "=d" (mySP)

    return (mySP+2);

1) It is an AVR port, but this is irrelevant
2) Any RTOS will have a small amount of assembler as you can see above

Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900