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
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