473,382 Members | 1,362 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Strange Pointer Behavior

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
Aug 27 '07 #1
2 1675
>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.
Aug 27 '07 #2
Andy Carlson <na*******@gmail.comwrites:
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.

--
Ben.
Aug 27 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

36
by: Dmitriy Iassenev | last post by:
hi, I found an interesting thing in operator behaviour in C++ : int i=1; printf("%d",i++ + i++); I think the value of the expression "i++ + i++" _must_ be 3, but all the compilers I tested...
0
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ...
3
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ...
5
by: cpptutor2000 | last post by:
I am compiling and running the following code snippet on a Linux box - I am really puzzled by the answers. Could someone please tell me what might be wrong? void test(){ int m = 0; int n = 0;...
2
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ...
7
by: =?Utf-8?B?QnJpYW4gS3Vueg==?= | last post by:
Hello, I'm new to the boards, and I've been struggling with a problem porting my company's code from Visual C++ 6.0 to Visual C++ 2005. We've found some crashes that I've traced to the...
2
by: Pinux | last post by:
Hi, I now run into a very strange behavior when using ifstream. Can anyone please help me out here: Basically, what I want to do is to encrypt a plain text file into a cipher text and then...
19
by: david | last post by:
I took old code and decided to modify it a bit, and I just noticed that it does not compile at all and before server one of severs (main) crashed in the system it was working fine (I am really sure...
160
by: DiAvOl | last post by:
Hello everyone, Please take a look at the following code: #include <stdio.h> typedef struct person { char name; int age; } Person;
6
by: Gernot Frisch | last post by:
Hi, the program below workes w/o problems on a GP2X and on the PC, but my PocketPC (using GCC 3.3.3) crashes. Very dissapointing, since I expect some speed boost from it. Thnak you for your...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.