need help with a program for real-time OS class

hey everyone. First I must say that I’m a college student and a total a newbie about QNX :slight_smile: i’ve done some C programing during the C class at the university where i study, but it was all NOT under QNX :frowning: now i started a new class ‘real-time operating systems’ and to pass it i need to complete a project. I was told to write a program in C under QNX. The program’s goal is to flash with leds connected to the pc via the parralel port (LPT) in a certain way. I’ve connected two leds, so far i’ve written a program that comunicates with the port and flashes the led’s. What i should do is write it the way that both of the leds would flash in the same sequence (for instance 2 seconds on, one second off) but one of them would have tha highest priority and the second about middle priority). Then I’m planning to write a second program to ask for the priority that it should take and do something to slow down the system, so that when the amount of those programs (for instance with a priority higher than led1 but lower than led2) would grow, you will be able to see that the first led becomes unsynchronised with the second led. For now the main problem is that I don’t know what should i write in the program to set the functions priority. I’m aware that it may sound like 'no problem for you guys, and setting priorities is one of the essential things in a real-time OS like QNX, but as I’ve already said - I’m a newbie. I would realy aprecieate Your help. Sorry for any written mistakes, but I’m not English. Thanks :slight_smile:

Here is how I think you should do this.

  1. High priority process
    Loop
    sleep 1
    turn LED on
    sleep 1
    turn LED off

  2. Medium priority
    Loop
    sleep 1
    Suck cpu for 2 seconds

  3. Low priority
    Same as 1

If you start 1 and 3 at the same time, they will be fairly synchronized to start with. If you want to be fancy, you can build in a mechanism so they are really synchronzied.

After you start 2, 1 should continue blinking as before, but 3 should get flaky. It will only get cycles periodically to do its job.

This of course is for demonstration only. You would never put a cpu sucking process anywhere but at a very low priority.

BTW, if you are running an SMP system, this can still work, but you will need to run 3) more than once.

i know that i need to run the cpu sucking process more than once. That’s just what the professor said. the only thing at the moment is that i just don’t know how to give certain fuctions certain priorities. Should i make a thread inside the function? I’ve been reading help about making threads etc but it’s all still unclear for me. i suppose that it will all be about threads, but what are the functions called. I’ve read about pthread_create and other and there are some attributes in the “( )” - is the thread’s priority set in there?? or am i thinking the wrong way?? I’ve got the whole conception but i would need to know what exactly should i write to give a thread a certain priority (for instance 16). BTW BIG THANK’S for what you’ve already cleared out. :slight_smile:

You could do this with threads, look up pthread_. However it would be much easier to just create three separate processes and run them at different priorities. You can do this either with the “nice” command, or setprio() (I think). There’s also a sched_() routine that will change priorities as well as the scheduling algorithm.

I like your 2nd def 4th line

Suck cpu for 2 seconds ^^

this is like while(difftime(time0,time1)<2){time1=time(NULL);}
i guess this will suck cpu for about 2sec. ^^

what should I write in the place where ‘pid’ stands?? what is the process’s ID?

i guess, just dont get it. my program looks this way
main
{
initialising functions for the connection with the parallel port
for(i=1,i++ … etc
flashing led1 (each one of these functions: 1) turns led on for 1 sec, 2)turns it off for 1 sec)
flashing led2
}

later i modyficated it a little, putting the led flashig code in functions placed before the main code, and calling to these functions in the main, because i thought that the i would be able to give different priorities to the flashing functions, but now i guess that it’s not possible to run those two functions at the same time doing it this way. at the moment i only figured that writing it this way, only makes the leds flash ‘one after another’ (not at the sam time) cause it’s obviously impossible to do THIS way. I became really confused with this all. :frowning:(

Every process has a unique process ID. There is system subroutine which will return this number to you if you need it. Typically you can use zero to mean, my-process-id.

This isn’t coherent enough to give a good response. I’m not sure if you are confusing processes/threads and functions, or whether you really mean you want to run a specific function at a specific priority. To demonstrate what you described you need at least 3 threads, and possibly these three threads will each be part of a different process.
If you haven’t read the threads and processes chapters of the “QNX System Architechure” book, now would be a good time.

ok thank’s. you’re right i’m confused about processes/threads. I’ll try to figure something out after reading what you’ve mentioned. Thank’s very much :slight_smile:

hello, i’ve read the chapter you suggested and tried to compile a simple program using the threads, that i got from a friend. He said that the program worked, but as i was trying to compile it, i’ve encountered some… let’s say ‘difficulities’ :frowning: here’s the code of the program :

#include <stdio.h>
#include <unistd.h>
#include <stdint.h>
#include <hw/inout.h>
#include <sys/neutrino.h>
#include <sys/mman.h>
#include <pthread.h>

#define PORT_LENGTH 1
#define DATA_ADDRESS 0x378
#define CTRL_ADDRESS 0x37a

#define INIT_BIT 0x04

#define ZERO 0x00
#define DIODA1 0x01
#define DIODA2 0x02
#define MAX_COUNT 15

int * kod_LED1 (void)
{
int privity_err;
uintptr_t ctrl_handle;
uintptr_t data_handle;

privity_err = ThreadCtl( _NTO_TCTL_IO, NULL );
if ( privity_err == -1 )
{
	fprintf( stderr, "can't get root permissions\n" );

b [/b] return -1;
}

ctrl_handle = mmap_device_io( PORT_LENGTH, CTRL_ADDRESS );

out8( ctrl_handle, INIT_BIT );

data_handle = mmap_device_io( PORT_LENGTH, DATA_ADDRESS );
    putchar(12);
	out8( data_handle, DIODA1 );
	printf( "\a" );
	sleep( 1 );

	out8( data_handle,ZERO );
	sleep( 1 ); 

}

int * kod_LED2(void)
{
int privity_err;
uintptr_t ctrl_handle;
uintptr_t data_handle;

privity_err = ThreadCtl( _NTO_TCTL_IO, NULL );
if ( privity_err == -1 )
{
	fprintf( stderr, "can't get root permissions\n" );

b[/b] return -1;
}

ctrl_handle = mmap_device_io( PORT_LENGTH, CTRL_ADDRESS );

out8( ctrl_handle, INIT_BIT );

data_handle = mmap_device_io( PORT_LENGTH, DATA_ADDRESS );
    putchar(12);
	out8( data_handle, DIODA2 );
	printf( "\a" );
	sleep( 1 );
	
	out8( data_handle,ZERO );
	sleep( 1 ); 

}

int main( )
{
int count;

pthread_t LED1;
pthread_t LED2;

for ( count = 0; count < MAX_COUNT; count++ )
{	

b[/b] pthread_create(&LED1, NULL, &kod_LED1, NULL);
b [/b]pthread_create(&LED1, NULL, &kod_LED1, NULL);
}

    putchar(12);
return 0;

}

When i tried to compile it i was given such errors (in the brackets on the begining of some lines in the code i’ve written the numbers of the lines, that the compiler mentioned there were errors in):

ERRORS:
in function ‘kod_LED1’:
in line 34: warning: return maks pointer from integer without a cast
in function ‘kod_LED1’:
in line 63: warning: return maks pointer from integer without a cast
in function ‘main’:
in line 91 & 92: warning: passing arg 3 of ‘pthread_create’ from incompatible pointer type;

i’m aware that the first two errors are connected to the thing that the fuctions return -1, while they’re void. I’m not sure where should i correct the code. And about the second errors - what’s that all about? thanks in advance for your help. bye

i’ve corrected the first two errors, but stil got problems with the last two about pthread_create. What can it be caused by ?

Thread routines should look like this:

void *kod_LED1(void *arg)
{
}

Note that the “error” is just a warning. In your case it shouldn’t cause any harm.

when i try to run the program it sais : memory fault (core dumped) :frowning: what is the reason for that??

Usually it means that you are reading/writing to an illegal area of memory. If you can isolate where the
abort occurs, you may be able to see immediately what is causing it. By the way, you could call ThreadCtl() once
in main() before you create the threads. Looking at your code I don’t see much memory handling at all, which
makes the error curious. I am a little concerned about the 2nd parameter to pthread_create() where you pass
a NULL. I can’t remember what that parameter is, or whether NULL is valid there.

i’m not shure what should be between the brackets after the pthread_create. And what does the ThreadCtl() do??

i’m not sure what should be between the brackets after Pthread_create. And what does the ThreadCtl() do?

By brackets, do you mean parenthasis? ThreadCtl() is called here to allow the program to do port I/O.
BTW, I see that sending NULL as the attr parameter to pthread_create() is fine, so I don’t know what is
wrong with the program. I assume you are running the program as Super User, otherwise
ThreadCtl() is sure to fail.

yes, i did mean parenthasis, i just took the wrong word from the dictionary :stuck_out_tongue_winking_eye: (sorry, but as i said before - i’m not english). So is the ThreadCtl() necessary ? and should I write something there between the ‘parenthasis’ or just leave i the way ‘()’? BTW I’m not familiar with the ‘super user’ term - what exactly does it mean?

ThreadCtl() is necessary if you want the in8() and out8() calls to work. Yes you need something inside the parenthases.
I can’t see anything wrong with your program right now.

I compiled your program, and made a few minor changes to remove all the warnings. The program runs without causing the abort, even when I’m not super user, so I can’t really tell you much more. I should note that you are talking to the parallel (Printer) port. Are you LED’s hooked into the printer port?