Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7031343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:47:28+00:00 2026-05-28T00:47:28+00:00

I would really love some debugging help in this one. I’ve been working on

  • 0

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.c http://pastebin.com/iMCefaLZ
  • compute.c http://pastebin.com/Ejp214Up
  • pinakes.txt http://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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T00:47:28+00:00Added an answer on May 28, 2026 at 12:47 am

    The bad address problem arises because you run:

    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){
    

    The arguments to execl() must be strings; i and j are manifestly not strings (and sid1, sid2 and sid3 are 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 via execl() 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.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would really love some help on this one. What I have is a
I would really love some help with this. Currently when I use the following
I would love some help on a particular design. This is the code I
I would really love to push for TDD development inside the shop I'm working
I would really appreciate it if some of you could help optimize my tables,
This is something I've pondered/struggled with and would love to hear some opinions on.
So I'm getting really sick of E*TRADE and, being a developer, would love to
I would really appreciate any suggestions, no matter how simple or complex, to help
I would really appreciate some feedback on what I am trying to achieve: The
I would really like to do something like this: Callable<MyObject> myCallable = .... Future<MyObject>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.