Advertisement
Guest User

Untitled

a guest
May 19th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.06 KB | None | 0 0
  1. komunikaty
  2. 2 procesy
  3.  
  4. program ma tr\worzyc 2 procesy komunikujace sie za pomoca mechanixzmu komunikatow. proces serwer(odbioraca)
  5. musi utworzyc kanal polaczeniowy, a klient musi sie do niego podlaczyc i wtedy przeslac komunikat,
  6. wazne jest aby role podczas procesu sie nie zmienialy
  7.  
  8. 2 procesy w 1 programie(procesy potomne)
  9. funkcja fork(rozwidlanie procesow).
  10.  
  11. help: tam gdzie sygnaly tylko duzo wczesniej.
  12.  
  13.  #include <stdlib.h>
  14. #include <stdio.h>
  15.  
  16. #include <sys/neutrino.h>
  17. #include <errno.h>
  18. #include <process.h>
  19.  
  20.  
  21. int main(int argc, char *argv[])
  22. {
  23.   int connection_id = 0;
  24.   int rcvid = 0;
  25.   char Incommingbuffer[500];
  26.   struct _pulse * p;
  27.   struct _msg_info info;
  28.  
  29.  
  30.  
  31.  
  32.   // create channel
  33.   if ( (connection_id = ChannelCreate(_NTO_CHF_DISCONNECT )) == -1)
  34.   {
  35.      printf("Nie udaol sie stworzy kanau\n");
  36.      return 0;
  37.   }
  38.  
  39.   printf("Connection id is %d\n", connection_id);
  40.   printf("Process id is %d\n", getpid() );
  41.  
  42.   for ( ; ; )
  43.   {
  44.  
  45.      memset(Incommingbuffer, '\0',sizeof(info));
  46.  
  47.      rcvid = MsgReceive(connection_id, Incommingbuffer, sizeof(info), &info);
  48.  
  49.      printf("rcvid id %d \n", rcvid);
  50.      printf("Message is %s \n", Incommingbuffer);
  51.  
  52.      if (rcvid == 0 )
  53.      {
  54.         // got a pulse indicating logger died
  55.         p = (void *)Incommingbuffer;
  56.         if (p->code == _PULSE_CODE_DISCONNECT )
  57.         {
  58.            printf("Detected that the logger process died\n");
  59.            exit(-1);
  60.         }
  61.  
  62.      }
  63.      else
  64.      {
  65.         MsgReply(rcvid, EOK, NULL, 0);
  66.  
  67.      }
  68.    
  69.  
  70.   }
  71.  
  72.  
  73.  
  74.   return EXIT_SUCCESS;
  75. }
  76.  
  77. -------------------------------------------------------------------- 1 ----------------------------------------------
  78. #include <stdlib.h>
  79. #include <stdio.h>
  80. #include <pthread.h>
  81. #include <time.h>
  82. #define uint unsigned int
  83.  
  84. void* thread1(void* );
  85. void* thread2(void* );
  86.  
  87. sigset_t set;
  88.  
  89.  
  90. int main(int argc, char *argv[]) {
  91.  
  92.    pthread_t thrd1;
  93.    pthread_t thrd2;
  94.    sigemptyset( &set );
  95.    sigaddset( &set, 41 );
  96.    sigaddset( &set, 42 );
  97.    pthread_create(&thrd1,NULL,thread1,NULL);
  98.    pthread_create(&thrd2,NULL,thread2,NULL);
  99.  
  100.  
  101.  
  102.        fflush(stdout);
  103.  
  104.        while(1){}
  105.        return EXIT_SUCCESS;
  106. }
  107.  
  108.  
  109. void* thread1(void* not_used) {
  110.        extern void handler1();
  111.        struct sigaction action1;
  112.        sigset_t set;
  113.        //siginfo_t info;
  114.        action1.sa_flags = 0;
  115.    action1.sa_mask = set;
  116.    action1.sa_handler = &handler1;
  117.    //sigaddset( &set, SIGUSR1 );
  118.        sigaction( 41, &action1, NULL );
  119.        
  120.        //sigwaitinfo(&set, &info);
  121.  
  122.  
  123.        return NULL;
  124. }
  125.  
  126. void* thread2(void* not_used) {
  127.        extern void handler2();
  128.        struct sigaction action2;
  129.        sigset_t set;
  130.       // siginfo_t info;
  131.         action2.sa_flags = 0;
  132.    action2.sa_mask = set;
  133.    action2.sa_handler = &handler2;
  134.        sigaction( 42, &action2, NULL );
  135.      
  136.  
  137.        return NULL;
  138. }
  139.  
  140.  
  141. void handler1( signo ) {
  142.    printf("Otrzymano sygnal 1");
  143.    fflush(stdout);
  144.  }
  145.  
  146.  void handler2( signo ) {
  147.    printf("Otrzymano sygnal 2");
  148.    fflush(stdout);
  149.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement