Tuesday, October 11, 2011

Simulate UNIX command ' ls | grep '\.pdf' ' using pipe (Print the names of all PDF files in current directory)

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <dirent.h>
#include <string.h>
struct dirent *dptr;
int main(int argc, char *argv[]){
   int pid,a,p[2],i=0,j,n,f;
   char buffr1[100][100],buffr2[100][100],t[20];
   DIR *dirp;
   a = pipe(p);
   if(a == -1)
   {
     printf("Pipe Failed.\n");
     return 0;
   }
   pid = fork();
   switch(pid){
     case -1:perror("main: fork");
     exit(1);
     case 0: read(p[0],buffr2,sizeof(buffr2));
     n=(int) buffr2[0][0];
     for(i=0;i<n;i++){
       j=0;
       while(buffr2[i][j]!='\0'){
       f=0;
       while(buffr2[i][j+f]==argv[1][f] && argv[1][f]!='\0')
         f++;
       if(f==strlen(argv[1])){
         printf("%s\n",buffr2[i]);
         break;
       }
       j++;
     }
     }
      exit(1);
     break;
     default:
     dirp=opendir(".");
     if(dirp==NULL){
       printf("Error");
       exit(1);
     }
     while(dptr=readdir(dirp))
       strcpy(buffr1[++i],dptr->d_name);
     closedir(dirp);
     //Sorting
     n=i;
     for(i=1;i<n;i++){
       for(j=0;j<n-i;j++){
         if(strcmp(buffr1[j],buffr1[j+1])>0){
           strcpy(t,buffr1[j]);
           strcpy(buffr1[j],buffr1[j+1]);
           strcpy(buffr1[j+1],t);
         }
       }
     }
     buffr1[0][0]=i;
     write(p[1],buffr1,sizeof(buffr1));
     waitpid(pid,NULL,0);
     break;
   }
   close(p[0]);
   close(p[1]);
   return 0;
}


Output


Print lines that contain the searched string ( Simulate UNIX GREP command )

#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int printLine(int,int);
int cmpStr(int,int,char[]);
int main(int argc, char *argv[]){
   int fd,count=0,len,line=0;
   char buffer,flag='y';
   fd = open(argv[2],O_RDWR);
   if(fd!=-1){
     len=lseek(fd,0L,2);
     while(lseek(fd,count,0)<len){
       count++;
       read(fd,&buffer,sizeof(char));
       if(argv[1][0]==(char)buffer){
         if(cmpStr(fd,count,argv[1])){
           count=printLine(fd,line);
           line=count;
         }
       }
       else if((char)buffer=='\n')
         line=count;
     }

   }
   else
     printf("\nFile not found\n");
   close(fd);
   return 0;
}
int printLine(int fd,int start){
   char bfr;
   int len;
   len=lseek(fd,0L,2);
   while(lseek(fd,start,0)<len){
     start++;
     read(fd,&bfr,sizeof(char));
     printf("%c",(char)bfr);
     if((char)bfr=='\n')
       return start;
  }
  exit(1);
}
int cmpStr(int fd,int start,char str[]){
   char bfr;
   int len,i;
   len=lseek(fd,0L,2);
  for(i=1;i<strlen(str);i++){
     if(lseek(fd,start,0)<len){
       start++;
       read(fd,&bfr,sizeof(char));
       if((char)bfr!=str[i])
         return 0;
     }
     else
       exit(1);
  }
   return 1;
}

Output


No. of characters, words and lines in a file ( Simulate UNIX WC command)

#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[]){
   int fd,count=0,len,wCount=0,lCount=0,f=0;
   char buffer,flag='y';
   fd = open(argv[1],O_RDWR);
   if(fd!=-1){
     len=lseek(fd,0L,2);
     while(lseek(fd,count,0)<len){
       count++;
       read(fd,&buffer,sizeof(char));
       if((char)buffer==' ' || (char)buffer=='\t')
         f=0;
       else if((char)buffer=='\n'){
         lCount++;
         f=0;
       }
       else if(f==0){
         f=1;
         wCount++;
       }
     }
     printf("No of Lines= %d",lCount);
     printf("\nNo of Words= %d",wCount);
     printf("\nNo of Characters= %d\n",len);
   }
   else
     printf("\nFile not found\n");
   close(fd);
   return 0;
}

Output


Display the contents of a directory ( simulate DOS DIR command)

#include<stdio.h>
#include<dirent.h>
struct dirent *dptr;
int main(int argc, char *argv[]){
   DIR *dirp;
   dirp=opendir(argv[1]);
   if(dirp==NULL){
     printf("Error");
     exit(1);
   }
   while(dptr=readdir(dirp)){
     printf("%s\n",dptr->d_name);
   }
   closedir(dirp);
}

Output


Copy simulate -- Command Line Arguments

#include<stdio.h>
#include<stdlib.h>
int main(int n,char *args[])
{
   char command[100];
   int ret;
   if(n==3){
     sprintf(command,"cp %s %s",args[1],args[2]);
     ret=system(command);
     if(ret==0)
       printf("Successfully Copied from %s to %s\n",args[1],args[2]);
   }
   else
     printf("Invalid Command\n");
   return 0;
}

Output


Thursday, October 6, 2011

Producer-consumer problem

#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<sys/sem.h>
#define EMPTY 0
#define FULL 1
#define MUTEX 2
#define SIZE 10
void up(int sem_id,int sem_num,struct sembuf *semaphore){
  semaphore->sem_num=sem_num;
   semaphore->sem_op=1;
   semaphore->sem_flg=0;
   semop(sem_id,semaphore,1);
}
void down(int sem_id,int sem_num,struct sembuf *semaphore){
   semaphore->sem_num=sem_num;
   semaphore->sem_op=-1;
   semaphore->sem_flg=0;
   semop(sem_id,semaphore,1);
}
void initsem(int sem_id,int sem_num,int val){
   union semnum{
      int val;
      struct semid_ds *buf;
      unsigned short *array;
   }argument;
   argument.val=val;
  semctl(sem_id,sem_num,SETVAL,argument);
}
int main(){
   key_t shm_key=1234,sem_key=3456;
   int shm_id,sem_id;
   int *shm;
   struct sembuf semaphore;
   shm_id=shmget(shm_key,SIZE+1,IPC_CREAT|0666);
   sem_id=semget(sem_key,3,IPC_CREAT|0666);
   shm=shmat(shm_id,NULL,0);
  shm[0]=0;
  initsem(sem_id,EMPTY,SIZE);
   initsem(sem_id,FULL,0);
  initsem(sem_id,MUTEX,1);
   if(fork()==0){
      while(1){
         int item,i;
         sleep(2);
         down(sem_id,FULL,&semaphore);
         down(sem_id,MUTEX,&semaphore);
         item=shm[1];
         for(i=1;i<shm[0];i++)
           shm[i]=shm[i+1];
         shm[0]--;
         printf("\nConsumed %d",item);
         up(sem_id,MUTEX,&semaphore);
         up(sem_id,EMPTY,&semaphore);
      }
  }
  else{
      while(1){
         int item=random()%10;
         sleep(1);
         down(sem_id,EMPTY,&semaphore);
         down(sem_id,MUTEX,&semaphore);
         shm[++shm[0]]=item;
         printf("\nProduced %d",item);
         up(sem_id,MUTEX,&semaphore);
         up(sem_id,FULL,&semaphore);
      }
   }
}