assignment no:a-1 - wordpress.com€¦assignment no:a-1 title:-develop an application using...

39
ASSIGNMENT NO:A-1 TITLE:- Develop an application using Beaglebone Black/ ARM Cortex A5 development board to simulate the operations of LIFT. CLASS:- TE Computer NAME:- ROLL NO:- BATCH:- T- 3 -gpio.py import sys import os SYSFS_GPIO_DIR = "/sys/class/gpio" def gpioUnexport (gpio): try: fo = open(SYSFS_GPIO_DIR + "/unexport","w") fo.write(gpio) fo.close() return except IOError: return def gpioExport (gpio): try: fo = open(SYSFS_GPIO_DIR + "/export","w") fo.write(gpio) fo.close() return except IOError: return def gpioSetDir (gpio, flag): try: fo = open(SYSFS_GPIO_DIR + "/gpio" + gpio + "/direction" ,"w") fo.write(flag) fo.close() return except IOError: return

Upload: truongminh

Post on 13-May-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ASSIGNMENT NO:A-1 - WordPress.com€¦ASSIGNMENT NO:A-1 TITLE:-Develop an application using Beaglebone Black/ ARM Cortex A5 development board to simulate the operations of LIFT. CLASS:-

ASSIGNMENT NO:A-1

TITLE:- Develop an application using Beaglebone Black/ ARM Cortex A5 development board to simulate the operations of LIFT.

CLASS:- TE Computer

NAME:-

ROLL NO:-

BATCH:- T- 3

-gpio.pyimport sysimport os

SYSFS_GPIO_DIR = "/sys/class/gpio"

def gpioUnexport (gpio):try:

fo = open(SYSFS_GPIO_DIR + "/unexport","w") fo.write(gpio) fo.close() return except IOError: return

def gpioExport (gpio): try:

fo = open(SYSFS_GPIO_DIR + "/export","w") fo.write(gpio) fo.close() return except IOError: return

def gpioSetDir (gpio, flag):try: fo = open(SYSFS_GPIO_DIR + "/gpio" + gpio + "/direction" ,"w") fo.write(flag) fo.close() return

except IOError: return

Page 2: ASSIGNMENT NO:A-1 - WordPress.com€¦ASSIGNMENT NO:A-1 TITLE:-Develop an application using Beaglebone Black/ ARM Cortex A5 development board to simulate the operations of LIFT. CLASS:-

def gpioSetVal (gpio, val):try:

fo = open(SYSFS_GPIO_DIR + "/gpio" + gpio + "/value" ,"w") fo.write(val)fo.close()return

except IOError: return

def gpioSetEdge (gpio, flag): try:

fo = open(SYSFS_GPIO_DIR + "/gpio" + gpio + "/edge" ,"w") fo.write(flag)fo.close()

returnexcept IOError:

return

/******************************** liftpins.py ******************************/LED_1= (0 * 32) + 3LED_2= (0 * 32) + 23LED_3= (0 * 32) + 2LED_4= (0 * 32) + 26

LED_5= (1 * 32) + 17LED_6= (1 * 32) + 15LED_7= (0 * 32) + 15LED_8= (1 * 32) + 14

LED_9= (0 * 32) + 30LED_10 = (2 * 32) + 2LED_11 = (1 * 32) + 28LED_12 = (2 * 32) + 3LED_13 = (0 * 32) + 31LED_14 = (2 * 32) + 5LED_15 = (1 * 32) + 18

SW_1 = (0 * 32) + 14SW_2 = (0 * 32) + 27SW_3 = (0 * 32) + 22SW_4 = (2 * 32) + 1

# DIRECTIN LEDS: to represent lift direction (up or down)

Page 3: ASSIGNMENT NO:A-1 - WordPress.com€¦ASSIGNMENT NO:A-1 TITLE:-Develop an application using Beaglebone Black/ ARM Cortex A5 development board to simulate the operations of LIFT. CLASS:-

LIFT_DIR_1 = LED_9LIFT_DIR_2 = LED_10LIFT_DIR_3 = LED_11LIFT_DIR_4 = LED_12LIFT_DIR_5 = LED_13LIFT_DIR_6 = LED_14LIFT_DIR_7 = LED_15

# POSITION LEDS: LEDs to indicate the current position of Lift LIFT_POS_0 = LED_5LIFT_POS_1 = LED_6LIFT_POS_2 = LED_7LIFT_POS_3 = LED_8

# LIFT BUTTONS: corresponding to each floor of the Lift LIFT_BUTTON_0 = SW_1LIFT_BUTTON_1 = SW_2LIFT_BUTTON_2 = SW_3LIFT_BUTTON_3 = SW_4

# LIFT LEDS: indication for BUTTON Press on each floor LIFT_LED_0 = LED_1LIFT_LED_1 = LED_2LIFT_LED_2 = LED_3LIFT_LED_3 = LED_4

# An array of DIRECTIN LEDS dir_leds = [ LIFT_DIR_1,

LIFT_DIR_2,LIFT_DIR_3,LIFT_DIR_4,LIFT_DIR_5,LIFT_DIR_6,LIFT_DIR_7

]

# An array of POSITION LEDS pos_leds = [

LIFT_POS_0,LIFT_POS_1,LIFT_POS_2,LIFT_POS_3

]

lift_leds = [LIFT_LED_0,LIFT_LED_1,LIFT_LED_2,LIFT_LED_3

Page 4: ASSIGNMENT NO:A-1 - WordPress.com€¦ASSIGNMENT NO:A-1 TITLE:-Develop an application using Beaglebone Black/ ARM Cortex A5 development board to simulate the operations of LIFT. CLASS:-

]

lift_buttons = [ LIFT_BUTTON_0, LIFT_BUTTON_1, LIFT_BUTTON_2, LIFT_BUTTON_3

]

NO_OF_FLOORS = 4NO_OF_DIR_LEDS = 7DEFAULT_LIFT_POS = 0

floor_set = [{"fd":-1, "button":LIFT_BUTTON_0, "led":LIFT_LED_0},{"fd":-1, "button":LIFT_BUTTON_1, "led":LIFT_LED_1},{"fd":-1, "button":LIFT_BUTTON_2, "led":LIFT_LED_2}, {"fd":-1, "button":LIFT_BUTTON_3, "led":LIFT_LED_3}

]

/**************************** elevator.py *******************************/import sysimport timeimport selectfrom liftpins import *from gpio import *

def liftLEDExit (gpio):gpioSetVal(gpio, val="0")gpioUnexport(gpio)return

def liftLEDInit (gpio):gpioExport(gpio)gpioSetDir(gpio, flag="out")

gpioSetVal(gpio, val="0") return def liftLEDOn (gpio):

gpioSetVal(gpio, val="1")return

def liftLEDOff (gpio):gpioSetVal(gpio, val="0")return

def liftButtonExit (gpio):

Page 5: ASSIGNMENT NO:A-1 - WordPress.com€¦ASSIGNMENT NO:A-1 TITLE:-Develop an application using Beaglebone Black/ ARM Cortex A5 development board to simulate the operations of LIFT. CLASS:-

gpioUnexport(gpio)return

def liftButtonInit (gpio):gpioExport(gpio)gpioSetDir(gpio, flag="in")

gpioSetEdge(gpio, flag="falling") return

def liftInitAll():for i in range(0, NO_OF_DIR_LEDS):

liftLEDInit(str(dir_leds[i]))

for i in range(0, NO_OF_FLOORS):liftLEDInit(str(pos_leds[i]))liftLEDInit(str(lift_leds[i]))liftButtonInit(str(lift_buttons[i]))

return

def liftExitAll():for i in range(0, NO_OF_DIR_LEDS):

liftLEDExit(str(dir_leds[i]))

for i in range(0, NO_OF_FLOORS):liftLEDExit(str(pos_leds[i]))liftLEDExit(str(lift_leds[i]))liftButtonExit(str(lift_buttons[i]))

print "\n=== Demonstration END ===\n"return

def liftDefaultPos():liftLEDOn(str(pos_leds[DEFAULT_LIFT_POS]))return

def liftDirUp():for i in range(0, NO_OF_DIR_LEDS):

liftLEDOn(str(dir_leds[i]))time.sleep(0.5)

for i in range(0, NO_OF_DIR_LEDS):liftLEDOff(str(dir_leds[i]))

return

def liftDirDown():

Page 6: ASSIGNMENT NO:A-1 - WordPress.com€¦ASSIGNMENT NO:A-1 TITLE:-Develop an application using Beaglebone Black/ ARM Cortex A5 development board to simulate the operations of LIFT. CLASS:-

for i in range(NO_OF_DIR_LEDS, 0, -1):liftLEDOn(str(dir_leds[i-1]))time.sleep(0.5)

for i in range(0, NO_OF_DIR_LEDS):liftLEDOff(str(dir_leds[i]))

return

def GetButtonVal(): try:

fo0 = open(SYSFS_GPIO_DIR + "/gpio" + str(LIFT_BUTTON_0) + "/value" ,"r") fo0.read(2)floor_set[0]["fd"] = fo0

fo1 = open(SYSFS_GPIO_DIR + "/gpio" + str(LIFT_BUTTON_1) + "/value" ,"r") fo1.read(2)floor_set[1]["fd"] = fo1

fo2 = open(SYSFS_GPIO_DIR + "/gpio" + str(LIFT_BUTTON_2) + "/value" ,"r") ffo2.read(2)floor_set[2]["fd"] = fo2

fo3 = open(SYSFS_GPIO_DIR + "/gpio" + str(LIFT_BUTTON_3) + "/value" ,"r") fo3.read(2)floor_set[3]["fd"] = fo3

print "\nWaiting for button press ..."r, w, ex = select.select([], [], [fo0, fo1, fo2, fo3])

for i in range(len(floor_set)):if floor_set[i]["fd"] in ex:

print "LIFT button is pressed for floor # %d" % iliftLEDOn(str(floor_set[i] ["led"]))time.sleep(1)but = ifo = floor_set[i]["fd"]fo.seek(0, 0);str1 = fo.read(1)#print "Button Pressed ! Value = ", str1

fo0.close()fo1.close()fo2.close()fo3.close()return but

except IOError: return

try:print "\nLift Operation Simulation using Python\n"

Page 7: ASSIGNMENT NO:A-1 - WordPress.com€¦ASSIGNMENT NO:A-1 TITLE:-Develop an application using Beaglebone Black/ ARM Cortex A5 development board to simulate the operations of LIFT. CLASS:-

print "-----------------------------------------------\n" liftInitAll()liftDefaultPos()

cur_flr = DEFAULT_LIFT_POS

while True:new_flr = GetButtonVal()if new_flr > cur_flr:

tmp = cur_flrprint "LIFT going UP to floor #%d" %new_flrwhile (tmp != new_flr):

liftDirUp()time.sleep(0.01)liftLEDOff(str(pos_leds[tmp]))tmp += 1liftLEDOn(str(pos_leds[tmp]))time.sleep(0.5)

elif new_flr < cur_flr:tmp = cur_flrprint "LIFT going DOWN to floor #%d" %new_flrwhile (tmp != new_flr):

liftDirDown()time.sleep(0.01)liftLEDOff(str(pos_leds[tmp]))tmp -= 1liftLEDOn(str(pos_leds[tmp]))time.sleep(0.5)

cur_flr = new_flrliftLEDOff(str(lift_leds[new_flr]))time.sleep(1)

liftExitAll()exit()

except KeyboardInterrupt:liftExitAll()print "Program Exit due to CTRL-C"exit()

sys.exit(0)

/************************************Output***********************************sl165@sl243-HP-Compaq-4000-Pro-SFF-PC:~$ sudo ssh [email protected][sudo] password for sl165:root@beaglebone:~# cd ..root@beaglebone:/# cd homeroot@beaglebone:/home# cd debian/root@beaglebone:/home/debian# cd Desktop/root@beaglebone:/home/debian/Desktop# ls

root@beaglebone:/home/debian/Desktop# python gpio.pyroot@beaglebone:/home/debian/Desktop# python liftpins.py

Page 8: ASSIGNMENT NO:A-1 - WordPress.com€¦ASSIGNMENT NO:A-1 TITLE:-Develop an application using Beaglebone Black/ ARM Cortex A5 development board to simulate the operations of LIFT. CLASS:-

root@beaglebone:/home/debian/Desktop# python e2.py

Lift Operation Simulation using Python

-----------------------------------------------

Waiting for button press ...LIFT button is pressed for floor # 2LIFT going UP to floor #2

Waiting for button press ...LIFT button is pressed for floor # 3LIFT going UP to floor #3

Waiting for button press ...LIFT button is pressed for floor # 1LIFT going DOWN to floor #1

Waiting for button press ...LIFT button is pressed for floor # 2LIFT going UP to floor #2

Waiting for button press ...LIFT button is pressed for floor # 0LIFT going DOWN to floor #0

Waiting for button press ...LIFT button is pressed for floor # 3LIFT going UP to floor #3

Waiting for button press ...LIFT button is pressed for floor # 0LIFT going DOWN to floor #0

Waiting for button press ...LIFT button is pressed for floor # 2LIFT going UP to floor #2

Waiting for button press ...^Z[1]+ Stopped python e2.pyroot@beaglebone:/home/debian/Desktop# */

Page 9: ASSIGNMENT NO:A-1 - WordPress.com€¦ASSIGNMENT NO:A-1 TITLE:-Develop an application using Beaglebone Black/ ARM Cortex A5 development board to simulate the operations of LIFT. CLASS:-

ASSIGNMENT NO:A-2

TITLE:- Develop an application using Beeglebone Black/ ARM Cortex A5 development boardto simulate the working of signal lights.

CLASS:- TE Computer

NAME:-

ROLL NO:-

BATCH:- T -3

- gpio.pyimport sysimport os

SYSFS_GPIO_DIR = "/sys/class/gpio"

def gpioUnexport (gpio):try:

fo = open(SYSFS_GPIO_DIR + "/unexport","w") fo.write(gpio) fo.close() return except IOError: return

def gpioExport (gpio): try:

fo = open(SYSFS_GPIO_DIR + "/export","w") fo.write(gpio) fo.close() return except IOError: return

/******************************* ledpins.py *************************/LED_1= (0 * 32) + 3LED_2= (0 * 32) + 23LED_3= (0 * 32) + 2 LED_4= (0 * 32) + 26

LED_5= (1 * 32) + 17LED_6= (1 * 32) + 15LED_7= (0 * 32) + 15LED_8= (1 * 32) + 14

Page 10: ASSIGNMENT NO:A-1 - WordPress.com€¦ASSIGNMENT NO:A-1 TITLE:-Develop an application using Beaglebone Black/ ARM Cortex A5 development board to simulate the operations of LIFT. CLASS:-

LED_9= (0 * 32) + 14LED_10 = (0 * 32) + 27LED_11 = (0 * 32) + 22LED_12 = (2 * 32) + 1

NORTH_GREEN = LED_1NORTH_YELLOW = LED_5NORTH_RED = LED_9

EAST_GREEN = LED_2EAST_YELLOW = LED_6EAST_RED = LED_10

SOUTH_GREEN = LED_3SOUTH_YELLOW = LED_7SOUTH_RED = LED_11

WEST_GREEN = LED_4WEST_YELLOW = LED_8WEST_RED = LED_12

def gpioSetDir (gpio, flag):try: fo = open(SYSFS_GPIO_DIR + "/gpio" + gpio + "/direction" ,"w") fo.write(flag) fo.close() return

except IOError: return

def gpioSetVal (gpio, val):try:

fo = open(SYSFS_GPIO_DIR + "/gpio" + gpio + "/value" ,"w") fo.write(val)fo.close()return

except IOError: return

/****************************** traffic.py **************************/#!/usr/bin/python import sysimport timefrom gpio import * from ledpins import *

def lightExit (gpio):

Page 11: ASSIGNMENT NO:A-1 - WordPress.com€¦ASSIGNMENT NO:A-1 TITLE:-Develop an application using Beaglebone Black/ ARM Cortex A5 development board to simulate the operations of LIFT. CLASS:-

gpioSetVal(gpio, val="0")gpioUnexport(gpio)return

def lightInit (gpio):gpioExport(gpio)gpioSetDir(gpio, flag="out")

gpioSetVal(gpio, val="0") return def lightOn (gpio):

gpioSetVal(gpio, val="1")return

def lightOff (gpio):gpioSetVal(gpio, val="0")return

def trafficInitAll():lightInit(str(NORTH_GREEN))lightInit(str(NORTH_YELLOW))lightInit(str(NORTH_RED))lightInit(str(EAST_GREEN))lightInit(str(EAST_YELLOW))lightInit(str(EAST_RED))lightInit(str(SOUTH_GREEN))lightInit(str(SOUTH_YELLOW))lightInit(str(SOUTH_RED))lightInit(str(WEST_GREEN))lightInit(str(WEST_YELLOW))lightInit(str(WEST_RED))return

def trafficExitAll():lightExit(str(NORTH_GREEN))lightExit(str(NORTH_YELLOW))lightExit(str(NORTH_RED))lightExit(str(EAST_GREEN))lightExit(str(EAST_YELLOW))lightExit(str(EAST_RED))lightExit(str(SOUTH_GREEN))lightExit(str(SOUTH_YELLOW))lightExit(str(SOUTH_RED))lightExit(str(WEST_GREEN))lightExit(str(WEST_YELLOW))lightExit(str(WEST_RED))return

def northSouthOn():lightOff(str(EAST_YELLOW))lightOff(str(WEST_YELLOW))

Page 12: ASSIGNMENT NO:A-1 - WordPress.com€¦ASSIGNMENT NO:A-1 TITLE:-Develop an application using Beaglebone Black/ ARM Cortex A5 development board to simulate the operations of LIFT. CLASS:-

lightOff(str(NORTH_RED))lightOff(str(SOUTH_RED))

lightOn(str(EAST_RED))lightOn(str(WEST_RED))

lightOn(str(NORTH_GREEN))lightOn(str(SOUTH_GREEN))

time.sleep(10)

lightOff(str(NORTH_GREEN))lightOff(str(SOUTH_GREEN))

lightOn(str(NORTH_YELLOW))lightOn(str(SOUTH_YELLOW))time.sleep(1)return

def eastWestOn():lightOff(str(NORTH_YELLOW))lightOff(str(SOUTH_YELLOW))lightOff(str(EAST_RED))lightOff(str(WEST_RED))

lightOn(str(NORTH_RED))lightOn(str(SOUTH_RED))

lightOn(str(EAST_GREEN))lightOn(str(WEST_GREEN))

time.sleep(10)

lightOff(str(EAST_GREEN))lightOff(str(WEST_GREEN))

lightOn(str(EAST_YELLOW))lightOn(str(WEST_YELLOW))time.sleep(1)return

try:print "\nTraffic Signal Light Simulation using Python"print "-----------------------------------------------"trafficExitAll()trafficInitAll()for x in range(0, 10):

print "\nNORTH-SOUTH --> [GO]"print "EAST-WEST --> [STOP]\n"northSouthOn()

Page 13: ASSIGNMENT NO:A-1 - WordPress.com€¦ASSIGNMENT NO:A-1 TITLE:-Develop an application using Beaglebone Black/ ARM Cortex A5 development board to simulate the operations of LIFT. CLASS:-

time.sleep(1)print "\nEAST-WEST--> [GO]"print "NORTH-SOUTH --> [STOP]\n"eastWestOn()time.sleep(1)

trafficExitAll()print "done"exit()

except KeyboardInterrupt:trafficExitAll()print "Program Exit due to CTRL-C"exit()

sys.exit(0)

/*******************************Output****************************sl165@sl243-HP-Compaq-4000-Pro-SFF-PC:~$ sudo ssh [email protected][sudo] password for sl165: root@beaglebone:~# cd ..root@beaglebone:/# cd mediaroot@beaglebone:/media# lsBEAGLEBONEroot@beaglebone:/media# cd BEAGLEBONE/root@beaglebone:/media/BEAGLEBONE# lsroot@beaglebone:/media/BEAGLEBONE# python gpio.pyroot@beaglebone:/media/BEAGLEBONE# python ledpins.pyroot@beaglebone:/media/BEAGLEBONE# python traffic.py

Traffic Signal Light Simulation using Python-----------------------------------------------

NORTH-SOUTH --> [GO]EAST-WEST --> [STOP]

EAST-WEST --> [GO]NORTH-SOUTH --> [STOP]

NORTH-SOUTH --> [GO]EAST-WEST --> [STOP]

EAST-WEST --> [GO]NORTH-SOUTH --> [STOP]*/

Page 14: ASSIGNMENT NO:A-1 - WordPress.com€¦ASSIGNMENT NO:A-1 TITLE:-Develop an application using Beaglebone Black/ ARM Cortex A5 development board to simulate the operations of LIFT. CLASS:-

ASSIGNMENT NO:A-3

TITLE:- Implement an calculator (64 bit Binary Multiplication) application using concurrentlisp.

CLASS:- TE Computer

NAME:-

ROLL NO:-

BATCH:- T - 3

(defvar a)(defvar b)(defvar c)

(write-line "Enter the binary value of a and b (start with #b)")(setq a (read))(setq b (read))

(sb-thread:make-thread (lambda () (progn (sleep 0)(setq c (+ a b))(print "1.Addition in binary: ") (format t " ~b" c ) (print "1.Adition is:")(write c))))

(sb-thread:make-thread (lambda () (progn (sleep 0)(setq c (- a b))(print "2.Subtraction in binary: ") (format t " ~b" c ) (print "2.Subtraction is:")(write c))))

(sb-thread:make-thread (lambda () (progn (sleep 0)(setq c (* a b))(print "3.Multiplication in binary: ") (format t " ~b" c ) (print "3.Multiplication is:")(write c))))

(sb-thread:make-thread (lambda () (progn (sleep 0)(setq c (/ b a))(print "4.Division in binary: ") (format t " ~b" c ) (print "4.Division is:")(write c))))

Page 15: ASSIGNMENT NO:A-1 - WordPress.com€¦ASSIGNMENT NO:A-1 TITLE:-Develop an application using Beaglebone Black/ ARM Cortex A5 development board to simulate the operations of LIFT. CLASS:-

(sb-thread:make-thread (lambda () (progn (sleep 0)(setq c (* a a))(print "5.Square in binary: ") (format t " ~b" c ) (print "5.Square of a is:")(write c))))

(sb-thread:make-thread (lambda () (progn (sleep 0)(setq c (* a a a))(print "6.Cube in binary: ") (format t " ~b" c ) (print "6.Cube of a is:")(write c))))

(sb-thread:make-thread (lambda () (progn (sleep 0)(setq c (sin a))(print "7.Sine of a is:")(write c))))

(sb-thread:make-thread (lambda () (progn (sleep 0)(setq c (cos a))(print "8.Cosine of a is:")(write c))))

(sb-thread:make-thread (lambda () (progn (sleep 0)(setq c (tan a))(print "9.Tan of a is:")(write c))))

(sb-thread:make-thread (lambda () (progn (sleep 0)(setq c (min a b))(print "10.Min in binary: ") (format t " ~b" c ) (print "10.Min is:")(write c))))

(sb-thread:make-thread (lambda () (progn (sleep 0)(setq c (max a b))(print "11.Max in binary: ") (format t " ~b" c ) (print "11.Max is:")(write c))))

(exit)

#|****************************Output************************pl9@pl9:~$ cd Desktoppl9@pl9:~/Desktop$ sbcl* (load "e.lisp")

Enter the binary value of a and b (start with #b)

Page 16: ASSIGNMENT NO:A-1 - WordPress.com€¦ASSIGNMENT NO:A-1 TITLE:-Develop an application using Beaglebone Black/ ARM Cortex A5 development board to simulate the operations of LIFT. CLASS:-

#b0100#b1000

"1.Addition in binary: " 1100"1.Adition is:" 12

"2.Subtraction in binary: " -100"2.Subtraction is:" -4

"3.Multiplication in binary: " 100000"3.Multiplication is:" 32

"4.Division in binary: " 10"4.Division is:" 2

"5.Square in binary: " 10000"5.Square of a is:" 16

"6.Cube in binary: " 1000000"6.Cube of a is:" 64

"7.Sine of a is:" -0.7568025

"8.Cosine of a is:" -0.6536436

"9.Tan of a is:" 1.1578213

"10.Min in binary: " 100"10.Min is:" 4

"11.Max in binary: " 1000"11.Max is:" 8pl9@pl9:~/Desktop$ |#

Page 17: ASSIGNMENT NO:A-1 - WordPress.com€¦ASSIGNMENT NO:A-1 TITLE:-Develop an application using Beaglebone Black/ ARM Cortex A5 development board to simulate the operations of LIFT. CLASS:-

ASSIGNMENT NO:A-6

TITLE:- Write an application to parse input text file concurrently and compare the result ofconcurrent parsing with serial parsing ( Use concurrent YACC parser)

CLASS:- TE Computer

NAME:-

ROLL NO:-

BATCH:- T - 3

//ss.l%{#include<stdio.h>%}letter [a-zA-Z]digit [0-9]under [_]%%void |int |char |include |main |printf |if |for |break |while {printf("\n %s is a keyword\n",yytext);}"+" |"-" |"*" |"/" {printf("\n %s is a binary operator",yytext);}{digit}+ {printf("\n%s is an integer constant\n",yytext);}{letter}({letter}|{digit}|{under})* {printf("\n %s is an identifier\n",yytext);}\"[a-zA-Z0-9\(\)\% ]*\" {printf("\n%s is a literal\n",yytext);}

%%int main(int argc, char **argv){FILE *fp;fp=fopen(argv[1],"r");//argv[1] is string array has data taken //from file yyin=fp;yylex();return 1;}

Page 18: ASSIGNMENT NO:A-1 - WordPress.com€¦ASSIGNMENT NO:A-1 TITLE:-Develop an application using Beaglebone Black/ ARM Cortex A5 development board to simulate the operations of LIFT. CLASS:-

/******************************* oc.c **************************/yywrap(){}

#include<stdio.h>#include<omp.h>#include<string.h>int main(int argc, char **argv){#pragma omp parallel{int tid=omp_get_thread_num();char inst[100];strcpy(inst,"./yacc_file ");strcat(inst,argv[tid+1]);system(inst);}}

/********************************** p1.c ****************************/main(){printf("pune vidyarthi ghiha ");

if(0=2)

}

/*********************************** p2.c ***************************/main(){int x,y,i;for(i;i<10;i++)}

/******************************Output****************************anandraj@Anand:~$ cd Desktopanandraj@Anand:~/Desktop/as6$ lex ss.lanandraj@Anand:~/Desktop/as6$ cc lex.yy.c -o yacc_fileanandraj@Anand:~/Desktop/as6$ gcc -fopenmp oc.canandraj@Anand:~/Desktop/as6$ ./a.out p1.c p2.c

main is a keyword(){

printf is a keyword("pune vidyarthi ghiha " is a literal);

Page 19: ASSIGNMENT NO:A-1 - WordPress.com€¦ASSIGNMENT NO:A-1 TITLE:-Develop an application using Beaglebone Black/ ARM Cortex A5 development board to simulate the operations of LIFT. CLASS:-

if is a keyword(0=2)

}

main is a keyword(){

int is a keyword x,y,i;

for is a keyword(i;i<10;i + is a binary operator + is a binary operator)}*/

Page 20: ASSIGNMENT NO:A-1 - WordPress.com€¦ASSIGNMENT NO:A-1 TITLE:-Develop an application using Beaglebone Black/ ARM Cortex A5 development board to simulate the operations of LIFT. CLASS:-

ASSIGNMENT NO:B-1

TITLE:- Write an application to and demonstrate the change in BeagleBoard/ ARM CortexA5 /Microprocessor /CPU frequency or square wave of programmable frequency.

CLASS:- TE Computer

NAME:-

ROLL NO:-

BATCH:- T - 3

#include<stdlib.h> //System() Function#include<signal.h> //signal Handler for CNTR-C#include<iostream> //cout , cin#include<string> //string

using namespace std;void delay(int num);

int main(){int option;string frq;string command;

while(1){

cout << "\n1. Press 1 to set the Beagle Board CPU Frequency\n"; cout << "2. Press 2 to get the current CPU Frequency\n"; cout << "3. Press 3 or CNTR-Z to exit\n"; cin >> option;

switch(option) { case 1: cout << "Please Enter the frequency in range 100-1000 (MHz)\n"; cin >> frq; command="cpufreq-set -f "+frq+"MHz";

if(system(command.c_str())) cout << "\nCannot Set the Frequency. Make sure logged in as ROOT\n";

case 2:

Page 21: ASSIGNMENT NO:A-1 - WordPress.com€¦ASSIGNMENT NO:A-1 TITLE:-Develop an application using Beaglebone Black/ ARM Cortex A5 development board to simulate the operations of LIFT. CLASS:-

cout << "Press any key to go Back in main menu \n";

system("cpufreq-info | tail -2"); delay(50000);

break; case 3: exit(0);

} } } void delay(int num) { int i,j; for(i=0; i < num;i++) for(j=0; j < 1275;j++); }

/***************************Output******************************

cc5@cc5-HP-dx2480-MT-VP562PA:~$ ssh [email protected] GNU/Linux 7

BeagleBoard.org Debian Image 2015-03-01

Support/FAQ: http://elinux.org/Beagleboard:BeagleBoneBlack_Debian

default username:password is [debian:temppwd]

Last login: Sun Mar 1 20:56:04 2015 from cc5-hp-dx2480-mt-vp562pa.localroot@beaglebone:~# cd ..root@beaglebone:/# lsbin cpufreq.py etc lib media opt root sbin srv tmp varboot dev home lost+found mnt proc run selinux sys usrroot@beaglebone:/# cd homeroot@beaglebone:/home# lsdebianroot@beaglebone:/home# cd debianroot@beaglebone:/home/debian# lsDesktop a.outadd.cpp apps.py bin hello.pyroot@beaglebone:/home/debian# cd Desktoproot@beaglebone:/home/debian/Desktop# vi cpu.cpproot@beaglebone:/home/debian/Desktop# g++ cpu.cpproot@beaglebone:/home/debian/Desktop# ./a.out

1. Press 1 to set the Beagle Board CPU Frequency2. Press 2 to get the current CPU Frequency3. Press 3 or CNTR-Z to exit2Press any key to go Back in main menu

Page 22: ASSIGNMENT NO:A-1 - WordPress.com€¦ASSIGNMENT NO:A-1 TITLE:-Develop an application using Beaglebone Black/ ARM Cortex A5 development board to simulate the operations of LIFT. CLASS:-

current CPU frequency is 300 MHz (asserted by call to hardware). cpufreq stats: 300 MHz:98.55%, 600 MHz:0.08%, 800 MHz:0.08%, 1000 MHz:1.29% (15)

1. Press 1 to set the Beagle Board CPU Frequency2. Press 2 to get the current CPU Frequency3. Press 3 or CNTR-Z to exit1Please Enter the frequency in range 100-1000 (MHz)600Press any key to go Back in main menu current CPU frequency is 600 MHz (asserted by call to hardware). cpufreq stats: 300 MHz:98.52%, 600 MHz:0.08%, 800 MHz:0.09%, 1000 MHz:1.31% (19)

1. Press 1 to set the Beagle Board CPU Frequency2. Press 2 to get the current CPU Frequency3. Press 3 or CNTR-Z to exit2Press any key to go Back in main menu current CPU frequency is 600 MHz (asserted by call to hardware). cpufreq stats: 300 MHz:98.30%, 600 MHz:0.31%, 800 MHz:0.09%, 1000 MHz:1.30% (19)

1. Press 1 to set the Beagle Board CPU Frequency2. Press 2 to get the current CPU Frequency3. Press 3 or CNTR-Z to exit*/

Page 23: ASSIGNMENT NO:A-1 - WordPress.com€¦ASSIGNMENT NO:A-1 TITLE:-Develop an application using Beaglebone Black/ ARM Cortex A5 development board to simulate the operations of LIFT. CLASS:-

ASSIGNMENT NO:B-2

TITLE:- Implement a Parallel Quick Sort algorithm using NVIDIA GPU or equivalent ARMboard.

CLASS:- TE Computer

NAME:-

ROLL NO:-

BATCH:- T - 3

#include <stdio.h> #include <stdlib.h>

int partition(int * a, int p, int r) { int lt[r-p]; int gt[r-p]; int i; int j; int key = a[r]; int lt_n = 0; int gt_n = 0;

#pragma omp parallel for for(i = p; i < r; i++){ if(a[i] < a[r]){ lt[lt_n++] = a[i]; }else{ gt[gt_n++] = a[i]; } }

for(i = 0; i < lt_n; i++){ a[p + i] = lt[i]; }

a[p + lt_n] = key;

for(j = 0; j < gt_n; j++){ a[p + lt_n + j + 1] = gt[j]; }

return p + lt_n; }

void quicksort(int * a, int p, int r)

Page 24: ASSIGNMENT NO:A-1 - WordPress.com€¦ASSIGNMENT NO:A-1 TITLE:-Develop an application using Beaglebone Black/ ARM Cortex A5 development board to simulate the operations of LIFT. CLASS:-

{ int div;

if(p < r){ div = partition(a, p, r); #pragma omp parallel sections { #pragma omp section quicksort(a, p, div - 1); #pragma omp section quicksort(a, div + 1, r);

} } }

int main(void) { int a[10] = {6, 1, 10, 9, 2, 0, 12, 7, 8, 6}; int i;

quicksort(a, 0, 9);

for(i = 0;i < 10; i++){ printf("%d\t", a[i]); } printf("\n"); return 0; }

/****************************Output ******************************anandraj@Anand:~$ cd Desktop anandraj@Anand:~/Desktop$ gcc -fopenmp quick.c anandraj@Anand:~/Desktop$ ./a.out 1 2 6 6 6 7 8 9 10 12 anandraj@Anand:~/Desktop$ */

Page 25: ASSIGNMENT NO:A-1 - WordPress.com€¦ASSIGNMENT NO:A-1 TITLE:-Develop an application using Beaglebone Black/ ARM Cortex A5 development board to simulate the operations of LIFT. CLASS:-

ASSIGNMENT NO:B-4

TITLE:- Implement a Parallel ODD-Even Sort algorithm using GPU or ARM equivalent.

CLASS:- TE Computer

NAME:-

ROLL NO:-

BATCH:- T - 3

#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <omp.h>

#define NO_OF_THREADS 2 #define ARR_SIZE 20

int num_arr[ARR_SIZE];

void print_sorted_arr(int *a) {

int i; printf("\nSorted Array is: ["); for(i=0; i<ARR_SIZE; i++) {

if(i+1 == ARR_SIZE) break;

printf("%d, ", a[i]); } printf("%d]\n", a[i]);

}

void get_random_arr() {

int i; printf("\nEnter the 20 number :"); for(i=0; i<ARR_SIZE; i++) {

scanf("%d",&num_arr[i]); } printf("%d]\n", num_arr[i]);

}

Page 26: ASSIGNMENT NO:A-1 - WordPress.com€¦ASSIGNMENT NO:A-1 TITLE:-Develop an application using Beaglebone Black/ ARM Cortex A5 development board to simulate the operations of LIFT. CLASS:-

void odd_even_sort_openmp(int* a, int n) { int phase, i, temp; for(phase=0;phase<n;++phase) { if(phase%2==0) //even phase { #pragma omp parallel for num_threads(NO_OF_THREADS) default(none) shared(a,n) private(i,temp) for(i=1;i<n;i=i+2)

if(a[i-1] > a[i]) { temp = a[i]; a[i] = a[i-1]; a[i-1] = temp; }

} else //odd phase { #pragma omp parallel for num_threads(NO_OF_THREADS) default(none) shared(a,n) private(i,temp) for(i=1;i<n-1;i+=2)

if(a[i] > a[i+1]) {

temp = a[i]; a[i] = a[i+1]; a[i+1] = temp; }

} } }

int main () {

int i; int *arr1; double start_time_omp, end_time_omp, elapsed_time_omp;

printf("\nParallel Odd-Even Sort using OpenMP:"); printf("\n------------------------------------------\n");

/* Prepare data to sort */ printf("\nData Array:"); printf("\n-------------"); get_random_arr();

/* Allocate memory for new array */ arr1 = (int*) malloc(sizeof(int)*ARR_SIZE);

Page 27: ASSIGNMENT NO:A-1 - WordPress.com€¦ASSIGNMENT NO:A-1 TITLE:-Develop an application using Beaglebone Black/ ARM Cortex A5 development board to simulate the operations of LIFT. CLASS:-

/* Copy data to sort in the new array */ memcpy(arr1, num_arr, sizeof(int)*ARR_SIZE);

/* Call function to sort the array. Also record the start and end time */ printf("\nOpenMP Method:"); printf("\n-----------------"); printf("\nSorting the data parallely with OpenMP ..."); start_time_omp = omp_get_wtime(); odd_even_sort_openmp(arr1, ARR_SIZE); end_time_omp = omp_get_wtime(); elapsed_time_omp = end_time_omp - start_time_omp; printf("[Done]\n");

/* Print the result which is a sorted array */ printf("\nResult of OpenMP:"); printf("\n-----------------"); print_sorted_arr(arr1);

/* Print the result (Sorted Array) */ printf("\nTime Info:"); printf("\n-----------------"); printf("\nTime taken to Run Algorithm\t: %lf (s)\n",elapsed_time_omp);

/* Free the memory allocated by maclloc() */ free(arr1);

return 0; }

/*****************************Output ****************************anandraj@Anand:~$ cd Desktop/ anandraj@Anand:~/Desktop$ gcc -fopenmp even.c anandraj@Anand:~/Desktop$ ./a.out

Parallel Odd-Even Sort using OpenMP: ------------------------------------------

Data Array: ------------- Enter the 20 number :2 6 8 5 4 3 7 10 11 16 11 19 9

Page 28: ASSIGNMENT NO:A-1 - WordPress.com€¦ASSIGNMENT NO:A-1 TITLE:-Develop an application using Beaglebone Black/ ARM Cortex A5 development board to simulate the operations of LIFT. CLASS:-

0 1 4 5 17 13 7 0]

OpenMP Method: ----------------- Sorting the data parallely with OpenMP ...[Done]

Result of OpenMP: ----------------- Sorted Array is: [0, 1, 2, 3, 4, 4, 5, 5, 6, 7, 7, 8, 9, 10, 11, 11, 13, 16, 17, 19]

Time Info: ----------------- Time taken to Run Algorithm: 0.000209 (s) anandraj@Anand:~/Desktop$ */

Page 29: ASSIGNMENT NO:A-1 - WordPress.com€¦ASSIGNMENT NO:A-1 TITLE:-Develop an application using Beaglebone Black/ ARM Cortex A5 development board to simulate the operations of LIFT. CLASS:-

ASSIGNMENT NO:B-5

TITLE:- Implement n-ary search algorithm using OPENMP

CLASS:- TE Computer

NAME:-

ROLL NO:-

BATCH:- T - 3

#include<stdlib.h> #include<stdio.h> #include<omp.h> #define SIZE 10 enum locate_t {

EQUAL, LEFT, RIGHT

}; #define N 250

nary_search(int *a, long int size, int key) {

long int mid[N+1]; int i; enum locate_t locate[N+2]; locate[0] = RIGHT; locate[N + 1] = LEFT; long int lo = 0; long int hi = size - 1; long int pos = -1; double step, offset; #pragma omp parallel {

while(lo <= hi && pos == -1) {

#pragma omp single {

mid[0] = lo - 1; step = (hi - lo + 1) / (N + 1);

} #pragma omp for private(offset) firstprivate(step) for(i=1;i<=N;i++) {

offset = step * i + (i - 1); int lmid = mid[i] = lo + offset;

Page 30: ASSIGNMENT NO:A-1 - WordPress.com€¦ASSIGNMENT NO:A-1 TITLE:-Develop an application using Beaglebone Black/ ARM Cortex A5 development board to simulate the operations of LIFT. CLASS:-

if(lmid <= hi) {

if(a[lmid] > key) {

locate[i] = LEFT; } else if(a[lmid] < key) {

locate[i] = RIGHT; } else {

locate[i] = EQUAL; pos = lmid;

} } else {

mid[i] = hi + 1; locate[i] = LEFT;

} } #pragma omp single {

for(i=1;i<=N;i++) {

if(locate[i] != locate[i-1]) {

lo = mid[i - 1] + 1; hi = mid[i] - 1;

} } if(locate[N] != locate[N+1]) {

lo = mid[N] + 1; }

} }

} return pos;

}

int main() {

int *array = (int *) malloc(sizeof(int)*SIZE); long int pos, i; double start_time;

int key = 0; printf("\nEnter the array element\n"); for(i=0;i<SIZE;i++) {

Page 31: ASSIGNMENT NO:A-1 - WordPress.com€¦ASSIGNMENT NO:A-1 TITLE:-Develop an application using Beaglebone Black/ ARM Cortex A5 development board to simulate the operations of LIFT. CLASS:-

scanf("%d",&array[i]); } printf("\nEnter the key element\n"); scanf("%d",&key);

printf("\nSearching..."); start_time = omp_get_wtime();

pos = nary_search(array, SIZE, key); if(pos!=-1) {

printf("\nKey: %d is found in the record at location: %ld", key, pos); } else {

printf("\n Key is not found"); }

printf("\nExecution time = %lf seconds\n", omp_get_wtime() - start_time); free(array); return 0;

}

/******************************Output **************************anandraj@Anand:~$ cd Desktop/ anandraj@Anand:~/Desktop$ gcc -fopenmp nary.c anandraj@Anand:~/Desktop$ ./a.out Enter the array element 10 5 9 3 0 7 12 15 6 8

Enter the key element 15

Searching... Key: 15 is found in the record at location: 7 Execution time = 0.001544 seconds anandraj@Anand:~/Desktop$ */

Page 32: ASSIGNMENT NO:A-1 - WordPress.com€¦ASSIGNMENT NO:A-1 TITLE:-Develop an application using Beaglebone Black/ ARM Cortex A5 development board to simulate the operations of LIFT. CLASS:-

ASSIGNMENT NO:B-9

TITLE:- Implement a Multi-threading application for echo server using socket programming inJAVA.

CLASS:- TE Computer

NAME:-

ROLL NO:-

BATCH:- T - 3

//Server2.javaimport java.io.*;import java.net.*;

public class Server2 {public static void main(String args[]) {int port = 3128;Server2 server = new Server2( port );server.startServer();}

// declare a server socket and a client socket for the server;// declare the number of connections

ServerSocket echoServer = null;Socket clientSocket = null;int numConnections = 0;int port;

public Server2( int port ) {this.port = port;}

public void stopServer() {System.out.println( "Server cleaning up." );System.exit(0);}

public void startServer() {// Try to open a server socket on the given port// Note that we can't choose a port less than 1024 if we are not// privileged users (root)

try {echoServer = new ServerSocket(port);}

Page 33: ASSIGNMENT NO:A-1 - WordPress.com€¦ASSIGNMENT NO:A-1 TITLE:-Develop an application using Beaglebone Black/ ARM Cortex A5 development board to simulate the operations of LIFT. CLASS:-

catch (IOException e) {System.out.println(e);}

System.out.println( "Server is started and is waiting for connections." );System.out.println( "With multi-threading, multiple connections are allowed." );System.out.println( "Any client can send -1 to stop the server." );

// Whenever a connection is received, start a new thread to process the connection// and wait for the next connection.

while ( true ) {try {clientSocket = echoServer.accept();numConnections ++;Server2Connection oneconnection = new Server2Connection(clientSocket, numConnections, this);new Thread(oneconnection).start();} catch (IOException e) {System.out.println(e);}}}}

class Server2Connection implements Runnable {BufferedReader is;PrintStream os;Socket clientSocket;int id;Server2 server;

public Server2Connection(Socket clientSocket, int id, Server2 server) {this.clientSocket = clientSocket;this.id = id;this.server = server;System.out.println( "Connection " + id + " established with: " + clientSocket );try {is = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));os = new PrintStream(clientSocket.getOutputStream());} catch (IOException e) {System.out.println(e);}}

public void run() {String line;try {boolean serverStop = false;

while (true) {line = is.readLine();

Page 34: ASSIGNMENT NO:A-1 - WordPress.com€¦ASSIGNMENT NO:A-1 TITLE:-Develop an application using Beaglebone Black/ ARM Cortex A5 development board to simulate the operations of LIFT. CLASS:-

System.out.println( "Received " + line + " from Connection " + id + "." );int n = Integer.parseInt(line);if ( n == -1 ) {serverStop = true;break;}if ( n == 0 ) break;os.println("" + n*n ); }

System.out.println( "Connection " + id + " closed." );is.close();os.close();clientSocket.close();

if ( serverStop ) server.stopServer();} catch (IOException e) {System.out.println(e);}}}

/********************************Output ***************************sl165@sl243-HP-Compaq-4000-Pro-SFF-PC:~$ cd Desktop/sl165@sl243-HP-Compaq-4000-Pro-SFF-PC:~/Desktop$ javac Server2.javasl165@sl243-HP-Compaq-4000-Pro-SFF-PC:~/Desktop$ java Server2Server is started and is waiting for connections.With multi-threading, multiple connections are allowed.Any client can send -1 to stop the server.Connection 1 established with: Socket[addr=/80.0.0.46,port=45729,localport=3128]Received 5 from Connection 1.Received 4 from Connection 1.Received -1 from Connection 1.Connection 1 closed.Server cleaning up.sl165@sl243-HP-Compaq-4000-Pro-SFF-PC:~/Desktop$*/

/******************************Client2.java************************ */import java.io.*;import java.net.*;

public class Client2 {public static void main(String[] args) {

String hostname = "80.0.0.46";int port = 3128;

// declaration section:// clientSocket: our client socket// os: output stream// is: input stream

Page 35: ASSIGNMENT NO:A-1 - WordPress.com€¦ASSIGNMENT NO:A-1 TITLE:-Develop an application using Beaglebone Black/ ARM Cortex A5 development board to simulate the operations of LIFT. CLASS:-

Socket clientSocket = null; DataOutputStream os = null;BufferedReader is = null;

// Initialization section:// Try to open a socket on the given port// Try to open input and output streams

try {clientSocket = new Socket(hostname, port);os = new DataOutputStream(clientSocket.getOutputStream());is = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));} catch (UnknownHostException e) {System.err.println("Don't know about host: " + hostname);} catch (IOException e) {System.err.println("Couldn't get I/O for the connection to: " + hostname);}

// If everything has been initialized then we want to write some data// to the socket we have opened a connection to on the given port

if (clientSocket == null || os == null || is == null) {System.err.println( "Something is wrong. One variable is null." );return;}

try {while ( true ) {System.out.print( "Enter an integer (0 to stop connection, -1 to stop server): " );BufferedReader br = new BufferedReader(new InputStreamReader(System.in));String keyboardInput = br.readLine();os.writeBytes( keyboardInput + "\n" );

int n = Integer.parseInt( keyboardInput );if ( n == 0 || n == -1 ) {break;}

String responseLine = is.readLine();System.out.println("Server returns its square as: " + responseLine);}

// clean up:// close the output stream// close the input stream// close the socket

os.close();is.close();clientSocket.close(); } catch (UnknownHostException e) {System.err.println("Trying to connect to unknown host: " + e);

Page 36: ASSIGNMENT NO:A-1 - WordPress.com€¦ASSIGNMENT NO:A-1 TITLE:-Develop an application using Beaglebone Black/ ARM Cortex A5 development board to simulate the operations of LIFT. CLASS:-

} catch (IOException e) {System.err.println("IOException: " + e);}} }

/********************************Output******************************sl165@sl243-HP-Compaq-4000-Pro-SFF-PC:~$ cd Desktop/sl165@sl243-HP-Compaq-4000-Pro-SFF-PC:~/Desktop$ javac Client2.javasl165@sl243-HP-Compaq-4000-Pro-SFF-PC:~/Desktop$ java Client2Enter an integer (0 to stop connection, -1 to stop server): 5Server returns its square as: 25Enter an integer (0 to stop connection, -1 to stop server): 4Server returns its square as: 16Enter an integer (0 to stop connection, -1 to stop server): -1sl165@sl243-HP-Compaq-4000-Pro-SFF-PC:~/Desktop$ */

Page 37: ASSIGNMENT NO:A-1 - WordPress.com€¦ASSIGNMENT NO:A-1 TITLE:-Develop an application using Beaglebone Black/ ARM Cortex A5 development board to simulate the operations of LIFT. CLASS:-

ASSIGNMENT NO:B-10

TITLE:- Implement Reader-Writer problem using OPENMP

CLASS:- TE Computer

NAME:-

ROLL NO:-

BATCH:- T - 3

#include<stdio.h> #include<omp.h>

main() { int semaphore=0; #pragma omp parallel num_threads(2) { int i; int tid=omp_get_thread_num(); do { printf("\n thread %d\n%d.read\n%d.write\n5.exit",tid,tid*2,tid*2+1);

scanf("%d",&i);

switch(i) { case 0: if(semaphore==1) { while(semaphore==1) { printf("\n shared data is in use"); sleep(1); } }

printf("\n reading in thread 0"); sleep(5); printf("\n reading done in thread 0"); break; case 1: if(semaphore==1)

{ while(semaphore==1)

Page 38: ASSIGNMENT NO:A-1 - WordPress.com€¦ASSIGNMENT NO:A-1 TITLE:-Develop an application using Beaglebone Black/ ARM Cortex A5 development board to simulate the operations of LIFT. CLASS:-

sleep(1); }

semaphore=1; printf("\n writing in thread 0"); sleep(5); printf("\n writing done in thread 0"); semaphore=0; break; case 2: if(semaphore==1) { while(semaphore==1) { printf("\n shared data is in use"); sleep(1); } }

printf("\n reading in thread 1"); sleep(5); printf("reading done in thread 1"); break; case 3: if(semaphore==1) { while(semaphore==1) { printf("\n shared data is in use"); sleep(1); } } semaphore=1;

printf("\n writing in thread 1"); sleep(5); printf("writing done in thread 1"); semaphore=0;

break; } } while(i<5); } }

/*******************************Output *************************anandraj@Anand:~$ cd Desktop/ anandraj@Anand:~/Desktop$ gcc -fopenmp rwr.c anandraj@Anand:~/Desktop$ ./a.out thread 0 0.read 1.write

Page 39: ASSIGNMENT NO:A-1 - WordPress.com€¦ASSIGNMENT NO:A-1 TITLE:-Develop an application using Beaglebone Black/ ARM Cortex A5 development board to simulate the operations of LIFT. CLASS:-

5.exit thread 1 2.read 3.write 0 5.exit reading in thread 0 reading done in thread 0 thread 0 0.read 1.write 1 5.exit writing in thread 0 writing done in thread 0 thread 1 2.read 3.write 2 5.exit reading in thread 1reading done in thread 1 thread 0 0.read 1.write 3 5.exit writing in thread 1writing done in thread 1 thread 1 2.read 3.write 5 5.exit */