Friday, 22 November 2013

To send N numbers from a parent process to child process and display them.

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

REVERSE FILE CONTENT

#include <stdio.h>
#include <stdlib.h>

int main()
{
   char ch, source_file[20], dest_file[20],temp[500];
int count = 0,i;
   FILE *source, *dest;

   printf("Enter name of file to read :  ");
   gets(source_file);

   source = fopen(source_file, "r");

   printf("Enter name of target file where you want to store reversed characters :  ");
   gets(dest_file);

   dest = fopen(dest_file, "w");
int j=0;
   while( ( ch = fgetc(source) ) != EOF )
{
count += 1;
temp[j] = ch;
j++;
}

i=count-1;
while(i>-1)
{
fputc(temp[i], dest);
i--;
}

   printf("File copied successfully.\n");

   fclose(source);
   fclose(dest);

   return 0;
}

DISPLAY FILE ATTRIBUTES

#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: %ld bytes",filestat.st_size);
    printf("\nFile Permission (Octal Notation): %o", filestat.st_mode);
   
printf("\nFile Permissions (Symbolic Notation): \t");
printf( (S_ISDIR(filestat.st_mode))      ?    "d"  : "-" );
printf( (filestat.st_mode & S_IRUSR)  ?    "r"   : "-" );
printf( (filestat.st_mode & S_IWUSR) ?    "w" : "-" );
printf( (filestat.st_mode & S_IXUSR)  ?    "x"  : "-" );
printf( (filestat.st_mode & S_IRGRP)  ?    "r"   : "-" );
printf( (filestat.st_mode & S_IWGRP) ?    "w" : "-" );
printf( (filestat.st_mode & S_IXGRP)  ?    "x"  : "-" );
printf( (filestat.st_mode & S_IROTH)  ?    "r"  : "-" );
printf( (filestat.st_mode & S_IWOTH) ?    "w" : "-" );
printf( (filestat.st_mode & S_IXOTH)  ?   "x"  : "-" );

    printf("\nDate of creation: %s",ctime(&filestat.st_ctime));
    printf("Last Modified Date: %s",ctime(&filestat.st_mtime));
  printf("\n");
    }
    else
        printf("\nError!!!");
    return 0;
}

REPLICATING DOS COPY COMMAND

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{

if(argc!=3)
{
printf("Not enough arguments... :P ");
exit(0);
}
   char ch;
   FILE *source, *dest;

   source = fopen(argv[1], "r");

   dest = fopen(argv[2], "w");

   while( ( ch = fgetc(source) ) != EOF )
      fputc(ch, dest);

   printf("File copied successfully.\n");

   fclose(source);
   fclose(dest);

   return 0;
}


TO RUN THE PROGRAM:
gcc program_name.c -o cpy
./cpy source_file_name dest_file_name

------------------------------------------------------------------------------------------------------------

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

TO RUN THE PROGRAM:
gcc program_name.c -o cpy
./cpy

Thursday, 21 November 2013

S7 CSE LAB SCHEDULE (EXTERNAL LAB EXAM)



Is there any difficulty in looking in picture, Download PDF here: LP LAB & NOS LAB

NOTE: Subject codes are incorrect... It won't be a problem for regular students... Supplementary students please re-check the schedule on 23rd evening. Thank-you.

Subject