need help with a program for real-time OS class

yes they are hooked and it’s done properly, because before when i was running a simplier program the led’s were flashing :slight_smile:. maby you could paste the version that as you said ‘made a few minor changes’ so that i could take a look at it? the only thing now if i could run this version is to set the priority of each thread (i’m pretty sure i’ve seen that function somewhere on the qnx page…) thank’s in advance :slight_smile: bye

ow and by the way, if my led’s were flashing before - i guess it means that the in8 / out8 calls do work, even without the Pthread_ctl(), so is it necessary? thx, bye :slight_smile:

I have a feeling you are suffering from version error. This would happen if for example you change the code, but don’t recompile it, or more commonly, you recompile it, but somehow you are running an older version. I think this because when I recompiled your program without ThreadCtl() and ran it, I immediately go this (familiar) error.

Memory fault (core dumped)

Here is the code that worked for me.#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

void *kod_LED1(void *arg)
{
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" ); 
	return ((void *) -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 ); 
return(0);

}

void *kod_LED2(void *arg)
{
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" ); 
	return((void *) -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 ); 
return(0);

}

int main( )
{
int count;

pthread_t LED1; 
pthread_t LED2; 

for ( count = 0; count < MAX_COUNT; count++ ) 
{ 
	pthread_create(&LED1, NULL, &kod_LED1, NULL); 
	pthread_create(&LED1, NULL, &kod_LED1, NULL); 
} 

putchar(12); 
return 0; 

}

i’m pretty sure i run and compile always the up-to-date version.after every change made i compile it and run, i keep my eye on this, because i know that would be a really lame mistake ;P. i’ll try to run the version you’ve corrected, but i guess i already see what’s my next error… but i’ll write if it comes true. thx very much :slight_smile:

If you ran your program without ThreadCtl() and it worked, you have a version control problem.

well i compiled your program and it works without any errors but it only flashes one led and stops :frowning: i tried to take the loop that surrounded pthread_create out and paste it in kod_Dioda1 and 2 but it still works the same way. i want to make it work the way that it would start flashing both leds a couple of times (until counter hits max_count). Any suggestions ? :frowning:

I’m not sure if you realize this but in the main loop the for loop will actually create 30 threads all running concurrently, trying to access the hardware “at the same time”, very hard to predict what is going to happen ;-)

pthread_create will return as soon as the thread is created, it doesn’t wait for the thread to finish.

aaah! so what now, how should i do this? i guess that if you’ve put it this way, than trying to write it with threads is not a good idea :frowning:. or am i wrong:(. please help me, because im realy confused about it… again :frowning:

hello, i’ve done some modifications in my program, now it consists of two different programs, each one controling one led (now i used in8 and out8, so everytime it’s trying to modify the led’s state it doesnt interrupt the second led’s work) and of course the famous “cpu sucking program” :smiley:. now i’ve tested it all by giving priority by writing ‘nice’ (with certain priorities and adding the program’s name) and i’ve been thinking, how could i write a program to run these 2 led controling programs at the same time. Just a simple program that could just run first program (placed in a different file) with a certain prio, and do the same with the second program (with another prio). what do you think about that? what command should i use ? thanks in advance :slight_smile: bye

Maybe if you could explain what exactly you need to achieve. You seems to tackle this with very little background hence the more we know what you need/want to accomplish people can offer more suited advice. The way you want to apply usage of priority if very strange.

You mentionned that each program control one led (using in and out), but see you have two programs sharing one device ( the io ports ). There must be some sort of protection. Because lets assume your code that turns led 1 on is
out8 ( handle, in8(handle) | DIAO1 );

and led two is:
out8 ( handle, in8(handle) | DIAO2 );

This will work 99% of the time, but ultimately it will fail, because the out8(in8()) operation is NOT atomic. That means one program can do the in8 and before it has to execute the out8 a context switch occurs and the other program gets to run and also run out8(in8()) and changes led 1 state. When execution of the first program resume it will perform the out8 but with the wrong state.