Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

segmentation fault 1

Status
Not open for further replies.

sramki

Programmer
May 8, 2001
27
0
0
IN
hi all

cou;ld someone help me out with this segmentation fault .

i have given the code here .
somewhere there is a segmentation fault .
iam unable to find out .could u help me .

the main aim of the code is to run a particular process
"get" at an interval as specified in the ENv variable "TIME" .
The process is getting executed once but the second time iam getting a segmentation fault error .
iam not able to pass through it .
if can put ur idea it would be useful.

ramki

THE CODE :
-----------


#include<stdio.h>
#include<signal.h>
#include<time.h>
#include<unistd.h>
#include<errno.h>
#include<stdlib.h>
#include &quot;/home/ipcpe/process/lib/ERR_logerror.c&quot;
#define CONFIG &quot;/home/ipcpe/process/config.ini&quot;
#define YES 1
#define NO 0
#define TRUE 1
#define FALSE 0
/*********Global Variables ***********/
int CDRcoll_pid = -1; /* pid of the get process */
int CDRdaemon_pid; /* pid of this daemon */
FILE *fp1;
/******************Function Declaration *******************/
int collt();
void sigtrap();
void sigcld();
void sigalrm();
main()
{
char *ptr,*getenv();
char c;
int sig,i;
/********** Change to the root ************/
chdir(&quot;/&quot;);
/************ Mask the file permissions*******/
umask(0);
/**************** set the effective gid and pid ************/
setpgrp();
/**********The Following Signals are ignored *********/
signal(SIGHUP,SIG_IGN);
signal(SIGINT,SIG_IGN);
signal(SIGQUIT,SIG_IGN);

/***********For the following Signals execute sigtrap ************/

signal(SIGILL,sigtrap);
signal(SIGIO,sigtrap);
signal(SIGIOT,sigtrap);
signal(SIGPOLL,sigtrap);


signal(SIGCLD,sigcld);
signal(SIGALRM,sigalrm);
while(1)
{
/*
fp1=fopen(&quot;/home/ipcpe/process/test&quot;,&quot;r&quot;);

if(fp1==NULL)
{
printf(&quot;Error opening file \n&quot;);
exit(1);
}

rewind(fp1);
for(c=fgetc(fp1); !feof(fp1) && c != '=';c=fgetc(fp1));
fscanf(fp1,&quot;%d&quot;,&i);
fclose(fp1);
*/
if( ( ptr = getenv(&quot;TIME&quot;)) == (char *) 0)
{
printf(&quot;TIME is not defined \n&quot;);
}
else
{
i=atoi(ptr);
printf(&quot;%d\n&quot;,i);
}
alarm(i);
sleep(i);
}
}
 
sorry i missed half the code
here is the full code
iam getting a segmentation fault after it executes for the first time .

#include<stdio.h>
#include<signal.h>
#include<time.h>
#include<unistd.h>
#include<errno.h>
#include<stdlib.h>
#include &quot;/home/ipcpe/process/lib/ERR_logerror.c&quot;
#define CONFIG &quot;/home/ipcpe/process/config.ini&quot;
#define YES 1
#define NO 0
#define TRUE 1
#define FALSE 0
/*********Global Variables ***********/
int CDRcoll_pid = -1; /* pid of the get process */
int CDRdaemon_pid; /* pid of this daemon */
FILE *fp1;
/******************Function Declaration *******************/
int collt();
void sigtrap();
void sigcld();
void sigalrm();
main()
{
char *ptr,*getenv();
char c;
int sig,i;
/********** Change to the root ************/
chdir(&quot;/&quot;);
/************ Mask the file permissions*******/
umask(0);
/**************** set the effective gid and pid ************/
setpgrp();
/**********The Following Signals are ignored *********/
signal(SIGHUP,SIG_IGN);
signal(SIGINT,SIG_IGN);
signal(SIGQUIT,SIG_IGN);

/***********For the following Signals execute sigtrap ************/

signal(SIGILL,sigtrap);
signal(SIGIO,sigtrap);
signal(SIGIOT,sigtrap);
signal(SIGPOLL,sigtrap);


signal(SIGCLD,sigcld);
signal(SIGALRM,sigalrm);
while(1)
{
/*
fp1=fopen(&quot;/home/ipcpe/process/test&quot;,&quot;r&quot;);

if(fp1==NULL)
{
printf(&quot;Error opening file \n&quot;);
exit(1);
}

rewind(fp1);
for(c=fgetc(fp1); !feof(fp1) && c != '=';c=fgetc(fp1));
fscanf(fp1,&quot;%d&quot;,&i);
fclose(fp1);
*/
if( ( ptr = getenv(&quot;TIME&quot;)) == (char *) 0)
{
printf(&quot;TIME is not defined \n&quot;);
}
else
{
i=atoi(ptr);
printf(&quot;%d\n&quot;,i);
}
alarm(i);
sleep(i);
}
}

void sigtrap(int sig )
{
}

void sigcld()
{
}

void sigalrm(int sig)
{

static int k=0;

if((CDRcoll_pid = fork()) == -1)
{
ERR_LOGERROR(&quot;7000&quot;,errno,&quot;&quot;);
exit(1);
}
if(CDRcoll_pid == 0)
{
execl(&quot;/home/ipcpe/process/get&quot;,(char *)0,(char *)0);
ERR_LOGERROR(&quot;7001&quot;,errno,&quot;&quot;);
exit(1);
}

k=k+1;
printf(&quot;waiting for child to die for %d time \n&quot;,k);
wait();
printf(&quot; child is dead for %d time \n&quot;,k);
return;
}
 
>#include<stdio.h>
>#include<signal.h>
>#include<time.h>
>#include<unistd.h>
>#include<errno.h>
>#include<stdlib.h>
>#include &quot;/home/ipcpe/process/lib/ERR_logerror.c&quot;

Typically include files are suffixed with .h rather than
.c. Also, to make it a little cleaner, your compiler
almost certainly has an option to specify additional
library search paths so that you don't have to write
out the entire path here.

>#define CONFIG &quot;/home/ipcpe/process/config.ini&quot;
>#define YES 1
>#define NO 0
>#define TRUE 1
>#define FALSE 0
>/*********Global Variables ***********/
>int CDRcoll_pid = -1; /* pid of the get process */
>int CDRdaemon_pid; /* pid of this daemon */
>FILE *fp1;
>/******************Function Declaration *******************/
>int collt();
>void sigtrap();
>void sigcld();
>void sigalrm();
>main()
>{
> char *ptr,*getenv();

You shouldn't add another prototype here. getenv() is already
prototyped in <stdlib.h> which you've included at the top
of your source file.

> char c;

This should be int c; fgetc() returns an int.

> int sig,i;
> /********** Change to the root ************/
> chdir(&quot;/&quot;);
> /************ Mask the file permissions*******/
> umask(0);
> /**************** set the effective gid and pid ************/
> setpgrp();
> /**********The Following Signals are ignored *********/
> signal(SIGHUP,SIG_IGN);
> signal(SIGINT,SIG_IGN);
> signal(SIGQUIT,SIG_IGN);

> /***********For the following Signals execute sigtrap ************/

> signal(SIGILL,sigtrap);
> signal(SIGIO,sigtrap);
> signal(SIGIOT,sigtrap);
> signal(SIGPOLL,sigtrap);


> signal(SIGCLD,sigcld);
> signal(SIGALRM,sigalrm);
> while(1)
> {
>/*
> fp1=fopen(&quot;/home/ipcpe/process/test&quot;,&quot;r&quot;);

> if(fp1==NULL)
> {
> printf(&quot;Error opening file \n&quot;);
> exit(1);
> }

> rewind(fp1);

This is unnecessary because the file pointer already points
to the beginning of the stream when opened in &quot;r&quot; mode.

> for(c=fgetc(fp1); !feof(fp1) && c != '=';c=fgetc(fp1));

This is a sort of &quot;off by one&quot; error. feof() doesn't evaluate
the stream, so c will get assigned the EOF character before feof()
detects end of file. Though this is probably not an issue with
this program, since you don't use the value of c below.

> fscanf(fp1,&quot;%d&quot;,&i);

What if you did hit end of file above and the loop didn't terminate
when it hit an equal sign?

> fclose(fp1);
>*/
> if( ( ptr = getenv(&quot;TIME&quot;)) == (char *) 0)

What's the cast to char * here for?

> {
> printf(&quot;TIME is not defined \n&quot;);
> }
> else
> {
> i=atoi(ptr);
> printf(&quot;%d\n&quot;,i);

If the environment variable &quot;TIME&quot; is found, you assign it to
i for your alarm seconds below. Why not call this first and
/then/ if the variable isn't found read your config file?
Otherwise you're unnecessarily reading the config file whenever
the environment variable is present.

> }
> alarm(i);
> sleep(i);
> }
>}

>void sigtrap(int sig )
>{
>}

>void sigcld()
>{
>}

>void sigalrm(int sig)
>{

Be aware that calling library functions (such as printf()) or
referring to an object of static storage duration (such as k)
in a signal handler that was not invoked by calling abort() or
raise() invokes undefined behavior. In other words, this is all
highly non-portable code (at best).

A good way to duplicate this functionality is to have a file
scope variable like this:

volatile sig_atomic_t alarm_raised;

This is initialized to 0 being that it's at file scope. Also
the type volatile sig_atomic_t results in defined behavior
when being modified by signal handlers. Then have your signal
handler set it to 1 when it's called:

void sigalrm(int sig)
{
alarm_raised=1;
}

Then, in your main() function (or possibly somewhere else) have it
test alarm_raised on every loop:

if (alarm_raised) {

> static int k=0;
>
> if((CDRcoll_pid = fork()) == -1)
> {
> ERR_LOGERROR(&quot;7000&quot;,errno,&quot;&quot;);
> exit(1);
> }
> if(CDRcoll_pid == 0)
> {
> execl(&quot;/home/ipcpe/process/get&quot;,(char *)0,(char *)0);

These should be NULL instead of 0 cast to pointer to
char:

execl(&quot;/home/ipcpe/process/get&quot;,NULL,NULL);

> ERR_LOGERROR(&quot;7001&quot;,errno,&quot;&quot;);
> exit(1);
> }

> k=k+1;
> printf(&quot;waiting for child to die for %d time \n&quot;,k);
> wait();

If this is the POSIX wait() function, you need a parameter. You
also need to include the proper headers to bring a prototype in
scope:

#include <sys/types.h>
#include <sys/wait.h>

You can either call wait() with a NULL parameter:

wait(NULL);

or give it a pointer to int:

int status;

/* ... */

wait(&status);

status will contain information about the child's termination
status. See your man page.

> printf(&quot; child is dead for %d time \n&quot;,k);
> return;

main() returns an int:

return 0;

Even though you don't define main() explicitly to return an int,
the compiler adds it implicitly.
>}

I don't know what's causing your seg fault exactly, but hopefully the suggestions above will help. Good luck.
Russ
bobbitts@hotmail.com
 
hi russ

thanks for ur valuable comments and suggestions
they were really very useful .
great man great stuff!!!!!!!!

thanks again
ramki

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top