473,806 Members | 2,771 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Segmentation fault

62 New Member
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 3406
ashitpro
542 Recognized Expert Contributor
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 New Member
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 Recognized Expert Contributor
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 New Member
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
6816
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 works fine and do all what I need. But at the end of the execution, I can read "Segmentation Fault". The segmentation fault appear at the end of my script execution,
3
1939
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: /var/www/cgi-bin/script.cgi when i debug the program i get Segmentation fault gdb ./script.cgi
16
9004
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 who don't speak french arbre means tree.
3
11452
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 () from /lib/tls/libc.so.6 It's really strange; I just call malloc() like "tmp=malloc(size);" the system gives me Segmentation fault I want to write a code to do like a dynamic array, and the code is as
5
2999
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 understood that a segmentation fault can occur whenever I declare a pointer and I leave it un-initialized. So I thought the problem here is with the (const char *)s in the stuct flightData (please note that I get the same fault declaring as char * the...
18
26125
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)? Thanks.
27
3375
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! #include <stdlib.h> #include <stdio.h> typedef struct _node_t {
7
5883
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 testcases we experiance Segmentation fault from the python libraries. If i know how to handle the exception for Segmentation fault , it will help me complete the run on any testcase , even if i experiance Seg Fault due to any one or many functions in...
3
5188
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 from 10g release2 PHP is configured with the following parameters './configure' '--prefix=/opt/oracle/php' '--with-apxs=/opt/oracle/apache/bin/apxs' '--with-config-file-path=/opt/oracle/apache/conf' '--enable-safe-mode' '--enable-session'...
6
5046
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 gcc. The only difference is that in first I only call "main" and in second call
0
9719
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9598
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10371
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10111
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9192
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7650
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5546
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3852
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3010
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.