-1

I am using shared memory to pass values between processes. It's working the first time, but when I try changing it again, the value is not getting reflected in other processes. First process A changes the value and signal B. It uses it and passes back the control to A. Then A again changes the value, but this new value is not getting reflected in process B.

// FILE 1

#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<arpa/inet.h>
#include<sys/time.h>
#include<iostream>
#include<netinet/in.h>
#include<sys/select.h>
#include<sys/shm.h>
#include<signal.h>

using namespace std;

struct shm
{
    int aid;
    int sid;
    int bid;
    int cid;
    int portno;
    int pno;
//int ports[4];
}*val;
int ports[4];
int sfds[4];
int count = 4;

void func1(int a)
{
    fd_set rfds;
    while (1)
    {
        FD_ZERO(&rfds);
        struct timeval timer;
        timer.tv_sec = 0;
        timer.tv_usec = 10;
        for (int i = 0; i < count; i++)
        {
            FD_SET(sfds[i], &rfds);
        }
        if (select(sfds[count - 1] + 1, &rfds, NULL, NULL, &timer))
        {
            for (int i = 0; i < count; i++)
            {
                if (FD_ISSET(sfds[i], &rfds))
                {
                    val->pno = i;
                    val->portno = ports[i];
                    cout << "port number:" << val->portno << endl;
                    close(sfds[i]);
                    // Remove the corresponding sfd                     
                    for (int j = i; j < count - 1; j++)
                    {
                        sfds[j] = sfds[j + 1];
                        ports[j] = ports[j + 1];
                    }
                    count--;
                    kill(val->aid, SIGUSR2);
                    if (count > 0)
                        signal(SIGUSR1, &func1);
                    else
                        exit(1);
                    return;
                }
            }
        }
    }
}

void func2(int b)
{
    struct sockaddr_in cliaddr;
    socklen_t clilen;
    int lc = 0;
    for (int i = 0; i < 1; i++)
    {
        int nsfd = accept(sfds[val->pno], (struct sockaddr*) &cliaddr, &clilen);
        if (nsfd < 0)
        {
            perror("Error");
            exit(1);
        }
        cout << "Connection accepted";
        send(nsfd, "OkaA", 5, 0);
        close(nsfd);
    }
    if (close(sfds[val->pno]) < 0)
    {
        perror("Error closing");
        exit(1);
    }
    cout << "Closed" << endl;
    // Remove the corresponding sfd

    for (int i = val->pno; i < count - 1; i++)
    {
        sfds[i] = sfds[i + 1];
        ports[i] = ports[i + 1];
    }

    count--;
    cout << " val->portnumber " << val->portno << endl;

    kill(val->bid, SIGUSR1);
    if (count > 0)
        signal(SIGUSR2, &func2);
    else
        exit(1);
    return;
}

int main()
{
    int shmid = shmget(123456, sizeof(struct shm), IPC_CREAT | 0666);
    if (shmid < 0)
    {
        perror("Error");
    }
    val = (struct shm*) shmat(shmid, NULL, 0);
    //cout<<"yo2";  
    signal(SIGUSR1, &func1);
    signal(SIGUSR2, &func2);
    for (int i = 0; i < 4; i++)
    {
        //val->ports[i] = 7590+i;       
        sfds[i] = socket(AF_INET, SOCK_STREAM, 0);

        const int optVal = 1;
        const socklen_t optLen = sizeof(optVal);

        int rtn = setsockopt(sfds[i], SOL_SOCKET, SO_REUSEADDR, (void*) &optVal,
                optLen);
        if (rtn < 0)
        {
            perror("Error");
            exit(1);
        }
        struct sockaddr_in servaddr;
        servaddr.sin_family = AF_INET;
        servaddr.sin_port = htons(7590 + i);
        ports[i] = i;
        servaddr.sin_addr.s_addr = INADDR_ANY;
        if (bind(sfds[i], (struct sockaddr*) &servaddr, sizeof(servaddr)) < 0)
        {
            perror("Error");
            exit(1);
        }
        if (listen(sfds[i], 1) < 0)
        {
            perror("Error");
            exit(1);
        }
    }
    int c = fork();

    if (c > 0)
    {
        val->aid = c;
        val->sid = getpid();
        kill(getpid(), SIGUSR1);
        while (1)
            ;
    }
    else if (c == 0)
    {
        while (1)
            ;
    }
    else
    {
        perror("Error");
        exit(1);
    }

}

// FILE 2

#include<stdio.h>
#include<signal.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<arpa/inet.h>
#include<sys/time.h>
#include<iostream>
#include<netinet/in.h>
#include<sys/select.h>
#include<sys/shm.h>

using namespace std;

struct shm
{
    int aid;
    int sid;
    int bid;
    int cid;
    int pno;
    int portno;
}*val;

int sfds[4];
int count = 4;

void func1(int a)
{
    int sfd = socket(AF_INET, SOCK_STREAM, 0);
    struct sockaddr_in servaddr;
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(7590 + val->portno);
    servaddr.sin_addr.s_addr = INADDR_ANY;
    const int optVal = 1;
    const socklen_t optLen = sizeof(optVal);
    cout << val->portno << " vpn " << endl;
    int rtn = setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, (void*) &optVal,
            optLen);

    while (bind(sfd, (struct sockaddr*) &servaddr, sizeof(servaddr)) < 0)
    {
        perror("Error");
    }
    cout << endl << "Done" << endl;
    if (listen(sfd, 1) < 0)
    {
        perror("Error");
        exit(1);
    }
    for (int i = 0; i < 1; i++)
    {
        struct sockaddr_in cliaddr;
        socklen_t clilen;
        int nsfd = accept(sfd, (struct sockaddr*) &cliaddr, &clilen);
        send(nsfd, "OkaB", 5, 0);
    }
    close(sfd);
    signal(SIGUSR1, &func1);
    kill(val->cid, SIGUSR1);
}

int main()
{
    signal(SIGUSR1, &func1);
    int shmid = shmget(123456, sizeof(struct shm), IPC_CREAT | 0666);
    if (shmid < 0)
    {
        perror("Error");
    }
    val = (struct shm*) shmat(shmid, NULL, 0);
    val->bid = getpid();
    while (1)
        ;
}
6
  • 1
    It smells like (I'm sure...) c++ without OOP
    – LPs
    Commented Feb 22, 2017 at 9:25
  • 1
    minimal reproducible example. The above code has alphabet soup variable names, few comments, and does piles of things unrelated to your problem. Clean your code up, have tests that explicitly fail and generate clear error messages, eliminate fields and variables and code not required to reproduce the issue. This takes work, but as it stands you are simply asking someone else to do that work for you. Commented Feb 22, 2017 at 9:41
  • Dude, seriously? I mentioned it that clearly and still you are looking at the entire code and still you commented like this. The question is clear. First time I am able to change the value of val->portno in shared memory, but the next time it's failing. That's all I need to know. And yeah, there are no error messages. I debugged it. Right before the moment A is signalling B, I printed the shared value. It's showing the new value in process A but the old one in process B.
    – KingC
    Commented Feb 22, 2017 at 9:47
  • 1
    Well, you should be a little bit less rude if you want help here. BTW Your problem is that your client open shared memory using IPC_CREAT. The client side mustn't.
    – LPs
    Commented Feb 22, 2017 at 9:53
  • 1
    It might be a good idea to put the definition of the shared memory data structure in a shared file which, like the shared memory, is common to both programs... Commented Feb 22, 2017 at 11:44

1 Answer 1

2

The problem may come from the struct shm definition:

In file 1

struct shm
{
    int aid;
    int sid;
    int bid;
    int cid;
    int portno;
    int pno;
//int ports[4];
}*val;

In other file :

struct shm
{
    int aid;
    int sid;
    int bid;
    int cid;
    int pno;
    int portno;
}*val;

pno and portno are not at the same place.

And as LPs pointed, only one process should create the shared memory (IPC_CREAT)

1
  • Good catch!.......
    – LPs
    Commented Feb 22, 2017 at 11:52

Not the answer you're looking for? Browse other questions tagged or ask your own question.