summaryrefslogtreecommitdiff
path: root/Let_us_C/chapter-21.ipynb
diff options
context:
space:
mode:
Diffstat (limited to 'Let_us_C/chapter-21.ipynb')
-rw-r--r--Let_us_C/chapter-21.ipynb226
1 files changed, 226 insertions, 0 deletions
diff --git a/Let_us_C/chapter-21.ipynb b/Let_us_C/chapter-21.ipynb
new file mode 100644
index 00000000..7cf37612
--- /dev/null
+++ b/Let_us_C/chapter-21.ipynb
@@ -0,0 +1,226 @@
+{
+ "metadata": {
+ "name": "chapter-21.ipynb"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h1>Chapter 21: More Linux Programming <h1>"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>SIGINT Example , Page number: 669<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to prevent the termination of a program even after hitting Ctrl + C'''\n",
+ "\n",
+ "import signal, os\n",
+ "\n",
+ "def sighandler ( signum,arg ):\n",
+ " print ( \"SIGINT received. Inside sighandler\" ) \n",
+ "\n",
+ "\n",
+ "signal.signal(signal.SIGINT,sighandler)\n",
+ "while ( 1 ):\n",
+ " print \"Program Running\" \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Handling Multiple Signals , Page number: 671<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to handle multiple signals.'''\n",
+ "\n",
+ "import signal, os\n",
+ "\n",
+ "def inthandler ( signum,arg ):\n",
+ " print \"SIGINT Received\" \n",
+ " \n",
+ "def termhandler ( signum ,arg):\n",
+ " print \"SIGTERM Received\" \n",
+ " \n",
+ "def conthandler ( signum,arg ):\n",
+ " print \"SIGCONT Received\" \n",
+ " \n",
+ "\n",
+ "signal.signal(signal.SIGINT,inthandler)\n",
+ "signal.signal(signal.SIGTERM,termhandler)\n",
+ "signal.signal(signal.SIGCONT,conthandler)\n",
+ "\n",
+ "while ( 1 ):\n",
+ " print \"Program Running\" "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Registering A Common Handler , Page number: 673<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to demonstrate a common signal handler to handle all signals\n",
+ "instead of registering a separate handler for each signal'''\n",
+ "\n",
+ "import signal, os\n",
+ "\n",
+ "def sighandler ( signum ,arg):\n",
+ " if(signum == SIGINT):\n",
+ " print \"SIGINT Received\" \n",
+ " elif(signum == SIGTERM):\n",
+ " print \"SIGTERM Received\" \n",
+ " elif(signum == SIGCONT):\n",
+ " print \"SIGCONT Received\" \n",
+ "\n",
+ "signal.signal(signal.SIGINT,sighandler)\n",
+ "signal.signal(signal.SIGTERM,sighandler)\n",
+ "signal.signal(signal.SIGCONT,sighandler)\n",
+ "\n",
+ "while ( 1 ):\n",
+ " print \"Program running\" \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Blocking Signals , Page number: 675<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to understand signal blocking '''\n",
+ "\n",
+ "import signal, os\n",
+ "\n",
+ "def sighandler ( signum ,arg):\n",
+ " if(signum == SIGINT):\n",
+ " print (\"SIGINT Received\") \n",
+ " elif(signum == SIGTERM):\n",
+ " print (\"SIGTERM Received\" )\n",
+ " elif(signum == SIGCONT):\n",
+ " print ( \"SIGCONT Received\" )\n",
+ "\n",
+ "buffer = \"\\0\"\n",
+ "signal.signal(signal.SIGINT,sighandler)\n",
+ "signal.signal(signal.SIGTERM,sighandler)\n",
+ "signal.signal(signal.SIGCONT,sighandler)\n",
+ "\n",
+ "signal.pthread_sigmask(signal.SIG_BLOCK, [])\n",
+ "\n",
+ "while ( buffer == \"\\0\" ):\n",
+ " buffer = input(\"Enter a string\")\n",
+ " print (buffer)\n",
+ "signal.pthread_sigmask(signal.SIG_UNBLOCK, [])\n",
+ "while(1):\n",
+ " print(\"Program running\")\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Event Driven Programming , Page number: 678<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program that uses GTK toolkit to create a window on the screen.'''\n",
+ "\n",
+ "from tkinter import *\n",
+ "\n",
+ "\n",
+ "class Example(Frame):\n",
+ " def __init__(self, parent):\n",
+ " Frame.__init__(self, parent)\n",
+ "\n",
+ " self.display = Canvas(self, width=700, height=200)\n",
+ " self.display.pack(side=\"top\", fill=\"both\", expand=True)\n",
+ " \n",
+ "if __name__ == \"__main__\":\n",
+ " root = Tk()\n",
+ " root.title(\"Sample Window\")\n",
+ " Frame = Example(parent=root)\n",
+ " Frame.pack(side=\"top\", fill=\"both\", expand=True)\n",
+ " root.mainloop()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>MyShapes , Page number: 681<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''Program to draw a few shapes in a window'''\n",
+ "\n",
+ "from tkinter import *\n",
+ "\n",
+ "\n",
+ "top = Tk()\n",
+ "top.title(\"Sample Window\")\n",
+ "C = Canvas(top, height=500, width=500)\n",
+ "rcoor = 10,20,200,100\n",
+ "rect = C.create_rectangle(rcoor,fill=\"black\")#rectangle\n",
+ "picoor = 250,0,350,100\n",
+ "pie = C.create_arc(picoor, start=300, extent=100, fill=\"black\")#pie\n",
+ "pocoor = 250, 150, 250, 300, 300, 350, 400, 300, 320, 190\n",
+ "polygon = C.create_polygon(pocoor,fill=\"black\")#polygon\n",
+ "C.pack()\n",
+ "top.mainloop()\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+} \ No newline at end of file