Thread: Help with understanding C code in my text

  1. #1
    Registered User mothergoose729's Avatar
    Join Date
    Mar 2009
    Posts
    6

    Help with understanding C code in my text

    I am reading a book on operating systems before I go on to try and learn C. The book often lists code to illustrate points, and while it is not mandatory I understand it, I would like to so I can better learn from it. Both of the following codes were presented:

    Code:
    #include <sys/types.h>
    #include <stdio.h>
    #include <unistd.h>
    int main()
    {
    pid-t pid;
    /* fork a child process */
    pid = fork();
    if (pid < 0) {/* error occurred */
    fprintf(stderr, "Fork Failed");
    exit (-1) ;
    }
    else if (pid == 0} {/* child process */
    execlpf"/bin/Is","Is",NULL);
    }
    else {/* parent process */
    /* parent will wait for the child to complete */
    wait(NULL);
    printf("Child Complete");
    exit (0) ;
    This is supposed to represent how a child process that is running a copy of a program from the parent process looks in C coding for unix. I understand some things, like the idea of a fork statement, and the text tells me what the equation tries to accomplish. More direct and specific insight would be great though.

    The second equation below is supposed to represent how to create a child process function. How though is yet unclear to me. I have been told that it has something to do with microsoft paint as a shared resource, but they were very vague:
    Code:
    #include <stdio.h> i
    #include <windows.h>
    int main(VOID)
    {
    STARTUPINFO si;
    PROCESS_INFORMATION pi;
    // allocate memory
    ZeroMemory(&si, sizeof (si)) ;
    si.cb = sizeof (si) ;
    ZeroMemory(&pi, sizeof(pi));
    // create child process
    if (!CreateProcess(NULL, // use command line
    "C:\\WINDOWS\\system32\\mspaint.exe", // command line
    NULL, // don't inherit process handle
    NULL, // don't inherit thread handle
    FALSE, // disable handle inheritance
    0, //no creation flags
    NULL, // use parent's environment block
    NULL, // use parent's existing directory
    &si,
    &pi})
    {
    fprintf(stderr, "Create Process Failed");
    return -1;
    }
    // parent will wait for the child to complete
    WaitForSingleObject(pi.hProcess, INFINITE);
    printf("Child Complete");
    // close handles
    CloseHandle(pi.hProcess);
    CloseHandle(pi.hThread);
    }
    Thanks a lot in advanced

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Did you type that in?

    Code:
    execlpf"/bin/Is","Is",NULL);
    should almost certaibly be "ls" not "Is"....

    As to how fork works etc, you probably need to give us a better question than "more insight" - I could probably write two pages worth of information on what fork does and how it works, but it would quite possibly NOT answer your question.

    I have no idea what your question as to the second piece of code is? What do you mean by "shared resource"? mspaint is a very simple application supplied as part of Windows installations.

    CreateProcess() is teh call that creates a new process.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DirectX | Drawing text
    By gavra in forum Game Programming
    Replies: 4
    Last Post: 06-08-2009, 12:23 AM
  2. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM
  3. Replies: 14
    Last Post: 11-23-2005, 08:53 AM
  4. Am I understanding this C code correctly ???
    By AntonsonDJ in forum C Programming
    Replies: 3
    Last Post: 05-09-2005, 07:19 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM