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