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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:03:26+00:00 2026-05-18T05:03:26+00:00

HI, I have a program in which a master processes spawns N workers who

  • 0

HI,

I have a program in which a master processes spawns N workers who will invert, each one, each row of an image, giving me an inverted image at the end. The program uses shared memory and posix semaphores, unnamed sems, more spefically and I use shmctl with IPC_RMID and sem_close and sem_destroy in the terminate() function.
However, when I run the program several times, sometimes it gives me a segmentation fault and is in the first shmget. I’ve already modified my shmmax value in the kernel, but I can’t do the same to the shmall value, I don’t know why.

Can someone please help me? Why this happens and why isn’t it all the time? The code seems fine, gives me what I want, efficient and so…but sometimes I have to reboot Ubuntu to be able to run it again, even thought I’me freeing the resources.

Please enlighten me!

EDIT:

Here are the 3 files needed to run the code + the makefile:
http://pastebin.com/JqTkEkPv
http://pastebin.com/v7fQXyjs
http://pastebin.com/NbYFAGYq

http://pastebin.com/mbPg1QJm

You have to run it like this ./invert someimage.ppm outimage.ppm
(test with a small one for now please)

Here are some values that may be important:

$ipcs -lm
------ Shared Memory Limits --------
max number of segments = 4096
max seg size (kbytes) = 262144
max total shared memory (kbytes) = 8388608
min seg size (bytes) = 1

$ipcs -ls

------ Semaphore Limits --------
max number of arrays = 128
max semaphores per array = 250
max semaphores system wide = 32000
max ops per semop call = 32
semaphore max value = 32767

EDIT: the seg fault was solved! I was allocating an **array in shared memory and that was a little bit odd.So, I’ve allocated segment for an *array only and voilà. If you want, check the new code and comment.

  • 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-18T05:03:27+00:00Added an answer on May 18, 2026 at 5:03 am

    Now that you posted your code we can say a bit more.

    Without having read all in detail, I think the cleanup phase of your main looks suspicious. In fact it seems to me that all your worker processes will perform that cleanup phase too.

    After the fork you should more clearly distinguish what main does and what the workers do. Alternatives:

    • Your main process could just
      wait on the pids of the workers
      and only then do the rest of
      processing and cleanup.
    • All the worker processes could
      return in main after the call to
      worker.
    • Call exit at the end of the worker
      function.

    Edit after your code update:

    I think still a better solution would be to do a classical wait for all the processes.

    Now let’s look into your worker process. In fact these never terminate, there is no break statement in the while (1) loop. I think what is happening is that once there is no more work to be done

    • the worker is stuck in
      sem_wait(sem_remaining_lines)
    • your main process gets notified of
      the termination
    • it destroys the sem_remaining_lines
    • the worker returns from sem_wait
      and continues
    • since mutex3 is also already
      destroyed (or maybe even unmapped) the wait on it returns
      immediately
    • now it tries to access the data, and
      depending on how far the main
      process got on destruction the data
      is mapped or not and the worker
      crashes (or not)

    As you can see you have many problems in there. What I would do to clean up this mess is

    • waitpid before destroy the shared
      data
    • sem_trywait instead of the 1 in
      while (1). But perhaps I didn’t completely understand your control flow. In any case, give them a termination condition.
    • capture all returns from system
      functions, in particular the sem_t
      family. These can be interrupted by
      IO, so you definitively must
      check for EINTR on these.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a script that appends some rows to a table. One of the
I have a login.jsp page which contains a login form. Once logged in the
i have a input tag which is non editable, but some times i need
I have a project that adds elements to an AutoCad drawing. I noticed that
I have a new web app that is packaged as a WAR as part
I have several USB mass storage flash drives connected to a Ubuntu Linux computer
I have a snippet to create a 'Like' button for our news site: <iframe
I have found this example on StackOverflow: var people = new List<Person> { new
Let say I have the following desire, to simplify the IConvertible's to allow me
I want to have generalised email templates. Currently I have multiple email templates with

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.