������� ������������ / ������ "������������ ��� Linux" / ���������� ���������
next up previous contents index
Next: �������� Up: ������� Previous: �������   Contents   Index

������� ���������

  /* ��������� ������������
     ����������� ���������� ������ msgget()
     (��������� �������������� ������� ���������) */

  #include <stdio.h>
  #include <sys/types.h>
  #include <sys/ipc.h>
  #include <sys/sem.h>
  #include <errno.h>

  main ()
  {
    key_t key;        /* ��� ������ ��� ����� */
    int opperm, flags;    /* ����� �� �������� � ����� */
    int msgflg, msqid;

    /* ������ ��������� ���� */
    printf ("\n������� ����������������� ����: ");
    scanf ("%x", &key);

    /* ������ ����� �� �������� */
    printf ("\n������� ����� �� �������� ");
    printf ("� ������������ ������: ");
    scanf ("%o", &opperm);

    /* ���������� ��������� ����� */
    printf ("\n������� ���, �������������� ");
    printf ("������ ���������� ������:\n");
    printf ("  ��� ������           = 0\n");
    printf ("  IPC_CREAT            = 1\n");
    printf ("  IPC_EXCL             = 2\n");
    printf ("  IPC_CREAT � IPC_EXCL = 3\n");
    printf ("  �����                = ");

    /* �������� �����, ������� ����� ���������� */
    scanf ("%d", &flags);

    /* ��������� �������� */
    printf ("\n���� = 0x%x, ����� = 0%o, ����� = %d\n",
            key, opperm, flags);

    /* ���������� ����� � ������� �� �������� */
  switch (flags) {
    case 0:     /* ����� �� ������������� */
      msgflg = (opperm | 0);
      break;
    case 1:     /* ���������� ���� IPC_CREAT */
      msgflg = (opperm | IPC_CREAT);
      break;
    case 2:     /* ���������� ���� IPC_EXCL */
      msgflg = (opperm | IPC_EXCL);
      break;
    case 3:     /* ���������� ��� ����� */
      msgflg = (opperm | IPC_CREAT | IPC_EXCL);
  }

  /* ��������� ��������� ����� msgget */
  msqid = msgget (key, msgflg);

  if (msqid == -1) {
    /* �������� � ��������� ���������� */
    printf ("\nmsgget ���������� ��������!\n"
    printf ("��� ������ = %d\n", errno);
  }
  else
    /* ��� �������� ���������� �������� msqid */
    printf ("\n������������� msqid = %d\n", msqid);

  exit (0);
  }



 /* ��������� ������������
    ����������� ���������� ������ msgctl()
    (���������� ��������� ���������) */

 #include <stdio.h>
 #include <sys/types.h>
 #include <sys/ipc.h>
 #include <sys/msg.h>

 main ()
 {
   extern int errno;
   int msqid, command, choice, rtrn;
   struct msqid_ds msqid_ds, *buf;
   buf = &msqid_ds;

   /* ������ ������������� � �������� */
   printf ("������� ������������� msqid: ");
   scanf ("%d", &msqid);

   printf ("������� ����� ���������� ��������:\n");
   printf ("  IPC_STAT = 1\n");
   printf ("  IPC_SET  = 2\n");
   printf ("  IPC_RMID = 3\n");
   printf ("  �����    = ");
   scanf ("%d", &command);

   /* ��������� �������� */
   printf ("������������� = %d, �������� = %d\n",
           msqid, command);

   switch (command) {
     case 1: /* ����������� ����������
                � ��������� ������� ���������
                � ���������������� ���������
                � ������� �� */
       rtrn = msgctl (msqid, IPC_STAT, buf);
       printf ("\n ������������� ������������ = %d\n",
               buf->msg_perm.uid);
       printf ("\n ������������� ������ = %d\n",
               buf->msg_perm.gid);
       printf ("\n ����� �� �������� = 0%o\n",
               buf->msg_perm.mode);
       printf ("\n ������ ������� � ������ = %d\n",
               buf->msg_qbytes);
       break;

     case 2: /* ������� � �������� ���� (����)
                 ��������������� ��������� ������ */
       /* ������� �������� �������� ��������
          ��������� ������ */
       rtrn = msgctl (msqid, IPC_STAT, buf);
       printf ("\n������� ����� ����, ");
       printf ("������� ����� ��������:\n");
       printf ("  msg_perm.uid  = 1\n");
       printf ("  msg_perm.gid  = 2\n");
       printf ("  msg_perm.mode = 3\n");
       printf ("  msg_qbytes    = 4\n");
       printf ("  �����         = ");
       scanf ("%d", &choice);

       switch (choice) {
         case 1:
           printf ("\n������� ��-� ������������: ");
           scanf ("%d", &buf->msg_perm.uid);
           printf ("\n��-� ������������ = %d\n",
                   buf->msg_perm.uid);
           break;
         case 2:
           printf ("\n������� ��-� ������: ");
           scanf ("%d", &buf->msg_perm.gid);
           printf ("\n��-� ������ = %d\n",
                   buf->msg_perm.uid);
           break;

         case 3:
           printf ("\n������� ������������ ��� ����: ");
           scanf ("%o", &buf->msg_perm.mode);
           printf ("\n����� �� �������� = 0%o\n",
                   buf->msg_perm.mode);
           break;

         case 4:
           printf ("\n������� ������ ������� = ");
           scanf ("%d", &buf->msg_qbytes);
           printf ("\n����� ���� � ������� = %d\n",
                   buf->msg_qbytes);
           break;
       }

       /* ������ ��������� */
       rtrn = msgctl (msqid, IPC_SET, buf);
       break;

    case 3: /* ������� ������������� �
               ��������������� � ��� �������
               ��������� � ��������� ������ */
      rtrn = msgctl (msqid, IPC_RMID, NULL);
   }

   if (rtrn == -1) {
     /* �������� � ��������� ���������� */
     printf ("\nmsgctl ���������� ��������!\n");
     printf ("\n��� ������ = %d\n", errno);
   }

   else {
     /* ��� �������� ���������� �������� msqid */
     printf ("\nmsgctl ���������� �������,\n");
     printf ("������������� = %d\n", msqid);
   }

   exit (0);
 }


 /* ��������� ������������
    ����������� ��������� ������� msgsnd() � msgrcv()
    (�������� ��� ��������� ���������) */

 #include <stdio.h>
 #include <sys/types.h>
 #include <sys/ipc.h>
 #include <sys/msg.h>

 #define MAXTEXTSIZE 8192

 struct msgbufl {
   long mtype;
   char mtext [MAXTEXTSIZE];
 } sndbuf, rcvbuf, *msgp;

 main ()
 {
   extern int errno;
   int flag, flags, choice, rtrn, i, c;
   int rtrn, msqid, msgsz, msgflg;
   long msgtyp;
   struct msqid_ds msqid_ds, *buf;
   buf = &msqid_ds;

   /* ������� ��������� �������� */
   printf ("\n������� ���, ��������������� ");
   printf ("������� ��� ������ ���������:\n");
   printf ("  ������� = 1\n");
   printf ("  ������� = 2\n");
   printf ("  �����   = ");
   scanf ("%d", &choice);

   if (choice == 1) {
     /* ������� ��������� */
     msgp = &sndbuf; /* ��������� �� ��������� */

     printf ("\n������� ������������� ");
     printf ("������� ���������,\n");
     printf ("� ������� ���������� ���������: ");
     scanf ("%d", &msqid);

     /* ���������� ��� ��������� */
     printf ("\n������� ������������� ����� - ");
     printf ("��� ���������: ");
     scanf ("%d", &msgp->mtype);

     /* ������ ���������� ��������� */
     printf ("\n������� ���������: \n");

     /* ����������� ������������������ CTRL+D
        ��������� ���� ��������� */

     /* ��������� ������� ���������
        � ��������� �� � ������ mtext */
     for (i = 0; ((c = getchar ()) != EOF); i++)
       sndbuf.mtext [i] = c;

     /* ���������� ������ ��������� */
     msgsz = i + 1;

     /* ������ ����� ����������� ��������� */
     for (i = 0; i < msgsz; i++)
       putchar (sndbuf.mtext [i]);

     /* ���������� ���� IPC_NOWAIT, ���� ��� ����� */
     printf ("\n������� 1, ���� ������ ���������� ");
     printf ("���� IPC_NOWAIT: ");
     scanf ("%d", &flag);
     if (flag == 1)
       msgflg = IPC_NOWAIT;
     else
       msgflg = 0;

     /* ��������� ���� */
     printf ("\n���� = 0%o\n", msgflg);

     /* ������� ��������� */
     rtrn = msgsnd (msqid, msgp, msgsz, msgflg);
     if (rtrn == -1) {
       printf ("\nmsgsnd ���������� ��������!\n");
       printf ("��� ������ = %d\n", errno);
     }
     else {
       /* ������� ���������; ��� ��������
          ���������� �� ������ ��������� ���� */
       printf ("\n��������� = %d\n", rtrn);

       /* ������� ������ ��������� */
       printf ("\n������ ��������� = %d\n", msgsz);

       /* ����� ���������� ��������� ������ */
       msgctl (msqid, IPC_STAT, buf);

       /* ������� ������������ ���� */
       printf ("����� ��������� � ������� = %d\n",
               buf->msg_qnum);
       printf ("��-� ���������� ����������� = %d\n",
               buf->msg_lspid);
       printf ("����� ���������� ����������� = %d\n",
               buf->msg_stime);
     }
   }

   if (choice == 2) {
     /* ������� ��������� */
     msgp = &rcvbuf;

     /* ���������� ������ ������� ��������� */
     printf ("\n������� ��-� ������� ���������: ");
     scanf ("%d", &msqid);

     /* ���������� ��� ��������� */
     printf ("\n������� ��� ���������: ");
     scanf ("%d", &msgtyp);

     /* ������������ ����������� �����
        ��� ��������� �������� */
     printf ("\n������� ���, �������������� ");
     printf ("������ ���������� ������:\n");
     printf ("  ��� ������               = 0\n");
     printf ("  MSG_NOERROR              = 1\n");
     printf ("  IPC_NOWAIT               = 2\n");
     printf ("  MSG_NOERROR � IPC_NOWAIT = 3\n");
     printf ("  �����                    = ");
     scanf ("%d", &flags);

     switch (flags) {
       /* ���������� msgflg ��� �������� ���
          ��������������� �������� */
       case 0:
         msgflg = 0;
         break;
       case 1:
         msgflg = MSG_NOERROR;
         break;
       case 2:
         msgflg = IPC_NOWAIT;
         break;
       case 3:
         msgflg = MSG_NOERROR | IPC_NOWAIT;
         break;
     }

     /* ����������, ����� ����� ���� ������� */
     printf ("\n������� ����� ����, ������� ");
     printf ("����� ������� (msgsz): ");
     scanf ("%d", &msgsz);

     /* ��������� �������� ���������� */
     printf ("\n������������� msqid = %d\n", msqid);
     printf ("��� ��������� = %d\n", msgtyp);
     printf ("����� ���� = %d\n", msgsz);
     printf ("����� = %o\n", msgflg);

     /* ������� msgrcv ��� ������ ��������� */
     rtrn = msgrcv (msqid, msgp, msgsz, msgtyp, msgflg);

     if (rtrn == -1) {
       printf ("\nmsgrcv ���������� ��������!\n");
       printf ("��� o����� = %d\n", errno);
     }
     else {
       printf ("\nmsgrcv ���������� �������,\n");
       printf ("������������� ������� = %d\n", msqid);

         /* ���������� ����� �������� ����,
            ��� ����� ������������� �������� */
         printf ("������� ����: %d\n", rtrn);

         /* ����������� �������� ��������� */
         for (i = 0; i < rtrn; i++)
           putchar (rcvbuf.mtext [i]);
     }

     /* ����� ��������������� ��������� ������ */
     msgctl (msqid, IPC_STAT, buf);

     printf ("\n����� ��������� � ������� = %d\n",
             buf->msg_qnum);
     printf ("��-� ���������� ���������� = %d\n",
             buf->msg_lrpid);
     printf ("����� ���������� ��������� = %d\n",
             buf->msg_rtime);
   }

   exit (0);
 }



Alex Otwagin 2002-12-16



������:
PostgresPro
Inferno Solutions
Hosting by Hoster.ru
�������:

�������� �� �����
���������� �� ���������
Created 1996-2026 by Maxim Chirkov
��������, ����������, ����������