Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Segmentation fault while working with C

Segmentation fault while working with C

Scheduled Pinned Locked Moved C / C++ / MFC
csharphelp
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Aqueel
    wrote on last edited by
    #1

    Hi I have written following code but when i execute it, i get an error "segmentation fault". Can anyone plz tell me why i am getting the error. I have surfed the internet for 2 days but still could not get any idea. Code is #include //#include #include #include #include void* increment(void *msg); int count = 0; int num[1]; pthread_mutex_t mutex;// = PTHREAD_MUTEX_INITIALIZER; int main() { //int num [1]; FILE *f; f=fopen("data.txt","w"); num[0]=0; fwrite(num,sizeof(num),1,f); fclose(f); pthread_t t1, t2, t3, t4, t5; pthread_mutex_init(&mutex,NULL); int tid1, tid2, tid3, tid4, tid5; char msg1[] = "Thread 1"; char msg2[] = "Thread 2"; char msg3[] = "Thread 3"; char msg4[] = "Thread 4"; char msg5[] = "Thread 5"; pthread_attr_t *attr; pthread_attr_init(attr); tid1 = pthread_create( &t1, NULL, increment, (void*)msg1); tid2 = pthread_create( &t2, NULL, increment, (void*)msg2); tid3 = pthread_create( &t3, NULL, increment, (void*)msg3); tid4 = pthread_create( &t4, NULL, increment, (void*)msg4); tid5 = pthread_create( &t5, NULL, increment, (void*)msg5); printf("Threads are created!\n"); pthread_join(t1, NULL); pthread_join(t2, NULL); pthread_join(t3, NULL); pthread_join(t4, NULL); pthread_join(t5, NULL); return 0; } void *increment(void* msg) { char *message = (char*)msg; printf("%s starts: ", message); for (int i=0;i<20;i++) { pthread_mutex_lock( &mutex ); FILE* fh = fopen("data.txt", "r+"); fread(num, sizeof(num), 1, fh); num[0] = num[0]+1; printf("%d\n", num[0]); fseek(fh,0,SEEK_SET); fwrite(num, sizeof(num), 1, fh); fclose(fh); pthread_mutex_unlock( &mutex ); } pthread_exit(NULL); }

    We Believe in Excellence www.aqueelmirza.cjb.net

    C 1 Reply Last reply
    0
    • A Aqueel

      Hi I have written following code but when i execute it, i get an error "segmentation fault". Can anyone plz tell me why i am getting the error. I have surfed the internet for 2 days but still could not get any idea. Code is #include //#include #include #include #include void* increment(void *msg); int count = 0; int num[1]; pthread_mutex_t mutex;// = PTHREAD_MUTEX_INITIALIZER; int main() { //int num [1]; FILE *f; f=fopen("data.txt","w"); num[0]=0; fwrite(num,sizeof(num),1,f); fclose(f); pthread_t t1, t2, t3, t4, t5; pthread_mutex_init(&mutex,NULL); int tid1, tid2, tid3, tid4, tid5; char msg1[] = "Thread 1"; char msg2[] = "Thread 2"; char msg3[] = "Thread 3"; char msg4[] = "Thread 4"; char msg5[] = "Thread 5"; pthread_attr_t *attr; pthread_attr_init(attr); tid1 = pthread_create( &t1, NULL, increment, (void*)msg1); tid2 = pthread_create( &t2, NULL, increment, (void*)msg2); tid3 = pthread_create( &t3, NULL, increment, (void*)msg3); tid4 = pthread_create( &t4, NULL, increment, (void*)msg4); tid5 = pthread_create( &t5, NULL, increment, (void*)msg5); printf("Threads are created!\n"); pthread_join(t1, NULL); pthread_join(t2, NULL); pthread_join(t3, NULL); pthread_join(t4, NULL); pthread_join(t5, NULL); return 0; } void *increment(void* msg) { char *message = (char*)msg; printf("%s starts: ", message); for (int i=0;i<20;i++) { pthread_mutex_lock( &mutex ); FILE* fh = fopen("data.txt", "r+"); fread(num, sizeof(num), 1, fh); num[0] = num[0]+1; printf("%d\n", num[0]); fseek(fh,0,SEEK_SET); fwrite(num, sizeof(num), 1, fh); fclose(fh); pthread_mutex_unlock( &mutex ); } pthread_exit(NULL); }

      We Believe in Excellence www.aqueelmirza.cjb.net

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      Aqueel wrote:

      I have surfed the internet for 2 days but still could not get any idea.

      Wouldn't it be faster to use your debugger ? You'll get direct information about where the problem resides and you can check your variables.


      Cédric Moonen Software developer
      Charting control [v1.1]

      A 1 Reply Last reply
      0
      • C Cedric Moonen

        Aqueel wrote:

        I have surfed the internet for 2 days but still could not get any idea.

        Wouldn't it be faster to use your debugger ? You'll get direct information about where the problem resides and you can check your variables.


        Cédric Moonen Software developer
        Charting control [v1.1]

        A Offline
        A Offline
        Aqueel
        wrote on last edited by
        #3

        Yeah. :) . At last i got it. I was getting error when i tried to call pthread_attr_init(). Thanks

        We Believe in Excellence www.aqueelmirza.cjb.net

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups