Showing posts with label Unix program. Show all posts
Showing posts with label Unix program. Show all posts

Monday, April 23, 2012

Message Queues


#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>

struct my_msgbuf {
long mtype;
char mtext[200];
};

int main(void)
{
struct my_msgbuf buf;
int msqid;
key_t key;

if ((key = ftok("unix3.c", 'B')) == -1) {
perror("ftok");
exit(1);
}

if ((msqid = msgget(key, 0644 | IPC_CREAT)) == -1) {
perror("msgget");
exit(1);
}
printf("Enter lines of text, ^D to quit:\n");

buf.mtype = 1;

while(fgets(buf.mtext, sizeof buf.mtext, stdin) != NULL) {
int len = strlen(buf.mtext);

if (buf.mtext[len-1] == '\n') buf.mtext[len-1] = '\0';

if (msgsnd(msqid, &buf, len+1, 0) == -1)
perror("msgsnd");
}

if (msgctl(msqid, IPC_RMID, NULL) == -1) {
perror("msgctl");
exit(1);
}

return 0;
}


Thursday, April 12, 2012

Upper to Lower Case


echo "program to convert uppercase to lower case"
echo "enter the filename to be created:"
read str
vi $str
echo "1.lowercase to uppercase"
echo "2.uppercase to lowercase"
echo "select your choice:"
read ch
case $ch in
1)cat $str | tr "{a-z}" "{A-Z}" | tee $str cat $str;;
2)cat $str | tr "{A-Z}" "{a-z}" | tee $str cat $str;;
*)echo "invalid choice"
esac

Unlink


clear
echo
ch=0
while [ $ch_lt 2 ]
do
echo "1.create file
      2.unlink"
echo
echo "enter your choice:"
read ch
echo $ch
case $ch in
1)echo "enter file name:"
read fn
vi $fn;;
2)unlink $fn;
esac
done

System Calls


clear
echo "program to show file commands"
con='Y'
while [ $con='Y' ]
do
echo "1.list of files and folders
2.user's
3.process running currently
4.today's date
5.calender
6.quit"
echo "enter your choice:"
read choice
case $choice in
1)ls;;
2)who;;
3)ps;;
4)date;;
5)cal;;
6)exit;;
*)echo "invalid choice"
esac
echo
echo "Do you want to continue(y/n)"
read con
if [ $con='n' ]
then
exit;
echo "continue"
fi
done

Sorting and Unsorting a File


clear
echo
ch=0
while [ $ch -lt 6 ]
do
echo
echo "1.create a file
     2.sort a file
     3.sorted file
     4.unsorted file
     5.clear
     6.quit    "
echo
echo "Enter your choice:"
read ch
echo $ch
case $ch in
1) echo "Enter file name"
read fn
vi $fn;;
2) echo "sorted process is over";;
3) sort $fn;;
4) cat $fn;;
5) clear;;
6) exit;;
esac
done

Signalling


#include<stdio.h>
#include<stdlib.h>
#include<signal.h>
void signal_callback_handler(int signum)
{
printf("caught signal%d \n",signum);
exit(signum);
}
int main()
{
signal(SIGINT,signal_callback_handler);
while(1)
{
printf("program processing stuff here \n");
sleep(1);
}
return EXIT_SUCCESS;
}

Semaphores


#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/sem.h>
#include<stdio.h>
#define KEY (1492)
void main()
{
int id;
union semun
{
int val;
struct semid_ds *buf;
ushort * array;
}
argument;
argument.val=0;
id=semget(KEY,1,0666 | IPC_CREAT);
if (id<0)
{
fprintf(stderr,"unable to obtain semaphore.\n");
}
if(semctl(id,0,SETVAL,argument)<0)
{
fprintf(stderr,"cannot set semaphore value.\n");
}
else
{
fprintf(stderr,"semaphore %d initialized.\n",KEY);
}
}


PipeLining


#include<sys/types.h>
#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
void
read_from_pipe(int file)
{
 FILE *stream;
 int c;
 stream=fdopen(file,"r");
 while((c=fgetc(stream))!=EOF)
 putchar(c);
 fclose(stream);
}

void
write_to_pipe(int file)
{
 FILE *stream;
 stream=fdopen(file,"w");
 fprintf(stream,"Hello,world!\n");
 fprintf(stream,"Gud bye,world!\n");
 fclose(stream);
}

int main (void)
{
 pid_t pid;
 int mypipe[2];
 if (pipe(mypipe))
 {
  fprintf(stderr,"pipe failed \n");
  return EXIT_FAILURE;
 }
 pid=fork();
 if(pid==(pid_t)0)
 {
  close(mypipe[1]);
  read_from_pipe(mypipe[0]);
  return EXIT_SUCCESS;
 }
 else if (pid<(pid_t)0)
 {
  fprintf(stderr,"forkfailed \n");
  return EXIT_FAILURE;
 }
 else
 {
  close(mypipe[0]);
  write_to_pipe(mypipe[1]);
  return EXIT_SUCCESS;
 }
}

Mark Statement


clear
echo "enter stud's name:"
read name
echo "enter roll no:"
read roll
echo "enter mark in p1:"
read m1
echo "enter mark in p2:"
read m2
echo "enter mark in p3:"
read m3
echo "enter mark in p4:"
read m4
echo "enter mark in p5:"
read m5
echo "enter mark in p6:"
read m6
clear
echo "......."
echo "result"
echo "......."
echo "name:$name"
echo
echo "roll no:$roll"
echo
echo "M.Tech"
echo
echo "p1:$m1"
echo
echo "p2:$m2"
echo
echo "p3:$m3"
echo
echo "p4:$m4"
echo
echo "p5:$m5"
echo
echo "p6:$m6"
echo
echo "Total:`expr $m1 + $m2 + $m3 + $m4 + $m5 + $m6`"
echo
echo "Average:`expr $m1 / 6 + $m2 / 6 + $m3 / 6 + $m4 / 6 + $m5 / 6 + $m6 / 6`%"
echo
if [ $m1 -gt 50 -a $m2 -gt 50 -a $m3 -gt 50 -a $m4 -gt 50 -a $m5 -gt 50 -a $m6 -gt 50 ]
then
echo "Pass"
else
echo "Fail"
fi
echo
echo "......"

Insert and Search


clear
echo "Enter your file name:"
read fn
search=` find $fn `
if [ $fn = $search ]
then
echo "Enter your word to be searched:"
read se
word=`fgrep -a -o -m 1 $se $fn`
if [ $se = $word ]
then
choice=`zenity --list \
--title="search result" \
--column= " "\
"sucess-found the specified word"`
else
choice=`zenity --list \
--title="search result" \
--column=" "\
"failed-specified word not found"`
fi
else
choice=`zenity --list \
--title="error" \
--column=" "\
"failed-file not found"`
fi

Fork Child


#include <stdio.h>
int main(void)
{
 int pid;
 pid=fork();
if(pid==0)
 {
   int j;
  for(j=0;j<10;j++)
  {
   printf("child:=%d\n",j);
   sleep(1);
  }
  exit(0);
}
  else if(pid>0)
 {
 int i;
 for(i=0;i<10;i++)
 {
  printf("parent:%d\n",i);
  sleep(1);
 }
 }
 else
 {
  fprintf(stderr,"couldn't fork");
  exit(1);
 }
}

Fan of Process


#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
int main()
{
int i;
int n=10;
pid_t childpid;
for(i=1;i<n;++i)
if((childpid=fork())<=0)
break;
fprintf(stderr,"this is process %ld with parent %ld\n",(long)getpid(),(long)getpid());
}

Error Checking


#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
const char *FILE_NAME="File.c";
int main ( int argc, char ** argv )
{
int fd=0;
printf ("FINDING %s...\n",FILE_NAME);
fd = open (FILE_NAME,O_RDONLY,0644);
if
(fd<0)
{
perror("Error opening file");
printf("Error opening file:%s\n",strerror( errno ) );
}
else
{
printf("\n File Present");
}
return EXIT_SUCCESS;
}

Error Handling


#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int divident = 50;
int divisor = 0;
int quotient;
if (divisor == 0)
 {
 fprintf (stderr,"Division by zero ! aborting.....\n");
 exit(EXIT_FAILURE);
 }
else
 {
 quotient=divident /divisor ;
 exit (EXIT_SUCCESS);
 }
}

Chain of Process


#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
int main()
{
  int i;
  int n=10;

  pid_t childpid;

  for(i=1;i<n;++i)
   if(childpid=fork())
   break;

 printf("this is process %ld with parent %ld\n",(long)getpid(),(long)getppid());

}