Showing posts from April, 2014

C program demonstrating use of pipe system call in linux (sending message from child process to parent process)

#include <sys/wait.h>#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <string.h>intmain(int argc, char *argv[]){int pipefd[2];pid_t cpid;char buf[30];if (pipe(pipefd) == -1) {printf("pipe error");exit(EXIT_FAILURE);}cpid = fork();if (cpid == -1) {printf("fork error");exit(EXIT_FAILURE);}if (cpid == 0) { /* Child reads from pipe */char buffer2[30]="message from child";printf("nnNow in child processn");close(pipefd[0]); /* Close unused write end */write(pipefd[1], buffer2, sizeof(buffer2));close(pipefd[1]);_exit(EXIT_SUCCESS);} else {printf("nnNow in parent process n");wait(NULL);close(pipefd[1]); /* Close unused read end */read(pipefd[0], buf, sizeof(buf));printf("nnNow again in parent process n");printf("nn%snn",buf);close(pipefd[0]);/* Wait for child */exit(EXIT_SUCCESS);}}   Output : dell@linuxmint ~ $ gcc pipe.c dell@linuxmint ~ $ ./a.out Now in parent process Now in c

C program to demonstrate use of fork and wait system call in linux

/*Here we read a number from parent process send it to the child process and then child process calculates factorial of that number and returns to the parent process which then prints the final answer*/#include <sys/wait.h>#include <stdlib.h>#include <unistd.h>#include <stdio.h>main( ){int num;int fact=1;int pid, w=1;int status;printf("Enter any number....nn");scanf("%d",&num);printf("n");pid = fork();if (pid == -1) {printf("nError Occured in Forking a Process..n");exit(0);}if (pid==0) { /* Code executed by child */printf("nnChild PID is %ldnn", (long) getpid());/* Wait for signals */int i=0;if(num==0||num==1){fact=1;exit(fact);}else{for(i=1;i<=num;i++){fact = fact * i;//printf("fact= %d",fact);}printf("n Child Execution Completed...n");exit(fact);}}else {/* Code executed by parent */wait(&w);printf("n W= %d",w);int f=WEXITSTATUS(w);printf("nNow in parentnn");pri

C program demonstrating use of signal and alarm system call and signal handler function

#include<stdio.h>#include<signal.h>#include<stdlib.h>typedef void (*sighandler_t)(int);void signal_handler(int signum){printf("nn Sorry Time Outnn");exit(0);}int main(){char uname[20],passwd[20];signal(SIGALRM, signal_handler);printf("nEnter your Username and Password");alarm(10);scanf("%s %s",uname,passwd);printf("nnUsername: %s n Password:%s n You Have Logged In Successfully.. ",uname,passwd);return 0;}  

C program demonstrating use of stat system call

#include<stdio.h>#include <sys/types.h>#include <dirent.h>#include <sys/stat.h>#include <unistd.h>#include <string.h>#include <time.h>#include <stdlib.h>#define DIR1 "test/"const char dir[20]="test/";/* struct dirent {ino_t d_ino; // inode numberoff_t d_off; // offset to the next direntunsigned short d_reclen; // length of this recordunsigned char d_type; // type of file; not supportedby all file system typeschar d_name[256]; // filename};   struct stat {dev_t st_dev; // ID of device containing fileino_t st_ino; // inode numbermode_t st_mode; // protectionnlink_t st_nlink; // number of hard linksuid_t st_uid; // user ID of ownergid_t st_gid; // group ID of ownerdev_t st_rdev; // device ID (if special file)off_t st_size; // total size, in bytesblksize_t st_blksize; // blocksize for file system I/Oblkcnt_t st_blocks; // number of 512B blocks allocatedtime_t st_atime; // time of last accesstime_t st_mtime; // time of last

MATLAB code for Circular Convolution using Matrix method

% circular convolution using matrix method %clc;x=[1,2,3,1];h=[4,3,2,2];H=zeros(0);h_index=1;N=length(x);y=zeros(0);disp('x(n)=');disp(x);disp('h(n)=');disp(h);subplot(4,1,1);stem(x);title('Signal One : x(n) ');subplot(4,1,2);stem(h);title('Signal Two : h(n)');temp=0;for i=1:NH(i,1)=h(i);end;for i=2:N    h=circshift(h, [0,1]);    for j=1:N     H(j,i)= h(j);   end;end;disp('H =>>');disp(H);for i=1:N    for j=1:N        temp=temp+H(i,j)*x(j);    end;    y(i)=temp;    temp=0;end;disp('Circular Convolution : y(m)==>');disp(y);subplot(4,1,3);stem(y);title('Circular Convolution: y(m)');   Output:

Circular Convolution using DFT and IDFT ( W4 Matrix )

% W4 circular_convolution_using_dft_idft clc; x1=[1,2,3,4]; x2=[1,1,1,0]; N=4;   disp('x1(n)='); disp(x1); disp('x2(n)='); disp(x2);     % finding dft of x1 % x1k=zeros(); w4=zeros(); for i=0:N-1 for j=0:N-1 w4(i+1,j+1) = exp(complex(0,(-2*pi*i*j)/4)); end; end;   fprintf('W %d Matrix =',N); disp(w4); temp=zeros(); for i=1:N for j=1:N temp=temp+(w4(i,j).*x1(j)); end;   x1k(i)=temp; temp=0; end; disp('x1(k) = ') disp(x1k);       % finding dft of x2 % x2k=zeros(); temp=zeros(); for i=1:N for j=1:N temp=temp+(w4(i,j).*x2(j)); end;   x2k(i)=temp; temp=0; end; disp('x2(k) ='); disp(x2k);     % multiplying obtained DFT outputs X1(K)*X2(K) % Y_K=zeros(); for i=1:N Y_K(i)=x1k(i)*x2k(i); end; disp('Y(K) ='); disp(Y_K);     %finding IDFT Y(K) that is output of %circular conv.   %computing complex conjugate of w8 w4=conj(w4); disp('complex conjugate of w4'); disp(w4); y_m=zeros(); temp=zeros(); for i=1:N for j=1:N temp=temp+(1/4*(w4(i,j).*Y_

Circular convolution using IDFT and DFT ( using W8 matrix )

% W8 circular_convolution_using_dft_idft%clc;x1=[1,1,1,0,0,0,0,0];x2=[0,1,2,2,1,0,0,0];N=8; % finding dft of x1 %x1k=zeros();w8=zeros(); for i=0:N-1for j=0:N-1w8(i+1,j+1) = exp(complex(0,(-2*pi*i*j)/N));end;end;fprintf('W %d Matrix =',N);disp(w8);  temp=zeros();for i=1:Nfor j=1:Ntemp=temp+(w8(i,j)*x1(j));end;x1k(i)=temp;temp=0;end; disp('x1(k) = ')disp(x1k);  % finding dft of x2 %x2k=zeros();temp=zeros();for i=1:Nfor j=1:Ntemp=temp+(w8(i,j)*x2(j));end;x2k(i)=temp;temp=0;end;disp('x2(k) =');disp(x2k);   % multiplying obtained DFT outputs X1(K)*X2(K) %Y_K=zeros();for i=1:NY_K(i)=x1k(i)*x2k(i);end;disp('Y(K) =');disp(Y_K);   %finding IDFT Y(K) that is output of circular conv.% %computing complex conjugate of w8w8=conj(w8);  disp('complex conjugate of w8');disp(w8);  y_m=zeros();temp=zeros();for i=1:Nfor j=1:Ntemp=temp+((1/N)*(w8(i,j)*Y_K(j)));end; y_m(i)=temp;temp=0;end;disp('y_m=');disp(y_m);  %plottingsubplot(3,2,1);stem(x1);title('x1(

MATLAB code for making simple calculator with GUI

function varargout = gui(varargin) % GUI M-file for gui.fig% GUI, by itself, creates a new GUI or raises the existing% singleton*. % H = GUI returns the handle to a new GUI or the handle to% the existing singleton*. % GUI('CALLBACK',hObject,eventData,handles,...) calls the local% function named CALLBACK in GUI.M with the given input arguments. % GUI('Property','Value',...) creates a new GUI or raises the% existing singleton*. Starting from the left, property value pairs are% applied to the GUI before gui_OpeningFcn gets called. An% unrecognized property name or invalid value makes property application% stop. All inputs are passed to gui_OpeningFcn via varargin.%% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one% instance to run (singleton)".%% See also: GUIDE, GUIDATA, GUIHANDLES % Edit the above text to modify the response to help gui % Last Modified by GUIDE v2.5 21-Apr-2014 11:36:48 % Begin initialization code - DO NOT EDIT  g