summaryrefslogtreecommitdiff
path: root/Let_us_C_by_Yashavant_P._Kanetkar/chapter-20.ipynb
blob: 16c8fdc84089133b495514f4cce45c3bb8b81620 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
{
 "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": {}
  }
 ]
}