Strange Pointer Behavior

A

Andy Carlson

I have a strange problem. I am trying to use a global variable, but I
can't get the second routine to recognize changes to the globabl. I
finally realize, that I was forking, so the global was probably getting
copied. So, I changed it to a pointer, and malloc an int so that it can
be changed. But, it still doesn't appear to be working. I am fairly
sure I am doing something stupid, so please help if you can.

Oh - I made this kind of a test program that changes haltagcbsm after 5
seconds, but the forked function runcmdserv does not ever register the
change.

//agcbsm.c
#include <unistd.h> /* Symbolic Constants */
#include <sys/types.h> /* Primitive System Data Types */
#include <errno.h> /* Errors */
#include <stdio.h> /* Input/Output */
#include <sys/wait.h> /* Wait for Process Termination */
#include <stdlib.h> /* General Utilities */
#include <libgen.h>
#include <time.h>
#include "logwrite.h"
#include "runcmdserv.h"

int *haltagcbsm;

int main(int argc, char *argv[])
{

int ret;
int retval;
int status;
int servport=2080;
pid_t childpid;
struct bsmmsg bsmmess;

haltagcbsm=malloc(sizeof(int));
*haltagcbsm=0;
bsmmess.msgno=1;
ret=logwrite(&bsmmess);

childpid=fork();
if (childpid == 0) /* child process */
{
int cret;
cret=runcmdserv(servport);
exit(0);
}
if (childpid < 0) /* fork failed */
{
bsmmess.msgno=1001;
ret=logwrite(&bsmmess);
exit(1);
}
sleep(5);
*haltagcbsm=1;
wait(&status);
sleep(2);
bsmmess.msgno=9000;
bsmmess.extrai=*haltagcbsm;
ret=logwrite(&bsmmess);

bsmmess.msgno=9999;
ret=logwrite(&bsmmess);
return(0);
}

//agcbsm.h
extern int *haltagcbsm;

//runcmdserv.c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <sys/socket.h>
#include "agcbsm.h"
#include "logwrite.h"

int runcmdserv(int servport)
{
int I;
int rcsret;
struct bsmmsg bsmmess;

bsmmess.msgno=9000;
for(I=0; I<10; I++)
{
bsmmess.extrai=*haltagcbsm;
rcsret=logwrite(&bsmmess);
sleep(1);
}
}

//runcmdserv.h
int runcmdserv(int servport);

//Makefile
CC=gcc

CFLAGS=-O2
LDFLAGS=
DEBUG=-g

agcbsm: agcbsm.o logwrite.o runcmdserv.o
$(CC) $(CFLAGS) $(DEBUG) -o agcbsm agcbsm.o logwrite.o runcmdserv.o

agcbsm.o: agcbsm.h agcbsm.c logwrite.h runcmdserv.h
$(CC) $(CFLAGS) $(DEBUG) -c -o agcbsm.o agcbsm.c

logwrite.o: logwrite.c logwrite.h agcbsm.h
$(CC) $(CFLAGS) $(DEBUG) -c -o logwrite.o logwrite.c

runcmdserv.o: runcmdserv.c agcbsm.h logwrite.h
$(CC) $(CFLAGS) $(DEBUG) -c -o runcmdserv.o runcmdserv.c

clean:
rm -f agcbsm *.o
 
G

Gordon Burditt

I have a strange problem. I am trying to use a global variable, but I
can't get the second routine to recognize changes to the globabl. I
finally realize, that I was forking, so the global was probably getting
copied.

fork() (assuming you mean the POSIX function, not the companion to
knife() which is used in the eating API) does not result in shared
read/write memory between processes (unless perhaps you attached
to a shared memory block before the fork()). Copy-on-write (used
in some implementations) is used to *prevent* post-fork changes in
one process from being visible in another (consider how much of a
mess making changes in the stack frame in one process visible in
the other would make: it would be difficult to do anything). It
also puts your program beyond the realm of Standard C.
So, I changed it to a pointer, and malloc an int so that it can
be changed.

Sorry, but that doesn't work any better than witchcraft, and it
isn't as much fun. If you want shared memory, use functions to
deal with shared memory.
But, it still doesn't appear to be working. I am fairly
sure I am doing something stupid, so please help if you can.

A pointer in one process should not be able to point to memory in
another process, unless you have used OS calls to set up shared
memory (such as sysvshm() or mmap()). Oh, yes, nobody said the
memory would be mapped at the same address in both processes, so
watch out about pointers *in* the shared memory segment that try
to point elsewhere in the same shared memory segment. Storing
offsets, not pointers, from the beginning of the segment may be
useful.
Oh - I made this kind of a test program that changes haltagcbsm after 5
seconds, but the forked function runcmdserv does not ever register the
change.

Your system is apparently working correctly.
 
B

Ben Bacarisse

Andy Carlson said:
I have a strange problem. I am trying to use a global variable, but I
can't get the second routine to recognize changes to the globabl. I
finally realize, that I was forking, so the global was probably getting
copied.
Indeed.

So, I changed it to a pointer, and malloc an int so that it can
be changed.

Both it (the pointer) and the storage to which it points are being
copied so you gain nothing by making this change.

By now, we are way outside the topicality of comp.lang.c (standard C
has no notion of forking).

To go further you need to ask in another group. If you think fork is
what you need (the processes will then need to talk be some other
means) post in comp.unix.programmer. If you think you need the shared
address space you are trying to get, post in a group about some thread
package. If you don't know which way to go, post in comp.programmer
and describe what you are trying to achieve.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top