Segmentation fault

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jewel87
    New Member
    • Jan 2007
    • 62

    Segmentation fault

    Hi everyone!
    I am writing a program in C under UNIX, the task basically is this:

    create three processes, two write down the pipe and one that reads from the pipe. The first child process writes the first 20 odd integers, and the second child process writes the first 20 even integers. Make the reading process (the third child process) print any messages it receives on its standard output. Find out a solution and modify the program to guarantee the reader can get the numbers in numerical order.

    After executing some code I have, I get a Segmentation fault.
    Can anyone give me a hint why am i getting this??
    I know it is something about unallocated memory, but i'm so new to all these multiprocesses and pipes, and not very clear about them.
    Please, help!

    Here is my code:
    Code:
    #include <stdio.h>
    
    main()
    {
    
       int buff;
       int odd =1;
       int count =1;
       int flag =0;
       int even =2;
    
       int p[2], pid1, pid2, pid3;
       pipe(p);
       pid1 = fork();
       pid2 = fork();
       pid3 = fork();
    
    while(count < 21)
       {
            if (( pid1 = 0 )&&( flag == 0 ))
                    {
                            printf("Odd number: \n");
                            write(p[1], odd);
                            odd = odd + 1;
                            flag = 1;
                    }
            else
            {
              sleep(1);
              read(p[0], buff);
              printf("Number read: %s\n", buff);
            }
    
            if (( pid2 = 0 )&&( flag == 1 ))
                    {
                            printf("Even number: \n");
                            write(p[1], even);
                            even = even + 2;
                            flag = 0;
                    }
            else
              {
               sleep(1);
               read(p[0], buff);
               printf("Number read: %s\n", buff);
            }
    
         count = count + 1;
      }
    
    }
  • ashitpro
    Recognized Expert Contributor
    • Aug 2007
    • 542

    #2
    Originally posted by jewel87
    Hi everyone!
    I am writing a program in C under UNIX, the task basically is this:

    create three processes, two write down the pipe and one that reads from the pipe. The first child process writes the first 20 odd integers, and the second child process writes the first 20 even integers. Make the reading process (the third child process) print any messages it receives on its standard output. Find out a solution and modify the program to guarantee the reader can get the numbers in numerical order.

    After executing some code I have, I get a Segmentation fault.
    Can anyone give me a hint why am i getting this??
    I know it is something about unallocated memory, but i'm so new to all these multiprocesses and pipes, and not very clear about them.
    Please, help!

    Here is my code:
    Code:
    #include <stdio.h>
    
    main()
    {
    
       int buff;
       int odd =1;
       int count =1;
       int flag =0;
       int even =2;
    
       int p[2], pid1, pid2, pid3;
       pipe(p);
       pid1 = fork();
       pid2 = fork();
       pid3 = fork();
    
    while(count < 21)
       {
            if (( pid1 = 0 )&&( flag == 0 ))
                    {
                            printf("Odd number: \n");
                            write(p[1], odd);
                            odd = odd + 1;
                            flag = 1;
                    }
            else
            {
              sleep(1);
              read(p[0], buff);
              printf("Number read: %s\n", buff);
            }
    
            if (( pid2 = 0 )&&( flag == 1 ))
                    {
                            printf("Even number: \n");
                            write(p[1], even);
                            even = even + 2;
                            flag = 0;
                    }
            else
              {
               sleep(1);
               read(p[0], buff);
               printf("Number read: %s\n", buff);
            }
    
         count = count + 1;
      }
    
    }

    check your variable "buff", you declare it as a "int" and using everywhere as a "string"

    Comment

    • jewel87
      New Member
      • Jan 2007
      • 62

      #3
      Originally posted by ashitpro
      check your variable "buff", you declare it as a "int" and using everywhere as a "string"
      Thanks, i've changed it to char buff[2];
      but now i'm getting output as:
      Number read:
      Number read:
      Number read:
      Number read:
      ...
      it basically does not write anything to the pipe. What is wrong??

      Comment

      • ashitpro
        Recognized Expert Contributor
        • Aug 2007
        • 542

        #4
        Originally posted by jewel87
        Thanks, i've changed it to char buff[2];
        but now i'm getting output as:
        Number read:
        Number read:
        Number read:
        Number read:
        ...
        it basically does not write anything to the pipe. What is wrong??

        please read the about "write" system call
        I can see from your code that your trying to write "int" into pipe.
        you should convert it(odd and even variables) to string and then write it.

        I'm still surprised, How can you compile your code successfully? It should end up with couple of warnings.

        Comment

        • jewel87
          New Member
          • Jan 2007
          • 62

          #5
          Originally posted by ashitpro
          please read the about "write" system call
          I can see from your code that your trying to write "int" into pipe.
          you should convert it(odd and even variables) to string and then write it.

          I'm still surprised, How can you compile your code successfully? It should end up with couple of warnings.
          as far as i understand, it doesn't even enter the child processes at all, otherwise at least it would print the messages "Odd number" and "Even number".

          Comment

          Working...