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

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…!

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.

"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

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.

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?

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

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.

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);

Just to add to the confusion…

Suppose we have two “mainsâ€

I could very well not work if the current directory is not specified in PATH.

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…

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.

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

thanks…!

&argv

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]);

phtread_create (…, &argv )

and the declaration of the thread?

void * thread_mdan(char *argv[]){…} ?
void * thread_mdan(char *argv){…} ?
void * thread_mdan(void *argv[]){…} ?
void * thread_mdan(void *argv){…} ?

as I sad, I use argv like this:
IDMain = atoi(argv[0]);
Verbose=atoi(argv[1]);

I tried but, it didn’t works… sorry piss you off… hehe

Thanks!

What do you want to do with this?

IDMain = atoi(argv[0]);

argv[0] is the name of the program… it’s a char* … I don’t understand the idea of the atoi()…

JM

I took 5 minutes to do this example, Is this what you’re looking for?

void * thread_argv ( void *data )
{
char argv = (char) data;

printf ("I'm thread %d and the name of the proccess is: %s\n", pthread_self(), argv[0] );

}

int main ( int argc, char *argv[] )
{
pthread_attr_t
attrt;

time_t  now;

memset ( &attrt, 0, sizeof (pthread_attr_t) );

printf ("%s - main() starting at %s", argv[0], ctime (&now));

pthread_create ( NULL, &attrt, thread_argv, (void*) argv );

sleep (1);

printf ("main() ending at %s", ctime (&now));
	
return ( EXIT_SUCCESS );

}

I don’t know… I’m just adapting an existing program from qnx4 to QNX6…
I guess is the identification of the main… but I’m think that is not in use…
the verbose is used…

the atoi convert a string into an int isn’t