473,797 Members | 3,166 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Seg fault in Fibonacci fork program??

1 New Member
I cannot find what is giving me the segfault in my code...I used printf statements to check for errors....anyon e see the problem??

#include <stdio.h>
#include <sys/ptrace.h>
#include <sys/wait.h>
#include <unistd.h> //required for fork
#include <stdlib.h>

int main(int argc, char *argv[]) {

int fib[75];
int i;
pid_t pid = fork(); //fork and save pid
siginfo_t siginfo; //this will save info on signals from traced process
int waitval = 0; //value returned by wait. used to see if child ended

if(argc != 2){
printf("Please enter a valid integer.\n");
exit(1);
}

if(*argv[2] < 0){
printf("Please enter an integer greater than 0.\n");
}

if(*argv[2] == 0){
printf("0\n");
exit(1);
}

if(*argv[2] == 1){
printf("1\n");
exit(1);
}

if(pid == 0){ //child

ptrace(PTRACE_T RACEME, 0, NULL, NULL); //tell parent to trace child

for(i = 0; i < *argv[2]; i++)
*argv[i] = *argv[i-1] + *argv[i-2];

for(i = 1; i < *argv[2]; i++)
printf("%10d\n" , *argv[2]);

execvp("ls", NULL);
}
else{
wait(&waitval); //wait for signal from child
while(1){
ptrace(PTRACE_G ETSIGINFO, pid, NULL, &siginfo); //get siginfo from child
printf("Signal number: %d\n", siginfo.si_sign o); //print siginfo

if(WIFEXITED(wa itval)) //quit loop if the signal returned from wait
break; //was child exiting
ptrace(PTRACE_C ONT, pid, NULL, NULL); //tell child to continue
wait(&waitval); }} //wait again for next signal


return 0;}
Sep 12 '08 #1
4 3733
donbock
2,426 Recognized Expert Top Contributor
It would have been a lot easier to read your program if you used CODE tags. It would also help if you told us where in the program the Seg Fault occurred.

It looks like your intent is for the child process to do all the computations and for the parent process to simply wait til the child is done (although that execvp call looks tricky). It isn't obvious to me what benefit you gain from the parent process in that case.
  1. You should add comments explaining what the command line arguments are; and what the output should look like when the program works.
  2. If you want a single process to do all the computations, then you should start by writing that process first (don't use fork). You can add fork later.
  3. On the other hand, there might be a more fruitful way to use multiple processes. What is your algorithm for computing the fibonacci sequence? Try stating the procedure in English first (not C). (It wouldn't be a bad idea to enshrine those words as comments in your program.)
  4. Take a close look at your code. You aren't using all of your variables (hint).
  5. What do you expect the execvp() call to do?
Cheers,
donbock
Sep 12 '08 #2
donbock
2,426 Recognized Expert Top Contributor
There are two basic techniques for implementing repetition in software: iteration and recursion. The typical way to achieve recursion is to nest a series of self-referential function calls. I suppose another way to achieve the same end is a cascade of forked child processes.

Cheers,
donbock
Sep 13 '08 #3
JosAH
11,448 Recognized Expert MVP
Expand|Select|Wrap|Line Numbers
  1. for(i = 0; i < *argv[2]; i++)
  2.     *argv[i] = *argv[i-1] + *argv[i-2];
  3.  
Check what happens when i == 0 or i == 1; your array index is out of bounds.
Another thing is that argv[i] points to a character sequence, not ints.

kind regards,

Jos
Sep 13 '08 #4
song846079
1 New Member
siginfo_t siginfo; //this will save info on signals from traced process
siginfo should be static
Oct 27 '14 #5

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

Similar topics

28
13123
by: dleecurt | last post by:
Hello, I have a small problem, I am trying to write a program that will calculate the Fibonacci number series, and I have the code complete with one problem. I used a long in to store the numbers, and when the numbers get too large it maxes out the int and I can't count any higher. I am trying to use extremely large numbers, I would like to use up to 10^50 or so. So my question is how do I do this? I'm just learning the language and I...
0
2346
by: Alex Vinokur | last post by:
An algorithm which computes very long Fibonacci numbers http://groups.google.com/groups?selm=bnni5p%2412i47o%241%40ID-79865.news.uni-berlin.de was used as a performance testsuite to compare speed of the code produced by various compilers. =========================================================== Windows 2000 Professional Ver 5.0 Build 2195 Service Pack 2 Intel(R) Celeron(R) CPU 1.70 GHz GNU time 1.7 (to get the real time used)
12
2706
by: CII | last post by:
Hi everybody. I've been reading posts a year old about the fibonacci series right on this newsgroup, and while it's not directly games related, I'll share my own notes as well. On another newsgroup there is an ongoing discussion about the famous fibonacci sequences, and a fine program written by a poster got my attention so just for fun I wrote one myself and put it on the web just about two days ago. It can be found on the site ...
0
3302
by: misho | last post by:
Write a C program using the fork() system call that that generates the Fibonacci sequence in the child process. The number of the sequence will be provided in the command line. For example, if 5 is provided, the first five numbers in the Fibonacci sequence will be output by the child process. Because the parent and child processes have their own copies of the data, it will be necessary for the child to output the sequence. Have the parent invoke...
13
3174
by: mac | last post by:
Hi, I'm trying to write a fibonacci recursive function that will return the fibonacci string separated by comma. The problem sounds like this: ------------- Write a recursive function that creates a character string containing the first n Fibonacci numbers - F(n) = F(n - 1) + F(n - 2), F(0) = F(1) = 1 -, separated by comma. n should be given as an argument to the program. The recursive function should only take one parameter, n, and...
6
4978
by: Andrew Tatum | last post by:
I'm having some problems with the below equation. I have no problems when it comes to positives. Negatives create the problem.. C 2 1 4 However, this doesn't work:
3
5688
by: CMorgan | last post by:
Hi everybody, I am experiencing an annoying problem with fork() and execv(). In my program I need to launch the "pppd" from a thread, so, I create a new process with fork and then in the child process I launch with execv the pppd to connect an embedded system to internet. The problem is that pppd fails. I have made two test program just to discard bugs (my applications is really big) and the result is that when I call execv from a...
4
3405
by: jewel87 | last post by:
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...
1
6368
by: iugamage | last post by:
Help me to write a C program using the fork() system call that that generates the Fibonacci sequence in the child process. The number of the sequence will be provided in the command line. For example, if 5 is provided, the first five numbers in the Fibonacci sequence will be output by the child process. Because the parent and child processes have their own copies of the data, it will be necessary for the child to output the sequence. Have the...
0
10469
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10209
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9066
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
7560
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
6803
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4135
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3750
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2934
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.