[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Bus Error with Unix

Posted on 2004-11-09
3
Medium Priority
?
720 Views
Last Modified: 2010-08-05
Good day all . . .

I have an error which is "Bus Error" when trying to run the executable file . . .

The following is the code for both the header file and the executable file . . .

Please help me out . . .

Thanks in advanced . . .

This is the header file:

/* Demo of dynamic data structures in C                      */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

#define FALSE 0
#define NULL 0

typedef struct listelement{
    long     dataitem;
    struct listelement *link;
}listelement ;

void Menu (int *choice);
listelement * AddItem (listelement * listpointer, int data);
listelement * RemoveItem (listelement * listpointer);
void PrintQueue (listelement * listpointer);
void ClearQueue (listelement * listpointer);
int isEmpty(listelement * listpointer);

int isEmpty(listelement * listpointer)
{
      if(listpointer -> link == NULL)
            return 1;
      else
            return 0;
}

listelement * AddItem (listelement * listpointer, int data) {

listelement * lp = listpointer;

    if (listpointer != NULL) {
      while (listpointer -> link != NULL)
          listpointer = listpointer -> link;
      listpointer -> link = (struct listelement  *) malloc (sizeof
(listelement));
      listpointer = listpointer -> link;
      listpointer -> link = NULL;
      listpointer -> dataitem = data;
      return lp;
    }

    else {
      listpointer = (struct listelement  *) malloc (sizeof
(listelement));
      listpointer -> link = NULL;
      listpointer -> dataitem = data;
      return listpointer;
    }
}

listelement * RemoveItem (listelement * listpointer) {

    listelement * tempp;
    printf ("Element removed is %d\n", listpointer -> dataitem);
    tempp = listpointer -> link;
    free (listpointer);
    return tempp;
}

void PrintQueue (listelement * listpointer) {

    if (listpointer == NULL)
      printf ("queue is empty!\n");
    else
      while (listpointer != NULL) {
          printf ("%d\t", listpointer -> dataitem);
          listpointer = listpointer -> link;
      }
    printf ("\n");
}

void ClearQueue (listelement * listpointer) {

    while (listpointer != NULL) {
      listpointer = RemoveItem (listpointer);
    }
}

AND this is the executable file:

#include <stdio.h>
#include <unistd.h>/* contains fork prototype */
#include "gll.h"

listelement listmember, *listpointer, *temp;
long pid, newPid;

void oneMoreDepth();

main ( )
{      
      listelement listmember, *listpointer, *temp;
      int depth;
      int enteredDepth = 4;
      listpointer = NULL;
      
      listpointer = AddItem (listpointer, getpid());

      for(depth = enteredDepth; depth < 0; depth--)
      {
            while(isEmpty(listpointer) == 1)
            {
                  oneMoreDepth();
            }

            ClearQueue (listpointer);
            listpointer = temp;
      } // for

      ClearQueue(listpointer);
      ClearQueue(temp);
}

void oneMoreDepth()
{
      newPid = RemoveItem(listpointer)->dataitem;
      
      pid = fork();
      
      if(pid == newPid)
      {
            printf ("Parent: %d ID: %d\n", getppid(), getpid()) ;
            pid = getpid(); // in order for its second chiled not to fork
      }
      
      else if(pid == 0)
      {
            printf ("Parent: %d ID: %d\n", getppid(), getpid());
            temp = AddItem (listpointer, getpid());
      }

      if(pid == newPid)
      {
            pid = fork();
            if(pid == 0)
             {
                           printf ("Parent: %d ID: %d\n", getppid(),getpid());
                  temp = AddItem (listpointer, getpid());
                   }
      }
}
0
Comment
Question by:zaaki
  • 2
3 Comments
 
LVL 12

Expert Comment

by:stefan73
ID: 12542204
Hi zaaki,
Bus errors in Unix normally happen when the CPU tries to dereference a pointer to multi-byte data (short, int, long, etc) using an address which is not a multiple of the data size. Many RISC architectures (such as SPARC) don't like this.

You can easily create a bus error:

int main(){
    long l=1;
    long *lp=(long*)((char*)&l + 1);

    printf("l=%ld\n",*lp);
}

Most of these errors are easy to find. Just compile your source with debug info (-g) and run the binary. Once you get a buss error and a core dump, you can use the debugger to see the place where it happened:

dbx your_binary core

(Solaris example)

Cheers!

Stefan
0
 
LVL 12

Accepted Solution

by:
stefan73 earned 80 total points
ID: 12542778
zaaki,
Another one is a jump to an odd address:

#include <stdio.h>

int run=1;

typedef int (*main_ptr)();

int main(){
        main_ptr mp;
        printf("Call %d\n",run++);
        mp = (main_ptr)((char*)main+1);

        mp();
        return 0;
}
0
 

Author Comment

by:zaaki
ID: 12542798
Thank you very much for your responce, but I am a beganer in C Language -I know Java & C#- . . .

I could not understand the error . . .

"Segmentation fault" is a new error, and "-1073743228" is printed as the pid (process ID) . . . !!!???

Please help me to solve these problems since the C Language is used in my Operatin Systems Lab. and this code is a part of a long project which is, as I see, is the only efficient, fast, easier, etc.

I was workin during my vecation for 5 days day & night to finish this project. On the other hand I have two more projects to be done. More Importantly I have 9 more days before returning back to my university . . . SO, PLEASE HELP ME . . .

The output of the execution after using gcc with "-o" option is:

Element removed is 23425
Element removed is -1073743228
Segmentation fault

WHILE the errors after compliling the source code using gcc with "-g" option is:

pp2(.text+0x1e8):/usr/src/bs/BUILD/glibc-2.2.2/csu/init.c: multiple definition of `PrintQueue'
/tmp/ccQEzKGZ.o:/home/ugics/st205329/ICS431/Projects/ProjectI/Problem2/tests/gll.h:66: first defined here
/usr/bin/ld: Warning: size of symbol `PrintQueue' changed from 90 to 94 in pp2
pp2(.rodata+0x0):/usr/src/bs/BUILD/glibc-2.2.2/csu/init.c: multiple definition of `_fp_hw'
/usr/lib/gcc-lib/i386-redhat-linux/2.96/../../../crt1.o(.rodata+0x0): first defined here
pp2: In function `_init':
pp2(.init+0x0): multiple definition of `_init'
/usr/lib/gcc-lib/i386-redhat-linux/2.96/../../../crti.o(.init+0x0): first defined here
pp2(.text+0x248):/usr/src/bs/BUILD/glibc-2.2.2/csu/init.c: multiple definition of `ClearQueue'
/tmp/ccQEzKGZ.o:/home/ugics/st205329/ICS431/Projects/ProjectI/Problem2/tests/gll.h:78: first defined here
pp2(.text+0x1ac):/usr/src/bs/BUILD/glibc-2.2.2/csu/init.c: multiple definition of `RemoveItem'
/tmp/ccQEzKGZ.o:/home/ugics/st205329/ICS431/Projects/ProjectI/Problem2/tests/gll.h:57: first defined here
pp2: In function `_start':
pp2(.text+0x0): multiple definition of `_start'
/usr/lib/gcc-lib/i386-redhat-linux/2.96/../../../crt1.o(.text+0x0): first defined here
pp2(.text+0x310):/usr/src/bs/BUILD/glibc-2.2.2/csu/init.c: multiple definition of `oneMoreDepth'
/tmp/ccQEzKGZ.o:/home/ugics/st205329/ICS431/Projects/ProjectI/Problem2/tests/pp2.c:35: first defined here
pp2(.text+0x274):/usr/src/bs/BUILD/glibc-2.2.2/csu/init.c: multiple definition of `main'
/tmp/ccQEzKGZ.o:/home/ugics/st205329/ICS431/Projects/ProjectI/Problem2/tests/pp2.c:11: first defined here
pp2(.fini+0x0):/usr/src/bs/BUILD/glibc-2.2.2/csu/init.c: multiple definition of `_fini'
/usr/lib/gcc-lib/i386-redhat-linux/2.96/../../../crti.o(.fini+0x0): first defined here
pp2(.text+0x11c):/usr/src/bs/BUILD/glibc-2.2.2/csu/init.c: multiple definition of `AddItem'
/tmp/ccQEzKGZ.o:/home/ugics/st205329/ICS431/Projects/ProjectI/Problem2/tests/gll.h:33: first defined here
pp2(.got+0x0):/usr/src/bs/BUILD/glibc-2.2.2/csu/init.c: multiple definition of `_GLOBAL_OFFSET_TABLE_'
/usr/lib/gcc-lib/i386-redhat-linux/2.96/../../../crti.o(.got.plt+0x0): first defined here
pp2(.rodata+0x4):/usr/src/bs/BUILD/glibc-2.2.2/csu/init.c: multiple definition of `_IO_stdin_used'
/usr/lib/gcc-lib/i386-redhat-linux/2.96/../../../crt1.o(.rodata+0x4): first defined here
pp2(.text+0x100):/usr/src/bs/BUILD/glibc-2.2.2/csu/init.c: multiple definition of `isEmpty'
/tmp/ccQEzKGZ.o:/home/ugics/st205329/ICS431/Projects/ProjectI/Problem2/tests/gll.h:26: first defined here
pp2(.data+0x0):/usr/src/bs/BUILD/glibc-2.2.2/csu/init.c: multiple definition of `__data_start'
/usr/lib/gcc-lib/i386-redhat-linux/2.96/../../../crt1.o(.data+0x0): first defined here
/usr/lib/gcc-lib/i386-redhat-linux/2.96/../../../crti.o(.dynamic+0x0): multiple definition of `_DYNAMIC'
pp2(.dynamic+0x0):/usr/src/bs/BUILD/glibc-2.2.2/csu/init.c: first defined here
collect2: ld returned 1 exit status
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question