Skip navigation.
Home
The QNX Community Portal

View topic - More than one main... How is that possible?

More than one main... How is that possible?

For discussion of realtime and/or embedded programming.

More than one main... How is that possible?

Postby diogo_angnalo » Thu Mar 12, 2009 4:24 pm

I need to run two mains together.
In the principal main I use spawnl to create a process of the executable of the other main( that has to run together ).
How can I do it?

What is the type of the file wich is the executable of the program?

I'm using QNX Momentics in Windows connected to QNX 6.4.

I think that I explained correctly my problem, sorry about my english.


Thanks..!
diogo_angnalo
Active Member
 
Posts: 38
Joined: Tue Feb 17, 2009 4:57 pm

RE: More than one main... How is that possible?

Postby kwschumm » Thu Mar 12, 2009 5:17 pm

If you want two copies of the same program to run together just start two copies.

If you want one program to create a second copy of itself then look at fork().

If you want one program to spawn a different program then look at the spawn* functions.

If you want one executable to contain two or more threads then look at pthread_create() and the associated pthread* family of functions.

There are examples in the docs for most of these, however when you get into the pthread functions it can become quite complex depending on what your needs are.
kwschumm
Senior Member
 
Posts: 306
Joined: Fri Sep 17, 2004 12:46 pm

RE: More than one main... How is that possible?

Postby diogo_angnalo » Thu Mar 12, 2009 7:48 pm

"If you want one program to spawn a different program then look at the spawn* functions. "

I'm using it, but I don't know where i put the second main.
If I put in the same folder it not runs. If I put in other folder, it doesn't call the second main.
Now the second main is in another project, but it's not working.

I think that is some configuration, some detail, in Momentics. I guess is nothing in the code.


Thanks
diogo_angnalo
Active Member
 
Posts: 38
Joined: Tue Feb 17, 2009 4:57 pm

RE: More than one main... How is that possible?

Postby mario » Thu Mar 12, 2009 7:54 pm

Not sure what you mean by main. A program can only have one main() function.

A program can start another program, in order to do so it has to know where it is, either via PATH or because you are specifying the path yourself.

Can't tell if it's your code or not since I can't see your code.
mario
QNX Master
 
Posts: 4132
Joined: Sun Sep 01, 2002 1:04 am

RE: More than one main... How is that possible?

Postby kwschumm » Thu Mar 12, 2009 7:55 pm

This is confusing, main() is a function, only one per executable allowed.

I don't mean to offend, maybe this is a language issue, but are you new to C?

What does your spawn code look like?
kwschumm
Senior Member
 
Posts: 306
Joined: Fri Sep 17, 2004 12:46 pm

RE: More than one main... How is that possible?

Postby diogo_angnalo » Thu Mar 12, 2009 8:18 pm

itoa(getpid(), StrIDMain, 10);

IDProcessMDAN = spawnl(P_NOWAIT,"ProcessMDAN", StrIDMain, NULL);

I'm not new... I'm think that's weird too, two mains... but my chief sad to me change a project that was in QNX4 to QNX6.. and that was working.. I'm not a experienced programmer... that stuff of process, thread is new to me.

thanks
diogo_angnalo
Active Member
 
Posts: 38
Joined: Tue Feb 17, 2009 4:57 pm

RE: More than one main... How is that possible?

Postby kwschumm » Thu Mar 12, 2009 8:26 pm

OK, it's just passing the pid of the current process to the spawned process as an argument.

When it fails, what are the values of IDProcessMDAN and errno?

Probably it can't find the "ProcessMDAN" executable in the search path or ProcessMDAN does not have execute attributes set.
kwschumm
Senior Member
 
Posts: 306
Joined: Fri Sep 17, 2004 12:46 pm

RE: More than one main... How is that possible?

Postby mario » Thu Mar 12, 2009 9:57 pm

Chief can be wrong ;) If there is more then one main() in the same program, the linker will only use one and will ignored the others. More then one main() in the same program is very very very wrong...

Try specifying full path to the "ProcessMDAN" . You typically want the first argument to be the process name, this is mentioned in the documentation.

Try:

IDProcessMDAN = spawnl(P_NOWAIT,"/opt/sbin/ProcessMDAN", "ProcessMDAN", StrIDMain, NULL);
mario
QNX Master
 
Posts: 4132
Joined: Sun Sep 01, 2002 1:04 am

RE: More than one main... How is that possible?

Postby ysinitsky » Fri Mar 13, 2009 2:27 pm

Just to add to the confusion…

Suppose we have two “mainsâ€
ysinitsky
Senior Member
 
Posts: 164
Joined: Wed Dec 14, 2005 8:46 pm

RE: More than one main... How is that possible?

Postby mario » Fri Mar 13, 2009 4:20 pm

I could very well not work if the current directory is not specified in PATH.
mario
QNX Master
 
Posts: 4132
Joined: Sun Sep 01, 2002 1:04 am

RE: More than one main... How is that possible?

Postby diogo_angnalo » Wed Mar 18, 2009 4:46 pm

Now, instead of creating a process I'm creating a thread... like this:

IDProcessMDAN = pthread_create(NULL, NULL, thread_mdan, NULL);

and the thread_mdan:
void* thread_mdan(void *argc, char *argv[]) { ...... }

these are the shown errors:
initializing argument 3 of 'int pthread_create(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*)'
invalid conversion from 'void* (*)(void*, char**)' to 'void* (*)(void*)'

if I change the thread to:
void* thread_mdan(void *argc){ ......}

it works, but I have to use the "argv[]"... so, any idea?

Thanks for the advice..
diogo_angnalo
Active Member
 
Posts: 38
Joined: Tue Feb 17, 2009 4:57 pm

RE: More than one main... How is that possible?

Postby mario » Wed Mar 18, 2009 5:27 pm

Read the documentation, thread can only be passed a pointer, there is no way you can change this.

You call it *argc, but it's not, it's just a pointer and you get to decide what it points to. If you want to send argv you have to send the address of argv, that can be done with some casting.
mario
QNX Master
 
Posts: 4132
Joined: Sun Sep 01, 2002 1:04 am

RE: More than one main... How is that possible?

Postby diogo_angnalo » Thu Mar 19, 2009 4:55 pm

But how can I send the address of argv?
the argv contain the identification of the main.. I guess..hehe

thanks..!
diogo_angnalo
Active Member
 
Posts: 38
Joined: Tue Feb 17, 2009 4:57 pm

RE: More than one main... How is that possible?

Postby mario » Thu Mar 19, 2009 7:41 pm

&argv
mario
QNX Master
 
Posts: 4132
Joined: Sun Sep 01, 2002 1:04 am

RE: More than one main... How is that possible?

Postby diogo_angnalo » Thu Mar 19, 2009 7:54 pm

I know.. but where I put it??

argv is a char... and the code that I use it, is like this:
IDMain = atoi(argv[0]);
Verbose=atoi(argv[1]);
diogo_angnalo
Active Member
 
Posts: 38
Joined: Tue Feb 17, 2009 4:57 pm

Next

Return to Realtime and Embedded

Who is online

Users browsing this forum: No registered users and 2 guests