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 8811417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:21:29+00:00 2026-06-14T03:21:29+00:00

I started a week ago understanding and working with semaphores and shared memory, and

  • 0

I started a week ago understanding and working with semaphores and shared memory, and actually created this program; the problem is I can’t find anything wrong with it. I’ve been looking at it for hours and everything seems correct. The code compiles and i can create the build but when I execute it nothing happens.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ipc.h> 
#include <sys/shm.h>
#include <stdio.h>
#include <sys/fcntl.h>
#include <semaphore.h>

#define MAXCHILDS 4
#define MAX_SIZE 10
#define MAX_WRITES 4

typedef struct{
    int m[MAX_SIZE][MAX_SIZE];
} matrix;

/*fork variables*/
pid_t child[MAXCHILDS];
/*semphores variables */
sem_t *empty, *full, * mutex;
/*share memory id*/
int shmid;
/*shared memory array pointer */
matrix * sh_mem;
/*pointer to matrix*/
int **p;

void init(){
      /*create pointer to matrix*/
      p = &sh_mem->m;
     /*semaphores unlink and creation */    
     sem_unlink("EMPTY");
     empty=sem_open("EMPTY",O_CREAT|O_EXCL,0700,MAX_WRITES);
     sem_unlink("FULL");
     full=sem_open("FULL",O_CREAT|O_EXCL,0700,0);
     sem_unlink("MUTEX");
     mutex=sem_open("MUTEX",O_CREAT|O_EXCL,0700,1);
    /*initialize shared memory */
    shmid = shmget(IPC_PRIVATE,sizeof(matrix),IPC_CREAT|0777);
    /*map shared memory*/
    sh_mem = (matrix*)shmat(shmid,NULL,0);
    if(sh_mem== (matrix*)(-1)){
        perror("shmat");
    }
}

void writer(int ** m){
    int i,k;
    for(i = 0;i<MAX_SIZE;i++){
        for(k= 0;k<MAX_SIZE;k++){
            m[i][k] = 0;
        }
    }
}

void reader(int **m){
    int i = 0;
    int k = 0;
    for(i = 0;i<MAX_SIZE;i++){
        for(k= 0;k<MAX_SIZE;k++){
            printf(m[i][k]);
        }
        printf("\n");
    }
}

void terminate() {
  sem_close(empty);
  sem_close(full);
  sem_close(mutex);
  sem_unlink("EMPTY");
  sem_unlink("FULL");
  sem_unlink("MUTEX");
  shmctl(shmid, IPC_RMID, NULL);
}

int main(int argc, char **argv)
{
    int i;
    init();

    for(i = 0;i<MAXCHILDS;i++){
        if ((child[i] = fork()) < 0) // error occured
        {
            perror("Fork Failed");
            exit(1);
        }
        if ((child[i] = fork())==0){
            writer(sh_mem->m);
            exit(0);
        }
    }
    /*father*/  
    reader(sh_mem->m);
    wait(NULL);

    terminate();

    return 0;
}

The children are supposed to write the the matrix in shared memory, and the father is supposed to read the shared memory array and the print the matrix.
Can you help me with this? Thanks for the help …

  • 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-06-14T03:21:31+00:00Added an answer on June 14, 2026 at 3:21 am

    The primary error here is that reader and writer take a different type of argument than you’re passing to them, as gcc -Wall points out:

    test.c: In function ‘main’:
    test.c:92:13: warning: passing argument 1 of ‘writer’ from incompatible pointer type [enabled by default]
    test.c:49:6: note: expected ‘int **’ but argument is of type ‘int (*)[10]’
    test.c:97:5: warning: passing argument 1 of ‘reader’ from incompatible pointer type [enabled by default]
    test.c:58:6: note: expected ‘int **’ but argument is of type ‘int (*)[10]’
    

    As provided, the program segfaulted in the parent and every child. When I changed the parameter type of reader and writer from int **m to int m[MAX_SIZE][MAX_SIZE] (along with the fixes below), the program ran successfully, as far as I can tell.

    There are a number of other errors:

    • You need to #include <sys/wait.h>.
    • The global int **p isn’t used and its initialization has the same type error as the reader and writer functions did.
    • The printf call in reader needs a format string; I used "%d ".
    • As Jonathan Leffler pointed out, you need to call fork() only once each time through the loop in main.

    All but the last of those were highlighted by compiler warnings.

    In studying why this program was failing, I also used strace -f to identify which syscalls and processes were actually busted. The semaphore-related syscalls, for example, appear to be returning successfully–although as Jonathan pointed out, you should check their return values for errors, because failing as early as possible makes it much easier to debug problems.

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

Sidebar

Related Questions

I started studying Haskell one week ago and have one strange problem. I created
I just started working on backbone a week ago, please don't mind if this
Started working on a new application this week that is running the latest rails
I'm still working on that bitmap I/O problem from a week ago. I got
I have this problem that a week ago, when debugging, suddently the Locals window
I've started working on Play almost a week ago, what I'm trying to do
I've started this week to use Maven for some projects I'm working on and
I started coding in python a week ago, it is my mistake i started
I started Objective-C programming about a week ago and am stuck on creating a
I've just started learning Java (about a week ago) and the last few days

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.