Thread: Unexpected output for simple forking program

  1. #1
    Registered User
    Join Date
    Jan 2019
    Posts
    5

    Unexpected output for simple forking program

    Hello, Im a beginner in C and I’ve started learning about system calls and forking. I am forking in a loop but I get unexpected behaviour. My program takes one number and simply forks(), it also prints pids using getppid and pid:


    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <sys/wait.h>
    
    
    int main(int argc, char **argv) {
    
    
    	int i;
    	int n;
    	int kids;
    
    
    	if (argc != 2) {
    		fprintf(stderr, "error");
    		exit(1);
    	}
    
    
    	kids = strtol(argv[1], NULL, 10);
    
    
    	for (i = 0; i < kids; i++) {
    		n = fork();
    		if (n < 0) {
    			perror("fork");
    			exit(1);
    		}
     		printf("pid = %d, ppid = %d, i = %d\n", getpid(), getppid(), i);
            exit(0);
    	}
    
    
    	return 0;
    }
    when running this program with ANY command line the output is:

    Code:
    $ ./my_program 3
    
    pid = 7824, ppid = 7763, i=0
    pid = 7825, ppid = 7824, i=0

    My goal is to make sure the children dont fork, only the parent should fork. W Other sources also say to exit the loop in this way, and i cant exit any further up as i want my children to print the pid’s too. I expected 6 lines since I passed in 3, but with any argument I get the same thing. What mistake am I making?

    Thank you for your help!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Something like this?
    Code:
        for (i = 0; i < kids; i++) {
            n = fork();
            if (n < 0) {
                perror("fork");
                exit(1);
            }
            printf("pid = %d, ppid = %d, i = %d\n", getpid(), getppid(), i);
            if ( n == 0 ) {
                exit(0);
            } else {
                wait(NULL);
            }
        }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unexpected behaviour of a simple C program
    By Lucky Luka in forum C Programming
    Replies: 4
    Last Post: 02-24-2019, 06:53 AM
  2. Unexpected answers from simple calculator program
    By Aniruddha Bera in forum C Programming
    Replies: 7
    Last Post: 06-23-2015, 01:11 PM
  3. Piping, Forking, and Gathering execvp's Output
    By Scriptonaut in forum C Programming
    Replies: 6
    Last Post: 11-20-2012, 05:15 PM
  4. Very simple program with unexpected output
    By Draylath in forum C++ Programming
    Replies: 5
    Last Post: 07-04-2012, 10:38 AM
  5. Unexpected output
    By juice in forum Windows Programming
    Replies: 6
    Last Post: 03-10-2012, 11:13 AM

Tags for this Thread