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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:07:30+00:00 2026-06-16T00:07:30+00:00

This will probably require some looking into, but my question is very simple: Why

  • 0

This will probably require some looking into, but my question is very simple:

Why is numPassenger always 0 in the parentHandler2() function?

#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <wait.h>
#include <signal.h>
#include <sys/time.h>
#include <math.h>

int getRand()
{
  return (rand() % 5001);
}

//////////GLOBAL//////////

const int CAPACITY = 100;
const int MEMSIZE = 1024;

char* sharedmem;

pid_t pid;

int numPassenger;
int numTram;

//////////GLOBAL//////////

//handles SIGALRM, generates passengers, sends SIGUSR1
void parentHandler1()
{
    numPassenger = getRand();
    sprintf(sharedmem, "%d", numPassenger);
    kill(getpid(), SIGUSR1);
}

//handles SIGUSR1, calculates number of trams needed, sends SIGUSR2
void childHandler()
{
    double n = atoi(sharedmem);
    numTram = (ceil(n/100));
    sprintf(sharedmem, "%d", numTram);
    kill(pid, SIGUSR2);
}

//outputs
void parentHandler2()
{
    int n = atoi(sharedmem);
    printf("Passengers: %d, Trams: %d\n", numPassenger, n);
}

int main (int argc, char* argv[])
{
    srand(time(0));

    key_t key;
    int shmemaddr;

    //shared memory
    key=ftok(argv[0],1);
    shmemaddr=shmget(key,MEMSIZE,IPC_CREAT|S_IRUSR|S_IWUSR);
    sharedmem = shmat(shmemaddr,NULL,0);

    pid = fork();
    if ( pid > 0 )
    {
        //timer
        struct itimerval timer;
        timer.it_value.tv_sec = 3; 
        timer.it_value.tv_usec = 0; 
        timer.it_interval.tv_sec = 3;
        timer.it_interval.tv_usec = 0;  
        setitimer (ITIMER_REAL, &timer, NULL);

        signal(SIGALRM, parentHandler1);
        signal(SIGUSR1, childHandler);
    }
    else if ( pid == 0 )
    {
        signal(SIGUSR2, parentHandler2);
    }

    //not so busy waiting
    while(1) sleep(1);

   return 0;
}

https://gist.github.com/4299915

  • 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-16T00:07:31+00:00Added an answer on June 16, 2026 at 12:07 am

    Fork creates a new copy of the current process. Global variables aren’t shared between processes. The only memory that is shared between your two processes is the memory returned by shmget. The value of numPassenger is never set in the child process.

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

Sidebar

Related Questions

This will probably be a bot of a waffly question but ill try my
This will probably be just another unsolved thread but i'll fill in some info
This is a very newbie question and i will probably get downvoted for it,
This is a very specific question which will probably earn me the tumbleweed badge,
This will probably be obvious but I can't find the best way. I want
This is confusing me, so this question will probably be confusing. I have a
I don't know much about web development, so probably this question will sound exceptionally
It probably isn't even possible to do this, but I will ask anyway. Is
This will be probable quite odd question. But i thought I will give it
I know this is a little subjective, but I'm looking into the following situation:

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.