分段故障备案睡眠功能

分段故障备案睡眠功能

本文介绍了分段故障备案睡眠功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好每个人我得到一个问题。我使用的是睡眠功能。我有文件,我打开后5秒读取所有数据并显示它。它运行几年时间内给予分段错误我没有得到它,为什么它happening.i觉得有什么不妥的地方一定热已经运行其他程序明智的文件
谢谢
 好心帮我结果
这里是code

 #包括LT&;&stdio.h中GT;
#包括LT&; SYS / stat.h>
#包括LT&;&time.h中GT;
#包括LT&;&string.h中GT;
无效retrive()
{
FILE * FS;
焦炭CH;
const int的大小= 26;
const int的尺寸1 = 3;
const int的尺寸2 = 6;
        CHAR sourceip【尺寸】; CHAR destip【尺寸】; CHAR源端口[尺寸2]; char的destport [尺寸2];
        CHAR sessionstart【尺寸】; CHAR SESSIONEND【尺寸】;焦炭负载[尺寸2]; char的sessionnum [尺寸2];
  而(-1)
{
    FS =的fopen(processpacketlib.txt,R);
        如果(FS == NULL)
        {
               看跌期权(无法打开源文件);
               返回0;
        }
诠释J;
INT CNT = 0;
的printf(无条目%的D(noofentries(processpacketlib.txt)+ 1)/ 3);//while(cnt<(noofentries(\"processpacketlib.txt\")/3))
为(J = 0; J&小于10; J ++)
/// CH =龟etc(FS);
//而(CH!= EOF)
{
INT I = 0;
的printf(J =%d个,J);
CH =龟etc(FS);
sourceip [I] = CH;
我++;
而(CH!='\\ t')
        {CH =龟etc(FS);
sourceip [I] = CH;
        我++;
         }
sourceip [ - 我] ='\\ 0';
 I = 0;
// ================================================CH =龟etc(FS);
destip [I] = CH;
我++;
而(CH!='\\ t')
        {        CH =龟etc(FS);
        destip [I] = CH;我++;
         }destip [ - 我] ='\\ 0';
I = 0;
// ================================================ =CH =龟etc(FS);
源端口[I] = CH;
//输出(诶%C,诶[0]);
我++;
而(CH!='\\ t')
{
CH =龟etc(FS);
源端口[I] = CH;
//输出(诶%C,诶[1]);
我++;
 }
源端口[ - 我] ='\\ 0';
I = 0;
// ================================================ =CH =龟etc(FS);
destport [I] = CH;
//输出(NP%C,NP [0]);
我++;
而(CH!='\\ t')
{
CH =龟etc(FS);
destport [I] = CH;
//输出(NP%C,NP [1]);
我++;
 }
destport [ - 我] ='\\ 0';
I = 0;
// ===============================================CH =龟etc(FS);
有效载荷由[i] = CH;
//输出(NP%C,NP [0]);
我++;
而(CH!='\\ t')
{
CH =龟etc(FS);
有效载荷由[i] = CH;
//输出(NP%C,NP [1]);
我++;
 }
有效载荷[ - 我] ='\\ 0';
I = 0;// ================================================ ==// ================================================ ==CH =龟etc(FS);
sessionnum [I] = CH;
//输出(NP%C,NP [0]);
我++;
而(CH!='\\ t')
{
CH =龟etc(FS);
sessionnum [I] = CH;
//输出(NP%C,NP [1]);
我++;
 }
sessionnum [ - 我] ='\\ 0';
I = 0;
// ================================================ =
CH =龟etc(FS);
sessionstart [I] = CH;
//输出(NP%C,NP [0]);
我++;
而(CH!='\\ n')
{
CH =龟etc(FS);
sessionstart [I] = CH;
//输出(NP%C,NP [1]);
我++;
 }
sessionstart [ - 我] ='\\ 0';
I = 0;
// ================================================ ======CH =龟etc(FS);
SESSIONEND [I] = CH;
//输出(NP%C,NP [0]);
我++;
而(CH!='\\ n')
{
CH =龟etc(FS);
SESSIONEND [I] = CH;
//输出(NP%C,NP [1]);
我++;
 }
SESSIONEND [ - 我] ='\\ 0';CH =龟etc(FS);
// ------------------------------------------------ ---- * /
csvwrite(sourceip,destip,源端口,destport,sessionstart,SESSIONEND,有效载荷,sessionnum);的printf(\\ NSIP%S,sourceip);
的printf(\\ n探底%S,destip);
的printf(\\ n源%S,源端口);
的printf(\\ ndest%S,destport);
的printf(\\ NT付费%S,有效载荷);
的printf(\\ NT HDR%S,sessionnum);
的printf(\\ nsession STRAT%S,sessionstart);
的printf(\\ nsession最终%S,SESSIONEND);的printf(\\ n);CNT ++;
} //结束时
FCLOSE(FS);
睡眠(1);
} //而两端外无限}
// ================================================ ==================================无效csvwrite(CHAR sourceip [],CHAR destip [],炭源端口[],CHAR destport [],CHAR sessionstart [],CHAR SESSIONEND [],CHAR有效载荷[],CHAR sessionnum [])
 {INT retvalue;
INT POS = 0;
   炭SNUM [5];炭sstime [20];炭payload1 [10000];
结构TM TM;
time_t的吨;
个char [25] =周六02月19日12时53分39秒2011;
如果(strptime(sessionstart,%A%A%D%H:%M:%S%Y,&安培; TM)!= NULL)
tm.tm_isdst = -1;
T = mktime(安培; TM);
//如果(T!= -1)
//输出(时代以来秒:%ld的\\ n,(长)T);
字符的文件名[35];
sprintf的(文件名,%s%s%S%S,sourceip,_,destip,CSV);
的printf(%文件名的,文件名);   FILE * FE =的fopen(文件名,一个);   struct stat中ST;
   STAT(文件名,和放大器; ST);
   INT FSIZE = st.st_size;
   的printf(之前,文件大小%d个\\ n \\ n \\ n,FSIZE);
如果(FSIZE == 0)
 {
        炭stringtime [15];
    fprintf中(FE,%S,sessionnum);
    fprintf中(FE,%S,);
        sprintf的(stringtime,%LD,(长)T);
    fprintf中(FE,%S,stringtime);
    fprintf中(FE,%S,);
    fprintf中(FE,%S,有效载荷);
    fprintf中(FE,%S,\\ n);
        FCLOSE(FE);
        结构统计ST1;
        STAT(文件名,和放大器; ST1);
        INT fsize1 = st1.st_size;
        的printf(后的文件大小%d个\\ n \\ n \\ n,fsize1);
   }
// FCLOSE(filenname);
其他
 {
    诠释计数= noofentries(文件名);
    的printf(%计D,计数);
    INT I = 0;
    INT K = 0;
INT LEN = 0;    FILE * F1 = FOPEN(文件名,R);
    //炭SNUM [5];炭sstime [20];炭payload1 [100000]; INT payloadentries = 0; //改变粘贴以下
而(K&LT;计数)
     {炭SNUM [5];炭sstime [20];炭payload1 [100000]; INT payloadentries = 0;          焦炭CH;
      CH =龟etc(F1);
      POS ++;
          LEN ++;
          //的printf(\\ NCH =%C,CH);
          SNUM [I] = CH;
          我++;
        而(CH!='')
                {
                    CH =龟etc(F1);
                POS ++;
                LEN ++;
                    SNUM [I] = CH;
                    //输出(CH%C,CH);
                    我++;
            }
                SNUM [ - 我] ='\\ 0';
                I = 0;
// --------------------------------------------
                CH =龟etc(F1);
                POS ++;
                LEN ++;
                    //的printf(\\ NCH =%C,CH);
                    sstime [I] = CH;
                                我++;
              而(CH!='')
                     {
                    CH =龟etc(F1);
                POS ++;
                LEN ++;
                    sstime [I] = CH;
                    //输出(CH%C,CH);
                                我++;
                    }
                    sstime [ - 我] ='\\ 0';
                    I = 0;
// ---------------------------------------------
                    CH =龟etc(F1);
                POS ++;
                LEN ++;
                    //的printf(\\ NCH =%C,CH);
                payload1 [I] = CH;
                    我++;
         而(CH!='\\ n')
                   {
                    CH =龟etc(F1);
                POS ++;
                LEN ++;
                    payload1 [I] = CH;
                    //输出(CH%C,CH);
                    我++;
                如果(CH =='')
                      {
                     payloadentries ++;
                                      }                  }
                payload1 [ - 我] ='\\ 0';
                I = 0;
                的printf(\\ nsnum%S,SNUM);                的printf(\\ nsstime%S,sstime);                的printf(\\ npayload%S,payload1);// retvalue=addpayload(sourceip,destip,sourceport,destport,t,payload,sessionnum,snum,sstime,payload1,payloadentries+1,pos,count,filename);
//输出(rettttttttttvalue%D,retvalue);
        如果((retvalue == - 2)||(retvalue == - 1))
        {
的printf(\\ n \\ nBREAK \\ n \\ n);
         打破;
        }
           ķ++;   } //结束时
           FCLOSE(F1);
 } //其他结束
} //函数结束
// ================================================ ====================================


解决方案

如果的其他分支(FSIZE == 0)在条件 csvwrite() FCLOSE(FE)。有对可以由任一进程一次被打开的文件的数目的限制;如果你把这种足够的时间,你会击中了极限,接下来的 fopen()函数将返回 NULL (和错误号将被设置为 EMFILE - 打开的文件太多)

这可能是也可能不是问题,所以这里的一些更普遍的建议是:


  1. 使用调试器。如果你不知道怎么了,你应该学会!例如:

      $猫test.c的
    #包括LT&;&stdio.h中GT;INT主要(无效)
    {
        int类型的= 123,B,C;
        为int * p =&放大器;一个;
        INT * Q = NULL;    B = * P;
        C = * Q;
        的printf(%D \\ n,B,C);
        返回0;
    }
    $ GCC -Wall -o测​​试test.c的
    $ ./test
    分段故障
    $

    不是非常有帮助。但随着 -g 编译和 GDB 运行:

     $ gcc的 -g  -Wall -o测​​试test.c的
    $ GDB ./test
    GNU GDB 6.8的debian
    版权所有(C)2008自由软件基金会
    许可GPLv3的+:GNU GPL第3版或更高版本&LT; HTTP://gnu.org/licenses/gpl.html>
    这是自由软件:您可以自由修改和重新发布。
    没有担保,在法律允许的范围内。键入显示复​​制
    和显示保修的说明。
    这是GDB配置为i486的Linux的GNU的...
    (GDB)运行
    启动程序:/ tmp / test目录

    ...并在那里失败,将显示该行:


    计划接收信号SIGSEGV,分割过错。
    0x080483d4在main()在test.c的:10
    10 C = * Q;

    (GDB)退出
    该程序正在运行。退出呢? (是或否)Y
    $


  2. 启用警告编译(因为我已经用 -Wall 标记上面完成)。编译器能够发现有相当多的无聊的东西,但你要问它。


  3. 尝试明智地缩进code。可读code是更容易调试。


  4. 记住要检查返回值。 fopen()函数尤其可能失败任意数量的这是无关的程序的原因。


  5. 不要写大量几乎相同code的。如果必须做同样的工作一次以上,可以考虑分解出来成为一个单独的函数。


hi every one i am getting a problem. i am using a sleep function. i have file which i open after 5 sec read all data and display it. it run for few time the give segmentation fault i am not getting it why its happening.i think there is nothing wrong with the file of program other wise it must hot have runthanks kindly help me
here is the code

#include<stdio.h>
#include <sys/stat.h>
#include<time.h>
#include<string.h>
void retrive()
{
FILE *fs;
char ch;
const int size =26;
const int size1 =3;
const int size2 =6;
        char sourceip[size];char destip[size];char sourceport[size2];char destport[size2];
        char sessionstart[size];char sessionend[size];char payload[size2];char sessionnum[size2];
  while(-1)
{
    fs = fopen ( "processpacketlib.txt","r" ) ;
        if ( fs == NULL )
        {
               puts ( "Cannot open source file" ) ;
               return 0;
        }
int j;
int cnt =0;
printf("no of entries %d" , (noofentries("processpacketlib.txt")+1)/3);

//while(cnt<(noofentries("processpacketlib.txt")/3))
for(j=0;j<10;j++)
///ch= fgetc(fs);
//while(ch!=EOF)
{
int i=0;
printf("j = %d" ,j);
ch= fgetc(fs);
sourceip[i]=ch;
i++;
while ( ch!='\t')
        {

ch = fgetc ( fs ) ;
sourceip[i]=ch;
        i++;
         }
sourceip[--i]='\0';
 i=0;
//================================================

ch = fgetc ( fs ) ;
destip[i]=ch;
i++;
while ( ch!='\t' )
        {

        ch = fgetc ( fs ) ;
        destip[i]=ch;

i++;
         }

destip[--i]='\0';
i=0;
//=================================================

ch = fgetc ( fs ) ;
sourceport[i]=ch;
//printf("eh %c",eh[0])   ;
i++;
while (ch!='\t')
{
ch = fgetc ( fs ) ;
sourceport[i]=ch;
//printf("eh %c",eh[1])   ;
i++;
 }
sourceport[--i]='\0';
i=0;
//=================================================

ch = fgetc ( fs ) ;
destport[i]=ch;
//printf("np %c",np[0])   ;
i++;
while (ch!='\t')
{
ch = fgetc ( fs ) ;
destport[i]=ch;
//printf("np %c",np[1])   ;
i++;
 }
destport[--i]='\0';
i=0;
//===============================================

ch = fgetc ( fs ) ;
payload[i]=ch;
//printf("np %c",np[0])   ;
i++;
while (ch!='\t')
{
ch = fgetc ( fs ) ;
payload[i]=ch;
//printf("np %c",np[1])   ;
i++;
 }
payload[--i]='\0';
i=0;

//==================================================

//==================================================

ch = fgetc ( fs ) ;
sessionnum[i]=ch;
//printf("np %c",np[0])   ;
i++;
while (ch!='\t')
{
ch = fgetc ( fs ) ;
sessionnum[i]=ch;
//printf("np %c",np[1])   ;
i++;
 }
sessionnum[--i]='\0';
i=0;


//=================================================
ch = fgetc ( fs ) ;
sessionstart[i]=ch;
//printf("np %c",np[0])   ;
i++;
while (ch!='\n')
{
ch = fgetc ( fs ) ;
sessionstart[i]=ch;
//printf("np %c",np[1])   ;
i++;
 }
sessionstart[--i]='\0';
i=0;
//======================================================

ch = fgetc ( fs ) ;
sessionend[i]=ch;
//printf("np %c",np[0])   ;
i++;
while (ch!='\n')
{
ch = fgetc ( fs ) ;
sessionend[i]=ch;
//printf("np %c",np[1])   ;
i++;
 }
sessionend[--i]='\0';

ch = fgetc ( fs ) ;
//----------------------------------------------------*/
csvwrite(sourceip,destip,sourceport,destport,sessionstart,sessionend,payload,sessionnum);

printf("\nsip %s" , sourceip);
printf("\n dip %s" , destip);
printf("\nsource  %s",sourceport);
printf("\ndest %s",destport);
printf("\nt pay  %s",payload);
printf("\nt hdr %s",sessionnum);
printf("\nsession strat %s",sessionstart);
printf("\nsession end %s",sessionend);

printf("\n");

cnt++;
}// while ends
fclose(fs);
sleep(1);
}// outer infinite while ends

}
//==================================================================================

void csvwrite(char sourceip[],char destip[],char sourceport[],char destport[],char sessionstart[],char sessionend[],char payload[],char sessionnum[])
 {int retvalue;
int pos=0;
   char snum[5];char sstime[20];char payload1[10000] ;
struct tm tm;
time_t t;
char s[25]="Sat Feb 19 12:53:39 2011";
if (strptime(sessionstart, "%A %b %d %H:%M:%S %Y", &tm) != NULL)
tm.tm_isdst = -1;
t = mktime(&tm);
//if (t != -1)
//printf("seconds since the Epoch: %ld\n", (long) t);
char filename[35];
sprintf(filename,"%s%s%s%s",sourceip,"_",destip,".csv");
printf("filename %s",filename);

   FILE *fe = fopen(filename,"a");

   struct stat st;
   stat(filename, &st);
   int fsize = st.st_size;
   printf("before file size %d\n\n\n" ,fsize);
if(fsize==0)
 {
        char stringtime[15];
    fprintf(fe,"%s",sessionnum);
    fprintf(fe,"%s"," ");
        sprintf(stringtime,"%ld",(long)t);
    fprintf(fe,"%s",stringtime);
    fprintf(fe,"%s"," ");
    fprintf(fe,"%s",payload);
    fprintf(fe,"%s","\n");
        fclose(fe);
        struct stat st1;
        stat(filename, &st1);
        int fsize1 = st1.st_size;
        printf("after file size %d\n\n\n" ,fsize1);
   }
//fclose(filenname);
else
 {
    int count =noofentries(filename);
    printf("count%d",count);
    int i=0;
    int k=0;
int len =0;

    FILE *f1 = fopen(filename,"r");
    //char snum[5];char sstime[20];char payload1[100000] ; int payloadentries=0;// change pasted below
while(k<count)
     {char snum[5];char sstime[20];char payload1[100000] ; int payloadentries=0;

          char ch;
      ch= fgetc(f1);
      pos++;
          len++;
          // printf("\nch = %c" ,ch);
          snum[i]=ch;
          i++;
        while (ch!=' ')
                {
                    ch = fgetc ( f1 ) ;
                pos++;
                len++;
                    snum[i]=ch;
                    //  printf("ch %c",ch)   ;
                    i++;
            }
                snum[--i]='\0';
                i=0;
//--------------------------------------------
                ch= fgetc(f1);
                pos++;
                len++;
                    // printf("\nch = %c" ,ch);
                    sstime[i]=ch;
                                i++;
              while (ch!=' ')
                     {
                    ch = fgetc ( f1 ) ;
                pos++;
                len++;
                    sstime[i]=ch;
                    //  printf("ch %c",ch)   ;
                                i++;
                    }
                    sstime[--i]='\0';
                    i=0;
//---------------------------------------------
                    ch= fgetc(f1);
                pos++;
                len++;
                    // printf("\nch = %c" ,ch);
                payload1[i]=ch;
                    i++;
         while (ch!='\n')
                   {
                    ch = fgetc ( f1) ;
                pos++;
                len++;
                    payload1[i]=ch;
                    //  printf("ch %c",ch)   ;
                    i++;
                if(ch==' ')
                      {
                     payloadentries++;
                                      }

                  }
                payload1[--i]='\0';
                i=0;
                printf("\nsnum %s" ,snum);

                printf("\nsstime %s" ,sstime);

                printf("\npayload %s" ,payload1);

//  retvalue=addpayload(sourceip,destip,sourceport,destport,t,payload,sessionnum,snum,sstime,payload1,payloadentries+1,pos,count,filename);
//printf("rettttttttttvalue %d" ,retvalue);
        if ((retvalue==-2)||(retvalue==-1))
        {
printf("\n\nBREAK\n\n ");
         break;
        }
           k++;

   }//while end
           fclose(f1);
 } //else end
}// function end
//====================================================================================
解决方案

The else branch of the if(fsize==0) conditional in csvwrite() does not fclose(fe). There is a limit on the number of files that can be opened by any one process at once; if you call this enough times, you'll hit the limit, and the next fopen() will return NULL (and errno will be set to EMFILE - "Too many open files").

That may or may not be the problem, so here's some more general advice:

  1. Use a debugger. If you don't know how, you should learn! For example:

    $ cat test.c
    #include <stdio.h>
    
    int main(void)
    {
        int a = 123, b, c;
        int *p = &a;
        int *q = NULL;
    
        b = *p;
        c = *q;
        printf("%d %d\n", b, c);
        return 0;
    }
    $ gcc -Wall -o test test.c
    $ ./test
    Segmentation fault
    $
    

    Not very helpful. But compile with -g and run it with gdb:

    $ gcc -g -Wall -o test test.c
    $ gdb ./test
    GNU gdb 6.8-debian
    Copyright (C) 2008 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
    This is free software: you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
    and "show warranty" for details.
    This GDB was configured as "i486-linux-gnu"...
    (gdb) run
    Starting program: /tmp/test
    

    ...and it will reveal the line where it fails:

    Program received signal SIGSEGV, Segmentation fault.
    0x080483d4 in main () at test.c:10
    10        c = *q;
    (gdb) quit
    The program is running.  Exit anyway? (y or n) y
    $
    

  2. Compile with warnings enabled (as I have done above with the -Wall flag). The compiler is capable of spotting quite a lot of silly things, but you have to ask it.

  3. Try indenting the code sensibly. Readable code is easier to debug.

  4. Remember to check return values. fopen() in particular can fail for any number of reasons which are unrelated to your program.

  5. Don't write vast amounts of practically identical code. If you have to do the same job more than once, consider factoring it out into a separate function.

这篇关于分段故障备案睡眠功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 09:52