Tuesday, November 22, 2011

Process hierarchy ; A is the parent of B & B is the parent of C and D


A- reads a sentence and count no of vowels
B- Count no of upper case and lower case
C- Print ID of C and D
D-Print the sentence in reverse order




Using PIPE


#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
int main(){
   int pid1,pid2,pid3,pid4;
   int p1[2],p2[2];
   char bufr[30],rev[30];
   int countL=0,countU=0,i=-1,j=0,countV=0,len;
   pipe(p1);
   pipe(p2);
   if(pid1=fork()==0){

      if(pid2=fork()==0){
         read(p2[0],bufr,sizeof(bufr));
         len=strlen(bufr);
         for(i=len-1,j=0;j<len;i--,j++)
            rev[j]=bufr[i];
         rev[j]='\0';
         printf("Proccess D---- Reverse = %s \n",rev);
         exit(1);
      }
      else{
         read(p1[0],bufr,sizeof(bufr));
         write(p2[1],bufr,sizeof(bufr));
         if(pid3=fork()==0){
            printf("Poccess C--- ID of B = %d and ID of C = %d \n",getppid(),getpid());
         exit(1);
      }
      else{
         while(bufr[++i]!='\0')
            if(bufr[i]>='A' && bufr[i]<='Z')
               countU++;
         i=-1;
         while(bufr[++i]!='\0')
            if(bufr[i]>='a' && bufr[i]<='z')
               countL++;
         printf("Poccess B--- No of UpperCase letters = %d \n",countU);
         printf("Poccess B--- No of LowerCase letters = %d \n",countL);
         waitpid(pid2,NULL,0);
         waitpid(pid3,NULL,0);
      }
   }
   exit(1);
   }
   else{
      printf("Poccess A--- Enter a sentence ");
      gets(bufr);
      write(p1[1],bufr,sizeof(bufr));
      while(bufr[++i]!='\0')
         if(bufr[i]=='a' || bufr[i]=='e' || bufr[i]=='i' || bufr[i]=='o' || bufr[i]=='u' ||
            bufr[i]=='A' || bufr[i]=='E' || bufr[i]=='I' || bufr[i]=='O' || bufr[i]=='U' )
            countV++;
      printf("Poccess A--- No of Vowels = %d \n",countV);
      waitpid(pid1,NULL,0);
   }
   close(p1[0]);
   close(p1[1]);
   return 0;
}



Using SHARED MEMORY



#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
   int shmid,shm_size=30,*shmInt,pid1,i=-1,count1=0,count2=0;
   int pid2,pid3,len,j;
   char *shm,*temp,rev[30];
   key_t key=4546,key2=6788;
   shmid=shmget(key,shm_size,IPC_CREAT | 0666);
   shm=shmat(shmid,NULL,0);
   shmid=shmget(key2,1,IPC_CREAT | 0666);
   shmInt=shmat(shmid,NULL,0);
   *shmInt=0;
   if(pid1=fork()==0){
     while(*shmInt==0);
     temp=shm;
     if(pid2=fork()==0){
       len=strlen(temp);
       for(i=len-1,j=0;j<len;i--,j++)
         rev[j]=*(temp+i);
       rev[j]='\0';
       printf("Proccess D---- Reverse = %s \n",rev);
       exit(1);
     }
     else{
       if(pid3=fork()==0){
         printf("Poccess C--- ID of B = %d and ID of C = %d \n",getppid(),getpid());
         exit(1);
     }
     else{
       while(*(temp+(++i))!='\0')
         if(*(temp+i)>='A' && *(temp+i)<='Z')
           count1++;
       i=-1;
       while(*(temp+(++i))!='\0')
         if(*(temp+i)>='a' && *(temp+i)<='z')
           count2++;
       printf("Poccess B--- No of UpperCase letters = %d \n",count1);
       printf("Poccess B--- No of LowerCase letters = %d \n",count2);
       waitpid(pid2,NULL,0);
       waitpid(pid3,NULL,0);
     }
   }
   exit(1);
   }
   else{
   printf("Enter a sentence ");
   gets(shm);
   *shmInt=1;
   temp=shm;
   while(*(temp+(++i))!='\0')
     if(*(temp+i)=='a' || *(temp+i)=='e' || *(temp+i)=='i' || *(temp+i)=='o' || *(temp+i)=='u' ||
       *(temp+i)=='A' || *(temp+i)=='E' || *(temp+i)=='I' || *(temp+i)=='O' || *(temp+i)=='U' )
         count1++;
     printf("Poccess A--- No of Vowels = %d \n",count1);
     waitpid(pid1,NULL,0);
   }

   return 0;
}

Process hierarchy ; A is the parent of B & C

Create following process hierarchy:- A is d parent of B & C

A- Prints even no from 1 to 30

B- Prints odd no from 30 to 1

C- Prints multiplication table of 4




#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(){
   int pid1,pid2,i;
   if(pid1=fork()==0){
      for(i=30;i>=1;i=i-2)
         printf("Process B ------- %d\n",i);
      exit(1);
   }
   else{
      waitpid(pid1,NULL,0);
      for(i=2;i<=30;i=i+2)
         printf("Process A------- %d\n",i);
      if(pid2=fork()==0){
         for(i=1;i<=10;i++)
               printf("Process C------ %d * 4 = %d\n",i,i*4);
      exit(1);
   }
   waitpid(pid2,NULL,0);
   }
   return 0;
}

Saturday, November 19, 2011

Two process running parallelly are updating a shared value differently. Display the result after 10 updations.

#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<sys/sem.h>
#define MUTEX 0
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=32233,sem_key=45565;
   int shm_id,sem_id,pid;
   int *shm;
   struct sembuf semaphore;
   shm_id=shmget(shm_key,2,IPC_CREAT|0666);
   sem_id=semget(sem_key,1,IPC_CREAT|0666);
   shm=shmat(shm_id,NULL,0);
   printf("\nEnter the initial number ");
   scanf("%d",&shm[0]);
   shm[1]=0;
   initsem(sem_id,MUTEX,1);
   pid=fork();
   if(pid==0){
     while(shm[1]<10){
     int item,i;
     sleep(2);
     down(sem_id,MUTEX,&semaphore);
     if(shm[1]<10){
       shm[0]+=2;
       shm[1]++;
       printf("Incremented by 2\n");
     }
     up(sem_id,MUTEX,&semaphore);
   }
   exit(0);
  }
   else{
      while(shm[1]<10){
     sleep(1);
     down(sem_id,MUTEX,&semaphore);
     if(shm[1]<10){
       shm[0]-=4;
       shm[1]++;
       printf("Decremented by 4\n");
     }
     up(sem_id,MUTEX,&semaphore);
   }
   waitpid(pid,NULL,0);
   printf("\nResult:- %d\n",shm[0]);
   exit(0);

   }
}

Friday, November 18, 2011

Parent process read 2 filenames and compare the sizes returned by child processes (reading of sizes are done parallelly by two child process)

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
int main(){
   int pid1,pid2,p1[2],p2[2],p3[2],p4[2],i,j,buffr3[2],buffr4[2],flag,n2=0;
   char buffr1[2][30],buffr2[2][30];
   struct stat filestat;
   pipe(p1);
   pipe(p2);
   pipe(p3);
   pipe(p4);
   pid1 = fork();
   switch(pid1){
     case -1:perror("main: fork");
     exit(1);
     case 0: read(p1[0],buffr1,sizeof(buffr1));
     printf("In child process (ID: %d)\n", getpid());
     stat(buffr1[0],&filestat);
     buffr3[0]=filestat.st_size;
     write(p3[1],buffr3,sizeof(buffr3));
     exit(1);
     break;
     default:
     pid2 = fork();
     switch(pid2){
       case -1:perror("main: fork");
       exit(1);
       case 0: read(p2[0],buffr1,sizeof(buffr1));
       printf("In child process (ID: %d)\n", getpid());
       stat(buffr1[0],&filestat);
      buffr3[0]=filestat.st_size;
      write(p4[1],buffr3,sizeof(buffr3));
      exit(1);
      break;
      default:
       printf("In parent process (ID: %d)\n", getpid());
       printf("\nEnter the files \n");
       gets(buffr1[0]);
      gets(buffr2[0]);
      write(p1[1],buffr1,sizeof(buffr1));
      write(p2[1],buffr2,sizeof(buffr2));
      waitpid(pid1,NULL,0);
      waitpid(pid2,NULL,0);
      read(p3[0],buffr3,sizeof(buffr3));
      read(p4[0],buffr4,sizeof(buffr3));
      if(buffr3[0]>buffr4[0])
        printf("\n%s is larger than %s\n",buffr1[0],buffr2[0]);
       else
         printf("\n%s is larger than %s\n",buffr2[0],buffr1[0]);
       break;
       }
     }

   return 0;
}

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