إعـــــــلان

تقليص
لا يوجد إعلان حتى الآن.

using message queues in Linux

تقليص
X
 
  • تصفية - فلترة
  • الوقت
  • عرض
إلغاء تحديد الكل
مشاركات جديدة

  • Linux abo3abed
    رد
    يعطيك العافيه اخوي ستورم

    اترك تعليق:


  • St0rM-MaN
    كتب موضوع using message queues in Linux

    using message queues in Linux

    simple message queue program writes a phrase into a message queue and fork to another process read it then exit
    with signal supporting and clearing after
    كود PHP:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<unistd.h>
    #include<sys/types.h>
    #include<sys/ipc.h>
    #include<sys/msg.h>
    #include<signal.h>
    #include<sys/wait.h>

    struct msgbuf
    {
            
    long mtype;
            
    char mtext[1];
    };

    void sig_chld(int signo)
    {
            
    pid_t cdeath;

            
    signal(SIGCHLD sig_chld);
            
    printf("CHILD IS DEAD\n");

            
    wait(&cdeath);
    }

    int main()
    {
            
    int id;
            
    int rc;
            
    int len;
            
    pid_t child;
            
    struct msgbuf *send;
            
    struct msgbuf *recv;

            
    char *msg " Hello Bitchs";
            
    char shell_cmd[50];

            
    len strlen(msg);

            
    id msggetIPC_PRIVATE IPC_CREAT IPC_EXCL);

            
    signalSIGCHLD sig_chld);

            if(
    id 0)
            {
                    
    perror("msgget");
                    
    _exit(1);
            }

            
    send malloc(sizeof(struct msgbuf) + len);

            
    strncpysend->mtext msg len );

            
    send->mtype 1;

            
    rc msgsndid send len 0);

            if(
    rc 0)
            {
                    
    perror("msgsnd");
                    
    _exit(1);
            }

            
    child fork();

            switch(
    child)
            {
                    case -
    1:
                            
    perror("fork");
                            
    _exit(1);
                            break;
                    case 
    0:
                            
    recv malloc(sizeof(struct msgbuf) + len);

                            
    rc msgrcv(id recv len 0);

                            if(
    rc 0)
                            {
                                    
    perror("msgrcv");
                                    
    _exit(1);
                            }
                            
    printf("recived msg %s\n",recv->mtext);
                            
    free(recv);
                            
    _exit(0);

                            break;
                    default:
                            
    sleep(2);
                            break;
            }


            
    printf("DONE clearing messages queues\n");
            
    sprintf(shell_cmd "ipcrm -q %d"id);

            return 
    0;

    using message queues is better than using a pipe or named pipes
    pipe and named pipes are FIFO means first in first out
    so if you wanna read a particular type of a message you have to read it all to reach your goal !
    reading from a message queue could be blocked if there is no current messages is there unless
    there is a message placed
    or queue was removed
    or receiving a signal

    or it could not be wait or block at all
    if you summery IPC_NOWAIT to the fourth parameter of msgrecv(); system call

    so as a summery for this code creating a message queue and using it requires
    1-creating a message queue
    2-setting up a message queue structure and filling it
    3-send this message
    4- receiving it or not to it's your choice ;)

    are you one of those curious people ?
    man msgrcv , msgget , msgsnd
يعمل...
X