{ "metadata": { "name": "chapter-20.ipynb" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "

Chapter 20: C Under Linux

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Fork , Page number: 655

" ] }, { "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": [ "

Creating a Child Process , Page number: 656

" ] }, { "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": [ "

PID of Parent And Child Processes , Page number: 657

" ] }, { "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": [ "

Execl , Page number: 659

" ] }, { "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": [ "

Waitpid , Page number: 662

" ] }, { "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": {} } ] }