summaryrefslogtreecommitdiff
path: root/Let_us_C/chapter-20.ipynb
diff options
context:
space:
mode:
authorJovina Dsouza2014-06-18 12:43:07 +0530
committerJovina Dsouza2014-06-18 12:43:07 +0530
commit206d0358703aa05d5d7315900fe1d054c2817ddc (patch)
treef2403e29f3aded0caf7a2434ea50dd507f6545e2 /Let_us_C/chapter-20.ipynb
parentc6f0d6aeb95beaf41e4b679e78bb42c4ffe45a40 (diff)
downloadPython-Textbook-Companions-206d0358703aa05d5d7315900fe1d054c2817ddc.tar.gz
Python-Textbook-Companions-206d0358703aa05d5d7315900fe1d054c2817ddc.tar.bz2
Python-Textbook-Companions-206d0358703aa05d5d7315900fe1d054c2817ddc.zip
adding book
Diffstat (limited to 'Let_us_C/chapter-20.ipynb')
-rw-r--r--Let_us_C/chapter-20.ipynb181
1 files changed, 181 insertions, 0 deletions
diff --git a/Let_us_C/chapter-20.ipynb b/Let_us_C/chapter-20.ipynb
new file mode 100644
index 00000000..52b3cd37
--- /dev/null
+++ b/Let_us_C/chapter-20.ipynb
@@ -0,0 +1,181 @@
+{
+ "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": [
+ "'''Program to demonstrate how to create a child process'''\n",
+ "\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": [
+ "'''Program to demonstrate how to create a child process'''\n",
+ "\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": [
+ "'''Program to get the pids of parent and child processes'''\n",
+ "\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": [
+ "'''Program that uses execl( ) to run a new program in the child process.'''\n",
+ "\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": [
+ "'''Program that gets the exit code\n",
+ "of the terminated process and thereby ensures a proper cleanup.'''\n",
+ "\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": {}
+ }
+ ]
+} \ No newline at end of file