Results 1 to 11 of 11

Thread: qsharedmemory between Qt and c program on linux plateform

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Apr 2012
    Posts
    5
    Platforms
    Unix/X11

    Default qsharedmemory between Qt and c program on linux plateform

    Hi everybody
    I 'm beginner in Qt, I have a problem to read a shared memory segment generated by c program on qt application using nativekey().
    if someone have any idea or solution please help me
    my c prog generate a data on shared memory with this key-id 0x00313233
    and this is my source :

    Qt Code:
    1. QSharedMemory shm("123");
    2. if (shm.isAttached())
    3. shm.detach();
    4. if (!shm.attach(QSharedMemory::ReadWrite)) {
    5. // Shared memory n'est pas crée donc création
    6. if (!shm.create(1024, QSharedMemory::ReadWrite)) {
    7. QSharedMemory::SharedMemoryError err = shm.error();
    8. qDebug()<< err <<"Erreur lors de création de shared memory";
    9. }
    10. else {
    11. qDebug()<< "memory attached successfully";
    12. qDebug()<< "memory created successfully";
    13. qDebug()<< "memory keyid"<<shm.key().toAscii().toHex();
    14.  
    15. if(shm.lock()){
    16. qDebug()<<"Memory locked successefully";
    17. QByteArray s = QByteArray((char*) shm.constData(), shm.size());
    18. QString message = QString::fromUtf8(s.constData());
    19. qDebug() << message;
    20.  
    21. if(shm.unlock()){
    22. qDebug()<< "Memory unlocked successefully";
    23. if(shm.detach())
    24. qDebug()<<"Memory detached successefully";
    25. else qDebug()<<"Memory detached failed"<< shm.error();
    26. }
    27. else qDebug()<< "Memory unlock failed"<< shm.error();
    28. }
    29. else qDebug()<<"Memory lock failed"<< shm.error();
    30. }
    31. }
    32.  
    33.  
    34. QTextEdit *textedit= new QTextEdit(s);
    35. QHBoxLayout *layout = new QHBoxLayout( &mainwindow );
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 10th May 2012 at 00:22. Reason: missing [code] tags

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: qsharedmemory between Qt and c program on linux plateform

    [code][/code] tags please

    "this key-id 0x00313233
    and this is my source :

    QSharedMemory shm("123");"


    123 doesnt look very much like 0x00313233. What do you think about that?


    "...using nativekey()."
    Where are you using nativekey()? I don't see it anywhere...
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  3. #3
    Join Date
    Apr 2012
    Posts
    5
    Platforms
    Unix/X11

    Default Re: qsharedmemory between Qt and c program on linux plateform

    thank you for your interest
    first, I use Qsharedmemory shm("123") because of casting "123" to hex is equal to 0x00313233
    second, for the nativekey ,i just asked how can I use it in my application to assign the shared memory segment created by c program

    thank you.
    Last edited by marwan; 9th May 2012 at 22:58.

  4. #4
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: qsharedmemory between Qt and c program on linux plateform

    you must have qt 4.8 or later. you should NOT pass a key into ctor.

    I'm not sure if your casting of hex to string is right or not.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: qsharedmemory between Qt and c program on linux plateform

    Can we see the exact code you use to create the shared memory segment in your C program?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Apr 2012
    Posts
    5
    Platforms
    Unix/X11

    Default Re: qsharedmemory between Qt and c program on linux plateform

    I use qt 4.8.1 that offer the possibility to inter-process with other platform
    for the c program, i haven't problem to run it and allocate shared memory,it is so simple,it create a segment on the defined key and write something on it
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/ipc.h>
    #include <sys/shm.h>

    #define SHM_SIZE 1024 /* make it a 1K shared memory segment */

    int main(int argc, char *argv[])
    {
    key_t key;
    int shmid;
    char *data;
    int mode;

    if (argc > 2) {
    fprintf(stderr, "usage: shmdemo [data_to_write]\n");
    exit(1);
    }

    /* make the key: */
    /*if ((key = ftok("shm.c", 'R')) == -1) {
    perror("ftok");
    exit(1);
    }*/

    // static atribute of key

    key=0x00313233;

    /* connect to (and possibly create) the segment: */
    if ((shmid = shmget(key, SHM_SIZE, 0644 | IPC_CREAT)) == -1) {
    perror("shmget");
    exit(1);
    }

    /* attach to the segment to get a pointer to it: */
    data = shmat(shmid, (void *)0, 0);
    if (data == (char *)(-1)) {
    perror("shmat");
    exit(1);
    }

    /* read or modify the segment, based on the command line: */
    if (argc == 2) {
    printf("writing to segment: \"%s\"\n", argv[1]);
    strncpy(data, argv[1], SHM_SIZE);
    } else
    printf("segment contains: \"%s\"\n", data);
    printf("key ID: \"%x\"\n", key);
    printf("shmid ID: \"%d\"\n", shmid);

    /* detach from the segment: */
    /*if (shmdt(data) == -1) {
    perror("shmdt");
    exit(1);
    }*/

    return 0;
    }

    and this is the output:
    mar@mar-laptop:~/omapDev/sharedmem$ ./shm CAFEFADE
    writing to segment: "CAFEFADE"
    key ID: "313233"
    shmid ID: "1736737"

    thank you.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: qsharedmemory between Qt and c program on linux plateform

    Why do you think "key_t" is an equivalent of "char*"? At least on my system it is an int (through the chain of typedefs key_t -> __KEY_T_TYPE -> __S32_TYPE -> int), not a char*.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Apr 2012
    Posts
    5
    Platforms
    Unix/X11

    Default Re: qsharedmemory between Qt and c program on linux plateform

    @Wysota
    because on qt, the key id must be qstring to pass on constructor of Qsharedmemory.
    please if you have another solution , I get it.

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: qsharedmemory between Qt and c program on linux plateform

    Quote Originally Posted by marwan View Post
    @Wysota
    because on qt, the key id must be qstring to pass on constructor of Qsharedmemory.
    That doesn't mean that if another API (apparently not directly compatible with this one as key_t is obviously a handler and not a key itself) is using an integer, you can pass a hexadecimal equivalent of the key and expect them to cooperate. A direct equivalent of Qt API is shm_open and family.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #10
    Join Date
    Apr 2012
    Posts
    5
    Platforms
    Unix/X11

    Default Re: qsharedmemory between Qt and c program on linux plateform

    Thank you wysota for help
    but, i want to know if i should use the Qsahredmemory functions(create(),setkey(),attach(),lock()) or shm functions (chmget(),shmat()...); personnaly i prefer use the first choice because it's more simple and acces management to the memory respected.

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: qsharedmemory between Qt and c program on linux plateform

    It doesn't matter as long as you interchange APIs that are compatible. What Qt does is it takes a file path you pass as the key, calls ftok() on it (with 'Q' as the proj_id parameter) and then uses the result with shmget(). Your code bypasses the first two steps thus you don't have a valid key.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. My first Linux QT program wont compile
    By GrahamLabdon in forum Newbie
    Replies: 1
    Last Post: 27th October 2010, 15:49
  2. How to shutdown Linux machine from QT Program?
    By Kevin Hoang in forum Qt Programming
    Replies: 3
    Last Post: 20th August 2010, 20:42
  3. Replies: 2
    Last Post: 22nd March 2010, 08:54
  4. Program in Windows, Deploy in Linux
    By suitto in forum Installation and Deployment
    Replies: 1
    Last Post: 8th April 2009, 13:11
  5. QT Program debug,GDB on Linux
    By darpan in forum General Programming
    Replies: 1
    Last Post: 26th January 2007, 22:02

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.