Question

System Programming in C

Explain what the problem is within the program. Fix the problem when you create a child process per column. The code is given below. So basically, after the child processes have successfully excuted their code, the final print statement does not give the correct values. It prints the original matrix rather than the multiplied matrix.

#include 
#include 
#include 
#include 
int main(int argc, char *argv[]){
	int row = 0;
	int column = 0;
	row = atoi(argv[1]);
	column = atoi(argv[2]);

	int *A = (int *) malloc((row * column) * sizeof(int)); 
	int i, j;
	printf("Enter the matrix elements:\n");
	for (i = 0; i < row; i++) {
		for (j = 0; j < column; j++) {
			printf("A[%d,%d] = ", i, j);
			scanf("%d", A + i * column + j);//matrix elements is stored in row-wise
		}
		printf("\n");
	}

	int n = 0;
	printf("Enter a number:");
	scanf("%d", &n);

	for(int i = 0; i < row; ++i){
		pid_t pid = fork();
		if (pid == -1){//not able to create child
			printf("parent PID %d => \n", getpid());
			for (j = 0; j < column; j++) {
				*(A + i * column + j) = n * (*(A + i * column + j));
				printf("%d * A[%d,%d] = %d\n", n, i, j, *(A + i * column + j));				
			}
		}
		else if (pid == 0) {//child
			printf("child%d PID: %d \n", i, getpid());
			for (j = 0; j < column; j++) {
				*(A + i * column + j) = n * (*(A + i * column + j));
				printf("%d * A[%d,%d] = %d\n", n, i, j, *(A + i * column + j));				
			}
			exit(0);
		}
	}
	
	wait(0);
	printf("Final matrix elements:\n");
	for (i = 0; i < row; i++) {
		for (j = 0; j < column; j++) {
			printf("A[%d,%d] = %d\n", i, j, *(A + i * column + j));		
		}
		printf("\n");
	}

	exit(0);
}



0 0
Add a comment Improve this question Transcribed image text
Request Professional Answer

Request Answer!

We need at least 8 more requests to produce the answer.

2 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the answer will be notified once they are available.
Know the answer?
Add Answer to:
System Programming in C
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • Please help me with this question (in C programming language and Unix) computer system book 3 edi...

    please help me with this question (in C programming language and Unix) computer system book 3 edition int *fun (int *x)f int value *xtt return &value; int main) int pl; int **p2; int a; char str [5] printf ("Enter a number:") scanf("%d", a) ; printf ("Enter a line:") gets (str); p1-malloc(sizeof (int)) *pl-p2; p2-malloc(sizeof (int)); pl-fun (a); n-0; for (pl-stripl<-str+5;pl++) if (isdigit (pl)) at+; return 0; Construct a table that shows for each line of code, what may be the...

  • In Programming language C - How would I convert my words char array into a string...

    In Programming language C - How would I convert my words char array into a string array so I can use the strcmp() function and alphabetically sort the words? #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> int main(int argc, char*argv[]){ int i =0; int j =0; int count =0; int length = strlen(argv[1]); for(i =0; i < length; i++){ if(isalpha(argv[1][i]) == 0 ||isdigit(argv[1][i] != 0)){ count ++; } printf("%c",argv[1][i]); } char *strings; int wordNum =0; int charNum =0; strings...

  • 1) Write a script(in Bash shell) that characterizes the application performance. Specifically, your script should automatically...

    1) Write a script(in Bash shell) that characterizes the application performance. Specifically, your script should automatically test both algorithms of the matrix math program for each of the array sizes shown in Table. Also, extend your script to automatically parse the output of the program. Table (Array Sizes in Test Suite) Algorithm 1 Algorithm 2 256 256 512 512 768 768 1024 1024 1280 1280 1536 1536 1792 1792 2048 2048 C source file // Adapted from https://gustavus.edu/+max/courses/F2011/MCS-284/labs/lab3/ // Max...

  • Modify the client server system program given below so that instead of sendto() and recvfrom(), you...

    Modify the client server system program given below so that instead of sendto() and recvfrom(), you use connect() and un-addresssed write() and read() calls. //Server.c #include #include #include #include #include #include #include #include #include #include # define PortNo 4567 # define BUFFER 1024 int main(int argc, char ** argv) { int ssd; int n; socklen_t len; char msg[BUFFER]; char clientmsg[BUFFER]; struct sockaddr_in server; struct sockaddr_in client; int max_iterations = 0; int count = 0, totalChar = 0, i = 0;...

  • ****C PROGRAMMING**** Why isn't the determinant part of my code not running? I have included the...

    ****C PROGRAMMING**** Why isn't the determinant part of my code not running? I have included the attachments below and would greatly appreciate some help. ---------------------------------------            case 6:                printf("You chose determinate!\n\n");                printf("Press 2 for 2x2\n OR \n Press 3 for 3x3 Matrix: ");                scanf("%d", &detChoice);                printf("\n");                if (detChoice == 2) {                    printf("Matrix: ");...

  • I'm having trouble sorting this square matrix (a 2d array that has number of rows and...

    I'm having trouble sorting this square matrix (a 2d array that has number of rows and same number of column n x n) using row-wise approach in C programming. Please help me program this in C to sort it using row-wise approach, here is the code: #include <stdio.h> #define MAX 100 int main() { int mat[MAX][MAX]; int i, j, m, n; int rowsum, columnsum, diagonalsum; int k; int magic = 0; int transpose[MAX][MAX]; printf("Enter the # of rows and columns...

  • Would u help me fixing this CODE With Debugging Code with GDB #include <stdio.h> #include <stdlib.h>...

    Would u help me fixing this CODE With Debugging Code with GDB #include <stdio.h> #include <stdlib.h> #define SIZE (10) typedef struct _debugLab { int i; char c; } debugLab; // Prototypes void PrintUsage(char *); void DebugOption1(void); void DebugOption2(void); int main(int argc, char **argv) { int option = 0; if (argc == 1) { PrintUsage(argv[0]); exit(0); } option = atoi(argv[1]); if (option == 1) { DebugOption1(); } else if (option == 2) { DebugOption2(); } else { PrintUsage(argv[0]); exit(0); } }...

  • operating system programming i need ans and explen after 20 min 2 When we are implementing...

    operating system programming i need ans and explen after 20 min 2 When we are implementing the following program, then we press "CTRL+C" on the keyboard; that will cause: #include <stdio.h> #include<signal.h> #include <stdlib.h> #include<unistd.h> void handle_sigint (int sig) { } printf("Caught signal $d\n", sig); int main(int argc, char *argv[]) { signal (SIGCONT, handle_sigint); while (1) { printf("the process id is $d \n",getpid()); sleep (1); } return 0; } O print the sentence" Caught signal 2" on the terminal 53,65,67,37,14,98,122,124,183...

  • Directions: use only the signal mechanism system calls don’t use ( wait() or pipe() )in this...

    Directions: use only the signal mechanism system calls don’t use ( wait() or pipe() )in this problem. You can still read/write from/to a file. You must use ( kill() and pause() ) system calls. rewrite code below to do kill and pause system calls #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/wait.h> // Function ptototypes int readX(); void writeX(int); int main() { int pid; // pid: used to keep track of the child process int x...

  • I want to change this C code so that instead of prompting the user to enter...

    I want to change this C code so that instead of prompting the user to enter the argument value, or requiring the argument be entered after by pressing the “enter” key, instead I want the code changed so that the program opens a file, (which include the input values) read in the elements and pass in the input file as a command line argument. (the input file includes the size of the matrix and the matrix values) Here's an example...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT