I have looked in C books but I cannot find threads available in C. So I guess I will have to use someone’s library to get thread functionality in my C programs. Can some please tell me from where can I download such a library? My basic problem is that I have made a real time scheduler in C with custom functions working as threads, the problem is that in order to have complete functionality of real time one must be able to stop the function at any time and latter resume them. That functionality is only possible with threads but simple C does not has this functionality built in. I will be compiling on windows in visual C compiler as a consol app, but I cannot use visual C threads. Please Help.
Still if such a solution is not possible then I will have to make a sort of a parser that will parse the functions and perform every single addition, multiplication, and division through a set of variables that I will consider registers. By storing those registers and resuming their value I might be able to achieve thread functionality. But there is another problem with that; firstly I store the functions as function pointers in an array, and latter after scheduling I pick them up from there. So is there a way that I can convert the contents of the function by that function pointer to a string so that latter I can parse the contents of the function or do I have to follow another approach. Please help with this problem as my deadline is nearing.
stober
July 15th, 2002, 10:00 PM
why can't you use _beginthread() and _endthread(). Look these function up on MSDN for an example *.c program.
cup
July 16th, 2002, 01:33 AM
Threads are not part of C/C++.
Look up CreateThread on MSDN for thread info.
Microsoft also do something called fibers (Note the US spelling if you are used to spelling fibre the British way). With fibers, you control the scheduling. With threads, Windows controls the scheduling. Look for CreateFiber on MSDN and go from there.
dude_1967
July 16th, 2002, 10:18 AM
Hello,
I think that this developer is trying to simulate the essential portions of a pure cooperative multitasking operating system using threads, where each thread represents a task in the multitasking system. In this fashion, one simulates the saving/restoring of the CPU-context upon "task"-switch (simulated with thread sleep, wait, etc).
Is this your developmental goal?
I have experience with this type of simulation. It is straight forward using threads synchronized in a clearly defined fashion.
If you are seriously considering doing this and although threads are not part of the C or C++ language, it is not really all that difficult.
Respond if you are interested in discussing this topic further.
Chris.
:)
onlyhuman23
July 16th, 2002, 10:43 AM
You are absolutely right Chris. Basically the s/w I am trying to make is supposed to be a RTOS (REAL TIME OPERATING SYSTEM) supposedly for embedded devices. Although I do not have a particular chip, as I cannot afford one, therefore it is generic and I will simulate it on pc, preferably on windows; as much of the coding I have done is in the visual C 6 compiler. But the functionality should be as close to being independent, otherwise it won’t be much of an OS. The second reason is as most of the RTOS for embedded devices are in simple C; therefore I made mine in C as well. Therefore it is structured; hence mixing it with MFC will cause problems. The rest of the problem is as it is described in my first question above.
If you need more explanation or my code I would be willing to share it. I appreciate your co-operation and would welcome further suggestions. :)
dude_1967
July 16th, 2002, 12:33 PM
Will extract and provide appropriate sample code...
Chris.
:)
onlyhuman23
July 16th, 2002, 01:20 PM
/*
The required data types and function prototypes for explanation are given below.
What happens is that the programer writes the threads the relative deadlines for
which are stored in a array. The function pointers of each of these threads or
functions are stored in another array of type function pointers. The threads or
function pointers are put in a doubly linked list according to their deadlines.From
this linked list are the function pointers transfered to a execution stack by the
schedule function, where the top thread function pointer is executed and then
removed by a thread_end_instance function. The problem is that inorder for this to
be a real time scheduler, if a thread with a higher priority and lesser deadline
is entered while execution of any other thread, the executing thread should be
stopped and stored. after the execution of the important thread it can be resumed.
But this is not possible without real thread functionality. So here is the problem.
what can be the possible solutions.
*/
//Task relative deadlines in seconds
clock_t th_reldline[]={3000,1000,2000,4000,5000,6000,7000,8000,9000,10000,11000,12000,14000,13000,15000,16000,6000};
unsigned int th_ready_prio[]={14,16,15,13,12,11,10,9,8,7,6,5,3,4,2,1,11}; //Task preemption level
//structure for the ready queue and the execution stack
struct node2
{
struct node2 *prev;
signed int index;//will store the thread or function number reflected in the function pointer array
void (*fn_ptr)();//the function pointer for every element
struct node2 *next;
};
//following are the function prototypes
void thread0(void);
void thread1(void);
void thread2(void);
void thread3(void);
void thread4(void);
void thread5(void);
void thread6(void);
void thread7(void);
void thread8(void);
void thread9(void);
void thread10(void);
void thread11(void);
void thread12(void);
void thread13(void);
void thread14(void);
void thread15(void);
void thread16(void);
//array containing the pointers of threads
void (*th_bd_ptr[17])() = {thread0,thread1,thread2,thread3,thread4,thread5,
thread6,thread7,thread8,thread9,thread10,thread11,
thread12,thread13,thread14,thread15,thread16};
void rq_insert(signed int t,struct node2 **f,struct node2 **r);
//rq2stk_exchange() takes thread from ready queue and puts it on top of the stack.
signed int rq2stk_exchange(struct node2 **f,struct node2 **r,struct node2 **s);
void system_scheduler(void);//schedules the threads for one cycle
void end_thread_instance(struct node2 **s,struct node2 **ss,struct node2 **ss1,struct node2 **ss2);//Ends every thread and does the related work
void stk2blk_exchange(struct node2 **s,struct node2 **b);//puts thread from stack to blocked queue
void make_thread_ready(signed int t, unsigned char nact);//puts the thread in the ready queue
dude_1967
July 16th, 2002, 04:37 PM
onlyhuman23:
You want to simulate full-preemptive multitasking. I only have simulations of full cooperative multitasking readily available. I wrote the sample included here a few years ago. It is in plain C using the Win32 API.
Your development goals differ from the sample solution in two main areas:
1) Your task control blocks are stored in a linked list, the sample code stores them in a static array.
2) You want full preemptive simulation: Tasks can interrupt each other. The enclosed simulation in pure cooperative.
The first difference is merely cosmetic, the second quite a bit more. Nevertheless, take a look at the sample. Just build the project and run it. It's simplicity might "win you over".
Unfortunately I do not have time to really delve into the problem at hand right now, and I feel that a full-blown discussion about RTOS simulations (although toatally interesting to me) might be beyond the scope of this forum.
Happy development.
Chris.
:)
onlyhuman23
July 16th, 2002, 08:15 PM
I would just like to ask you one more thing. I was under the impression that thread mechanism is compiler dependant rather than OS dependant; is this true or not. Are both things possible, for example to have a OS based thread mechanism and a compiler based one too.
Alexey B
July 16th, 2002, 08:50 PM
Threads are managed by OS entirely in Windows. There isn't much a compiler has to do. In Unix, threads had to be written manually, without much OS help. To ease the porting of UNIX applications, Windows implements fibers, which are much like threads, except that the scheduling is implemented by the application.
dude_1967
July 17th, 2002, 02:16 AM
onlyhuman23:
The comments of Alexy B are correct.
In Unix/Linux using GCC/CC, one can develop multithreaded applications using Posix threads (http://www.amazon.com/exec/obidos/ASIN/0201633922/qid=1026889516/sr=8-1/ref=sr_8_1/002-9646980-0930441). There is library support for Posix threads using the pthreads (http://www.amazon.com/exec/obidos/ASIN/1565921151/ref=pd_sim_books/002-9646980-0930441). In Windows using VC6 or VC.NET, one can develop multithreaded applications using Windows threads (http://www.amazon.com/exec/obidos/ASIN/1572319968/ref=pd_sim_books/002-9646980-0930441), Windows fibers are also described within this book. Both books will be on the shelf of a good technical bookstore.
Threads are managed by the operating system. Good compilers tend do support libraries of function calls which allow the management (creation, suspension, continuation, synchronization, etc) of threads.
Please note that the simulation code provided above is absolutely not a typical use of multithreading. It is, rather, a tutorial on simulating basic elements which are common among many cooperative multitasking RTOS's.
Chris.
:)
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.