Advertisement
Guest User

Untitled

a guest
Mar 19th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.58 KB | None | 0 0
  1. ////////////////////////////////////////////////////////////////////////////////
  2. // server.c
  3. //
  4. // A QNX msg passing server.  It should receive a string from a client,
  5. // calculate a checksum on it, and reply back the client with the checksum.
  6. //
  7. // The server prints out its pid and chid so that the client can be made aware
  8. // of them.
  9. //
  10. // Using the comments below, put code in to complete the program.  Look up
  11. // function arguments in the course book or the QNX documentation.
  12. ////////////////////////////////////////////////////////////////////////////////
  13.  
  14. #include <ctype.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <errno.h>
  18. #include <sys/neutrino.h>
  19. #include <sys/netmgr.h>
  20. #include <process.h>
  21. #include <string.h>
  22. #include <unistd.h>
  23.  
  24. #define SIZE 100
  25. #define THREADS 10
  26.  
  27. typedef struct {
  28.         int  typ;            // Typ komunikatu
  29.         int  od;             // Numer procesu
  30.         char tekst[SIZE];    // Tekst komunikatu
  31. } kom_t;
  32.  
  33. int main(void) {
  34.     int chid;
  35.     int pid;
  36.     int i;
  37.     int donethreads = 0;
  38.  
  39.     chid = ChannelCreate(0); //PUT CODE HERE to create a channel
  40.     if(-1 == chid) {                //was there an error creating the channel?
  41.         perror("ChannelCreate()");  //look up the errno code and print
  42.         exit(EXIT_FAILURE);
  43.     }
  44.  
  45.     pid = getpid();                 //get our own pid
  46.     printf("Server's pid: %d, chid: %d\n", pid, chid); //print our pid/chid so
  47.                                                  //client can be told where to
  48.                                                  //connect
  49.  
  50.     for(i = 0; i < THREADS; ++i) {
  51.  
  52.         if(fork() == 0) {
  53.             int reply, coid;
  54.             kom_t s_msg;
  55.             coid = ConnectAttach(ND_LOCAL_NODE, pid, chid, _NTO_SIDE_CHANNEL, 0);
  56.  
  57.             if(coid > 0) {
  58.                 int j;
  59.                 for(j = 0; j < 9; ++j) {
  60.                     s_msg.typ = 0;
  61.                     s_msg.od = getpid();
  62.                     strcpy(s_msg.tekst, "Wiadomosc");
  63.                     MsgSend(coid, &s_msg, sizeof(s_msg), &reply, sizeof(reply));
  64.                 }
  65.  
  66.  
  67.                 s_msg.typ = 1;
  68.                 s_msg.od = getpid();
  69.                 strcpy(s_msg.tekst, "Koniec");
  70.                 MsgSend(coid, &s_msg, sizeof(s_msg), &reply, sizeof(reply));
  71.  
  72.                 return EXIT_SUCCESS;
  73.             } else {
  74.                 perror("ConnectAttach()");
  75.                 return EXIT_FAILURE;
  76.             }
  77.         }
  78.     }
  79.  
  80.     int rcvid;
  81.     int reply = 1;
  82.     kom_t rc_msg;
  83.  
  84.     do {
  85.         rcvid = MsgReceive(chid, &rc_msg, sizeof(rc_msg), NULL);
  86.         printf("Typ: %d, od: %d, tekst: %s\n", rc_msg.typ, rc_msg.od, rc_msg.tekst);
  87.         MsgReply(rcvid, EOK, &reply, sizeof(reply));
  88.  
  89.         if(rc_msg.typ) {
  90.             ++donethreads;
  91.         }
  92.     } while (donethreads != THREADS);
  93.  
  94.     int status;
  95.  
  96.     for(i = 0; i < 10; ++i) {
  97.         wait(&status);
  98.     }
  99.  
  100.     return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement