POSIX_SPAWN

Hi everyone!

Have someone ever seen something about the command posix_spawn?

I want to spawn a new process using this command because it’s more flexible.You can set things just like below:

  • posix_spawnattr_setrunmask:
    Sets, in theory, the CORE processor that will run the spawned process.

  • posix_spawnattr_setschedparam:
    Sets the parameters of the attributes of the spawned process.

  • posix_spawnattr_setschedpolicy:
    Sets the parameters of the scheduling policy attribute of the spawned process.

  • posix_spawnattr_setstackmax:
    Sets the maximum stack size for the spawned process.

Follows the code below:

	char * _ExecutablePath[] = { "/usr/sbin/qconn", NULL };

	parentProcessID = getpid();
    printf("Parent Process ID: %d\n", parentProcessID);

    fflush(stdout);

	_RestrictSched_p.sched_priority = 20;

	status = posix_spawnattr_setrunmask(&_RestrictAttrp, uint32RunMask);
	if (status)
		handle_error_en(status, "posix_spawnattr_setrunmask");

	status = posix_spawnattr_setschedparam(&_RestrictAttrp, &_RestrictSched_p);
	if (status)
		handle_error_en(status, "posix_spawnattr_setschedparam");

	status = posix_spawnattr_setschedpolicy(&_RestrictAttrp, SCHED_RR);
	if (status)
		handle_error_en(status, "posix_spawnattr_setschedpolicy");

	status = posix_spawnattr_setstackmax(&_RestrictAttrp, uint32Size);
	if (status)
		handle_error_en(status, "posix_spawnattr_setstackmax");

	status = posix_spawn(&childProcessID, _ExecutablePath[0], &fileActions,
			&_RestrictAttrp, _ExecutablePath, NULL);
	if (status)
		handle_error_en(status, "posix_spawnp");

	printf("Child Process ID: %d\n", childProcessID);

	killpg(getpid(), SIGKILL);

Thanks in advance!

What is the question?

Hello Mario. How are you?

I just want some way to manipulate the spawned process, bind to him some pthreads, etc.

I’m good thanks, best wishes for the coming year.

All the functions you listed above are available in 6.4.1 and later. Don’t know if they are available in older version. It’s part of the 1003.1 RealTime Signal Extension.

Thanks! I wish all the best things to you too.

So, there’s no way to associate POSIX Threads (pthread) to the spawned process?

don`t understand your question? What is it exactly that you want to acheive?

I want to manipulate or associate to the new process some work, for example bind him some pthreads.

Because when I create a new process, I can’t bind pthreads to it.
If I create them (pthreads) they associate to the parent process and not to the child process.

I’m not sure I understand what you mean by “associate to” but it doesn’t seem to make sense.

When a “parent” process creates a “child” process, it creates a process. If that child process creates a new thread, then that thread is part of the child process. It executes using the child’s data segment, not the parent’s.

spawn creates new processes (generally), pthread_create creates new threads.

Binding is usually used in reference to TCP/IP. Calls to bind() refer to the calling process which includes any threads that are part of it. Since sockets are fd’s, If you then call spawn, the fd’s are duplicated in the child process.

If you open a file in a process would you expect the existing children to also have that file open for them? It’s the same for threads.

Could you post a example code?

#include <libc.h>
int main()
{
printf(“Hello World\n”);
}

I’m sorry if I was not especific.

I was asking for an example code of creating a process using the POSIX_SPAWN command and making it executes a pthread.

You can’t do that. Process A spawns process B and then process B need to start a thread. Process A cannot start a thread in process B.

It seems you have misconception about process/thread. You should read on the subject.

I’ll try to explain better I can.
I’ll use my code that I sent above.


                          1º - Here I'm configuring the new spawned process:

char * _ExecutablePath[] = { “/usr/sbin/qconn”, NULL };
_RestrictSched_p.sched_priority = 20;

status = posix_spawnattr_setrunmask(&_RestrictAttrp, uint32RunMask);
if (status)
handle_error_en(status, “posix_spawnattr_setrunmask”);

status = posix_spawnattr_setschedparam(&_RestrictAttrp, &_RestrictSched_p);
if (status)
handle_error_en(status, “posix_spawnattr_setschedparam”);

status = posix_spawnattr_setschedpolicy(&_RestrictAttrp, SCHED_RR);
if (status)
handle_error_en(status, “posix_spawnattr_setschedpolicy”);

status = posix_spawnattr_setstackmax(&_RestrictAttrp, uint32Size);
if (status)
handle_error_en(status, “posix_spawnattr_setstackmax”);


2º - Now, I’m executing the command to create the new process:

status = posix_spawn(&childProcessID, _ExecutablePath[0], &fileActions, &_RestrictAttrp, _ExecutablePath, NULL);
if (status)
handle_error_en(status, “posix_spawnp”);

printf(“Child Process ID: %d\n”, childProcessID);


3º - And here, I kill the child process:

killpg(getpid(), SIGKILL);

So, is there some place at my code that can call another method that could create pthreads?

Not from the child process. The child must create it’s own threads.

Just to be very very clear, when you spawn you do create one thread.
All processes have one thread. When you run spawn you create this
first thread but it is not a thread of the process that called spawn.

Thank’s for all folks!!

I’ve solved my problem calling the binaries of a compiled project (which was added in QNX Neutrino) through the new spawned process.

Really thanks for the explanation that you guys have posted.

There’s just one more question.

I need to know if these properties below really changes the attributes of the process:

1º) Priority of the process:
_RestrictSched_p.sched_priority = 20;
posix_spawnattr_t _RestrictAttrp = POSIX_SPAWNATTR_INITIALIZER;
status = posix_spawnattr_setschedparam(&_RestrictAttrp, &_RestrictSched_p);
if (status)
handle_error_en(status, “posix_spawnattr_setschedparam”);

2º) Core of the process:
uint32_t uint32RunMask = POSIX_SPAWN_EXPLICIT_CPU;
posix_spawnattr_t _RestrictAttrp = POSIX_SPAWNATTR_INITIALIZER;
status = posix_spawnattr_setrunmask(&_RestrictAttrp, uint32RunMask);
if (status)
handle_error_en(status, “posix_spawnattr_setrunmask”);

3º) Schedule of the process:
posix_spawnattr_t _RestrictAttrp = POSIX_SPAWNATTR_INITIALIZER;
status = posix_spawnattr_setschedpolicy(&_RestrictAttrp, SCHED_RR);
if (status)
handle_error_en(status, “posix_spawnattr_setschedpolicy”);

4º) Maximum stack size of the process:
uint32_t uint32Size = POSIX_SPAWN_SETSTACKMAX;
posix_spawnattr_t _RestrictAttrp = POSIX_SPAWNATTR_INITIALIZER;
status = posix_spawnattr_setstackmax(&_RestrictAttrp, uint32Size);
if (status)
handle_error_en(status, “posix_spawnattr_setstackmax”);

After you start the processes, you might want to use pidin to see if the properties you set above are correct.

But PIDIN runs at QNX Neutrino (the target) doesn’t it?

Is there some code to run in the code level with QNX Momentics?