Manual

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 41

1.

(a) GENERATION OF AM, DSBSC & SSB-SC WAVES AIM: To generate amplitude modulated, double side band and single side band suppressed carrier using MAT LAB Program. EQUIPMENT REQUIRED: 1. Personal Computer With MATLAB 2007 r 1. AMPLITUDE MODULATION FORMULA Vm(t) = Vm Sin

mt mt)

Vc(t) = Vc Sin ct Vam = Vc + Vm = Vc(1+Vm/Vc sin

Vam = Vc(1+Ma Sin mt) Instantaneous amplitude of modulated signal is Vam(t) = Vc Sin

ct + maVc/2 cos(c- m)t - maVc/2 * cos(c+


Vm- amplitude of modulating signal Vc amplitude of carrier signal Vam amplitude of carrier signal after modulation Ma modulation index Wm angular frequency of modulating signal Wc angular frequency of carrier signal Vam (t) amplitude modulated signal

m)t
Where,

ALGORITHM:
1

Read the amplitude and frequency of message and carrier signal Generate message and carrier signals for the assigned time intervals Plot the message and carrier signal Calculate the lower side band and upper side band frequency of the spectrum and generate the amplitude modulated signal End the execution of the program

2. DOUBLE SIDE BAND SUPPRESSED CARRIER: FORMULA: Let the modulating signal be , Vm(t) = vm Sin wmt Let the carrier signal be, Vc (t) = vc sin wct DSb-sc signal = Vm (t)* Vc(t) ALGORITHM: Start the execution of the program Read the amplitude and frequency of message and carrier signal Generate message and carrier signals for the assigned time intervals Calculate the lower side band and upper side band frequency of the spectrum and generate the Double side band suppressed carrier signal Plot the DSB-SC signal waveform End the execution of the program

3. SINGLE SIDE BAND SUPPRESSED CARRIER: ALGORITHM: Read the sampling frequency fs and time interval t Generate sin wave (message signal) for a fixed frequency Plot the message signal in time domain and frequency domain Modulate the message signal for the given carrier frequency and plot it in the frequency domain Demodulate the modulated signal to obtain the message signal and plot it

1. PROGRAM CODING FOR AMPLITUDE MODULATION: clc; clear all; close all; am=2; ac=4; ka=am/ac; fm=100; t=0:0.01:50; wm=(2*(3.14)*fm*t); fc=1000; wc=2*3.14*fc*t; m=am*(sin(wm)); subplot(3,1,1); plot(t,m); c=ac*(sin(wc)); subplot(3,1,2); plot(t,c); lsb=(ka*ac/2)*(cos(wc-wm)); msb=(ka*ac/2)*(cos(wc+wm)); x=c+lsb-msb; subplot(3,1,3); plot(t,x); OUTPUT OF AMPLITUDE MODULATION:

2. PROGRAM FOR DOUBLE SIDED BAND SUPRESSED CARRIER:


fm=100; fc=3000; t=0:0.01:50; am=0.9; ac=1; mi=am/ac; mt=am*sin(2*3.14*fm*t); subplot(3,1,1); plot (t,mt); title ('Double Sided band Supressed Carrier'); ct=ac*sin(2*3.14*fc*t); subplot(3,1,2); plot (t,ct); lsb=cos((2*3.14*fc*t)-(2*3.14*fm*t)); msb=cos((2*3.14*fc*t)+(2*3.14*fm*t)); st=((am*ac)/2)*(lsb-msb); subplot(3,1,3); plot (t,st);

OUTPUT OF DOUBLE SIDED BAND SUPRESSED CARRIER:

3. PROGRAM CODING FOR SINGLE SIDEDDED SUPRESSED CARRIER: fs=200; t=(1:100)/fs; y=sin(2*3.14*10*t); figure(1); plot(t,y); xlabel('time ---->'); ylabel('amplitude ---->'); title('modulating signal'); s=fft(y,512); w=(0:255)/256*(fs/2); abs_s=abs(s(1:256')); figure(2); plot(w,abs_s); xlabel('frequncy ---->'); ylabel('magnitude of Fourier transform ---->'); title('frequency content of sine wave of 10hz'); mod_out=amod(y,50,fs,'amssb'); figure(3); plot(t,mod_out); xlabel('time ---->'); ylabel('amplitude ---->'); title('SSB-SC'); fft1_out=fft(mod_out,512); abs_out=abs(fft1_out(1:256')); figure(4); plot(w,abs_out); xlabel('frequncy ---->'); ylabel('magnitude of Fourier demod_out=demod(mod_out,50,fs,'amssb'); figure(5); plot(t,demod_out); xlabel('time ---->'); ylabel('amplitude ---->'); title('demodulated waveform');

transform

---->');

OUTPUT OF SINGLE SIDED BAND SUPRESSED CARRIER:

RESULT: Thus the amplitude modulated , double side band and single side band suppressed carrier signal are generated using MAT LAB Program and the outputs are verified.
6

1.(b) DIGITAL MODULATION TECHNIQUES\ AIM: To generate amplitude shift keying, frequency shift keying and phase shit keying using MATLAB program. EQUIPMENT REQUIRED: 1. Personal Computer With MATLAB 2007 r ALGORITHM: 1. Amplitude shift keying: Start the execution of the program Read the two different amplitude values for high and low inputs Generate the output waveform for high and low inputs and plot them in time domain Stop the execution of the program 2. Frequency shift keying: Start the execution of the program Read the two different Frequencies for high and low inputs Generate the output waveform for high and low inputs and plot them in time domain Stop the execution of the program 3. Phase shift keying: Start the execution of the program Read the amplitude, Frequency and time interval Generate the output signal with inphase and outphase components according to high and low inputs Stop the execution of the program

1. PROGRAM CODING FOR AMPLITUDE SHIFT KEYING: E1=5; E2=3; f=3000; t1=1:1:50; t=100; res1=sqrt(2*E1/t)*(cos((2*3.14*f*t1)+90)); t2=51:1:100; res2=sqrt(2*E2/t)*(cos((2*3.14*f*t2)+90)); t=1:1:100; result(1:50)=res1(1:50); result(51:100)=res2(1:50); figure(1); plot(t,result); title('AMPLITUDE SHIFT KEYING'); xlabel('time ---->'); ylabel('amplitude ---->'); grid on; OUTPUT OF AMPLITUDE SHIFT KEYING

2. PROGRAM CODING FOR FREQUENCY SHIFT KEYING: E=15; t1=1:1:50; f1=100; T=50; res1=(sqrt(2*E/T))*(cos(2*3.14*f1*t1)); t2=51:1:100; f2=250; res2=(sqrt(2*E/T))*(cos(2*3.14*f2*t2)); t=1:1:100; res(1:50)=res1(1:50); res(51:100)=res2(1:50); plot(t,res); title('FREQUENCY SHIFT KEYING'); xlabel('time ---->'); ylabel('amplitude ---->'); OUTPUT OF FREQUENCY SHIFT KEYING

3. PROGRAM CODING FOR PHASE SHIFT KEYING: E=10; f1=1:1:50; f=3000; T=100; i=1; m=2; res1=(sqrt(2*E/T))*(cos((2*3.14*f*t1)+(2*180*i/m))); t2=51:1:100; i=2; res2=(sqrt(2*E/T))*(cos((2*3.14*f*t2)+(2*180*i/m))); t=1:1:100; result(1:50)=res1(1:50); result(51:100)=res2(1:50); figure(1); plot(t,result); title('PHASE SHIFT KEYING'); xlabel('time---->'); ylabel('amplitude---->'); OUTPUT OF PHASE SHIFT KEYING

RESULT: Thus the amplitude shift keying, frequency shift keying and phase shit keying are generated using MATLAB program and the outputs are verified.
10

2 AIM:

(a) PERIODOGRAM

To perform periodogram of the given data using FFT. EQUIPMENT REQUIRED: TMS 320c50 DSP trainer kit Rs 232 cable ALGORITHM: Assign location for input data count normal sequence order etc. Perform FFT of the input data using decimation in the algorithm Square the output of FFT output data Divide the squared FFT data with the number of sequence Display the periodogram output PROGRAM CODING FOR SPECTRUM ESTIMATION PERIODOGRAM METHOD: INPUT .set 8010H BIT_REV .set 8020H INPLACE .set 8030H TWIDLE .set 8090H PERIO .set 8040H N .set 0H STG .set 1H STGC .set 3H GRP .set 4H GRPC .set 5H BFLY .set 6H BFLYC .set 7H DNS .set 8H DNSC .set 9H .MMREGS .TEXT LDP #100H CALL FFT LAR AR0,#8030H LAR AR1,#PERIO LACC 0H SUB #1H SACL 75H LAR AR2,75H
11

START

H FFT

ZAP MAR *,AR0 LT * MPY *+ PAC BSAR 0CH SACL 65H LT * MPY *+,AR1 PAC BSAR 0CH ADD 65H BSAR 4 AND #0FFFH CALL 0B400H MAR *,AR1 SACH *+,AR2 BANZ START,*B H LDP #100H LACC N CALL 0B000H LAR AR0,#BIT_REV LAR AR5,#INPLACE CALL 0B100H SPLK #3H,STG SPLK #1H,BFLY SPLK #2H,DNS SPLK #2H,0FH ZAP LT 0FH MPY N SPL 2H LT 2H MPY STG

LAR

AR1,#TWIDLE MAR *,AR1 RPT 2H BLPD #TABLE,*+ LAR AR1,#TWIDLE LACC STG SUB #1H SACL STGC LACC 00
12

STGLOP

GRPLOP BFLYLOP

SACL GRP LAR AR7,STGC LACC GRP BSAR 1H SACL GRP SUB #1H SACL GRPC LAR AR6,GRPC LACC BFLY SUB #1H SACL BFLYC LACC DNS SUB #1H SACL DNSC LAR AR0,#INPLACE LAR AR2,BFLYC CALL 0B200H MAR *,AR0 RPT DNSC LACC *+ CALL 0B200H MAR *,AR0 RPT DNSC LACC *CALL 0B300H MAR *,AR0 RPT DNSC LACC *MAR *,AR2 BANZ BFLYLOP,*MAR *,AR0 RPT DNSC LACC *+ MAR *,AR6 BANZ GRPLOP,*LT BFLY MPY #2H SPL BFLY LT DNS MPY #2H SPL DNS MAR *,AR7 BANZ STGLOP,*RET

TABLE
13

.word 00100H .word 00000H .word 00100H .word 00000H .word 00100H .word 00000H .word 00100H .word 00000H .word 00100H .word 00000H .word 00100H .word 00000H .word 00100H .word 00000H .word 00100H .word 00000H .word 00100H .word 00000H .word 00100H .word 00000H .word 00100H .word 00000H .word 00000H .word 0FF00H .word 00100H .word 00000H .word 00100H .word 00000H .word 00100H .word 00000H .word 00000H .word 0FF00H .word 00100H .word 00000H .word 00100H .word 00000H .word 00100H .word 00000H .word 000B5H .word 0FF4BH .word 00100H .word 00000H .word 00000H .word 0FF00H .word 00100H .word 00000H
14

.word 0FF4BH .word 0FF4BH .END

INPUT / OUTPUT Location: N: 8000 = A Normal order : 8010 = 1,0,1,0 Bit reversal : 8020 = 1,1,0,0 FFT output : 8030 = {2,0,2,0} Periodogram output : 8040 = 4

RESULT: Thus the periodogram of the input data is performed and displayed.

15

2. AIM:

(b) MULTIRATE SIGNAL PROCESSING

To implement multirate signals of input frequency of 35 KHZ by a sampling rate of 4 factors APPARATUS REQUIRED: TMS 320C50 DSP TRAINER KIT CRO AFO RS232 CABLE ALGORITHM: Decimation: Get the samples of the input frequency signal at 35 kHz Get the sample rate values Load the auxiliary register ARO with factor 4 Perform the convolution of the samples of filter co efficient The output of the fir filter is sent out of the DAC once every 4 outputs Next data is loaded in DAC after convolution and send out after next 4 sample Interpolation: Get the samples of the input frequency signal at 35 KHZ at lower sampling rate Load the filter co efficient from program memory location Move the filter co efficient from program memory location to data memory location Find convolution of input data with the filter co efficient The output is increased by a factor 4 BLOCK DIAGRAM FOR DECIMATION

16

FLOW CHART DECIMATION:

PROGRAM FOR DECIMATION Program description: In this program the sampling rate at the input is 35 KHz. This input sampling rate is reduced by a factor of 4 at the output. For this, the auxiliary register AR0 is loaded with the value of 4 initially. Then the output of the FIR filter is sent out to the DAC only once out of 4 outputs. i.e. when ever the register AR0 becomes 0, the filter output is sent to the DAC and reloaded with 4. Where 4 is the decimation factor of this decimator. Approximation type: Window design - Rectangular Window Filter type: Low pass filter Filter Order: 52 Cutoff frequency in KHz = 4.000000
17

.mmregs .text B START CTABLE: .word 0FF82H .word 083H .word 0167H .word 01CFH .word 0188H .word 09DH .word 0FF5BH .word 0FE38H .word 0FDACH .word 0FE01H .word 0FF31H .word 0DEH .word 0271H .word 0342H .word 02DDH .word 0132H .word 0FEADH .word 0FC20H .word 0FA92H .word 0FAEEH .word 0FDB6H .word 02CCH .word 096AH .word 0104CH .word 015F9H .word 0192EH .word 0192EH .word 015F9H .word 0104CH .word 096AH .word 02CCH .word 0FDB6H .word 0FAEEH .word 0FA92H .word 0FC20H .word 0FEADH .word 0132H .word 02DDH .word 0342H .word 0271H .word 0DEH .word 0FF31H
18

.word 0FE01H .word 0FDACH .word 0FE38H .word 0FF5BH .word 09DH .word 0188H .word 01CFH .word 0167H .word 083H .word 0FF82H * * Move the Filter coefficients * from program memory to data memory * START: MAR *,AR0 LAR AR0,#0200H RPT #33H BLKP CTABLE,*+ SETC CNF * * Input data and perform convolution * LAR AR0,#4H ISR: LDP #0AH LACC #0 SACL 0 OUT 0,05 ;pulse to find sampling frequency IN 0,06H LAR AR7,#0 ;change value to modify sampling freq. MAR *,AR7 BACK: BANZ BACK,*IN 0,4 NOP NOP NOP NOP MAR *,AR1 LAR AR1,#0300H LACC 0 AND #0FFFH SUB #800H SACL * LAR AR1,#333H MPY #0
19

ZAC RPT #33H MACD 0FF00H,*APAC LAR AR1,#0300H SACH * ;give as sach *,1 incase of overflow LACC * ADD #800H SFR ;remove if o/p is less amplitude SACL * MAR *,AR0 BANZ NO_OUT,*MAR *,AR1 OUT *,4 LAR AR0,#4H NO_OUT: LACC #0FFH SACL 0 OUT 0,05 NOP B ISR .end

BLOCK DIAGRAM FOR INTERPOLATION

20

FLOW CHART INTERPOLATION:

PROGRAM FOR INTERPOLATION PROGRAM DESCRIPTION: In this program the sampling rate is increased at the input by including zeros. The samples are taken at the lower sampling rate and since this program uses the interpolation factor of 4, three zeros are included after each sample taken from the ADC. So the number of samples and thus the sampling rate is also increased. Approximation type: Window design - Rectangular Window Filter type: Low pass filter Filter Order: 52 Cutoff frequency in KHz = 4.000000

21

.mmregs .text B START CTABLE: .word 0FF82H .word 083H .word 0167H .word 01CFH .word 0188H .word 09DH .word 0FF5BH .word 0FE38H .word 0FDACH .word 0FE01H .word 0FF31H .word 0DEH .word 0271H .word 0342H .word 02DDH .word 0132H .word 0FEADH .word 0FC20H .word 0FA92H .word 0FAEEH .word 0FDB6H .word 02CCH .word 096AH .word 0104CH .word 015F9H .word 0192EH .word 0192EH .word 015F9H .word 0104CH .word 096AH .word 02CCH .word 0FDB6H .word 0FAEEH .word 0FA92H .word 0FC20H .word 0FEADH .word 0132H .word 02DDH .word 0342H .word 0271H .word 0DEH .word 0FF31H
22

.word 0FE01H .word 0FDACH .word 0FE38H .word 0FF5BH .word 09DH .word 0188H .word 01CFH .word 0167H .word 083H .word 0FF82H * * Move the Filter coefficients * from program memory to data memory * START: MAR *,AR0 LAR AR0,#0200H RPT #33H BLKP CTABLE,*+ SETC CNF * * Input data and perform convolution * LAR AR0,#4H ISR: LDP #0AH LACC #0 SACL 0 OUT 0,05 ;pulse to find sampling frequency MAR *,AR0 BANZ INC0,*IN 0,06H ; LAR AR7,#0 ;change value to modify sampling freq. ; MAR *,AR7 ;BACK: BANZ BACK,*RPT #10H IN 0,4 LAR AR0,#4H B STORE INC0: SPLK #0H,0H RPT #15H NOP STORE: NOP NOP NOP NOP
23

MAR *,AR1 LAR AR1,#0300H LACC 0 AND #0FFFH SUB #800H SACL * LAR AR1,#333H MPY #0 ZAC RPT #33H MACD 0FF00H,*APAC LAR AR1,#0300H SACH * ;give as sach *,1 incase of overflow LACC * ADD #800H SFR ;remove if o/p is less amplitude SACL * OUT *,4 LACC #0FFFH SACL 0 OUT 0,05 NOP B ISR .end

RESULT: Thus the incoming sine wave with frequency 35 KHZ is decimated and interpolated by the factor 4

24

3. QUADRATURE MIRROR FILTERS

AIM: To simulate mirror filters by using MATLAB EQUIPMENT REQUIRED: MATLAB SIMULINK THEORY: The basic building block in applications of quadrature mirror filters (QMF) is the two channel QMF bank. This is the multirate digital filter structure that employs two decimators in the signal analysis section and two interpolators in the signal synthesis section and the low pass and highpass filters in the analysis section have impulse response h0(0) and h1(n) respectively. Similarly lowpass and high pass filters contained in the synthesis section have impulse response g0(n) and g1(n) respectively. The scale factor 2 corresponse to the interpolation factor used to normalize the overall frequency response of the QMF. With this choice of the filter characteristics, the component due to aliasing vanishes. Thus the aliasing resulting from decimation in the analysis section of QMF bank is perfectly cancelled by the image signal spectrum that arises due to interpolation. As a result, two channels QMF behaves as a linear time invariant system. PROGRAM N = 32; h0 = QMFDesign(N, 0.3, 1); % H(z) h1 = (-1).^(0:N-1)' .* h0; % H(-z) h00 = conv (h0, 2*h0); h11 = conv (h1, -2*h1); [f, Hp] = PlotFilter (h00, 'linear-radian'); close; PlotFilter (h11, 'linear-radian'); hold on; plot(f, Hp); hold off; xlabel ('Frequency'); ylabel ('Amplitude') text (0.1, 1.84, '|{\itH}_0(\omega)|^2'); text (0.8, 1.84, '|{\itH}_1(\omega)|^2');

25

BLOCK DIAGRAM QMF:

CHARACTERSTICS OF QMF:

RESULT : Thus by using the above theory the QMF has been simulated using MATLAB filters Packages

26

4. IMPLEMENTATION OF LINEAR AND CYCLIC CODES AIM: To implement a cyclic code generator and syndrome calculator and to verify the same for the given polynomial EQUIPMENT REQUIRED: IC 7495 IC 7486 Trainer kit Connectivity wires THEORY: Cyclic code: Cyclic code from a subclass of linear block codes an advantage of cyclic code over most other types of codes is that are easy to encode further more cyclic codes posses a well defined mathematical structure, which has led to the development of very efficient decoding schemes A binary code is said to be a cyclic code if it exhibits two fundamental properties 1.linear property: The sum of two code words is also a code word 2.cyclic property: Any cyclic shift of a code word is also a code word The gate is switched on hence the M message bits are shifted into channel as soon as the K message bits have entered the shift register, the resulting (n-k) bits in the register from the parity bits The gate is switched OFF there by berating the feedback connection the contents of the shift register are shifted out into the channel the polynomial for the message (1001) is given by g(t).the contents of the shift register are modified by the incoming message bits as in tables, after A shifts the parity bits are (011), according appending these parity bits to the message bits (1001),we get the code word(0111001) SYNDROME CALCULATOR BLOCK DIAGRAM:

27

SYNDROME CIRCUIT DIAGRAM:

SYNDROME CALCULATOR: Suppose if the code word (X0,X1,X2,X3Xn) is transmitted over the noisy channel and received word (Y0,Y1.Y2,Y3..Yn) the syndrome is calculated for error, if syndrome contain zero there is an error and if non zero is present it contains error. In case of cyclic code, the received word be represented by polynomial of degree (n-1) as Yn=a(n).g(n) + s(d)S.T. The remainder S(n) is syndrome polynomial of degree n-k-1 or less. Its co-efficients are making up the (n-k) by -1 syndrome S once we know S, we can determine the corresponding error pattern and there by mate the approximate correction. The cyclic code generation includes the following steps: 1. Multiplication of the message polynomial D(x) by X(n-k). 2. Division of X(n-k) by D(x) by the generator polynomial G(x) to obtain the remainder. 3. Addition of the remainder to X(n-k) D(x) to form the desired code polynomial. The given polynomial is, G(x) = 1+X+X3 Connect the circuit as shown in the figure for(7,4 cyclic code) So the code word is 011101. The above steps are implemented by means of flip-flops, multipliers, adders, wholly as linear feedback shift register and an external clock controls the operation of Flipflops. Let the transmitted code word be (0111001) and the received word (0111001) and the received word (0110001) and the received word be (0110001) that is the middle bits is in error as the received bits are fed into ht shift register initially set to zero its content are modified as in table at the end of seventh shift register as 110. Since the syndrome is non zero the received word is in error pattern corresponding to this syndrome is in the middle bits of the received word.

28

PROCEDURE: CODE GENERATOR: Give the message bits and note down the outputs across flipflop P0,P1,P2 As the last message bits is given as the input the required code to be transmitted is get across the flipflop P0,P1,P2 The transmitted message = message + code CODE GENERATOR BLOCK DIAGRAM:

PIN DIAGRAM OF IC 7495

29

SYNDROME CALCULATOR: Connect the circuit as shown in the figure Give the code word as the input to one of the X OR gate as shown in the figure starting from LSB.note down the output across flipflop S0,S1,S2 At the MSb if the required bit is correct we get zeros across S0,S1,S2 we get error according to the bit corrupted which has to be checked in the table CIRCUIT DAGRAM FOR CODE CONVERTER:

CODE GENERATOR Messge I/O 0 1 0 0 1 P0 0 0 1 0 1 P1 0 1 1 1 1 P2 0 0 1 1 1

SYNDROME CALCULATOR WITH NO ERROR Error Code word I/P LSB 1 0 0 1 1 1 MSB 0 S0 1 0 0 0 1 0 0 S1 0 1 0 1 0 0 0 S2 0 0 1 0 1 0 0
30

SYNDROME CALCULATOR WITH BIT ERROR Shift 1 2 3 4 5 6 7 1st bit 0111000 000 000 000 100 110 111 101 2nd bit 0111010 100 110 011 011 011 011 011 3rd bit 0111101 100 010 101 000 100 110 100 4th bit 0110001 100 010 001 110 111 001 110 5th bit 1001001 100 010 001 010 001 010 001 6th bit 0011001 100 010 001 010 101 100 010 7th bit 1111001 100 010 001 010 101 000 100

ERROR PATTERN MSB 0000001 0000010 0000100 0001000 0010000 0100000 1000000

RESULT AND INTERFERENCES: The cyclic code was designed for generating word where message a = 1001 and parity bitgenerating was 011. The circuit was executed and output table was verified. The syndrome calculator circuit was designed first for the class where no error occurred and if one bit error was occurring both the output flipflop for these cases were verified and observation were written.

31

5. AIM:

LEXICAL ANALYZER

To write a C Program to implement a Lexical analyzer. ALGORITHM: 1)Start the program. 2)Declare all the variables and file pointers. 3)Display the input program. 4)Separate the keyword in the program and display it. 5)Display the header files of the input program. 6)Separate the operators of the input program and display it. 7)Print the punctuation marks. 8)Print the constant that are present in input program. 9)Print the identifiers of the input program. 10)Stop the program . PROGRAM: #include<stdio.h> #include<conio.h> #include<ctype.h> #include<string.h> void main() { FILE *fp; inti,j; chararr[100],k; char kw[10][10]={"int","float","double","end","main","void","include","printf","scanf"}; charhf[2][10]={"stdio.h","conio.h"};char op[5]={'+','-','*','/','%'}; charpunc[6]={'(',')','{','}',','}; clrscr(); fp=fopen("input.c","r"); printf("InputProgram\n"); while(!feof(fp)) { arr[0]=fgetc(fp); printf("%c",arr[0]); } fclose(fp); printf("\nSymboltable\n"); fp=fopen("input.c","r"); printf("\nKeywords");
32

while(!feof(fp)){arr[0]=fgetc(fp); fscanf(fp,"%s",arr); for(i=0;i<10;i++) { if(strcmp(arr,kw[i])==0) { printf("\t%s",arr);}}}fclose(fp); fp=fopen("input.c","r"); printf("\nHeaderfiles"); while(!feof(fp)) { arr[0]=fgetc(fp); fscanf(fp,"%s",arr); for(i=0;i<2;i++) { if(strcmp(arr,hf[i])==0) { printf("\t%s",arr); }}} fclose(fp); fp=fopen("input.c","r"); printf("\nOperators"); while(!feof(fp)) { arr[0]=fgetc(fp); for(i=0;i<5;i++) { if(arr[0]==op[i]) { printf("\t%c",arr[0]); }}} fclose(fp); fp=fopen("input.c","r"); printf("\npunctuation"); while(!feof(fp)) { arr[0]=fgetc(fp); for(i=0;i<6;i++) { if(arr[0]==punc[i])
33

{ printf("\t%c",arr[0]); }}} fclose(fp); fp=fopen("input.c","r"); printf("\nConstants"); while(!feof(fp)) { arr[0]=fgetc(fp); if(isdigit(arr[0])) { printf(" %c ",arr[0]); }} fclose(fp); fp=fopen("input.c","r"); printf("\nidentifier "); while(!feof(fp)) { fscanf(fp,"%s",arr); for(i=0;i<2;i++) { if(strcmp(arr,kw[i])==0) { fscanf(fp,"%s",arr); j=0; while(j<strlen(arr) &&arr[j]!=';') { printf("%c",arr[j]); j++; }}}} fclose(fp); getch(); }

INPUT: #include<stdio.h> #include<conio.h> void main() { inta,b,c; a=10; b=5; c=a+b;


34

printf(The sum is %d, c); getch(); }

OUTPUT:

RESULT: Thus the above the program is executed and the required output is obtained.

35

6.

IMPLEMENTATION OF SEMAPHORES AND MONITORS INCLASSICAL PROBLEMS OF SYNCHRONIZATION

ALGORITHM 1) Start 2) Define the maximum buffer size. 3) Enter the number of producers and consumers. 4) The producer produces the job and put it in the buffer. 5) The consumer takes the job from the buffer. 6) If the buffer is full the producer goes to sleep. 7) If the buffer is empty then consumer goes to sleep. 8) Stop PROGRAM #include<stdio.h> #include<unistd.h> #include<pthread.h> #define buffSize 1000 struct { pthread_mutex_tmutex; int buff[buffSize]; intproducedCount; intconsumedCount; }shared={PTHREAD_MUTEX_INITIALIZER}; void *produce(void *arg); void *consume(void *arg); void main() { intprodThreads,consThreads,i; printf(\nEnter the no of Producers: ); scanf(%d,&prodThreads); printf(\nEnter the no of Consumers: ); scanf(%d,&consThreads); intproducerArr[prodThreads],consumerArr[consThreads]; pthread_t producer[prodThreads],consumer[consThreads]; pthread_setconcurrency(prodThreads+consThreads); for(i=0;i<prodThreads;i++) { producerArr[i]=0; pthread_create(&producer[i],NULL,produce,&producerArr[i]); } for(i=0;i<consThreads;i++) { consumerArr[i]=0;
36

pthread_create(&consumer[i],NULL,consume,&consumerArr[i]); } for(i=0;i<prodThreads;i++) { pthread_join(producer[i],NULL); printf(\nThe Producer (%d) produced: [%d] Items,i,producerArr[i]); sleep(1); } printf(\n); for(i=0;i<consThreads;i++) { pthread_join(consumer[i],NULL); printf(\nThe Consumer (%d) Consumed: [%d] Items,i,consumerArr[i]); sleep(1); } } void *produce(void *arg) { while(1) { pthread_mutex_lock(&shared.mutex); if(shared.producedCount<buffSize) { shared.producedCount++; *((int *)arg) += 1; pthread_mutex_unlock(&shared.mutex); } else { pthread_mutex_unlock(&shared.mutex); return NULL; } } } void *consume(void *arg) { while(1) { pthread_mutex_lock(&shared.mutex); if(shared.consumedCount<shared.producedCount) { shared.consumedCount++; *((int *)arg) += 1; pthread_mutex_unlock(&shared.mutex); } else
37

{ pthread_mutex_unlock(&shared.mutex); if (shared.consumedCount>=buffSize) return NULL; } } } OUTPUT Enter the no of Producers: 4 Enter the no of Consumers: 3 Result The Producer (0) produced: [252] Items The Producer (1) produced: [415] Items The Producer (2) produced: [136] Items The Producer (3) produced: [197] Items The Consumer (0) Consumed: [703] Items The Consumer (1) Consumed: [260] Items The Consumer (2) Consumed: [37] Items RESULT Thus the semaphore is implemented and the output is verified for the classical problem of synchronization successfully.

38

7. USAGE OF SYSTEM CALLS

ALGORITHM 1) Start 2) Define the flags. 3) Define the system calls open(), Close(), write(), lseek()&perror() 4) Open and close a file using open() and close() function. 5) Write the data in to the opened file using write(). 6) Move the cursor by using lseek().. 7) Show the error message using perror(). 8) Stop PROGRAM

GENERAL CLASS SPECIFIC CLASS SYSTEM CALL --------------------------------------------------------------------File Structure Creating a Channel creat() Related Calls open() close() Input/Outputread() write() Random Access lseek() Channel Duplication dup() Aliasing and Removing link() Files unlink() File Status stat() fstat() Access Control access() chmod() chown() umask() Device Control ioctl() --------------------------------------------------------------------Process Related Process Creation and exec() Calls Termination fork() wait() exit() Process Owner and Group getuid() geteuid() getgid()
39

getegid() Process Identity getppid() Process Control kill() alarm() Change Working Directory chdir() ---------------------------------------------------------------------Interprocess Pipelines pipe() Communication Messages msgget() msgsnd() msgrcv() msgctl() Semaphores semget() semop() Shared Memory shmget() shmat() shmdt() ---------------------------------------------------------------------signal() getpid()

open(), Close(), & write():

#include <fcntl.h> /* defines options flags */ #include <sys/types.h> /* defines types used by sys/stat.h */ #include <sys/stat.h> /* defines S_IREAD & S_IWRITE */ static char message[] = "Hello, world"; int main() { intfd; char buffer[80]; fd = open("file.dat",O_RDWR | O_CREAT | O_EXCL, S_IREAD | S_IWRITE); if (fd != -1) { printf("file.dat opened for read/write access\n"); write(fd, message, sizeof(message)); lseek(fd, 0L, 0); /* go back to the beginning of the file */ if (read(fd, buffer, sizeof(message)) == sizeof(message)) printf("\"%s\" was written to file.dat\n", buffer); else printf("*** error reading file.dat ***\n"); close (fd);
40

} else printf("*** file.dat already exists ***\n"); exit (0); }

Lseek()&perror():

#include <stdio.h> #include <fcntl.h> int main() { intfd; long position; fd = open("file.dat", O_RDONLY); if ( fd != -1) { position = lseek(fd, 0L, 2); /* seek 0 bytes from end-of-file */ if (position != -1) printf("The length of file.dat is %ld bytes.\n", position); else perror("lseek error"); } else printf("can't open file.dat\n"); close(fd); }

RESULT Thus the system calls are implemented and the outputs are verified.

41

You might also like