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

  • Home
  • SEARCH
  • 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 8782701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:38:10+00:00 2026-06-13T20:38:10+00:00

I have been working on semaphores and shared memory for a week now, and

  • 0

I have been working on semaphores and shared memory for a week now, and have some difficulties yet,so i tried to make this program which the children are supposed to write to a memory shared multidimensional integer array and the father is suppose to read that array from the memory that is shared.

#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>
#include <sys/wait.h>

#define MAXCHILDS 1
#define MAX_SIZE 10
#define MAX_WRITES 100

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;




void init(){

     /*semaphores unlink and creation */    
     sem_unlink("EMPTY");
     empty=sem_open("EMPTY",O_CREAT|O_EXCL,0700,50);
     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[MAX_SIZE][MAX_SIZE],int n_child){
    int i,k;
    for(i = 0;i<MAX_SIZE;i++){
        for(k= 0;k<MAX_SIZE;k++){
            m[i][k] = 0;
            if(i==(k+1)){
                m[i][k] = 1;
            }
        }
    }

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

}

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,sum;
    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,i);
            exit(0);
        }
    }

    /*father*/
    sleep(10);
    sum++;
    printf("%d\n",sum); 
    reader(sh_mem->m);
    wait(NULL);

    terminate();


    return 0;
}

I have two problems right now … I need to save the array to a memory mapped file which i dont seem to understand and the output of the father is throwing is very strange …His output is

0000100000[i=]3[k=]10
0000010000[i=]4[k=]10
0000001000[i=]5[k=]10
0100000000[i=]0[k=]10
0000000100[i=]6[k=]10
0010000000[i=]1[k=]10
0000000010[i=]7[k=]10
0001000000[i=]2[k=]10
0000000001[i=]8[k=]10
0000100000[i=]3[k=]10
0000000000[i=]9[k=]10
0000010000[i=]4[k=]10
0000001000[i=]5[k=]10
0000000100[i=]6[k=]10
0000000010[i=]7[k=]10
0000000001[i=]8[k=]10
0000000000[i=]9[k=]10

And it should be something like this:

0000100000[i=]0[k=]10
0000010000[i=]1[k=]10
0000001000[i=]2[k=]10
0100000000[i=]3[k=]10
0000000100[i=]4[k=]10
0010000000[i=]5[k=]10
0000000010[i=]6[k=]10
0001000000[i=]7[k=]10
0000000001[i=]8[k=]10
0000100000[i=]9[k=]10
  • 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-13T20:38:12+00:00Added an answer on June 13, 2026 at 8:38 pm

    The code calls fork() in two places. Remove the second call.

    Change:

        if((child[i] =fork())==0){
    

    to:

        if(child[i] == 0){
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been working on this for a week now and have trouble executing
I have been working in C# for quite some time but come around this
Have been working on a Tower Defense game for iOS for some time now.
I have been working on this site for some time and just launched it
I have been working on a project for some time now, and I decided
I have been working now for few days on a small C program which
Have been working on this question for a couple hours and have come close
I have been working on this app for at least 3-4 months and just
I have been working with Zend for a few months now and am at
I have been working on this sort of ATM (With a maximum of 50

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.