0

Output of the program

I'm trying to compile my code and I feel it works perfectly fine and I can get it to compile. However, when I do I'm getting Segment fault error and I don't see where the error is in my code.

The error i'm getting is Segment fault: 11 I've looked this up and I know it has to do with memory allocation but haven't been able to find where in the code do I need to fix my memory allocation and fix the errors I have on here.

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>

/*The Main Function Start*/
void main(int argc, char *argv[])
{
    /*Storing The Process Id*/
    pid_t pid;
    int j;
    int status = 0;


    /*process of forking*/

    if (argc == 1){
        fprintf(stderr,"Usage: ./hw1 <starting value>\n");
    }
    int n = atoi(argv[1]);
    pid=fork();
    if (pid == -1){
        printf("Error in forking....\n");
        exit(0);
    }
    /*Child process*/
    if (pid == 0)
    {
        printf("Child PID: %d\n",getpid());

        while (n != 1){
            printf("%d ",n);
            if (n % 2 == 0){
                n = n/2;
            }
            else {
                n = 3*n + 1;
            }

        }
        printf("1\n");
    }
    else{
        printf("Parent PID: %d\n",getpid());
        /*Waiting for the child to finish*/
        wait(0);
    }
    exit(0);
}
black
  • 59
  • 2
  • 11
  • You get a `segfault` when you call it without any arguments, because you didn't `exit` or `return` after printing to `stderr`. The `atoi(argv[1])` is problem because of what I said before. – Seoul Feb 04 '18 at 21:36
  • Please don't post pictures of the output from the terminal, just copy & paste the output from terminal and post it in your code. – Pablo Feb 04 '18 at 21:59

1 Answers1

1

I only get a segfault when I'm not passing any parameters, because

int n = atoi(argv[1]);

would be doing essentially atoi(NULL), because argv[1] would be NULL.

if (argc != 2){
        fprintf(stderr, "usage: %s <starting value>\n", argv[0]);
        return 1; // <- you forgot this!
}

Calling your program with different values doesn't lead to a segfault, I've tried it with different numbers.

Another error: the main function should be defined as:

  • int main(void);
  • int main(int argc, char *argv[]);
  • int main(int argc, char **argv);

You should change that. See What should main() return in C and C++?

For general information. If you want to exit a void function, all you have to do is use return; without any value, like this:

void foo(void)
{
    do_some_work();

    if(should_i_terminate)
        return;

    keep_doing_work();
}
Pablo
  • 13,271
  • 4
  • 39
  • 59
  • I'm getting this now – black Feb 04 '18 at 21:38
  • void function 'main' should not return a value – black Feb 04 '18 at 21:39
  • @black `main` should not be `void`, I've missed that error. – Pablo Feb 04 '18 at 21:40
  • 1
    @black: See [What should `main()` return in C and C++?](https://stackoverflow.com/questions/204476/) for the sordid details. In general, `void main()` is a bug. – Jonathan Leffler Feb 04 '18 at 21:40
  • Thanks I fixed that but the program isn't producing the child as intende.d – black Feb 04 '18 at 21:43
  • 1
    Slightly more solid would be `if (argc != 2)` – Weather Vane Feb 04 '18 at 21:44
  • @black That's another problem that has nothing to do with the segfault. You haven't told us what the child should do/print, I don't know the algorith, so I cannot see any error. I merely answer your question. – Pablo Feb 04 '18 at 21:47
  • Thanks for the help. I got it to work but it's not printing the child. It just exits right after starting value – black Feb 04 '18 at 21:48
  • Program should also display the process IDs of both the parent and child process – black Feb 04 '18 at 21:49
  • @black what does *right after starting value* mean? The child is behaving accordingly to your code: it enters, calculates some values and prints them, when the value is 1 it exists the loop, prints 1, and then there's nothing more to do `exit(0)` is executed. What else did you expect? – Pablo Feb 04 '18 at 21:50
  • @black it does. I executed your program with the value 3: `./b 3`, the output is `Parent PID: 13630 Child PID: 13631 3 10 5 16 8 4 2 1`. – Pablo Feb 04 '18 at 21:52
  • Sorry i'm kinda new to programming. I just uploaded the output of my code. I want it to print the process ID of both parent and child. – black Feb 04 '18 at 21:52
  • I'm soooo stupid. I just ran it without putting any number in. – black Feb 04 '18 at 21:53