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: -
#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;
-
}
-
-
}
-
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: -
#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"
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??
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.
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".
Sign in to post your reply or Sign up for a free account.
Similar topics |
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,
|
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
|
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.
|
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
|
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...
| |
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.
|
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 {
|
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...
|
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'...
|
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
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
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...
|
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...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |