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


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);
      }
   }
}

Wednesday, September 21, 2011

Parent creates shared memory to store N names and child display the names in alphabetical order

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
   long shmid,shm_size;
   key_t key;
   char *shm,*s;
   int n,i;
   printf("\nEnter the limit :- ");
   scanf("%d",&n);
   shm_size=n*30*sizeof(char);
   key = 11021;
   if ((shmid = shmget(key, shm_size, IPC_CREAT | 0666)) < 0) {
   perror("shmget");
   exit(1);
   }
   if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) {
   perror("shmat");
   exit(1);
   }
   s = shm;
   for(i=0;i<n;i++,s=s+20)
   scanf("%s",s);
   if(fork()==0){
     s=shm;
     int j,temp;
     char t[20];
     for(i=0;i<n-1;i++){
       temp=i;
         for(j=i+1;j<n;j++)
           if(strcmp(s+(20*temp),s+(20*j))>0)
           temp=j;
       strcpy(t,s+(20*i));
       strcpy(s+(20*i),s+(20*temp));
       strcpy(s+(20*temp),t);
     }
   }
   else{
     wait(NULL);
     return 0;
   }
   s=shm;
   printf("Alphabetical Order\n");
   for(i=0;i<n;i++,s=s+20)
     printf("%s\n",s);
   return 0;
}

Parent creates shared memory to store N numbers and child find max and min number

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
   long shmid;
   key_t key;
   int *shm;
   int *s,shm_size,n,i,lar,smal;
   printf("\nEnter the limit :- ");
   scanf("%d",&n);
   shm_size=n*sizeof(long);
   key = 110200;
   if ((shmid = shmget(key, shm_size, IPC_CREAT | 0666)) < 0) {
     perror("shmget");
     exit(1);
   }
   if ((shm = shmat(shmid, NULL, 0)) == (int *) -1) {
     perror("shmat");
     exit(1);
   }
   s = shm;
   for(i=0;i<n;i++,s++)
     scanf("%d",s);
   if(fork()==0){
     s=shm;
     lar=*s;
     smal=*s;
     for(i=1;i<n;i++){
       if(*(s+i)>lar)
         lar=*(s+i);
       else if(*(s+i)<smal)
         smal=*(s+i);
     }
   }
   else{
     wait(NULL);
     return 0;
   }
   printf("\nLargest= %d \nSmallest= %d\n",lar,smal);
   return 0;
}

Parent send N strings to child and child return the palindromes

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int main(){
  int pid,a,n,p[2],p2[2],i,len,j,k,flag,n2=0;
  char buffr1[100][100],buffr2[100][100],buf[5];
  printf("Enter the limit:- ");
  scanf("%d",&n);
  a = pipe(p);
  if(a == -1)
  {
     fprintf(stderr, "Pipe Failed.\n");
     return EXIT_FAILURE;
  }
  a = pipe(p2);
   if(a == -1)
   {
     fprintf(stderr, "Pipe Failed.\n");
     return EXIT_FAILURE;
  }
   pid = fork();
   switch(pid){
     case -1:perror("main: fork");
       exit(1);
     case 0: read(p[0],buffr2,sizeof(buffr2));
       printf("In child process (ID: %d)\n", pid);
       for(i=0;i<n;i++){
         len=strlen(buffr2[i]);
         flag=0;
         for(j=0,k=len-1;j<len;j++,k--){
           if(buffr2[i][j]!=buffr2[i][k]){
             flag=1;
             break;
           }
         }
         if(flag==0){
           n2++;
           strcpy(buffr1[n2],buffr2[i]);
         }
       }
       buffr1[0][0]=n2;
       write(p2[1],buffr1,sizeof(buffr1));
       exit(1);
       break;
     default: printf("In parent process (ID: %d)\n", pid);
       for(i=0;i<n;i++){
         printf("Enter the string %d:- ",i+1);
         scanf("%s",buffr1[i]);
       }
       write(p[1],buffr1,sizeof(buffr1));
       waitpid(pid,NULL,0);
       read(p2[0],buffr2,sizeof(buffr2));
       printf("In parent process (ID: %d)\n", pid);
       n=(int) buffr2[0][0];
       for(i=1;i<=n;i++)
         printf("Paliandrome %d:- %s\n",i,buffr2[i]);
       break;
  }
   close(p[0]);
   close(p[1]);
   return 0;
}

Parent send N numbers to child and child return back the prime numbers

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(){
   int pid,a,n,p[2],p2[2],i,j,buffr1[100],buffr2[100],flag,n2=0;
a = pipe(p);
   if(a == -1)
   {
       fprintf(stderr, "Pipe Failed.\n");
       return EXIT_FAILURE;
    }
   a=pipe(p2);
   if(a == -1)
    {
       fprintf(stderr, "Pipe Failed.\n");
       return EXIT_FAILURE;
    }
    pid = fork();
   switch(pid){
       case -1:perror("main: fork");
          exit(1);
       case 0: n=read(p[0],buffr2,sizeof(buffr2));
          printf("In child process (ID: %d)\n", pid);
          n=n/sizeof(int);
          for(i=0;i<n;i++){
             flag=0;
             if(buffr2[i]<=1)
                flag=1;
             for(j=2;j<=buffr2[i]/2;j++){
                if((buffr2[i]%j)==0)
                {
                   flag=1;
                   break;
                }
             }
             if(flag==0){
                buffr1[n2]=buffr2[i];
                n2++;
             }
          }
          write(p2[1],buffr1,n2*sizeof(int));
          exit(1);
          break;
       default: printf("In parent process (ID: %d)\n", pid);
          printf("Enter the limit:- ");
          scanf("%d",&n);
          for(i=0;i<n;i++){
             printf("Enter the element %d:- ",i+1);
             scanf("%d",&buffr1[i]);
          }
          write(p[1],buffr1,n*sizeof(int));
          waitpid(pid,NULL,0);
          printf("In parent process (ID: %d)\n", pid);
          n2=read(p2[0],buffr2,sizeof(buffr2));
          n2=n2/sizeof(int);
          for(i=0;i<n2;i++)
             printf("Prime Number %d:- %d\n",i+1,buffr2[i]);
          break;
       }
      close(p[0]);
       close(p[1]);
      return 0;
}

Sunday, July 31, 2011

Send N strings from parent to child process

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(){
   int pid,a,n,p[2],i,vv;
   char buffr1[100][100],buffr2[100][100];
   printf("Enter the limit:- ");
   scanf("%d",&n);
   a = pipe(p);
   if(a == -1)
   {
       fprintf(stderr, "Pipe Failed.\n");
       return EXIT_FAILURE;
    }
    pid = fork();
   switch(pid){
       case -1:perror("main: fork");
          exit(1);
       case 0: read(p[0],buffr2,sizeof(buffr2));
          printf("In child process (ID: %d)\n", pid);
          for(i=0;i<n;i++)
             printf("String %d:- %s\n",i+1,buffr2[i]);
          exit(1);
          break;
       default: printf("In parent process (ID: %d)\n", pid);
          for(i=0;i<n;i++){
             printf("Enter the string %d:- ",i+1);
             scanf("%s",buffr1[i]);
          }
          write(p[1],buffr1,sizeof(buffr1));
          waitpid(pid,NULL,0);
          break;
    }
    close(p[0]);
    close(p[1]);
    return 0;
}

Send N numbers from parent to child process

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(){
    int pid,a,n,p[2],i,buffr1[100],buffr2[100];
    a = pipe(p);
    if(a == -1)
    {
       fprintf(stderr, "Pipe Failed.\n");
       return EXIT_FAILURE;
    }
    pid = fork();
    switch(pid){
       case -1:perror("main: fork");
          exit(1);
       case 0: n=read(p[0],buffr2,sizeof(buffr2));
          printf("In child process (ID: %d)\n", pid);
          n=n/sizeof(int);
          for(i=0;i<n;i++)
             printf("Number %d:- %d\n",i+1,buffr2[i]);
          exit(1);
          break;
       default: printf("In parent process (ID: %d)\n", pid);
          printf("Enter the limit:- ");
          scanf("%d",&n);
          for(i=0;i<n;i++){
             printf("Enter the element %d:- ",i+1);
             scanf("%d",&buffr1[i]);
          }
          write(p[1],buffr1,n*sizeof(int));
          waitpid(pid,NULL,0);
          break;
   }
   close(p[0]);
    close(p[1]);
    return 0;
}

Tuesday, July 12, 2011

Read the contents of a file and store it in reverse order

#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <stdio.h>
#include <string.h>
int main(){
   int fd,fd2,count=-1;
   char buffer,dest[30],source[30],flag='y';
   printf("\nEnter the source file name:- ");
   gets(source);
   fd = open(source,O_RDWR);
   if(fd!=-1){
       printf("\nEnter the destination file name:- ");
       gets(dest);
       fd2 = open(dest,O_RDWR);
       if(fd2!=-1){
          printf("\nDestination file already present. Enter 'y' to overwrite ");
          flag=getchar();
      }
      if(flag=='y'){
         fd2 = creat(dest,O_RDWR|0666);
         while(lseek(fd,count,2)!=-1){
             count--;
             read(fd,&buffer,sizeof(char));
             write(fd2,&buffer,sizeof(char));
         }
      }
   }
   else
       printf("\nSource file not found");
   close(fd);
   return 0;
}

Read a filename and display its attributes

#include<sys/types.h>
#include<sys/stat.h>
#include<pwd.h>
#include<time.h>
#include<stdio.h>
int main(){
   int s;
   char source[30],flag='y';
    struct stat filestat;
    struct passwd *pwd;
    printf("\nEnter the file name:- ");
    gets(source);
    s=stat(source,&filestat);
    if(s!=-1){
       printf("\nOwner id:- %d",filestat.st_uid);
       if((pwd = getpwuid(filestat.st_uid)) != NULL)
          printf("\tname:- %s", pwd->pw_name);
       printf("\nFile Size :- %d bytes",filestat.st_size);
       printf("\nPermission :- %o",filestat.st_mode);
       printf("\nDate of creation :- %s",ctime(&filestat.st_ctime));
       printf("\nLast Modified Date :- %s",ctime(&filestat.st_mtime));
    }
   else
       printf("\nError!!!!");
    return 0;
}


simulate DOS 'copy' command in c

#include<stdio.h>
#include<stdlib.h>
int main()
{
char command[100],source[100],destination[100];
int ret;
printf("\nEnter the source file name ");
gets(source);
printf("\nEnter the destination file name ");
gets(destination);
sprintf(command,"cp %s %s",source,destination);
ret=system(command);
if(ret==0)
    printf("Successfully Copied from %s to %s\n",source,destination);
return 0;
}