Skip to contentSkip to conversation

Why Xenomai kill my process?

Xenomai-help Salvatore De Astis 3 participants 13 messages Feb 13, 2008 – Feb 15, 2008 1 message not in the archive

lore
Share & tools
lore
#0 lore raw

Hi,
I need to implement a multitask application.
The most important point is to have a 'custom' exit procedure for each task. The calling of the exit procedure should happen in asynchronous mode. The exit procedure of each task, after the 'custom' code, must call the
rt_task_delete() before leaving.

The following code is a simple example:
There are two tasks, two semaphores and two other tasks (pending on semaphores) whose target is to execute the parent's exit procedure. In this case the exit procedure is the my_exit() routine for both tasks, but, of course, I'd like to have custom exit procedure for each task. Please read the questions following the code section.

/*#########################################################*/ #include <stdio.h>
#include <signal.h>
#include <sys/mman.h>
#include <native/task.h>
#include <native/sem.h>

#define TASK_PRIO 99 /* Highest RT priority */ #define TASK_MODE 0 /* No flags */
#define TASK_STKSZ 0 /* Stack size (use default one) */

typedef void (*TASK_EP)(void*);

RT_TASK task_desc, task_desc1;
RT_SEM sem_desc, sem_desc1;

int cleanup (RT_TASK*);
void catch_signal(int);
static char* unique_name_create(void* tid, const char* name); static RT_TASK* task_id_self(void);

static void my_exit(void* dum)

{
    char* name;
    RT_TASK_INFO info;
    RT_TASK *tid;

    tid = task_id_self();
    rt_task_inquire(tid, &info);
    name = info.name;
    printf("%s: exit\n", name);
    if(cleanup(tid))
        printf("Cleanup Error!\n");
    else
        printf("Cleanup successfull!\n");
}
typedef struct {
    TASK_EP break_routine;
    RT_SEM* sem;
} my_brk;

void break_receiver(my_brk* pbrk)

{
    rt_sem_p(pbrk->sem, TM_INFINITE);
    pbrk->break_routine(NULL);
    rt_sem_delete(pbrk->sem);
    free(pbrk);
}

int set_break_receiver(TASK_EP break_routine, RT_SEM* sem)

{
    const char *brktask_name;
    RT_TASK *tid, *tid_br;
    RT_TASK_INFO info;
    my_brk* pbrk;
    int err;

    pbrk = malloc(sizeof(my_brk));

    tid = rt_task_self();
    err = rt_task_inquire(tid, &info);
    if(err)
        printf("ERROR: rt_task_inquire. error:%d \n", err);


    pbrk->break_routine = break_routine;
    pbrk->sem = sem;

    brktask_name = unique_name_create(tid, "BRK_RCV");
    tid_br = malloc(sizeof(*tid));
    err = rt_task_spawn(tid_br, brktask_name, 20000, info.bprio, 0,
                (TASK_EP)break_receiver, pbrk);
    if(err)
        printf("ERROR: rt_task_spawn. error:%d \n", err);
    return(err);
}

void task_body (void *cookie)

{
    int i=0;

    set_break_receiver(my_exit, &sem_desc);
    rt_task_set_periodic(NULL,TM_NOW,20000);

    for (;;) {
        if(!i){
        i++;
            printf("Pippo task RUNNING!\n");
    }
        rt_task_wait_period(NULL);
    }
}

void task_body_1 (void *cookie)

{
    int i=0;

    set_break_receiver(my_exit, &sem_desc1);
    rt_task_set_periodic(NULL,TM_NOW,20000);

    for (;;) {
        if(!i) {
           printf("CruelPippo task RUNNING!\n");
       i++;
    }
        rt_task_wait_period(NULL);
    }
}

int send_break(void)
{
int err;

   /*  char *sem_name, *sem_name1;
    RT_SEM sem, sem1;
    RT_SEM_INFO info, info1;


    rt_sem_inquire(&sem_desc, &info);
    sem_name = info.name;
    rt_sem_inquire(&sem_desc1, &info1);
    sem_name1 = info1.name;
    err = rt_sem_bind(&sem, sem_name, TM_NONBLOCK) ||
          rt_sem_bind(&sem1, sem_name1, TM_NONBLOCK);
    if(err) return(err); */
    err = rt_sem_v(&sem_desc) ||
          rt_sem_v(&sem_desc1);
    return(err);
}

int main (int argc, char *argv[])

{
    int err;

    signal(SIGTERM, catch_signal);
    signal(SIGINT, catch_signal);

    mlockall(MCL_CURRENT|MCL_FUTURE);

    err = rt_sem_create(&sem_desc, "PippoBRK_SEM", 0, S_FIFO) ||
          rt_sem_create(&sem_desc1,"CruelPippoBRK_SEM", 0, S_FIFO);
    if (err)
       printf("ERROR: rt_sem_create fail.\n");

    err = rt_task_create(&task_desc,
             "Pippo",
             TASK_STKSZ,
             TASK_PRIO,
             TASK_MODE)  ||
      rt_task_create(&task_desc1,
             "CruelPippo",
             TASK_STKSZ,
             TASK_PRIO,
             TASK_MODE);
    if (!err){
    rt_task_start(&task_desc,&task_body,NULL);
    rt_task_start(&task_desc1,&task_body_1,NULL);
    }
    else
       printf("ERROR: rt_task_create fail.\n");

    pause();
    printf("MAIN exit\n");
    return(err);
}

void catch_signal(int sig)

{
    int err;
    err = send_break();
    if(err)
        printf("Error Sending Break!\n");
    return;
}

int cleanup (RT_TASK* tid)

{
    int err;
    RT_TASK_INFO info;

    rt_task_inquire(tid, &info);
    printf("rt_task_delete(%s) calling...\n", info.name);
    err = rt_task_delete(tid);
    if(!err)
        printf("rt_task_delete done!\n");
    return(err);
}

static char* unique_name_create(void* tid, const char* name)

{
    static char unique_name[XNOBJECT_NAME_LEN];
    unsigned int utid;

    if (strlen(name) + 11 > XNOBJECT_NAME_LEN)
        return(NULL);

    utid = (unsigned int)tid;
    sprintf(unique_name, "0x%8.8x", utid);
    strcpy(unique_name + strlen(unique_name), name);
    return(unique_name);
}

static RT_TASK* task_id_self(void)

{
    RT_TASK *tid;
    unsigned int utid;
    RT_TASK_INFO info;
    char* fullname;

    rt_task_inquire(rt_task_self(), &info);
    fullname = info.name;
    fullname[10] = 0;
    sscanf(fullname,"0x%x", &utid);

    tid = (RT_TASK*)utid;
    return(tid);
}

/*#########################################################*/

When I execute this code, Xenomai Kills my process and the shell shows me the following output:

Pippo task RUNNING!
CruelPippo task RUNNING!
Pippo: exit
rt_task_delete(Pippo) calling...
I am rt_task_delete user-space calling!
task: 0x804b0a8 - opaque2: 0
rt_task_delete: XENOMAI_SKINCALL1 calling...
Killed

As you see I put some printf in Xenomai*/src/skins/native/task.c to trace what happens.

I put some printk also in ../ksrc/.. task.c shadow.c and this is dmesg output:

Xenomai: I AM xnshadow_sys_bind() in shadow.c.
Xenomai: After ppd test in shadow.c.
Xenomai: in __rt_task_create(Pippo) --> ph.opaque2 =3086322608.
Xenomai: in __rt_task_create(CruelPippo) --> ph.opaque2 =3084864432.
Xenomai: in __rt_task_create(0x0804b0a8BRK_RCV) --> ph.opaque2 =3086256592.
Xenomai: in __rt_task_create(0x0804b120BRK_RCV) --> ph.opaque2 =3086232016.
Xenomai: I AM rt_task_delete() of task:df621010.
Xenomai: After xnpod_asynch_p.
Xenomai: After xnlock_get_irqsave.
Xenomai: After xeno_h2obj_validate.
Xenomai: After __native_task_safewait.
Xenomai: After xnpod_delete_thread.
Xenomai: After xnlock_put_irqrestore.
Xenomai: rt_task_delete END with error: 0
Xenomai: detach_ppd() in shadow.c.
Xenomai: native: cleaning up sem "PippoBRK_SEM" (ret=0).
Xenomai: native: cleaning up sem "CruelPippoBRK_SEM" (ret=0).

Why the opaque2 is 0 in Pippo's rt_task_delete call? Dmesg shows me 3086322608 in __rt_task_create() of Pippo task.
What's wrong with my code?
Does a better method exist to implemet asyncronous rt_task_delete in Xenomai?

Thanks for help

Salvatore De Astis

#1 lore raw
Quoted 14 lines · Salvatore De Astis

Hi,
I need to implement a multitask application.
The most important point is to have a 'custom' exit procedure for each task. The calling of the exit procedure should happen in asynchronous mode. The exit procedure of each task, after the 'custom' code, must call the
rt_task_delete() before leaving.

The following code is a simple example:
There are two tasks, two semaphores and two other tasks (pending on semaphores) whose target is to execute the parent's exit procedure. In this case the exit procedure is the my_exit() routine for both tasks, but, of course, I'd like to have custom exit procedure for each task.
Please read the questions following the code section.

Quoted 5 lines

Why the opaque2 is 0 in Pippo's rt_task_delete call? Dmesg shows me 3086322608 in __rt_task_create() of Pippo task.
What's wrong with my code?
Does a better method exist to implemet asyncronous rt_task_delete in
Xenomai?

The best way to do what you want to do is to call rt_task_delete from the task to be deleted itself, because trying to delete a thread from another thread entails problems which can only be solved by sending a SIGKILL to the whole application.

However, have you noted that Xenomai automatically deletes per-process objects upon process termination ? So maybe you do not really need a complicated cleanup mechanism.

Signature · 1 line
 Gilles Chanteperdrix
#2 lore raw
Quoted 1 line · On Thursday 14 February 2008 10:42:23 Gilles Chanteperdrix wrote:

On Wed, Feb 13, 2008 at 5:29 PM, Salvatore De Astis <[email protected]>

Quoted 20 lines · wrote:
Quoted 14 lines

Hi,
I need to implement a multitask application.
The most important point is to have a 'custom' exit procedure for each task. The calling of the exit procedure should happen in asynchronous mode. The exit procedure of each task, after the 'custom' code, must call
the rt_task_delete() before leaving.

The following code is a simple example:
There are two tasks, two semaphores and two other tasks (pending on semaphores) whose target is to execute the parent's exit procedure. In this case the exit procedure is the my_exit() routine for both tasks, but, of course, I'd like to have custom exit procedure for each task.
Please read the questions following the code section.

Quoted 5 lines

Why the opaque2 is 0 in Pippo's rt_task_delete call? Dmesg shows me 3086322608 in __rt_task_create() of Pippo task.
What's wrong with my code?
Does a better method exist to implemet asyncronous rt_task_delete in
Xenomai?

Quoted 4 lines

The best way to do what you want to do is to call rt_task_delete from the task to be deleted itself, because trying to delete a thread from another thread entails problems which can only be solved by sending a SIGKILL to the whole application.

or by using rt_task_join, naturally ! This requires that you create a joinable thread though, using T_JOINABLE in rt_task_spawn.

Peter

Signature · 1 line
Peter Soetens -- FMTC -- <http://www.fmtc.be>
#3 lore raw
Quoted 23 lines · Peter Soetens
Quoted 1 line · On Thursday 14 February 2008 10:42:23 Gilles Chanteperdrix wrote:

On Wed, Feb 13, 2008 at 5:29 PM, Salvatore De Astis <[email protected]>

Quoted 20 lines · wrote:
Quoted 19 lines

Hi,
I need to implement a multitask application.
The most important point is to have a 'custom' exit procedure for each task. The calling of the exit procedure should happen in asynchronous mode. The exit procedure of each task, after the 'custom' code, must call
the rt_task_delete() before leaving.

The following code is a simple example:
There are two tasks, two semaphores and two other tasks (pending on semaphores) whose target is to execute the parent's exit procedure. In this case the exit procedure is the my_exit() routine for both tasks, but, of course, I'd like to have custom exit procedure for each task.
Please read the questions following the code section.

Why the opaque2 is 0 in Pippo's rt_task_delete call? Dmesg shows me 3086322608 in __rt_task_create() of Pippo task.
What's wrong with my code?
Does a better method exist to implemet asyncronous rt_task_delete in
Xenomai?

Quoted 7 lines
Quoted 4 lines

The best way to do what you want to do is to call rt_task_delete from the task to be deleted itself, because trying to delete a thread from another thread entails problems which can only be solved by sending a SIGKILL to the whole application.

or by using rt_task_join, naturally ! This requires that you create a joinable thread though, using T_JOINABLE in rt_task_spawn.

I am afraid this will not help. As far as I understand the code, the SIGKILL signal is sent to the whole process if xnpod_delete_thread is reached before the task answered the pthread_cancel request by cleaning itself up properly.

Signature · 1 line
 Gilles Chanteperdrix

A message that is not in the archive (sent off-list or not yet received): [email protected]

#5 lore raw

Hi at all,
I'am sure that the same code run fine with Xenomai-2.1.1, kernel linux-2.6.15-ipipe-1.2-15.
Why there are problems now?
I want underline my purpose: my tasks should be exit in asyncronous way executing costom exit routine.
How can I do this in Xenomai?
I've considered the use of rt_add_hook() but compiling the code the compiler returns these errors:
warning: implicit declaration of function rt_task_add_hook' undefined reference to rt_task_add_hook'
collect2: ld returned 1 exit status

Thanks for help!

Salvatore De Astis

Quoted 70 lines

2008/2/14, Gilles Chanteperdrix <[email protected]>:

Quoted 64 lines
Quoted 2 lines · Peter Soetens
Quoted 1 line · On Thursday 14 February 2008 10:42:23 Gilles Chanteperdrix wrote:

On Wed, Feb 13, 2008 at 5:29 PM, Salvatore De Astis <

[email protected]>

Quoted 4 lines
Quoted 3 lines · wrote:
Quoted 3 lines

Hi,
I need to implement a multitask application.
The most important point is to have a 'custom' exit procedure for

each

Quoted 1 line
Quoted 1 line
Quoted 1 line

task. The calling of the exit procedure should happen in

asynchronous

Quoted 1 line
Quoted 1 line
Quoted 1 line

mode. The exit procedure of each task, after the 'custom' code,

must call

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

the rt_task_delete() before leaving.

The following code is a simple example:
There are two tasks, two semaphores and two other tasks (pending

on

Quoted 1 line
Quoted 1 line
Quoted 1 line

semaphores) whose target is to execute the parent's exit

procedure.

Quoted 1 line
Quoted 1 line
Quoted 1 line

In this case the exit procedure is the my_exit() routine for both

tasks,

Quoted 1 line
Quoted 1 line
Quoted 1 line

but, of course, I'd like to have custom exit procedure for each

task.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

Please read the questions following the code section.

Why the opaque2 is 0 in Pippo's rt_task_delete call? Dmesg shows

me

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

3086322608 in __rt_task_create() of Pippo task. What's wrong with my code?
Does a better method exist to implemet asyncronous

rt_task_delete in

Quoted 3 lines
Quoted 3 lines
Quoted 1 line

Xenomai?

The best way to do what you want to do is to call rt_task_delete

from

Quoted 1 line
Quoted 1 line

the task to be deleted itself, because trying to delete a thread

from

Quoted 1 line
Quoted 1 line

another thread entails problems which can only be solved by sending

a

Quoted 3 lines
Quoted 1 line

SIGKILL to the whole application.

or by using rt_task_join, naturally ! This requires that you create a

joinable

Quoted 1 line

thread though, using T_JOINABLE in rt_task_spawn.

I am afraid this will not help. As far as I understand the code, the SIGKILL signal is sent to the whole process if xnpod_delete_thread is reached before the task answered the pthread_cancel request by cleaning itself up properly.

--

Gilles Chanteperdrix

_______________________________________________
Xenomai-help mailing list
[email protected]
https://mail.gna.org/listinfo/xenomai-help

#6 lore raw
Quoted 8 lines · Salvatore De Astis

Hi at all,
I'am sure that the same code run fine with Xenomai-2.1.1, kernel linux-2.6.15-ipipe-1.2-15.
Why there are problems now?

Because now, xnpod_delete_thread sends a SIGKILL to the whole process when not killing the current task.

Quoted 3 lines

I want underline my purpose: my tasks should be exit in asyncronous way executing costom exit routine.
How can I do this in Xenomai?

run the custom cleanup routine from the deleted thread itself and call rt_task_delete for the current task only.

Quoted 5 lines

I've considered the use of rt_add_hook() but compiling the code the compiler returns these errors:
warning: implicit declaration of function rt_task_add_hook' undefined reference to rt_task_add_hook'
collect2: ld returned 1 exit status

rt_task_add_hook is a kernel-space only service.

Signature · 1 line
 Gilles Chanteperdrix
#7 lore raw

2008/2/15, Gilles Chanteperdrix <[email protected]>:

Quoted 19 lines

On Fri, Feb 15, 2008 at 10:45 AM, Salvatore De Astis

Quoted 9 lines · <[email protected]> wrote:

Hi at all,
I'am sure that the same code run fine with Xenomai-2.1.1, kernel linux-2.6.15-ipipe-1.2-15.
Why there are problems now?

Because now, xnpod_delete_thread sends a SIGKILL to the whole process when not killing the current task.

Quoted 7 lines
Quoted 3 lines

I want underline my purpose: my tasks should be exit in asyncronous way executing costom exit routine.
How can I do this in Xenomai?

run the custom cleanup routine from the deleted thread itself and call rt_task_delete for the current task only.

I can't run the custom cleanup routine from the delete thread itself because, in my application, the main procedures of all threads run in an infinite loop. Each tread suspend itsef after or before a cycle, another thread (called Scheduler) resumes the threads accordig to a custom scheduling policy. The cleanup routine of a thread is out of its infinite cycle, of course.
So, how can I do to preserve my thread's organization with xenomai?

Quoted 14 lines

I've considered the use of rt_add_hook() but compiling the code the compiler

Quoted 4 lines

returns these errors:
warning: implicit declaration of function rt_task_add_hook' undefined reference to rt_task_add_hook'
collect2: ld returned 1 exit status

rt_task_add_hook is a kernel-space only service.

--

Gilles Chanteperdrix

#8 lore raw
Quoted 29 lines · Salvatore De Astis

2008/2/15, Gilles Chanteperdrix <[email protected]>:

Quoted 18 lines

On Fri, Feb 15, 2008 at 10:45 AM, Salvatore De Astis

Quoted 9 lines · <[email protected]> wrote:

Hi at all,
I'am sure that the same code run fine with Xenomai-2.1.1, kernel linux-2.6.15-ipipe-1.2-15.
Why there are problems now?

Because now, xnpod_delete_thread sends a SIGKILL to the whole process when not killing the current task.

Quoted 7 lines
Quoted 3 lines

I want underline my purpose: my tasks should be exit in asyncronous way executing costom exit routine.
How can I do this in Xenomai?

run the custom cleanup routine from the deleted thread itself and call rt_task_delete for the current task only.

Quoted 7 lines

I can't run the custom cleanup routine from the delete thread itself because, in my application, the main procedures of all threads run in an infinite loop. Each tread suspend itsef after or before a cycle, another thread (called Scheduler) resumes the threads accordig to a custom scheduling policy. The cleanup routine of a thread is out of its infinite cycle, of course.
So, how can I do to preserve my thread's organization with xenomai?

Replace the infinite loop with a loop which waits for a flag, use rt_task_unblock to unblock the task when it is suspended, test if the suspending call returns -EINTR, and break the loop in this case.

Signature · 1 line
 Gilles Chanteperdrix
#9 lore raw

The loop which waits for a flag has a collateral effect: if my thread block itself in its main routine how can I kill it in asyncronous mode?

2008/2/15, Gilles Chanteperdrix <[email protected]>:

Quoted 52 lines

On Fri, Feb 15, 2008 at 11:15 AM, Salvatore De Astis

Quoted 22 lines · <[email protected]> wrote:

2008/2/15, Gilles Chanteperdrix <[email protected]>:

Quoted 19 lines

On Fri, Feb 15, 2008 at 10:45 AM, Salvatore De Astis

Quoted 9 lines · <[email protected]> wrote:

Hi at all,
I'am sure that the same code run fine with Xenomai-2.1.1, kernel linux-2.6.15-ipipe-1.2-15.
Why there are problems now?

Because now, xnpod_delete_thread sends a SIGKILL to the whole process when not killing the current task.

Quoted 1 line

I want underline my purpose: my tasks should be exit in asyncronous

way

Quoted 4 lines
Quoted 4 lines
Quoted 2 lines

executing costom exit routine.
How can I do this in Xenomai?

Quoted 8 lines
Quoted 2 lines

run the custom cleanup routine from the deleted thread itself and call rt_task_delete for the current task only.

I can't run the custom cleanup routine from the delete thread itself because, in my application, the main procedures of all threads run in an infinite loop. Each tread suspend itsef after or before a cycle, another thread (called Scheduler) resumes the threads accordig to a custom scheduling policy. The cleanup routine of a thread is out of its

infinite

Quoted 2 lines

cycle, of course.
So, how can I do to preserve my thread's organization with xenomai?

Replace the infinite loop with a loop which waits for a flag, use rt_task_unblock to unblock the task when it is suspended, test if the suspending call returns -EINTR, and break the loop in this case.

--

Gilles Chanteperdrix

#10 lore raw
Quoted 2 lines · Salvatore De Astis

The loop which waits for a flag has a collateral effect: if my thread block itself in its main routine how can I kill it in asyncronous mode?

Use rt_task_unblock/rt_task_resume to unblock it, and check either -EINTR or the deletion flag when returning from the blocking call.

Signature · 1 line
 Gilles Chanteperdrix
#11 lore raw
In reply to Salvatore De Astis
Quoted 2 lines · Salvatore De Astis

The loop which waits for a flag has a collateral effect: if my thread block itself in its main routine how can I kill it in asyncronous mode?

Another solution would be to use pthread_cancel().

Signature · 1 line
 Gilles Chanteperdrix
#12 lore raw

Thanks Gilles,
but I am new to Xenomai so can you give me more details?

2008/2/15, Gilles Chanteperdrix <[email protected]>:

Quoted 16 lines

On Fri, Feb 15, 2008 at 11:52 AM, Salvatore De Astis

Quoted 1 line · <[email protected]> wrote:

The loop which waits for a flag has a collateral effect: if my thread

block

Quoted 1 line

itself in its main routine how can I kill it in asyncronous mode?

Another solution would be to use pthread_cancel().

--

Gilles Chanteperdrix

#13 lore raw
Quoted 2 lines · Salvatore De Astis

Thanks Gilles,
but I am new to Xenomai so can you give me more details?

pthread_cancel is not related to Xenomai, it is the posix way of killing threads while having them execute a custom cleanup routine.

When pthread_cancel is called for a blocked thread, a signal is sent to the thread which unblocks it to let it execute in LIFO order the cleanup routines registered with pthread_cleanup_push.

For more details, see the manpages of pthread_cancel, pthread_cleanup_push, pthread_cleanup_pop.

Signature · 1 line
 Gilles Chanteperdrix