PDA

View Full Version : syncroniation between RTlinux thread and QT



raghvendramisra
1st February 2007, 11:27
hi,
i am making a project in which i am using QT at the front end and RTLinux at the backhand. My application involve making a FIFO in RTLinux,creating a thread, writting Data into fifo in the thread and reading the data at qt end.For reading i have to make a thread in the QT & read the data there.
but the problem is i am not able to make syncroniation between RTLinux and QT.
i tried with semaphores but it did'nt worked.
pl help me to solve the problem

bye

wysota
1st February 2007, 11:33
What does "didn't work" mean in this situation?

raghvendramisra
1st February 2007, 11:49
i mean the thread in the QT and RtLinux thread dosen't syncronise

can we use semaphore to syncronise thread in QT and RTLINUX???????????

wysota
1st February 2007, 11:56
Could we see a piece of code you tried? There is no reason for the synchronisation not to work.

raghvendramisra
2nd February 2007, 04:53
i am sending my code of both back hand and front hand..............
have a look at it.......
bye

wysota
2nd February 2007, 09:05
i am sending my code of both back hand and front hand
As a Word document? Why not in a text file?

Your problem is that the semaphores in the backend are not the same semaphores you use in the frontend. The fact that they both applications use the same variable names doesn't mean those variables represent the same objects. You should make the semaphore accessible to all processes by putting it in a shared memory segment:


If pshared is non-zero, then the semaphore is shared between processes,
and should be located in a region of shared memory (see shm_open(3),
mmap(2), and shmget(2)). (Since a child created by fork(2) inherits
its parent's memory mappings, it can also access the semaphore.) Any
process that can access the shared memory region can operate on the
semaphore using sem_post(3), sem_wait(3), etc.

raghvendramisra
5th February 2007, 09:06
hi,
i have tried ir by using shared mem. but bach hand is giving error.
i am attaching the code. pl have a look at it .
thanks
bye

wysota
5th February 2007, 12:41
addr=mmap(0,5003,PROT_READ|PROT_WRITE,MAP_SHARED,i d,0);
*addr=&bin_sem1;
Are you sure this is what you want? I doubt that... You're assigning a pointer containing a local address of the semaphore to the content of the address returned by mmap.... What is that supose to do?

I was thinking about using shmget and shmat... After attaching a shared memory segment , you'll probably have to create a semaphore and memcpy it to the shared segment, initialise it there and only then use it from the other process.

raghvendramisra
6th February 2007, 09:07
hi,
actually my back hand is in RTLinux and it doesen't support shmget & shmat. because it is showing error when i included the sys/ipc.h & sys/sem.h header files.

so i m using mmap for shared memory.


i have redefined the code. now it is giving segmentation error.
could u plz look at it ant tell wat can be the problem?????????
thnx

bye

wysota
6th February 2007, 12:26
because it is showing error when i included the sys/ipc.h & sys/sem.h header files.
What error?


so i m using mmap for shared memory.

mmap() is for mapping files or devices into memory, it's not a shared memory component. I don't know if you'll be able to use it this way...


i have redefined the code. now it is giving segmentation error.
could u plz look at it ant tell wat can be the problem?????????


Please either provide a minimal compilable example as an attachment or embed the code directly into your post using [code] tags.

Where does the segfault occur? Could you provide a backtrace using gdb?

raghvendramisra
13th February 2007, 10:28
hi,
actually when i included ipc.h and sem.h header files it showed that "No such file or Directory". beco RTLinux dosen't support these header files.

so i used mmap. it is working but with problem.
i am pasting a piece of my code and the o/p.
actually front end and back hand are syncroniosed for some time and the the program hangs.

pl look at it


///////////////////back hand/////////////////////////////////////

addr1=(sem_t*)mmap(0,5003,PROT_READ|PROT_WRITE,MAP _SHARED,id1,0);
addr2=(sem_t*)mmap(0,5003,PROT_READ|PROT_WRITE,MAP _SHARED,id2,0);

rtl_clock_gettime( CLOCK_REALTIME, &next);



while ( run )
{
timespec_add_ns( &next, TIME);

rtl_clock_nanosleep( CLOCK_REALTIME, TIMER_ABSTIME,&next, NULL);

printf("BUS 1");

sem_wait(&addr1[0]);

w = write(fd2,&ch,1);

if(w==-1)
{
printf("not written");
}

sem_post(&addr2[0]);
ret=read(fd1,&rec,1);
if(ret)
{
if (rec =='s')
{
run=0;
printf("s = %c\n",rec);
close(fd1);
close(fd2);
}
}


}

rtl_pthread_exit(NULL);
return(0);
}


///////////////////////////front hand/////////////////////


bk_addr1=(sem_t*)mmap(0,5003,PROT_READ | PROT_WRITE,MAP_SHARED,id1,0);
bk_addr2=(sem_t*)mmap(0,5003,PROT_READ | PROT_WRITE,MAP_SHARED,id2,0);

fd2=open(FIFO_NAME2,O_RDONLY|O_NONBLOCK); //openinf fifo


sem_post(&bk_addr1[0]);

while(1)
{
sleep(1);
sem_wait(&bk_addr2[0]);

q=read(fd2,&key,sizeof(key));
if(q==-1)
{
printf("not read\n");
}

printf(" read=%d ",q);
printf(" char=%c\n",key);

sem_post(&bk_addr1[0]);


}
munmap(bk_addr1,5003);
munmap(bk_addr2,5003);
close(id1);
close(id2);
fclose(fp1);
}



/////////////out put///////////////
BUS 1 read=1 char=a
BUS 1 read=1 char=a
BUS 1 read=1 char=a
BUS 1 read=1 char=a
BUS 1 read=1 char=a
BUS 1 read=1 char=a
BUS 1 read=1 char=a
BUS 1 read=1 char=a
BUS 1 read=1 char=a
BUS 1 read=1 char=a
BUS 1 read=1 char=a
BUS 1 read=1 char=a
BUS 1 read=1 char=a
BUS 1 read=1 char=a
BUS 1 read=1 char=a
BUS 1 read=1 char=a
BUS 1 read=1 char=a
BUS 1 read=1 char=a
BUS 1 read=1 char=a
BUS 1 read=1 char=a
BUS 1 read=1 char=a
BUS 1BUS 1 read=1 char=a
Segmentation fault