I would really love some debugging help in this one. I’ve been working on this since the morning and its 4am. (I’m suppose to deliver this in 7 hours [11am])
Everything in main.c works but when I create some child processes to run compute.c’s compiled file with execl it doesnt do it and sends an error of “Bad Address”.
I’ve attached 3 pastebin links with main.c and compute.c and a txt file containing the tables I mention below.
The program is suppose to read 2 tables with integers from a file called pinakes.txt and then by using POSIX’s shared memory API to place those tables in shared memory and create processes to calculate a ‘row * column’ sum from them and place that sum in another table.
sum += A[row][i] * B[i][column] = C[row][column]
Everything until the line below from main.c should work properly (I debugged it numerous times).
ppid = getpid();
main.chttp://pastebin.com/iMCefaLZcompute.chttp://pastebin.com/Ejp214Uppinakes.txthttp://pastebin.com/h8yKXFvv
compile and then run
./main pinakes.txt
main.c
188 lines of code
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <signal.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#include <errno.h>
int pinA_X = 0, pinA_Y = 0, pinB_X=0, pinB_Y=0;
int pinA[10][10], pinB[10][10], pinC[10][10];
main(int argc, char *argv[]){
int pid, ppid;
FILE *stream;
// general variables
int i, c, j, rc, converted, lines = 0;
//flags
int flagV=0, flagW=0, flagX=0, flagY=0, flagZ=0;
//shared memory
int dumpedArray[101];
int size = sizeof(dumpedArray);
int sid1 = shmget(IPC_PRIVATE, size, SHM_R | SHM_W);
int sid2 = shmget(IPC_PRIVATE, size, SHM_R | SHM_W);
int sid3 = shmget(IPC_PRIVATE, size, SHM_R | SHM_W);
int* shared_A = (int*) shmat(sid1, NULL, 0);
int* shared_B = (int*) shmat(sid2, NULL, 0);
int* shared_C = (int*) shmat(sid3, NULL, 0);
if(argc!=2){
printf("wrong number of arguments\n");
return -1;
}else{
stream = fopen(argv[1] , "r");
while((c = getc(stream))!= EOF){
if(flagZ == 0){
if(flagX == 1){pinA_X = c - 48;flagX = 0;}
if(c == 88){flagX = 1;}
if(flagY == 1){pinA_Y = c - 48;flagY = 0;}
if(c == 89){flagY = 1;}
if(c == 90){flagZ = 1;}
}else if(flagZ == 1){
if(flagX == 1){pinB_X = c - 48;flagX = 0;}
if(c == 88){flagX = 1;}
if(flagY == 1){pinB_Y = c - 48;flagY = 0;}
if(c == 89){flagY = 1;}
}
}
fclose(stream);
printf("pinA[%d][%d] * pinB[%d][%d] = C[%d][%d]\n\n", pinA_X, pinA_Y, pinB_X, pinB_Y, pinA_X, pinB_Y);
// get A
stream = fopen(argv[1] , "r");
i=0;j=0;
while((c = getc(stream))!= EOF){
if(i <= pinA_X && j <= pinA_Y){
if(flagW == 0){
if(c == 87){
flagW = 1;
}
}else{
if(c > 47 && c < 58){
pinA[i][j] = c - 48;
j++;
}
if(c == 13){
j=0;
i++;
}
}
}
}
fclose(stream);
// get B
stream = fopen(argv[1] , "r");
i=0;j=0;
while((c = getc(stream))!= EOF){
if(i <= pinB_X && j <= pinB_Y){
if(flagV == 0){
if(c == 86){
flagV = 1;
}
}else{
if(c > 47 && c < 58){
pinB[i][j] = c - 48;
j++;
}
if(c == 13){
j=0;
i++;
}
}
}
}
fclose(stream);
// print A
printf("A={\n");
for(j=0; j<pinA_X;j++){
for(i=0;i<pinA_Y;i++){
printf(" %d", pinA[j][i]);
}
printf("\n");
}
printf("}\n\n");
// print B
printf("B={\n");
for(j=0; j<pinB_X;j++){
for(i=0;i<pinB_Y;i++){
printf(" %d", pinB[j][i]);
}
printf("\n");
}
printf("}\n");
// Save pinA to shared Memory
converted = 0;
for(i=0;i<10;i++){
for(j=0;j<10;j++){
converted = (i * 10) + j;
shared_A[converted] = pinA[i][j];
}
}
// Save pinA to shared Memory
converted = 0;
for(i=0;i<10;i++){
for(j=0;j<10;j++){
converted = (i * 10) + j;
shared_B[converted] = pinB[i][j];
}
}
// Push size of arrays in shared memory
shared_A[100] = pinA_X;
shared_A[101] = pinA_Y;
shared_B[100] = pinB_X;
shared_B[101] = pinB_Y;
ppid = getpid();
for(i=0; i<pinA_X; i++){
for(j=0; j<pinB_Y; j++){
if(ppid == getpid()){
pid = fork();
if(pid==0){
if(execl("./compute", "compute", i, j, sid1, sid2, sid3, NULL) == -1){
printf("error exec\n");
printf("Error opening file: %s\n", strerror(errno));
};
}else if(pid<0){
printf("\nDen egine h fork!\n");
}else{
wait(0);
}
}
}
}
//print C
converted = 0;
printf("C={\n");
for(i=0;i<10;i++){
for(j=0;j<10;j++){
converted = (i * 10) + j;
pinC[i][j] = shared_C[converted];
printf(" %d", pinC[i][j]);
}
printf("\n");
}
printf("}\n");
}
}
Neither compute.c nor pintakes.txt is directly relevant to answering this question.
The bad address problem arises because you run:
The arguments to
execl()must be strings;iandjare manifestly not strings (andsid1,sid2andsid3are the identifiers for three chunks of shared memory).Convert those values to strings and try again.
Your program creates the shared memory with IPC_PRIVATE, so your code in
compute.c(which is executed viaexecl()is going to be hard to make work. You may get away with transferring the shared memory IDs like that; I’m not sure.I think I’d be using a single shared memory segment.
It also looked like your reading code is going to read the same data into the two arrays – but I may have been misreading it.
Finally, your PasteBin examples expire in 23 hours. That limits the usefulness of your question. You should really transfer the data into the question – with, I suggest, no tabs and tabstops set at 4 rather than 8. (Or use more functions to prevent such deep indentation.)