Social Icons

Showing posts with label Linux. Show all posts
Showing posts with label Linux. Show all posts

Saturday, 3 May 2014

Symbol Table Creation


To generate tokens
%%
{letter}({letter}|{digit})*       printf(“id: %s\n”, yytext);
%%
--------------------

1.
%{
#include<string.h>
%}
%%
(int|float|double|long|short|return|include|char) { printf("\n< Keyword,%s >",yytext);func(yytext,"Keyword");}
[\"] { printf("\n< Operator,%s >",yytext);func(yytext,"Operator"); }
['"'][\\a-zA-Z0-9|' ']+['"'] { printf("\n< id,%s >",yytext); func(yytext,"String");}

\+\+ { printf("\n< %s >",yytext);func(yytext,"Operator"); }
\-\- { printf("\n< %s >",yytext); func(yytext,"Operator");}
\=\= { printf("\n< %s >",yytext); func(yytext,"Operator");}
\!\= { printf("\n< %s >",yytext); func(yytext,"Operator");}
\+\= { printf("\n< %s >",yytext); func(yytext,"Operator");}
\-\= { printf("\n< %s >",yytext); func(yytext,"Operator");}
[/|'.']+ { printf("\n< %s >",yytext); func(yytext,"Operator");}
(printf|scanf) { printf("\n< function,%s >",yytext);func(yytext,"Function");}

[0-99]+ { printf("\n<Number,%s >",yytext);func(yytext,"Number"); }
[a-zA-Z|_][0-99|a-zA-Z|_]* { printf("\n< variable,%s>",yytext); func(yytext,"variable");}
['+'|\-|'='|':'|'%'|'&'|'('|')'|'{'|'}'|','|'#'|'!'|'@'|'$'|'*'|'<'|'>'|'?'] { printf("\n< %s >",yytext);func(yytext,"Operator");}

[';']+ { printf("\n< %s >",yytext);func(yytext,"Operator");}
\n func2();
%%
struct entry
{
char name[10];
char token[10];
}st[10];
int i=0;
func(char n[10],char t[10])
{
int j,flag=0;
for(j=0;j<i;j++)
{
if(strcmp(n,st[j].name)==0)
flag=1;
}
if(flag==0)
{
strcpy(st[i].name,n);
strcpy(st[i].token,t);
i++;
}
}
func2()
{
int k;
printf("\n===========Symbol Table==================\n");
for(k=0;k<=i;k++)
printf("%s   %s\n",st[k].name,st[k].token);
}



Friday, 23 August 2013

Full Duplex Mode Communication Using Pipe

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/ipc.h>
#include<sys/types.h>
int main()
{
int p1[2],p2[2],i;
char buf[20];
pipe(p1);
pipe(p2);
if(fork()==0)
{
printf("\n this is child(the input text is text)\n");
close(p1[1]);
close(p2[0]);
read(p1[0],buf,20);
write(p2[1],"Hello Papa ! I m fine",25);
printf("\n Parent send the message : %s \n",buf);

}
else
{
printf("\n this is parent(the output text is text)\n");
close(p2[1]);
close(p1[0]);
write(p1[1],"how r u beta",20);

read(p2[0],buf,20);
printf("\n child sending message to parent : %s \n",buf);

}
return 0;
}


Output:
             

Using Fork Command getting Process Id Of Child

#include<sys/types.h>
#include<stdio.h>
#include<stdlib.h>
int main()
{
int pid;
pid=fork();
if(pid==0)
{
printf("\n the process id of child is %d",getpid());
printf("\n the parent process id of child  is %d",getppid());
}

else
{
printf("\n the process id of parent is %d",getpid());
printf("\n the parent process id of parent  is %d",getppid());
}

return 0;

}

Friday, 3 May 2013

Time Using Strftime Function


#include<unistd.h>
#include<time.h>
#include<stdio.h>
int main()
{
time_t t1,t2;
struct tm *tm_ptr;
int m,p,u;
char *n;
char buf[256];
time(&t1);

tm_ptr=localtime(&t1);

strftime(buf,256,"%A:%d:%B:%I:%S:%p",tm_ptr);
printf("\n time is %s",buf);

return 0;
}

Time Using Ctime Function


#include<unistd.h>
#include<time.h>
#include<stdio.h>
int main()
{
time_t t1,t2;
struct tm *tm_ptr;
int m,p,u;
char *n;
time(&t1);

n=ctime(&t1);


printf("\n time is %s",n);
return 0;
}



Time Using Localtime Function


#include<unistd.h>
#include<time.h>
#include<stdio.h>
int main()
{
time_t t1,t2;
struct tm *tm_ptr;
int m,p,u;
char *n;
time(&t1);

tm_ptr=localtime(&t1);


printf("\n year %2d  : mon %2d    hour  %2d, Minute %2d , Second %2d",tm_ptr->tm_year+1900,tm_ptr->tm_mon+1,tm_ptr->tm_hour,tm_ptr->tm_min,tm_$
return 0;
}

Time Function Using C program


#include<unistd.h>
#include<time.h>
#include<stdio.h>
int main()
{
time_t t1,t2;
int m,p,u;
char *n;
time(&t1);

m=t1;
printf("\n time is %d",m);

printf("\n enter your name :");
scanf("%s",n);

time(&t2);

p=t2;
printf("\n time is %d",p);

u=p-m;
printf("\n time taken in writing name is : %d",u);


return 0;
}

Thursday, 2 May 2013

Thread cancelletion Using Deferred Cancel Type


#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<string.h>
void  *func1(void *arg);
char m[]="hi how r u";

int main()
{
int r,x;
void *p;
pthread_t a;
r=pthread_create(&a,NULL,func1,(void *)m);
if(r!=0)
{

perror("thread creation failed : ");
exit(EXIT_FAILURE);
}
else
sleep(3);
x=pthread_cancel(a);
if(x==-1)
{
perror("thrAed cancel failed ");


}

else
sleep(3);
x=pthread_cancel(a);
if(x==-1)
{
perror("thrAed cancel failed ");


}
else
{
printf("\n thread cancelled");

r=pthread_join(a,&p);
if(r!=0)
{
perror("thread joining failed :");
exit(EXIT_FAILURE);
}
printf("\n thread joined with value %s ", (char *)p);
printf("\n messafe is %s",m);
exit(EXIT_SUCCESS);
}
}



Wednesday, 1 May 2013

Machine Information Using Library Function getuid,getgid,gethostname


#include<stdio.h>
#include<sys/types.h>
#include<sys/utsname.h>
#include<unistd.h>
#include<stdlib.h>
#include<pwd.h>
int main()
{
uid_t uid;
gid_t gid;
char s[100];
struct passwd *pw;
struct utsname uts;

uid=getuid();
gid=getgid();

printf("\n userid : %d  , groupid : %d ",uid,gid);
printf("\n login name : %s ",getlogin());

  pw=getpwuid(uid);
printf("\n uid : %d , gid :  %d , name : %s",pw->pw_uid,pw->pw_gid,pw->pw_name);

gethostname(s,255);
printf("\n host name : %s",s);

uname(&uts);

printf("\n machine name :  %s , \n  sysname :  %s ,\n  version : %s ,\n Nodename : %s ,\n  ",uts.machine,uts.sysname,uts.version,uts.nodename);
return 0;
}

Sum And Product Using Getopt Function


#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
int main(int argc , char *argv[])
{

int n,i,sum=0, pr=1;
int a,b;
while((n=getopt(argc,argv,":if:::l::r"))!=-1)
{
 switch(n)
 {
 case 'i' :
 case 'r' : printf("\n option %c",n);
              break;
 case 'f'  :// printf("\n filename %s",optarg);
                a=optind;
               for(;optind<a+3;optind++)
                 {
                    i=atoi(argv[optind]);
                    sum=sum+i;
                  }
                    printf(" \n sum of the two no :  %d ",sum);
                 break;
 case 'l'  :     b=optind;
                 for(;optind<b+2;optind++)
                 {
                    i=atoi(argv[optind]);
                    pr=pr*i;
                  }
                 printf(" \n Product  of the two no :  %d ",pr);
                 break;

 case '?'  : printf("\n unknown option %c",optopt);
               break;
 case ':'  : printf("\n argument needed : ");
              break;
 }

 }

           for(;optind<argc;optind++)
    {
    printf(" \n arguments : %s ",argv[optind]);
    }


return 0;
}


             
             

Copying Files content Using Read & write system call


#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>

int main()
{
int nread;
char buf[150];
int  m,n,o;
m=open("kaha", O_RDONLY,S_IRUSR|S_IWUSR);
if(m==-1)
{
printf("\n Error Occured : ");
}

n=open("tu.txt",O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR);
if(n==-1)
                {

printf("\n Error Occured : ");

}

lseek(m,2,SEEK_SET);
lseek(n,5,SEEK_SET);

while(nread=read(m,buf,sizeof(buf)))
{
  write(n,buf,10);
}



return 0;
}

Reading Files Of a Directory


#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<dirent.h>
struct dirent *p;
int main()
{
DIR *q;
int  m;
q=opendir("1");
if(q==NULL)
{
printf("\n Error Occured : ");

}

else
{
printf("\n  File Name  |||||||     Inode No.\n");
printf("......................................");
while(p=readdir(q))
{

printf("\n %s",p->d_name);
m=p->d_ino;
printf("\t\t\t %d",m);
}
}
closedir(q);
return 0;
}


1 12 123 1234 12345 Pattern Using Shell Programme


#!/bin/sh
c=6;
m=1;
for((i=1;i<=5;i++))
do
for((k=1;k<=c;k++))
do
  echo -n  " "
done
for((j=1;j<=i;j++))
do
echo -n "$j"
echo -n " "
done
c=$(($c-1))
echo -e " "
done


exit 0


Loop Patterns Using Shell Commands


#!/bin/sh
c=5;
for((i=1;i<=5;i++))
do
for((k=1;k<=c;k++))
do
  echo -n  " "
done
for((j=1;j<=i;j++))
do
echo -n "*"
echo -n " "
done
c=$(($c-1))
echo -e " "
done


exit 0

Output :

Geometric Progression Using shell


#!/bin/sh

echo -n "Enter First No. of G.P :  "
read  a
echo -n "Enter Common Ratio of G.p :"
read r
echo -n  "how many no. you want to print : "
read  n
echo " .............G.p is : ........   "

while [ $n -gt 0 ]
do
echo -n "$a "
a=$(($a*$r))
n=$(($n-1))
done

exit 0

Cheking Number is Palindrome


#!/bin/sh

echo -n "Enter any no to find Reverse :  "
read  n
p=$n
rev=0
while [ $n -gt 0 ]
do
z=$(($n%10))
n=$(($n/10))
rev=$(($(($rev*10)) + z))
done
echo " .............reverse of the no. is : ........   : $rev"

if [ $rev -eq $p ]
then
echo "............Number is Palindrome.......... "
else
 echo  "........Number  is Not Palindrome ............."
fi
exit 0
output : 

Reverse Of a No.


#!/bin/sh

echo -n "Enter any no to find Reverse :  "
read  n
p=$n
rev=0
while [ $n -gt 0 ]
do
z=$(($n%10))
n=$(($n/10))
rev=$(($(($rev*10)) + z))
done
echo " .............reverse of the no. is : ........   : $rev"
exit 0
Output :



Factorial Of a No. Using Shell Program



#!/bin/sh
fact=1;
echo -n "Enter any no to find factorial :  "
read  n
for((i=1;i<=n;i++))
do
((fact=$fact * $i))

done

echo " .............factorial of the no. is : ........   : $fact"
exit 0


Shell Programming [If- Else]


#!/bin/sh
echo -n "Enter how many times you want to enter  :  "
read d
for((i=0;i<d;i++))
do
echo -n "Enter your age :  "
read n
if [ $n -gt 20 ] && [ $n -lt 50 ]
then
echo "...........you are middle aged men...................... "
elif [ $n -gt 5 ] && [ $n -lt 20 ]
then
echo "................you r a boy................"
else
 echo ".............you are a old man ............. "
fi
done
exit 0

Output:

Excuting Command in C program using "system" function


#include<stdio.h>
#include<stdlib.h>
int main()
{
printf("\n Showing Details of A file using System command : \n");


system("more first.txt");
return 0;


}



 

Sample Text

Sample text

 
Just Programming Cse DriveReputation Management