Operating System | Process Scheduler | Process Creation | System calls - cook the code

Monday, 1 January 2018

Operating System | Process Scheduler | Process Creation | System calls

Process Creation



  • Parent process create children processes, which, in turn create other processes, forming a tree of processes
  • Generally, process identified and managed via a process identifier (pid)
  • Resource sharing
    • Parent and children share all resources
    • Children share subset of parent’s resources
    • Parent and child share no resources
  • Execution

    • Parent and children execute concurrently
    • Parent waits until children terminate


  • Address space
    • Child duplicate of parent
    • Child has a program loaded into it
  • UNIX examples

    • fork system call creates new process
    • exec system call used after a fork to replace the process’ memory space with a new program

                                          Process Creation

    Process Termination




  • Process executes last statement and asks the operating system to delete it (exit)
    • Output data from child to parent (via wait)
    • Process’ resources are deallocated by operating system
  • Parent may terminate execution of children processes (abort)
    • Child has exceeded allocated resources
    • Task assigned to child is no longer required
    • If parent is exiting
      • Some operating systems do not allow child to continue if its parent terminates
        • All children terminated - cascading termination

  •   Unix Process Creation


    •  →Unix fork() creates a process
      • Creates a new address space
      • Copies text, data, & stack into new adress space
      • Provides child with access to open files
    • →Unix exec() allows a child to run a new program
    • →Unix wait() allows a parent to wait for a child to terminate
    Note

    In Unix, process creation and management uses multiple, fairly simple system calls. This provides extra flexability. If needed, the parent process may contain the code for the child process to run, so that exec() is not always needed. The child may also set up inter-process communication with the parent, such as with a pipe before running another program.

    int pidValue;
    ...
    pidValue = fork();       /* Creates a child process */
    if(pidValue == 0) {
    /* pidValue is 0 for child, nonzero for parent */
    /* The child executes this code concurrently with parent */
        childsPlay(...);      /* A procedure linked into a.out */
        exit(0);
    }
    /* The parent executes this code concurrently with child */
    parentsWork(...);
    wait(...);
    ...

    exec()  
    The exec() family of functions replaces the current process image with a new process image. It loads the program into the current process space and runs it from the entry point.
    It comes under the header file unistd.h (in c language)



    fork vs exec
    • fork starts a new process which is a copy of the one that calls it, while exec replaces the current process image with another (different) one.
    • Both parent and child processes are executed simultaneously in case of fork() while Control never returns to the original program unless there is an exec() error.

    No comments:

    Post a Comment