Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jan 2003
    Location
    UK, England
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Little Program help?

    Im new to C Programming and have been workin on afew examples off the internet, but I cant seem to work out what these 2 programs do, could some1 please tell me what these programs do, and help me understand what each line does? plz, thx in advance.

    Code:
    main()
    {
     	 printf("My pid = %d.  My parent's pid = %d\n", getpid(), getppid());
    }
    Code:
    fatal(s)
    char *s;
    {
    	perror(s);
    	exit(1);
    }
    
    main()
    {
    	int pid;
    
    	pid = fork();
    	printf("The PID is %d\n",pid);
    	if(pid > 0)
    	{
    		wait((int *)0);
    		printf("ls completed\n");
    		exit(0);
    	}
    
    	if (pid == 0)
    	{
    		execl("/bin/ls", "ls", "-l", (char *)0);
    		fatal("execl failed");
    	}
    	fatal("fork failed");
    }

  • #2
    New Coder
    Join Date
    Jan 2003
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts
    one problem that I have seen here is that there is no fork() function here so basically there could be no output in here. where did u get this?

  • #3
    New Coder
    Join Date
    Jan 2003
    Location
    UK, England
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

  • #4
    psp
    psp is offline
    New Coder
    Join Date
    Mar 2003
    Location
    CA
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts
    The first one is printing out the process ID of itself and process ID of its parent.
    Did not understand the second program.

  • #5
    Regular Coder
    Join Date
    Feb 2003
    Location
    California
    Posts
    925
    Thanks
    0
    Thanked 0 Times in 0 Posts
    the second one creates a process and the stores the value hence pid=fork() and then with that int it finds out if the creation of the new process with the fork() call actually works or not hence the use of the 'if' statements to find out if its valid.


    Jason


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •