Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <signal.h>
- #include <sys/wait.h>
- #include <sys/time.h>
- #include <sys/select.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <sys/errno.h>
- #define N_NEWSTEAMS 10
- #define EXAMPLE_NEWS 5
- #define NEWS_MAX_SIZE 512
- int main_pid;
- int pipes [N_NEWSTEAMS][2];
- fd_set read_set;
- // Just some news used as an example
- char example_news[EXAMPLE_NEWS][NEWS_MAX_SIZE] = {
- "all about the money",
- "food for the soul",
- "home team wins",
- "an amazing discovery",
- "newest tec came out"
- };
- // News structure that will be used to send news through the pipes
- typedef struct _news {
- int team;
- char newsText[NEWS_MAX_SIZE];
- } news;
- news news_data;
- //Treatment of signal generated by Ctrl+C
- //Editor waits for all the NEWSTEAMS to finish and removes resources (closes all open
- //pipes); NEWSTEAMS clean used resources (close all open pipes). Each process prints a
- //message to the screen (see example output)
- void sigint(int signum) {
- /*COMPLETE*/
- printf("^C pressed. Shutting down news team [%d]\n", getpid());
- for(int i=0;i<N_NEWSTEAMS; i++){
- close(pipes[i][0]);
- close(pipes[i][1]);
- wait(NULL);
- }
- /*END COMPLETE*/
- exit(0);
- }
- // Code run by NEWSTEAMS
- void newsTeam(int channel[], int i){
- news_data.team = i;
- srand(i);
- // *** COMPLETE: Handle CTRL-C ***
- signal(SIGINT, sigint);
- // *** END COMPLETE ***
- while(1){
- //pick a random news content
- strcpy(news_data.newsText , example_news[rand() % EXAMPLE_NEWS]);
- printf("News team %d Submitting news: \"%s\" \n", news_data.team,news_data.newsText);
- /*COMPLETE Send news to pipe and sleep between 1 to 3 seconds */
- close(channel[0]);
- write(channel[1], &news_data, sizeof(news_data));
- sleep(rand()%4+1);
- /*END COMPLETE*/
- }
- }
- // Main
- int main() {
- main_pid = getpid();
- // *** COMPLETE: Ignore CTRL-C ***
- signal(SIGINT, SIG_IGN);
- // *** END COMPLETE ***
- // *** COMPLETE: Create NEWSTEAMS (processes and pipes) ***
- for(int i=0; i<N_NEWSTEAMS; i++){
- pipe(pipes[i]);
- }
- for(int i=0; i<N_NEWSTEAMS; i++){
- if(fork()==0){
- newsTeam(pipes[i], i);
- exit(0);
- }
- }
- usleep(10);
- // *** END COMPLETE ***
- // Editor
- fd_set read_set;
- int received=0;
- // *** COMPLETE: Handle CTRL-C***
- signal(SIGINT, sigint);
- // *** END COMPLETE ***
- printf("Listening to pipes\n");
- while (1) {
- // *** COMPLETE: Read from Pipes, publish only 10% ***
- FD_ZERO(&read_set);
- for(int i=0; i<N_NEWSTEAMS; i++){
- FD_SET(pipes[i][0], &read_set);
- }
- if(select(pipes[N_NEWSTEAMS-1][0]+1, &read_set, NULL, NULL, NULL) > 0){
- for(int i=0; i<N_NEWSTEAMS; i++){
- if(FD_ISSET(pipes[i][0], &read_set)){
- news new;
- read(pipes[i][0], &new, sizeof(new));
- received++;
- if(received >=10){
- printf("NEWS FLASH: team[%d]: %s\n", new.team, new.newsText);
- fflush(stdout);
- received = 0;
- }
- }
- }
- }
- // *** END COMPLETE ***
- } // While(1)
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement