By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,806 Members | 1,343 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,806 IT Pros & Developers. It's quick & easy.

Segmentation fault

P: 62
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:
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. main()
  4. {
  5.  
  6.    int buff;
  7.    int odd =1;
  8.    int count =1;
  9.    int flag =0;
  10.    int even =2;
  11.  
  12.    int p[2], pid1, pid2, pid3;
  13.    pipe(p);
  14.    pid1 = fork();
  15.    pid2 = fork();
  16.    pid3 = fork();
  17.  
  18. while(count < 21)
  19.    {
  20.         if (( pid1 = 0 )&&( flag == 0 ))
  21.                 {
  22.                         printf("Odd number: \n");
  23.                         write(p[1], odd);
  24.                         odd = odd + 1;
  25.                         flag = 1;
  26.                 }
  27.         else
  28.         {
  29.           sleep(1);
  30.           read(p[0], buff);
  31.           printf("Number read: %s\n", buff);
  32.         }
  33.  
  34.         if (( pid2 = 0 )&&( flag == 1 ))
  35.                 {
  36.                         printf("Even number: \n");
  37.                         write(p[1], even);
  38.                         even = even + 2;
  39.                         flag = 0;
  40.                 }
  41.         else
  42.           {
  43.            sleep(1);
  44.            read(p[0], buff);
  45.            printf("Number read: %s\n", buff);
  46.         }
  47.  
  48.      count = count + 1;
  49.   }
  50.  
  51. }
  52.  
Feb 17 '08 #1
Share this Question
Share on Google+
4 Replies

ashitpro
Expert 100+
P: 542
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:
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. main()
  4. {
  5.  
  6.    int buff;
  7.    int odd =1;
  8.    int count =1;
  9.    int flag =0;
  10.    int even =2;
  11.  
  12.    int p[2], pid1, pid2, pid3;
  13.    pipe(p);
  14.    pid1 = fork();
  15.    pid2 = fork();
  16.    pid3 = fork();
  17.  
  18. while(count < 21)
  19.    {
  20.         if (( pid1 = 0 )&&( flag == 0 ))
  21.                 {
  22.                         printf("Odd number: \n");
  23.                         write(p[1], odd);
  24.                         odd = odd + 1;
  25.                         flag = 1;
  26.                 }
  27.         else
  28.         {
  29.           sleep(1);
  30.           read(p[0], buff);
  31.           printf("Number read: %s\n", buff);
  32.         }
  33.  
  34.         if (( pid2 = 0 )&&( flag == 1 ))
  35.                 {
  36.                         printf("Even number: \n");
  37.                         write(p[1], even);
  38.                         even = even + 2;
  39.                         flag = 0;
  40.                 }
  41.         else
  42.           {
  43.            sleep(1);
  44.            read(p[0], buff);
  45.            printf("Number read: %s\n", buff);
  46.         }
  47.  
  48.      count = count + 1;
  49.   }
  50.  
  51. }
  52.  

check your variable "buff", you declare it as a "int" and using everywhere as a "string"
Feb 18 '08 #2

P: 62
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??
Feb 18 '08 #3

ashitpro
Expert 100+
P: 542
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.
Feb 19 '08 #4

P: 62
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".
Feb 19 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.