Advertisement
Guest User

qnx

a guest
Jan 23rd, 2020
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.67 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/types.h>
  4. #include <unistd.h>
  5. #include <string.h>
  6. #include <time.h>
  7. #include <sys/netmgr.h>
  8. #include <sys/neutrino.h>
  9. #include <process.h>
  10. #include <sys/mman.h>
  11. #include <fcntl.h>
  12. #include <errno.h>
  13.  
  14.  
  15. struct message {
  16.     char name[100];
  17.     int size;
  18. };
  19.  
  20.  
  21. int main(void)
  22. {
  23.  /* Try to create channel */
  24.     int channel_id;
  25.     if ((channel_id = ChannelCreate(0)) == -1){
  26.         printf("Channel create failed!!!");
  27.         return EXIT_FAILURE;
  28.     }  
  29. /* Try to fork(), if not exit with failure */
  30.     pid_t pid = fork();
  31.     if(pid == -1)
  32.     {
  33.         perror("fork");
  34.         return EXIT_FAILURE;
  35.     }else if(pid != 0)
  36.     {
  37.         /*parent*/
  38.         printf("Parent pid: %d\n",getpid());
  39.         printf("Parent channel_id: %d\n",channel_id);
  40.         struct msg m;
  41.         //char msg[100];
  42.         int message_id = MsgRecive(channel_id, &m, sizeof(m), NULL);
  43.         printf(Parent message_id\n",message_id);
  44.         printf("Parent recived message: %s\n",m.name);
  45.         int fd = shm_open( m.name, O_RDWR, 0777 );
  46.         int* addr = (int*)mmap( 0, m.size*sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 );
  47.  
  48.         for (int i=0; i<m.size;i++)
  49.             for(int j=0;j<m.size-1-i;j++)
  50.             {
  51.             //  if ((m.direction==0)?addr[j]>addr[j+1]:addr[j]<addr[j+1]){
  52.                 if (addr[j]>addr[j+1])
  53.                 {
  54.                 int tmp=addr[j];
  55.                 addr[j]=addr[j+1];
  56.                 addr[j+1]=tmp;
  57.                 }
  58.             }
  59.         char rmsg[100];
  60.         //sprintf(rmsg, "%d", strlen(msg));
  61.         MsgReply(message_id, 0, rmsg, strlen(rmsg)+1);
  62.         munmap(addr,m.size*sizeof(int));
  63.         close(fd);
  64.         ChannelDestroy(channel_id);
  65.     }else
  66.     {
  67.         /*child*/
  68.         int connection_id = ConnectAttach(0, getppid(), channel_id, 0, 0);
  69.         printf("Child connection_id: %d\n",connection_id);
  70.         printf("Child pid: %d\n"getpid());
  71.         //char msg[100] = "abcd";
  72.         //sleep(60);   
  73.         char name[100]="/shm_object";
  74.        int size=10;
  75.         char rmsg[100];
  76.         int fd = shm_open( "/shm_object", O_RDWR | O_CREAT, 0777 );
  77.        ftruncate(fd, size*sizeof(int));
  78.        int* addr = (int*)mmap( 0, size*sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 );
  79.         srand(time(NULL));
  80.        for (int i=0;i<size;i++){
  81.            addr[i]=rand()%100;
  82.        }
  83.        for (int i=0;i<size;i++){
  84.                    printf("%d\n",addr[i]);
  85.                }
  86.        printf("\n\n");
  87.        //sleep(30);
  88.         struct msg m;
  89.        strcpy(m.name,name);
  90.        m.size=size;
  91.        m.direction=1;
  92.  
  93.        MsgSend(connection_id, &m, sizeof(m), rmsg, 100);
  94.         printf("Child recived rep: %s\n",rmsg);
  95.         for (int i=0;i<size;i++)
  96.             printf("%d\n",addr[i]);
  97.         ConnectDetach(connection_id);
  98.         munmap(addr,size*sizeof(int));
  99.        close(fd);
  100.        shm_unlink("/shm_object");
  101.     }
  102.    
  103.     return EXIT_SUCCESS;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement