Posts

Showing posts with the label Linux

Sparse matrix - 3 tuple representation

//program to 3-tuple representation #include<stdio.h> #include<unistd.h> #define SIZE 3000; void main() { int a[5][5],row,columns,i,j; printf("Enter the order of the matrix(5*5)"); scanf("%d %d",&row,&columns); printf("Enter the element of the matrix\n"); for(i=0;i<row;i++) for(j=0;j<columns;j++) { scanf("%d", &a[i][j]); } printf("3-tuple representation"); for(i=0;i<row;i++) for(j=0;j<columns;j++) { if(a[i][j]!=0) { printf("%d %d %d", (i+1),(j+1),a[i][j]); } } getchar(); }

Cache Blocking in C

/*:) cache_blk_mat_mult :) have fun */ // header files #include<unistd.h> #include<stdio.h> int min(int, int); //main func int main(int argc, char *argv[]) { int jj,kk,j,k,i; int B = 5, N = 10,r; int x[N][N],y[N][N],z[N][N]; for(i=0;i<N;i++) for(j=0;j<N;j++) { printf("%d%d ",i,j); x[i][j]=5; y[i][j]=6; z[i][j]=7; } for(jj=0;jj<N;jj=jj+B) { printf("jj = %d\n",jj); for(kk = 0;kk<N;kk=kk+B) { printf("kk = %d\n",kk); for(i=0;i<N;i++) { printf("i = %d\n",i); for (j = jj; j < min(jj+B-1,N); j++) { r = 0; for (k = kk; k < min(kk+B-1,N); k++) { printf("r = %d + y[%d][%d]*z[%d][%d]\n",r,i,k,k,j); r = r + y[i][k]*z[k][j]; } x[i][j] = x[...

P Threads in C

#include<pthread.h> #include<stdio.h> #include<stdlib.h> #define NUM_THREADS 5 int abi = 100; void *PrintHello(void *threadid) { long tid; long tid2; tid = (long)threadid; tid2 = tid; printf("Hello World! its me thread # %ld\n",tid); printf("incrementing tid2 in thread # %ld\n%ld\n",tid,tid2*2); pthread_exit(NULL); } int main(int argc, char *argv[]) { pthread_t threads[NUM_THREADS];//array of pthreads int rc; long t; abi++; for(t=0;t<NUM_THREADS;t++) { printf("In main: creating thread %ld\n",t); rc = pthread_create(&threads[t],NULL,PrintHello, (void *)t); if(rc) { printf("Error code returned from code pthread_create() is %d\n",rc); exit(-1); } } pthread_exit(NULL); }

System function in C

System function in C – Used to run shell commands inside a C program. /*:) system :) have fun */ // header files #include<unistd.h> #include<stdio.h> #include<stdlib.h> //main func int main(int argc ,char *argv[]) { printf("running ps with system() function\n"); system("ps -ax &"); printf("done"); return 0; }

Reading a file using fread

/*:) fread_fileV1 :) have fun */ // header files #include<unistd.h> #include<stdio.h> //main func int main(int argc, char *argv[]) { FILE *fp; char buff[11]; int pid; fp=fopen("baby","r"); pid=fork(); if(pid==0) { printf("Initial child pointer is %L\n",ftell(fp)); fread(buff,sizeof(buff),1,fp); buff[10]='\0'; printf("child read is %s\n",buff); printf("the child pointer after reading is %l\n",ftell(fp)); printf("the child is exiting \n"); } else { wait(0); printf("initial parent pointer is %l\n",ftell(fp)); fread(buff,sizeof(buff),1,fp); buff[10]='\0'; printf("parent read is %s\n",buff); printf("the parent poitner after read is %l\n",ftell(fp)); printf("parent exiting"); } return 0; }

File handler

files are the means to communicate between processes files are shared between them. when a read is made in one of the processes it moves the file posiion handler.here the read in the child process moves the file position pointer ten positions. when the read is made from the parent process the file is read from the eleventh position hence different things are displayed in the screen. // header files #include<unistd.h> #include<stdio.h> #include<fcntl.h> //main func int main(int argc, char *argv[]) { int fp,pid; char buff[11]; fp=open("baby",O_RDONLY); pid = fork(); if(pid ==0) { printf("child begins\n"); read(fp,buff,10); buff[10]='\0'; printf("child read\n"); puts(buff); printf("the file handle is :%d\n",lseek(fp,0,1)); printf("child ends\n"); ...

Fork - Program to fork a child process 2

/*:) fork_test :) have fun */ // header files #include<unistd.h> #include<stdio.h> #include<sys/types.h> //main func int main(int argc, char *argv[]) { pid_t new_pid; char *message; int n; printf("fork program starting"); new_pid = fork(); switch(new_pid) { case -1: printf("fork failed"); exit(1); case 0: message = "this is child"; n=5; break; default: message = "this is parent"; n=3; break; } for(;n>0;n--) { puts(message); sleep(1); } exit(0); }

Fork - Program to fork a child process

/*:) forktestV1 :) have fun 1. the variable i is declared common to both the parent as well as the child process. but parent and child each get their own version of the variable with the samme name.incrementing the variable in the child does not increment the variabble in the parent. 2. also if a pointer is used the value is not incremented. parent and child both gets a pointer to a variable. 3. when declared globally also does not increment the variable. each process gets a global variable. 4. printing the addres in child and parent gives the same value for the address 5. how it works is the child process executes and after its tiem slice it is moved to the swap space. the parent then executes and after its time slice it is moved to the swap space. the address of the variable remains the same. */ // header files #include<unistd.h> #include<stdio.h> //main func int i=10; int main(int argc, char *argv[]) { int pid; pi...

Program to automate pre program writing process

When writing a program, usually we need to create a folder and create a .C file inside the folder. In the .C file, header files need to be added. This program automates this and opens the .C file in Emacs for writing the program. /*usage: ./prog <directory name> <file name w/o extension.c> location: mybin need: to create a directory and program file to write programs. it creates the directory in the default progs folder at a single stroke. this program also writes a default code within the file creats a dir <directory name> and inside it creates a file <filename> no need to add extension .c as it automatically appends it */ #include<unistd.h> #include<sys/types.h> #include<sys/stat.h> #include<error.h> #include<errno.h> #include<dirent.h> #include<stdio.h> #include<stdlib.h> #include<wait.h> int main(int argc, char *argv[]) { int flag; DIR *dp; FILE ...

Core Dump - Check for core dump

/* core_dumpV1 */ // header files #include<unistd.h> #include<stdio.h> //main func void main(int argc,char *argv[]) { int i,pid,exitstat,status,j=0; pid=fork(); if(pid==0) { i=10/j; } else { wait(&status); if(status&80) printf("our core dumped\n"); else printf("core dumped my foot\n"); } }

Syncing Processes 2

Process Synchronization method 2 /* proc_syncV2 */ // header files #include<unistd.h> #include<stdio.h> int main() { int i,pid,exitstat,status; pid=fork(); if(pid==0) { sleep(10); exit(3); } else { wait(&status); printf("status returned by the process is %d\n",status); printf("status & 0xff = %d\n",status&0xff); if(status&0xff!=0) { printf("signal is interrupted\n"); } else { exitstat=(int)status/256; printf("Exit status from %d was %d\n",pid,exitstat); } } return 0; }

Syncing processes

Syncing Processes – When running two or more processes, synchronization is important. Two versions of synchronization is available here. /* proc_syncV1 */ // header files #include<unistd.h> #include<stdio.h> //main func int main(int argc, char *argv[]) { int i=0, pid,cpid,dip; printf("ready to fork\n"); pid=fork() if(pid==0) { printf("1st child process id is %d\n",getpid()); printf("first child terminating from memory\n"); } else { dip=fork(); if(dip==0) { printf("2nd child process id is %d\n",getpid()); printf("seoncd child is terminating from memory\n"); } else { cpid=wait(0); printf("child with pid %d died\n",cpid); cpid=wait(0); ...

Playing a playlist of MP3 files using the mpg player in Linux

/* mpg3V1 */ // header files #include<unistd.h> #include<stdio.h> #include<error.h> #include<errno.h> //main func int main(int argc, char *argv[]) { File *playlist;//file pointer for playlist char filename[256];//getting filename from the playlist pid_t pid_mpg;//the pid of child process for launching mpg player playlist=fopen("/home/abishek/Music/playlist.txt","r");//reads the playlist intp fp "playlist" if(playlist==NULL) { error(1,errno,"cannot open the playlist"); } while(filename!=NULL) { filename=fgets(filename,sizeof(filename),playlist);//gets first filename from playlist if(filename==NULL)//checking end of file { printf("end of file reached"); return 0; } pid_mpg=fork();//branching child process cp->mpg123 pp->wait if(pid_mpg==-1) { error(1,errno,"cant...

C programs - playing MP3 files using C

/* snd_testV1 */ // header files #include<unistd.h> #include<stdio.h> #include<error.h> #include<errno.h> //main func int main(int argc, char *argv[]) { FILE *song; FILE *snd_op; char song_data[256]; song=fopen("/home/abishek/Music/PPRY.mp3","r"); snd_op=fopen("/dev/dsp","w"); if(song==NULL) { error(1,errno,"cannot open file"); } if(snd_op==NULL) { error(1,errno,"cannot open player"); } while(song_data!=NULL) { fgets(song_data,sizeof(song_data),song); if(song_data==NULL) { //eof reached return return 0; } fprintf(snd_op,"%s",song_data); } fclose(snd_op); fclose(song); }

C Program - Copy data from a file to another

copy.c SYNTAX: copy file1 file2 file1 - file to be copied file2 - file on to which file1 is to be copied copies file1 to file2 after creating file2. if already existing it rewrites the oldfile with the new file remarks 1. error checking is done 2. if file1 is not present it shows error 3. working for files in the same directory as well as files in other directories 4. copies in blocks of 1024 bytes buffer size is 1024 5. if file2 is not present it creates a file if already present it over writes #include<unistd.h> #include<fcntl.h> #include<stdlib.h> #include<sys/stat.h> #include<stdio.h> #include<error.h> #include<errno.h> int main(int argc, char *argv[]) { char block[1024]; int in, out; size_t rlen,wlen; char *filein; char *fileout; filein=argv[1]; fileout=argv[2]; // printf("%s\n",filein); // printf("%s\n",fileou...

C Programs - Hello World

I am uploading source code of few commonly used functions. The programs are written in C and I used gcc to compile and test these programs. I hope you find them useful. As it is always the case, I am starting with hello world #include<stdlib.h> #include<stdio.h> void main() { printf("HEllo World"); }

Running a JAVA applet program

This is to test if JAVA is installed properly. A Java applet program which is compiled without using and IDE. The procedure is as follows Applet Hello world Running it from the console without any IDE A. Creating a class extending the applet class 1. open your favourite editor and type in the following code public class HWApplet extends java.applet.Applet {     public void paint(java.awt.Graphics g)     {         g.drawString("greetings",50,50);     } } 2. Save the file as HWApplet.java B. Creating a HTML file to run the applet 1. Open your editor and type in the following code <APPLET code="HWApplet.class" width=350 height=200></APPLET> 2. Save it as HW.html in the same directory as the HWApplet.java file C. Compile the Java program 1. you can compile the Java program by going into the folder containing the file and using Javac javac HWApplet.java D. Running the Applet 1...

Installation of Netbeans IDE in Linux

Installation of NETBEANS IDE in linux 1. Download netbeans from http://netbeans.org/downloads/ (choose the version depending on your system) 2. After download is complete, cd to the download location you will find a netbeans-ver.sh script 3. open terminal, cd to the location of the .sh file and type chmod u+x netbeans-ver.sh (changes the execute permissions) 4. Then type "./netbeans-ver.sh" to execute the shell script 5. The installer pops up and you can specify the destination for installtion. (say ~/netbeans-ver) 6. Agree to terms and conditions and complete the installation 7. After installation is done open ~/.bashrc file in your favourite editor (.bashrc is a hidden file) 8. Add the follwing lines to the end of the file  export PATH=$PATH:/location-to-netbeans/lib (for example in this case where the location is ~/netbeans-ver type export PATH=$PATH:~/netbeans-ver/lib) 9. close the terminal and open it again and type "netbeans" to open the IDE. 10...

playing wmv files in ubuntu

How to play windows media player formats (wmv etc) on ubuntu -  In the synaptic package manager, search for ubuntu-restricted and install the restricted codecs.

LFS - Binutils

INSTALLING BINUTILS package Shifted to www.linuxfromscratch.org - Linux From Scratch - version 6.8 (the book was outdated so shifting to the website by the same other) - It starts with installation of binutils and not BASH. no problem though. The main modifications I made is shifted the sources from /usr/src to a dedicated folder at $LFS/sources The location where the tools are to be installed is $LFS/tools. A symbolic link is created for the same in the host systems INSTALLATION OF BINUTILS check for $LFS, $LFS_TGT else export LFS=/mnt/lfs export LFS_TGT=$(uname -m)-lfs-linux-gnu 1. download the Binutils-2.21.tar.gz into the $LFS/sources 2. tar xvzf binutils-2.21.tar.gz - extracts into a folder called binutils-2.21 3. mkdir $LFS/sources/binutils-build 4. cd $LFS/binutils-build 5. ../binutils-2.21/confiure --targer=$LFS_TGT --prefix=/tools --disable-nls --disable-werror 6. make 7. make install LFS_TGT contains the description of the system. The config.guess inside b...