Memory management between threads in RTEMS

m.shahverdi sspardis2 at gmail.com
Fri Jul 20 11:14:09 UTC 2012


According to RTEMS POSIX API User’s Guide "RTEMS provides no memory
protection"! so what does this mean?

On Wed, Jul 18, 2012 at 8:46 PM, Gedare Bloom <gedare at rtems.org> wrote:

> Hi,
>
> You are right to have some concern because your global variables will
> need to be locked somehow (posix mutex/semaphore) since they are
> accessed by both your 'main' task and the pthread that is created.
> This is a correctness problem; without locking this program's behavior
> is poorly defined I would guess. I assume that sharing the global
> variables between 'main' and the spawned thread is the desired
> behavior.
>
> -Gedare
>
> On Wed, Jul 18, 2012 at 11:58 AM, m.shahverdi <sspardis2 at gmail.com> wrote:
> > This is my application that is written in C,I write the main code till
> now.
> > this app is an elevator that goes up and down in a 4 floors building:
> >
> >
> > #include<stdio.h>
> > #include<stdlib.h>
> > #include<string.h>
> > #include<stdbool.h>
> > #include<time.h>
> > #include <sys/time.h>
> > #include <sys/types.h>
> > #include <unistd.h>
> >
> >
> > #include <signal.h>
> >
> > void handler(int signo)
> > {
> >   return;
> > }
> >
> >
> > typedef struct person prs;
> > int elevator_run();
> > struct person
> > {
> >     int place;
> >     int distination;
> >     char direction;
> >     bool inout;// in:true,out:false
> >     bool exit;//if person exits true
> >     prs * next;
> > };
> > int stage[5];///////to know stop in which level
> > int up_down;///////to know direct  1 for up 0 for down  2 for none
> > int stop_move;/////to know if elevator is stop or no stop 0 move 1
> > int current_place;
> > int distination;
> > time_t base;
> > int xx;
> > int main()
> >       {
> >     int place;
> >     int direct;
> >     int counter_in=0;
> >     int counter_out=0;
> >     int in_out;////in=1;out=0
> >     int dis;///to get distination of person in elevator
> >     prs* headin;
> >     prs* headout;
> >     prs*newnode;
> >     prs*nextnode;
> >     prs*node;
> >     headin=(prs*)malloc(sizeof(prs));
> >     headout=(prs*)malloc(sizeof(prs));
> >     pthread_t thread_id;
> >
> >         pthread_create(&thread_id, NULL,  elevator_run, NULL);
> >           struct sigaction sa;
> >
> >   sa.sa_handler = handler;
> >   sigemptyset(&sa.sa_mask);
> >   sa.sa_flags = 0;
> >   sigaction(SIGALRM, &sa, NULL);
> >
> >
> >
> >     if(headin==NULL)
> >     {
> >         printf("\nError in memory allocation!\n ");
> >         return 1;
> >     }
> >     if(headout==NULL)
> >     {
> >         printf("\nError in memory allocation!\n ");
> >         return 1;
> >     }
> >     int update;
> >     printf("This is elevator stopped at ground \n tell us your place and
> > distination ");
> >     while(1)
> >     {
> >         update=0;
> >         alarm(1);
> >
> >   if (scanf("%d", &in_out) == 1)
> >   {
> >     alarm(0); // cancel the alarm
> >   }
> >   else
> >   {
> >     continue;
> >   }
> >          //   scanf("% d", & in_out);
> >
> >
> >
> >         if (in_out==1)
> >         {
> >           scanf("%d",& dis);
> >               if(current_place==distination)
> >           {
> >               update=1;
> >               distination=dis;
> >           }
> >               else if((up_down==1 && distination<dis) ||(up_down==0 &&
> > distination>dis))
> >               {
> >                   update=1;
> >                   stage[distination]=1;
> >                 distination=dis;
> >               }
> >               else if((up_down==1 && current_place<dis<distination)
> > ||(up_down==0 && distination<dis<current_place))
> >                        stage[dis]=1;
> >                     else
> >                     {
> >                        newnode=(prs*)malloc(sizeof(prs));
> >                        if(newnode==NULL)
> >                           {
> >                         printf("\nError in memory allocation!\n ");
> >                         return 1;
> >                           }
> >                        counter_in++;
> >                        newnode->exit=false;
> >                        newnode->distination=dis;
> >                           newnode->next=headin->next;
> >                           headin->next=newnode;
> >
> >             }
> >            }
> >          else {
> >             scanf("%d %d",& place,& direct);
> >             if(direct=='q')
> >             return 1;
> >         if(stop_move==0){
> >           distination = place;
> >           printf("/////");}
> >           else {
> >         if(direct==up_down &&((up_down==1&&
> > current_place<=place<=distination) ||(up_down==0&&
> > current_place>=place>=distination)))
> >            stage[place]=1;
> >         if(direct==up_down && ((up_down==1 && place > distination) ||
> > (up_down==0 && place < distination)))
> >            {
> >                stage[distination]=1;
> >                distination=place;
> >                update=1;
> >            }
> >         else
> >          {
> >            newnode=(prs*)malloc(sizeof(prs));
> >            if(newnode==NULL)
> >              {
> >             printf("\nError in memory allocation!\n ");
> >             return 1;
> >          }
> >            counter_out++;
> >            newnode->exit=false;
> >            newnode->next=headin->next;
> >            headin->next=newnode;
> >            newnode->place=place;
> >          }
> >
> >           }
> >           }
> >     }
> > }
> >
> >
> > int elevator_run()
> > {
> >
> >    time_t current;
> >    int i;
> >    int y=0;
> >    //float x=1;
> >    for(i=0;i<5;i++)
> >    stage[i]=0;
> >    up_down=1;
> >    stop_move=0;/////to know if elevator is stop or no
> >    current_place=0;
> >    distination=0;
> >
> >    while(1)
> >    {
> >
> >       if(stop_move==1)
> >       {
> >         if(up_down==1)
> >     {
> >
> >       current=time(NULL);
> >
> >       if(current==base + 5)
> >       {
> >       base=current;
> >       y=y+1;
> >       }
> >     //  printf("y = %f" ,y);
> >       }
> >     else
> >      {
> >      current=time(NULL);
> >
> >      if(current==base + 5)
> >       {
> >       base=current;
> >       y=y-1;
> >       }
> >     }
> >     }
> >     if(stop_move==0 && distination > current_place)
> >     {
> >         stop_move=1;
> >         base=time(NULL);
> >         up_down=1;
> >         printf("salam");
> >     }
> >     if(stop_move==0 && distination < current_place)
> >     {
> >         stop_move=1;
> >         base=time(NULL);
> >         up_down=0;
> >     }
> >
> >       if(y == 0) {current_place=0;if(distination==0) stop_move=0;}
> >       if(y == 1 && current_place !=1) { printf("place1");
> current_place=1;
> > if(distination==1) stop_move=0;}
> >       if(y == 2 && current_place != 2)
> > {current_place=2;printf("place2");if(distination==2) stop_move=0;}
> >       if(y == 3 && current_place != 3)
> > {current_place=3;printf("place3");if(distination==3) stop_move=0;}
> >       if(y == 4 && current_place != 4)
> > {current_place=4;printf("place4");if(distination==4) stop_move=0;}
> >
> >
> >    }
> > }
> >
> > On Wed, Jul 18, 2012 at 8:11 PM, Till Straumann <
> strauman at slac.stanford.edu>
> > wrote:
> >>
> >> Maybe you could share some example code so that we understand
> >> what exactly you're trying to do?
> >>
> >> T.
> >>
> >>
> >> On 07/18/2012 10:37 AM, m.shahverdi wrote:
> >>
> >> These two threads have some common fields that I use mutex on these
> fields
> >> but in general I read in c_user document of RTEMS that this OS has a
> flat
> >> memory and I'm worry about unsafe access to memory that each thread
> allocate
> >> in the beginning of execution.
> >>
> >> On Wed, Jul 18, 2012 at 7:42 PM, Thomas Doerfler
> >> <Thomas.Doerfler at embedded-brains.de> wrote:
> >>>
> >>> Hi,
> >>>
> >>> funny topic: one question, many different answers, none of them is
> >>> wrong ;-)
> >>>
> >>> Maybe you can specify, what you expect from a "memory protection"
> >>> mechanism:
> >>> - A way to synchronize two threads, so only one is allowed to
> >>> access/write to a memory area?
> >>> - A way to detect, when a thread inadvertedly tries to access/write to
> >>> another thread's private area?
> >>> - Something different?
> >>>
> >>> wkr,
> >>>
> >>> Thomas.
> >>>
> >>> Am 18.07.2012 16:20, schrieb m.shahverdi:
> >>> > Hi, I have an application with two threads but I don't know how to
> >>> > protect memory of each thread against other thread.please help me.
> >>> >
> >>> > thanks
> >>> >
> >>> >
> >>> >
> >>> > _______________________________________________ rtems-users mailing
> >>> > list rtems-users at rtems.org
> >>> > http://www.rtems.org/mailman/listinfo/rtems-users
> >>> >
> >>>
> >>>
> >>> --
> >>> IMD Ingenieurbuero fuer Microcomputertechnik
> >>> Thomas Doerfler           Herbststrasse 8
> >>> D-82178 Puchheim          Germany
> >>> email:    Thomas.Doerfler at imd-systems.de
> >>> PGP public key available on request
> >>>
> >>>
> >>> --
> >>> --------------------------------------------
> >>> embedded brains GmbH
> >>> Thomas Doerfler
> >>> Obere Lagerstrasse 30
> >>> D-82178 Puchheim
> >>> Germany
> >>> email: Thomas.Doerfler at embedded-brains.de
> >>> Phone: +49-89-18908079-2
> >>> Fax: +49-89-18908079-9
> >>> PGP: Public key available on request.
> >>>
> >>> Diese Nachricht ist keine geschäftliche Mitteilung im Sinne des EHUG.
> >>>
> >>> _______________________________________________
> >>> rtems-users mailing list
> >>> rtems-users at rtems.org
> >>> http://www.rtems.org/mailman/listinfo/rtems-users
> >>
> >>
> >>
> >>
> >> --
> >>
> >> به "هوالحی" زنده می کند؛
> >>
> >> آنسان که می میراند به "هوالممیت"..
> >>
> >> ما را به "هوالشهید" است که مدام می میراند و زنده می کند..
> >>
> >>
> >>
> >>
> >
> >
> >
> > --
> >
> > به "هوالحی" زنده می کند؛
> >
> > آنسان که می میراند به "هوالممیت"..
> >
> > ما را به "هوالشهید" است که مدام می میراند و زنده می کند..
> >
> >
> >
> > _______________________________________________
> > rtems-users mailing list
> > rtems-users at rtems.org
> > http://www.rtems.org/mailman/listinfo/rtems-users
> >
>



-- 

به "هوالحی" زنده می کند؛

آنسان که می میراند به "هوالممیت"..

ما را به "هوالشهید" است که مدام می میراند و زنده می کند..
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rtems.org/pipermail/users/attachments/20120720/12be8b4f/attachment-0001.html>


More information about the users mailing list