Advertisement
filipao223

solucao_Teste2016.c

Nov 25th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.18 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <signal.h>
  5. #include <sys/wait.h>
  6. #include <sys/time.h>
  7. #include <sys/select.h>
  8. #include <sys/types.h>
  9. #include <sys/socket.h>
  10. #include <sys/stat.h>
  11. #include <fcntl.h>
  12. #include <unistd.h>
  13. #include <sys/errno.h>
  14.  
  15. #define N_NEWSTEAMS 10
  16. #define EXAMPLE_NEWS 5
  17. #define NEWS_MAX_SIZE 512
  18.  
  19. int main_pid;
  20. int pipes [N_NEWSTEAMS][2];
  21. fd_set read_set;
  22.  
  23. // Just some news used as an example
  24.  
  25. char example_news[EXAMPLE_NEWS][NEWS_MAX_SIZE] = {
  26. "all about the money",
  27. "food for the soul",
  28. "home team wins",
  29. "an amazing discovery",
  30. "newest tec came out"
  31. };
  32.  
  33. // News structure that will be used to send news through the pipes
  34. typedef struct _news {
  35.   int team;
  36.   char newsText[NEWS_MAX_SIZE];
  37. } news;
  38.  
  39. news news_data;
  40.  
  41. //Treatment of signal generated by Ctrl+C
  42. //Editor waits for all the NEWSTEAMS to finish and removes resources (closes all open
  43. //pipes); NEWSTEAMS clean used resources (close all open pipes). Each process prints a
  44. //message to the screen (see example output)
  45.  
  46. void sigint(int signum) {
  47.   /*COMPLETE*/
  48.   printf("^C pressed. Shutting down news team [%d]\n", getpid());
  49.   for(int i=0;i<N_NEWSTEAMS; i++){
  50.     close(pipes[i][0]);
  51.     close(pipes[i][1]);
  52.     wait(NULL);
  53.   }
  54.   /*END COMPLETE*/
  55.   exit(0);
  56. }
  57.  
  58. // Code run by NEWSTEAMS
  59. void newsTeam(int channel[], int i){
  60.   news_data.team = i;
  61.   srand(i);
  62.   // *** COMPLETE: Handle CTRL-C ***
  63.   signal(SIGINT, sigint);
  64.  
  65.   // *** END COMPLETE ***
  66.   while(1){
  67.     //pick a random news content
  68.     strcpy(news_data.newsText , example_news[rand() % EXAMPLE_NEWS]);
  69.     printf("News team %d Submitting news: \"%s\" \n", news_data.team,news_data.newsText);
  70.     /*COMPLETE Send news to pipe and sleep between 1 to 3 seconds */
  71.     close(channel[0]);
  72.     write(channel[1], &news_data, sizeof(news_data));
  73.     sleep(rand()%4+1);
  74.     /*END COMPLETE*/
  75.   }
  76. }
  77.  
  78. // Main
  79. int main() {
  80.   main_pid = getpid();
  81.   // *** COMPLETE: Ignore CTRL-C ***
  82.   signal(SIGINT, SIG_IGN);
  83.  
  84.   // *** END COMPLETE ***
  85.  
  86.   // *** COMPLETE: Create NEWSTEAMS (processes and pipes) ***
  87.   for(int i=0; i<N_NEWSTEAMS; i++){
  88.     pipe(pipes[i]);
  89.   }
  90.  
  91.   for(int i=0; i<N_NEWSTEAMS; i++){
  92.     if(fork()==0){
  93.       newsTeam(pipes[i], i);
  94.       exit(0);
  95.     }
  96.   }
  97.  
  98.   usleep(10);
  99.  
  100.   // *** END COMPLETE ***
  101.   // Editor
  102.   fd_set read_set;
  103.   int received=0;
  104.   // *** COMPLETE: Handle CTRL-C***
  105.   signal(SIGINT, sigint);
  106.   // *** END COMPLETE ***
  107.  
  108.   printf("Listening to pipes\n");
  109.  
  110.   while (1) {
  111.     // *** COMPLETE: Read from Pipes, publish only 10% ***
  112.     FD_ZERO(&read_set);
  113.     for(int i=0; i<N_NEWSTEAMS; i++){
  114.       FD_SET(pipes[i][0], &read_set);
  115.     }
  116.     if(select(pipes[N_NEWSTEAMS-1][0]+1, &read_set, NULL, NULL, NULL) > 0){
  117.       for(int i=0; i<N_NEWSTEAMS; i++){
  118.         if(FD_ISSET(pipes[i][0], &read_set)){
  119.           news new;
  120.           read(pipes[i][0], &new, sizeof(new));
  121.           received++;
  122.           if(received >=10){
  123.             printf("NEWS FLASH: team[%d]: %s\n", new.team, new.newsText);
  124.             fflush(stdout);
  125.             received = 0;
  126.           }
  127.         }
  128.       }
  129.     }
  130.     // *** END COMPLETE ***
  131.  
  132.   } // While(1)
  133. return 0;
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement