473,385 Members | 1,661 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Segmentation fault

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
4 3386
ashitpro
542 Expert 512MB
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
jewel87
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
542 Expert 512MB
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
jewel87
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

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

Similar topics

2
by: sivignon | last post by:
Hi, I'm writing a php script which deals with 3 ORACLE databases. This script is launch by a script shell on an linux machine like this : /../php/bin/php ./MySript.php (PHP 4.3.3) My script...
3
by: diyanat | last post by:
i am writing a cgi script in C using the CGIC library, the script fails to run, i am using apache on linux error report from apache : internal server error Premature end of script headers:...
16
by: laberth | last post by:
I've got a segmentation fault on a calloc and I don'tunderstand why? Here is what I use : typedef struct noeud { int val; struct noeud *fgauche; struct noeud *fdroit; } *arbre; //for those...
3
by: Zheng Da | last post by:
Program received signal SIGSEGV, Segmentation fault. 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 (gdb) bt #0 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 #1 0x40094c54 in malloc...
5
by: Fra-it | last post by:
Hi everybody, I'm trying to make the following code running properly, but I can't get rid of the "SEGMENTATION FAULT" error message when executing. Reading some messages posted earlier, I...
18
by: Digital Puer | last post by:
Hi, I'm coming over from Java to C++, so please bear with me. In C++, is there a way for me to use exceptions to catch segmentation faults (e.g. when I access a location off the end of an array)?...
27
by: Paminu | last post by:
I have a wierd problem. In my main function I print "test" as the first thing. But if I run the call to node_alloc AFTER the printf call I get a segmentation fault and test is not printed! ...
7
by: pycraze | last post by:
I would like to ask a question. How do one handle the exception due to Segmentation fault due to Python ? Our bit operations and arithmetic manipulations are written in C and to some of our...
3
by: madunix | last post by:
My Server is suffering bad lag (High Utlization) I am running on that server Oracle10g with apache_1.3.35/ php-4.4.2 Web visitors retrieve data from the web by php calls through oci cobnnection...
6
by: DanielJohnson | last post by:
int main() { printf("\n Hello World"); main; return 0; } This program terminate just after one loop while the second program goes on infinitely untill segmentation fault (core dumped) on...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.