LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   translating C software from Win to Linux (http://www.linuxquestions.org/questions/programming-9/translating-c-software-from-win-to-linux-566216/)

altella 07-03-2007 02:48 AM

translating C software from Win to Linux
 
Hello,

At work I need to translate some software writted in C code from Windows to Linux.
This software uses some functions of the windows API, and I donīt know how to translate them to Linux. These are my main problems:
- Timers support: At the moment I am using "SetTimer()" to stablish events after some time has passed. How could I do it with Linux?
- Thread support. I think this will not issue problems. It is well explained in "Advanced Linux programming" book.

I would be very grateful if you could help me with the timers problem.

Best regards,

nc3b 07-03-2007 05:02 AM

Hello. Here's what my drunken neuron has come up with a long time ago.

First declare a structure to gold parameters.

PHP Code:

struct param
{
    
int(*user_function)(int);
    
int pass_int;
    
int interval;
}; 

Then declare an all-purpose function, which will call your functions.

PHP Code:

void * function(void_parameter)
{
    
param parameter=(param*)_parameter;
    if(
parameter->user_function)
    {
        for(;;)
        {
            (
parameter->user_function)(parameter->pass_int);
            
sleep(parameter->interval);
        }
    }


Declare some dumb class

PHP Code:

class timer
{
    
param parameter;
    
pthread_t thread_id;
public:
    
    
timer(int _interval,int(*_function)(int),int _pass_int)
    {
        
parameter.interval=_interval;
        
parameter.user_function=_function;
        
parameter.pass_int;
        
pthread_create(&thread_id,NULL,&function,&parameter);
    }
}; 

Two "user" functions
PHP Code:

int function_to_run(int a)
{
    
cout<<"bla"<<endl;
}

int function_to_run2(int a)
{
    
cout<<"fdgdfg"<<endl;


Cross your fingers..

PHP Code:

int main(int argcchar** argv)
{
    
timer *tm2=new timer(2,&function_to_run2,43);
    
timer *tm=new timer(1,&function_to_run,47);

    while(
true);
    return 
0;


Of course, I have done no error testing, and as you can see, I am really not using the int parameter to the functions, nor am I using the functions return values.. (ugh.. the values they should return :o ).

Anyways, I am sure there's lots and lots of timer classes on the net.

chrism01 07-04-2007 06:19 AM

Looks like C code to me ?

nc3b 07-04-2007 07:08 AM

What I wrote is not C code (look, there's that dumb class). I know the guy asked for C code, but I believe it's easy to quickly turn it into C code.

chrism01 07-04-2007 07:40 AM

True, what I meant was not the actual code content, but the 'PHP Code:' comment before each (C++) code block. It looks weird (possibly confusing to a newbie).

nc3b 07-04-2007 08:06 AM

You are right, it has happened before :) , but I php code has syntax highlighting.. In the future, I will avoid using php code tags or I will clearly state which language is the code written in. Cheers. :)

jtshaw 07-04-2007 01:12 PM

For time timer code I would investigate setitimer and getitimer.

nc3b 07-04-2007 03:42 PM

I agree jitshaw. I actually found an example here. This could prove useful for me too in the future.


All times are GMT -5. The time now is 06:50 PM.