0

Hi. I found this code online:

#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/uio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

/***********************************************************************
 *      Global Constants
 * ********************************************************************/
#define A_ROW 2
#define A_COL 3
#define B_ROW 3
#define B_COL 2
#define ARGUMENT_COUNT 13
#define PIPE_QTY 4

/***********************************************************************
 *      Global Variables
 * ********************************************************************/
int A[A_ROW][A_COL];
int B[B_ROW][B_COL];

/***********************************************************************
 *      Function Prototypes
 * ********************************************************************/

/*
 *  Verifies the command line input is valid
 *  @args argc number of command line arguments passed to program from
 *  user to main
 *  @args *argv[] array of c strings containing command line arguments
 *  @return 1 if valid 0 if invalid
 * */
int verifyInput( int argc, char *argv[] );

/*
 *  Verifies the command line argument is an integer
 *  @args *argV c string containing a command line argument
 *  @return 1 if valid 0 for invalid
 * */
int verifyArgument( char *argVi );

/*
 *  Loads comand line data into global arrays A[][] & B[][]
 *  @param argc number of command line arguments
 *  @param *argv[] array of c strings holding command line arguments
 * */
void loadVars(int argc, char *argv[]);

/*
 *  Prints Contents of global array A[][]
 * */
void printA();

/*
 *  Prints Contents of global array B[][]
 * */
void printB();

/*
 *  Creates pipes exits on failure
 *  @param int *p1... *p4 pipe FD's
 * */
void createPipes( int *p1, int *p2, int *p3, int *p4 );


/*
 *  Matrix multiplication rowA x colB
 *  @param int rowA the row index for global matrix A
 *  @param int colB the column index for global matrix B
 *  @return matrix multiplication result
 * */
int matrixMultipy( int rowA, int colB );

/***********************************************************************
 *      Main
 * ********************************************************************/
int main(int argc, char *argv[ ]) {
    int p1[2], p2[2], p3[2], p4[2];
    int status, c0, c1, c2, c3;
    pid_t pid1, pid2, pid3, pid4, pidDisp;
    if( !verifyInput(argc, argv) ) {
        return EXIT_FAILURE;
    }
    loadVars(argc,argv);
    printA();
    printB();
    createPipes(p1, p2, p3,p4);     
    /***********************************
     *  Forking Section
     * ********************************/

    /***************************************
     *  First Fork
     * ************************************/
    printf("Creating child process 1\n");
    pid1 = fork();
    switch(pid1) {
        case -1: //Error
            printf("Failure on first fork call\n");
            exit(EXIT_FAILURE);
        case 0: //Child
            printf("Child process 1 created pid: %d from parent: %d\n", getpid(), getppid());
            close(p1[0]);
            c0 = matrixMultipy(0,0);
            write(p1[1], &c0, sizeof(int));
            close(p1[1]);
            printf("c[0] computed: %d\n", c0);
            printf("Exiting child process 1\n");
            exit(EXIT_SUCCESS);
        default: //Parent
            /*****************************************
             *  2nd Fork
             * *************************************/
            printf("Creating child process 2\n");
            pid2 = fork();
            switch(pid2) {
                case -1://Error
                    printf("Failure on 2nd fork call\n");
                    exit(EXIT_FAILURE);
                case 0: //Child
                    printf("Child process 2 created pid: %d from parent: %d\n", getpid(), getppid());
                    close(p2[0]);
                    c1 = matrixMultipy(0,1);
                    write(p2[1], &c1, sizeof(int));
                    close(p2[1]);
                    printf("c[1] computed: %d\n", c1);
                    printf("Exiting child process 2\n");
                    exit(EXIT_SUCCESS);
                default: //Parent
                    /**********************************
                     *  3rd Fork
                     * ******************************/
                    printf("Creating child process 3\n");
                    pid3 = fork();
                    switch(pid3) {
                        case -1: //Error
                            printf("Failure on 3rd fork call\n");
                            exit(EXIT_FAILURE);
                        case 0: //Child
                            printf("Child process 3 created pid: %d from parent: %d\n", getpid(), getppid());
                            close(p3[0]);
                            c2 = matrixMultipy(1,0);
                            write(p3[1], &c2, sizeof(int));
                            close(p3[1]);
                            printf("c[2] computed: %d\n", c2);
                            printf("Exiting child process 3\n");
                            exit(EXIT_SUCCESS);
                        default: //Parent
                        /*******************************
                         *  4th Fork
                         * ******************************/
                            printf("Creating child process 4\n");
                            pid4 = fork();
                            switch( pid4 ) {
                                case -1://Error
                                    printf("Failure on 4th fork\n");
                                    exit(EXIT_FAILURE);
                                case 0://Child
                                    printf("Child process 4 created pid: %d from parent: %d\n", getpid(), getppid());
                                    close(p4[0]);
                                    c3 = matrixMultipy(1,1);
                                    write(p4[1], &c3, sizeof(int));
                                    close(p4[1]);
                                    printf("c[3] computed: %d\n", c3);
                                    printf("Exiting child process 4\n");
                                    exit(EXIT_SUCCESS);
                                default://Parent
                                /************************************
                                 *  Final fork to display proccess
                                 * *********************************/
                                    printf("Creating diplay process\n");
                                    pidDisp = fork();
                                    switch(pidDisp) {
                                        case -1:
                                            printf("Failure to fork diplay process");
                                            exit(EXIT_FAILURE);
                                        case 0:
                                            printf("Display process created pid: %d from parent: %d\n", getpid(), getppid());
                                            close(p1[1]);
                                            read(p1[0], &c0, sizeof(int));
                                            close(p1[0]);

                                            close(p2[1]);
                                            read(p2[0], &c1, sizeof(int));
                                            close(p2[0]);

                                            close(p3[1]);
                                            read(p3[0], &c2, sizeof(int));
                                            close(p3[0]);

                                            close(p4[1]);
                                            read(p4[0], &c3, sizeof(int));
                                            close(p4[0]);

                                            printf("C = \t[%d]\t[%d]\n\t[%d]\t[%d]\n", c0, c1, c2, c3);
                                            printf("Exiting display process\n");
                                            exit(EXIT_SUCCESS);
                                        default:
                                            printf("Parent waiting for display process\n");
                                            waitpid(pidDisp, &status,  0);
                                    }
                                    printf("Parent waiting for child process 4\n");
                                    waitpid(pid4, &status, 0);
                            }
                            printf("Parent waiting for child process 3\n");
                            waitpid(pid3, &status, 0);
                    }   
                    printf("Parent waiting for child process 2\n");
                    waitpid(pid2, &status, 0);
            }
            printf("Parent waiting for child process 1\n");
            waitpid(pid1, &status, 0);
            printf("Parent exiting successfully\n");
            exit(EXIT_SUCCESS);
    }
    return EXIT_FAILURE;
}

/***********************************************************************
 *      Function Implementations
 * ********************************************************************/
int verifyInput( int argc, char *argv[] ) {
    if( argc != ARGUMENT_COUNT) {
        printf("Invalid number of arguments\n");
        return 0;
    }
    int i = argc - 1;
    for( ; i > 0; i--) {
        if( !verifyArgument(argv[i]) ) {
            return 0;
        }
    }
    printf("Input: ok\n");
    return 1;
}

int verifyArgument( char *argVi ) {
    int i = 0,
        l = strlen(argVi);
    while( i < l ) {
        if( !isdigit(argVi[i]) ) {
            if( !( i == 0 && (argVi[i] == '-' || argVi[i] == '+')) ) {
                printf("Invalid input type: %s\n", argVi);
                return 0;
            }
        }
        i++;
    }
    return 1;
}

void loadVars(int argc, char *argv[]) {
    int i = 1;
    for(; i < argc; i++) {
        switch( i - 1 ) {
            case 0:
            case 1:
            case 2:
                A[0][i - 1] = atoi(argv[i]);
                break;
            case 3:
            case 4:
            case 5:
                A[1][(i-1)%A_COL] = atoi(argv[i]);
                break;
            case 6:
            case 7:
                B[0][(i-1)%B_COL] = atoi(argv[i]);
                break;
            case 8:
            case 9:
                B[1][(i-1)%B_COL] = atoi(argv[i]);
                break;
            case 10:
            case 11:
                B[2][(i-1)%B_COL] = atoi(argv[i]);
                break;
        }       
    }
}

void printA() {
    int i = 0, j = 0;
    printf( "A = " );
    for(; i < A_ROW; i++ ) {
        printf("\t");
        for(j = 0; j < A_COL; j++) {
            printf("[%d] ", A[i][j]);
        }
        printf("\n");
    }
    printf("\n");
}

void printB() {
    int i = 0, j = 0;
    printf( "B = " );
    for(; i < B_ROW; i++ ) {
        printf("\t");
        for(j = 0; j < B_COL; j++) {
            printf("[%d] ", B[i][j]);
        }
        printf("\n");
    }
    printf("\n");
}


void createPipes(int *p1, int *p2, int *p3, int *p4) {
    if( pipe(p1) < 0 ) {
        printf("Pipe 1 failure\n");
        exit(EXIT_FAILURE);
    }
    if( pipe(p2) < 0 ) {
        printf("Pipe 2 failure\n");
        exit(EXIT_FAILURE);
    }
    if( pipe(p3) < 0 ) {
        printf("Pipe 3 failure\n");
        exit(EXIT_FAILURE);
    }
    if( pipe(p4) < 0 ) {
        printf("Pipe 4 failure\n");
        exit(EXIT_FAILURE);
    }   
}

int matrixMultipy( int rowA, int colB ) {
    int i, result = 0;
    for( i = 0; i < A_COL; i++ ) {
        result += A[rowA][i] * B[i][colB];
    }
    return result;
}

It compiles and everything, the problem is that I dont know how to insert the correct number of arguments. Help please.

2
Contributors
1
Reply
11
Views
3 Years
Discussion Span
Last Post by Ancient Dragon
1

If you wrote that code then you should know what command-line arguments it needs. Looks like it takes 11 integers on the command line.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.