{
 "metadata": {
  "name": "chapter-20.ipynb"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h1>Chapter 20: C Under Linux <h1>"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Fork , Page number: 655<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "import os\n",
      "\n",
      "print  \"Before Forking\" \n",
      "child = os.fork() #create a child process\n",
      "print \"After Forking\\n\"  \n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 7
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Creating a Child Process , Page number: 656<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "import os\n",
      "\n",
      "pid = os.fork()\n",
      "if pid == 0:\n",
      "    print  \"In child process\"  # code to play animated GIF file\n",
      "else:\n",
      "    print  \"In parent process\"  #code to copy file \n",
      "         \n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>PID of Parent And Child Processes , Page number: 657<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "import os\n",
      "from multiprocessing import Process\n",
      "\n",
      "if __name__ == '__main__':\n",
      "    ppid=os.getpid()\n",
      "    p = Process()\n",
      "    p.start()\n",
      "    cid = os.getpid()\n",
      "  \n",
      "\n",
      "if (cid):\n",
      "    print (\"Child : Hello I am the child process\")\n",
      "    print (\"Child : Child\u2019s PID: \", os.getpid( ) )\n",
      "    print (\"Child : Parent\u2019s PID: \", os.getppid( ) )\n",
      "else:\n",
      "    print  (\"Parent : Hello I am the parent process\" )\n",
      "    print  (\"Parent : Parent\u2019s PID: \", os.getpid( ) )\n",
      "    print  (\"Parent : Child\u2019s PID: \", cid )\n",
      "\n",
      "\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Execl , Page number: 659<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "import os\n",
      "\n",
      "\n",
      "pid = os.fork()\n",
      "if pid == 0:\n",
      "    os.execl ( \"/bin/ls\",\"-al\", \"/etc\", NULL ) \n",
      "    print \"Child: After exec( )\"\n",
      "else:\n",
      "    print \"Parent process\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": "*"
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<h3>Waitpid , Page number: 662<h3>"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "import os\n",
      "\n",
      "i = 0 \n",
      "pid = os.fork( ) \n",
      "if ( pid == 0 ):\n",
      "    while ( i < 4294967295 ):\n",
      "        i=i+1\n",
      "        print  \"The child is now terminating\" \n",
      "else:\n",
      "    os.waitpid ( pid, status, 0 )\n",
      "    if ( os.WIFEXITED ( status ) ):\n",
      "        print  \"Parent: Child terminated normally\" \n",
      "    else:\n",
      "        print  \"Parent: Child terminated abnormally\" \n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": "*"
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}