Advertisement
MagnusArias

qnxc

Jun 19th, 2017
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.34 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <sys/types.h>
  4. #include <sys/neutrino.h>
  5. #include <process.h>
  6. #include <unistd.h>
  7. #include <sys/mman.h>
  8. #include <fcntl.h>
  9. #include <errno.h>
  10. #include <time.h>
  11.  
  12. int printErrorMessage(char* message, char* shortMessage);
  13.  
  14. int main(int argc, char *argv[])
  15. {
  16.  
  17.     pid_t server,client;
  18.     int * _memAddress;
  19.     int _channel,i,j,temp;
  20.     int receiveid;
  21.     int _sharedMemory;
  22.     char _data1[10],_data2[10];
  23.     int connection;
  24.    
  25.     server = getpid();
  26.    
  27.     if ((_channel = ChannelCreate(0)) == -1)
  28.         printErrorMessage("Could not create channel", "ChannelCreate failure\n", errno);
  29.    
  30.     if ((client = fork()) == -1)
  31.         printErrorMessage("Could not fork processes", "fork failure\n", errno);
  32.    
  33.     if (client == 0)
  34.     {
  35.        
  36.         printf("START: CHILD process \n");
  37.            
  38.         if ((connection = ConnectAttach(0, server, _channel, 0, 0)) == -1)
  39.             printErrorMessage("Could not attach to channel", "ConnectAttach failure", errno);
  40.        
  41.         strcpy(_data1, "kaktus");
  42.  
  43.         _sharedMemory = shm_open(_data1, O_RDWR | O_CREAT, 0777);
  44.        
  45.         if (_sharedMemory == -1)
  46.             printErrorMessage("Could not open shared memory", "shm_open failure", errno);
  47.  
  48.         if (ftruncate(_sharedMemory, 10) == -1 )
  49.             printErrorMessage("Could not truncate.", "ftruncate failure", errno);
  50.  
  51.         _memAddress = (int*)mmap(0, 10, PROT_READ | PROT_WRITE, MAP_SHARED, _sharedMemory, 0 );
  52.        
  53.         if (_memAddress == MAP_FAILED)
  54.             printErrorMessage("CHILD process mmap failed", "mmap failure", errno);
  55.  
  56.         srand( time( NULL ) );
  57.        
  58.         for (i = 0; i < 10; i++){          
  59.             _memAddress[i] = rand() % 500 + 100;
  60.             printf("%d ", _memAddress[i]);
  61.         }
  62.        
  63.         printf("\n");
  64.        
  65.         if ((MsgSend(connection, &_data1, sizeof(_data1), &_data2, sizeof(_data2))) == -1)
  66.             printErrorMessage("MessageSend failed", "MsgSend failure", errno);
  67.        
  68.         for(i = 0; i < 10; i++)
  69.             printf("%d ",_memAddress[i]);
  70.        
  71.         printf("\n");
  72.        
  73.         shm_unlink(_data1);
  74.    
  75.         printf("END: CHILD process \n");
  76.     }
  77.     else {
  78.         int status;
  79.         pid_t waitForClient;
  80.    
  81.         //odbieranie
  82.         printf("START: MOTHER process\n");
  83.         if((receiveid = MsgReceive(_channel, &_data1, sizeof(_data1), NULL)) == -1)
  84.             printErrorMessage("Receiving message failed", "MsgReceive failure", errno);
  85.  
  86.         _sharedMemory = shm_open(_data1, O_RDWR | O_CREAT, 0777);
  87.        
  88.         if(_sharedMemory == -1)
  89.             printErrorMessage("Could not open shared memory", "shm_open failure", errno);
  90.        
  91.         _memAddress = (int*)mmap(0,10,PROT_READ | PROT_WRITE,MAP_SHARED, _sharedMemory, 0 );
  92.        
  93.         if(_memAddress == MAP_FAILED)
  94.             printErrorMessage("MOTHER process mmap failed", "mmap failure", errno);
  95.                
  96.         for(i = 0; i < 10; i++){
  97.             for( j=i+1;j<10;j++){
  98.                 if(_memAddress[i]>_memAddress[j]){
  99.                     temp = _memAddress[i];
  100.                     _memAddress[i] = _memAddress[j];
  101.                     _memAddress[j] = temp;
  102.                 }
  103.             }
  104.         }
  105.         close(_sharedMemory);
  106.        
  107.         if((MsgReply(receiveid,0,&_data1, sizeof(_data1))) == -1)
  108.             printErrorMessage("Message reply failed.", "Message error", errno);
  109.        
  110.         do {
  111.             waitForClient = waitpid(client, &status, 0);
  112.         } while(WIFEXITED(status) == 0 );
  113.        
  114.         printf("END: MOTHER process\n");
  115.     }
  116.  
  117.     return EXIT_SUCCESS;
  118. }
  119.  
  120.  
  121. int printErrorMEssage(char* message, char* shortMessage, int err){
  122.     printf(message);
  123.     printf("   %d \n", err);
  124.     perror(shortMessage);
  125.     return EXIT_FAILURE;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement