Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <unistd.h>
- #include <sys/wait.h>
- #include <memory.h>
- #include <sys/fcntl.h>
- #define VEC_SIZE 1000
- #define CHILD_NUMBER 5
- #define VEC_SLICE 200
- /* Creates a vector of CHILD_NUMBER pipes. */
- void create_pipes(int pipes[CHILD_NUMBER][2]) {
- int i;
- for (i = 0; i < CHILD_NUMBER; i++)
- /* In case of an error exits with code -1 */
- if (pipe(pipes[i]) == -1) {
- exit(-1);
- }
- }
- void initialize_array(int numbers[VEC_SIZE]) {
- time_t t; /* needed to initialize random number generator (RNG) */
- int i;
- /* intializes RNG (srand():stdlib.h; time(): time.h) */
- srand((unsigned) time(&t));
- /* initialize array with random numbers (rand(): stdlib.h) */
- for (i = 0; i < VEC_SIZE; i++) {
- numbers[i] = rand() % 255;
- }
- }
- /* Creates CHILD_NUMBER child processes, stores each pid in the pids array and returns each child process id. */
- int babymaker() {
- int i;
- pid_t p;
- for (i = 0; i < CHILD_NUMBER; i++) {
- p = fork();
- /* In case of an error exits with code -1 */
- if (p < 0) {
- exit(-1);
- }
- /* Child process returns it's id */
- if (p == 0) {
- return i + 1;
- }
- }
- /* Parent process returns 0 */
- return 0;
- }
- /* Closes all the reading descriptors and all the writing descriptors except the one that belongs to the child.*/
- void close_child_file_descriptors(int pipes[CHILD_NUMBER][2], int id) {
- int i;
- for (i = 0; i < CHILD_NUMBER; i++) {
- close(pipes[i][0]);
- if (id != i) {
- close(pipes[i][1]);
- }
- }
- }
- /* Closes all the writing file descriptors for the parent process */
- void close_parent_file_descriptors(int pipes[CHILD_NUMBER][2]) {
- int i;
- for (i = 0; i < CHILD_NUMBER; i++) {
- close(pipes[i][1]);
- }
- }
- /* Each child process sums two values from the corresponding part of the vectors (vec1 and vec2) and writes each value in the pipe,
- when all the corresponding values are written the process closes the file descriptor previously used to write in the pipe. */
- void sum_vectors(int id, const int vec1[VEC_SIZE], const int vec2[VEC_SIZE], int pipes[CHILD_NUMBER][2]) {
- int start = (id - 1) * VEC_SLICE, i, identifier = id - 1, sum;
- for (i = 0; i < VEC_SLICE; i++) {
- sum = vec1[start + i] + vec2[start + i];
- write(pipes[identifier][1], &sum, sizeof(sum));
- }
- close(pipes[identifier][1]);
- }
- /* The parent process reads all the values written in the pipes by the children processes, stores them in the res array
- * and when it's finished closes the file descriptor previously used to read from the pipe.*/
- void accumulate_results(int pipes[CHILD_NUMBER][2], int res[VEC_SIZE]) {
- int i, j, index = 0;
- for (i = 0; i < CHILD_NUMBER; i++) {
- for (j = 0; j < VEC_SLICE; j++) {
- read(pipes[i][0], &res[index], sizeof(res[index]));
- index++;
- }
- close(pipes[i][0]);
- }
- }
- /* Prints the values from vec1, vec2 and results arrays.*/
- void print_results(int vec1[VEC_SIZE], int vec2[VEC_SIZE], int results[VEC_SIZE]) {
- int i;
- for (i = 0; i < VEC_SIZE; i++)
- printf("%d + %d = %d\n", vec1[i], vec2[i], results[i]);
- }
- int main() {
- int vec1[VEC_SIZE], vec2[VEC_SIZE], res[VEC_SIZE];
- int id;
- int pipes[CHILD_NUMBER][2];
- initialize_array(vec1);
- initialize_array(vec2);
- create_pipes(pipes);
- id = babymaker(5);
- if (id != 0) {
- /* Child process */
- close_child_file_descriptors(pipes, id - 1);
- sum_vectors(id, vec1, vec2, pipes);
- exit(id);
- } else {
- /* Parent process */
- close_parent_file_descriptors(pipes);
- accumulate_results(pipes, res);
- print_results(vec1, vec2, res);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement