#include #include #include #include #include #include #include #include #include #include #include /* * Disclaimer: * Warning! This code could damage your eyes! * It could make them tear untill you get to the end. * Hint: Use lots of coffee! */ /* * Variable to store the maximum number of random numbers * to be generated. */ int MAX_INPUT=100; /* * These are for the input and * output files */ FILE *file_des_in; FILE *file_des_out; /* * Filenames used */ char *tempfile="tempfile.dat"; char *input_file="input.dat"; char *output_file="output.dat"; //Not quite sure what this one is volatile int gen=0; /* * Function definitions */ char* file_read(); //to read from the common file int file_store(char *); //to output to the common file int generate(); //generates random numbers char *notify_number(char *); //notifies for the number that produces or consumes int consume_number(char *); //self-explanatory int generate_sleep(); //generates random sleep time (different) void parent_sigset_catcher(int);//self-explanatory void child_sigset_catcher(int); //self-explanatory void generate_store_numbers(); //generates the input file for the parent char *get_number(FILE *); //grabs a number from input file void leave(); //as a parameter to atexit() int consume_number(char *the_num) { char temp[50]; /* * This one just displays */ sprintf(temp, "child %d consumes number %s\n", getpid(), the_num); write(1, temp, strlen(temp)); fflush(stdout); return(0); } char *notify_number(char *the_num) { char temp[50]; /* * This one displays but also * returns the value for the "chain reaction" */ sprintf(temp, "parent %d generates number %s", getpid(), the_num); write(1, temp, strlen(temp)); return(the_num); } int generate() { return((rand()%33)+1); } int file_store(char *num) { char temp_var[10]; int filedes; /* * Creates it and puts it in * and closes it */ filedes=creat(tempfile, 0644); filedes=open(tempfile, O_WRONLY, 0644); sprintf(temp_var, "%s", num); write(filedes, temp_var, sizeof(temp_var)); close(filedes); return(filedes); } char* file_read() { char temp_char[10]; int filedes; /* * reads value and erases the file */ filedes=open(tempfile, O_RDONLY); read(filedes, temp_char, sizeof(temp_char)); close(filedes); unlink(tempfile); /* * This statement does the * main consuming but since they are * chained then it doesn't matter too much * where i put it */ fprintf(file_des_out,"%s", temp_char); return(temp_char); } int generate_sleep() { //Relatively smaller time //We don't want to wait too much return((rand()%5)+1); } void generate_store_numbers() { char temp_var[10]; int i, num; FILE *filedes; /* * Creates the input file * generates the random values one * by one and stores them */ creat(input_file, 0644); filedes=fopen(input_file,"w"); srand((unsigned) getpid()); for (i=0; idisplay it->put it in common file file_store(notify_number(get_number(file_des_in))); /* * and do it till the maximum requested by the * user is reached */ for(p=0; p