Find the answer to your Linux question:
Results 1 to 4 of 4
i m getting segmentation fault for running the following programs in fedora core 1 main() { int *i,pid; *i=10; pid=fork(); if(pid==0) { printf("Address of i in child is %d\n",i); } ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux User
    Join Date
    Nov 2002
    Posts
    420

    the reason for the segmentation fault


    i m getting segmentation fault for running the following programs in fedora core 1

    main()
    {
    int *i,pid;
    *i=10;
    pid=fork();
    if(pid==0)
    {
    printf("Address of i in child is %d\n",i);
    }
    else
    {
    printf("Address of i in parent is %d\n",i);
    }
    }


    program2

    #include<fcntl.h>
    #include<stdio.h>
    main()
    {
    int fp;
    char chr='A';
    int pid;
    pid=fork();
    if(pid==0)
    {
    fp=fopen("baby",O_WRONLY);
    printf("In child chr is:%c\n",chr);
    chr='B';
    write(fp,&chr,1);
    printf("Child exiting\n");
    fclose(fp);
    }
    else
    {
    wait((int *)0);
    fp=fopen("baby",O_RDONLY);
    read(fp,&chr);
    printf("Chr after parent reads is %c",chr);
    close(fp);

    }
    }

    plz tell me the reason

  2. #2
    Linux Newbie
    Join Date
    Feb 2004
    Location
    India
    Posts
    132
    Hi
    Am a noobie in C prog.

    But i dont think *i=10 is correct in the first prog.
    You are the one Linux!

  3. #3
    Linux Newbie
    Join Date
    Feb 2004
    Location
    India
    Posts
    132
    You are trying to read a value which is in the address 10.
    Which is not in the segment of the current program, thats why it gives a segfault.

    Heres the corrected code which i think you are expecting.

    main()
    {
    int *i;
    int pid;
    int j=10;
    i = &j;
    pid=fork();
    if(pid==0)
    {
    printf("PID = %d\n",pid);
    printf("Address of i in child is %d\n",&i);
    }
    else
    {
    printf("PID = %d\n",pid);
    printf("Address of i in parent is %d\n",&i);
    }
    }
    You are the one Linux!

  4. #4
    Just Joined!
    Join Date
    Aug 2002
    Location
    New Jersey
    Posts
    28
    Actually, your syntax is more or less right, you just forgot to allocate memory for the pointer to point to. You have a pointer that points to nothing (aka a dangling pointer), and are trying to put values into memory that hasn't been allocated.

    /*CHANGE*/
    #include <stdlib.h>

    main()
    {
    int *i,pid;

    /*CHANGE*/
    i = (int*)malloc(sizeof(int));

    *i=10;
    pid=fork();
    if(pid==0)
    {
    printf("Address of i in child is %d\n",i);
    }
    else
    {
    printf("Address of i in parent is %d\n",i);
    }
    }

    this should work


    as far as your 2nd prog is concerned, im not at a unix box, so i cant actually test it, but my gut tells me that passing 0 to wait() is ur problem. If im reading this right, your passing address 0 to wait. Try using wait(NULL); if u dont care about the stat_loc parameter

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •