• C

Bus Error with Unix

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());
                   }
      }
}
zaakiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
zaakiAuthor Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.