Thursday, 29 September 2011


SIMULATION OF LS COMMAND

AIM:
To write a C program to simulate ls command.

ALGORITHM:

  1. Start the process.
  2. Create a directory entry using dirent structure.
  3. To define a pointer to a structure, dp predefined structure DIR and another pointer to a structure called ep.
  4. The directory is opened using the opendir() function.
  5. In the while loop read each entry using the readdir() function which returns a pointer.
  6. If no pointer is returned the program is exited, else it will list the files inside the directory.
  7. The closedir() function closes this open directory.
  8. Stop the process.

PROGRAM:
#include<stdio.h>
#include<dirent.h>
Struct dirent *dptr;
int main(int argc,char* argv[])
{
char buff[256];
DIR *dirp;
Dirp=opendir(“.”);
printf(“\n \n enter directory name”);
scanf(“%s”,buff);
if((disp=opendir(buff))==NULL)
{
printf(“error”);
exit(1);
}
while (dptr==readir(disp))
{
printf(“%s \n”,dptr->d_name);
}
closedir(dirp);
}
OUTPUT:
Enter directory name      ex3
a.out
ex1.c
ls
ex3a
ex1
ex3.c

RESULT:
 Thus the given C Program is executed and output is verified.

IMPLEMENTATION OF I/O SYSTEM CALLS
[open(),read(),write()]

AIM:
To write a c program to perform operations on files using LINUX system calls open(),read(),write()

ALGORITHM:

Step1: Start the process.
Step2: Declare and initialize an array variable.
Step3: Declare a file pointer.
Step4: Using the open() system call, open a file in a write mode and store it in a file
Pointer.
Step5: Using the write()system call, write the contents of an array to the file.
Step6: The written file is read with the help of read() system call. Then Close the file.
Step7: Stop the process.

PROGRAM:
#include<stdio.h>
#include<stdlib.h>
double d[10]={1.3,1.0,1.23,1.9,0.91,1.5,1.4,0.0,1.01,8.15};
int main( void)
{
int I;
FILE *fp1;
if((fp=fopen(“my.txt”,”wb”))==NULL)
{
printf(“Cannot open file \n”);
exit(1);
}
/* write the entire array in one step */
if(fwrite(d,sizeof(d),1,fp)!=1)
{
printf(“write error \n”);
exit(1);
}
fclose(fp);
if((fp=fopen(“my.txt”,”rb”))==NULL)
{
printf(“Cannot open file \n”);
exit(1);
}
/*clear the array */
for(i=0;i<10;i++)
d[i]=0.0;
/* read the entire array in one step */
if(fread(d,sizeof(d),1,fp)!=1)
{
printf(“read error \n”);
exit(1);
}
fclose(fp);
/*display the array */
for(i=0;i<10;i++)
printf(“%f”,d[i]);
return 0;
}

OUTPUT:
1.300001.1.000001.230001.90000000.9100001.5000001.40000000.000001.0100008.150000

RESULT:
Thus the given C Program is executed and output is verified.


TO DEMONSTRATE EXECLP ( ) FUNCTION

AIM:
To write a shell program to implement the execlp() function.

ALGORITHM:
1.      Start the program
2.      Obtain the pid value using fork() function.
3.      If pid <0 , then print fork failed.
4.      Else if pid = 0,execlp() function will invoked.
5.      Else print child process complete.
6.      Terminate the program.
PROGRAM:
#include<stdio.h>
main(int argc,char *argv[])
{
int pid;
pid=fork();
if(pid<0)
{
fprintf(stderr,”Fork failed \n”);
exit(-1);
}
elseif(pid==0)
{
execlp(“/bin/ls”,”ls”,NULL);
}
else
{
wait(NULL);
printf(“Child Complete”);
exit(0);
}

OUTPUT:
a.out fib.sh mov.c len.sh str.sh fact.sh copy.c cat.c even.c odd.sh child complete

RESULT
Thus the execlp functions were implemented

IMPLEMENTATION OF LINUX SYSTEM CALL
[stat()]
AIM:

To write a C program to know the status of the file using LINUX system call stat().

ALGORITHM:

Step1: Start the process.
Step2: Define a structure stat.
Step3: Declare two unsigned variables for storing the address and total size of the file
Step4: Specify the filename and check the status of file using stat() system call
Step5: Print the address and size of the file.
Step6: Stop the process.

PROGRAM:

#include<sys/stat.h>
#include<stdio.h>
int main()
{
Struct stat s;
unsigned long a;
unsigned long b;
if(stat(“test.c”,&s)==(-1))
{
perror(“Error:cannot stat file”);
exit(EXIT_FAILURE);
}
a=s.st_blksize;
b=s.st_size;
printf(“%u \t %u \n”,a,b);
return 0;
}

OUTPUT:

gcc stat.c
address:4096                          total size:52

IMPLMENTATION OF LINUX SYSTEM CALLS


IMPLMENTATION OF LINUX SYSTEM CALLS
[opendir (),readdir(),closedir()]
AIM:

To write a C program to access directories using LINUX system calls                           
opendir (),readdir(),closedir().

ALGORITHM:
  1. Start the process.
  2. Create a directory entry in the dirent structure and a directory variable
  3. Get the name of the directory to be opened
  4. Using the system call opendir(),open the given directory and store it in the directory variable.
  5. Read the contents of the directory using readdir()system call and print the list of files in the directory
  6. Close the directory after the read and write process
  7. Stop the process.

PROGRAM:
#include<Stdio.h>
#include<dirent.h>
Struct dirent *dptr;
int main(int argc,char *argv[])
{
char buff[256];
DIR *dirp;
printf(“\n \n enter directory name”);
scanf(“%s”,buff);
if ((disp=opendir(buff))==null);
{
printf(“error”);
exit(1);
} while (dptr=readir(disp))
{
printf(“%s\n”,dptr->d_name);
}
closedir(dirp);
}
OUTPUT:
Enter directory name   raju
Ashwini.c
maha.c

RESULT:
Thus the given C Program is executed and output is verified.

IMPLEMENTATION OF LINUX SYSTEM CALLS


AIM:
To write a C program using LINUX system calls (fork(),wait(),getpid()).

ALGORITHM:
Step1: Start the process.
Step2: Create a new process (parent process) using fork() system call.
Step3: Create a identical copy of parent process(child process) using fork () system call
Step4: Process id is created for both child and parent process using getpid() system call
Step5: The Child process is made to execute.
Step6: After the exiting of the child process parent process will get exit
Step7: Stop the process.

PROGRAM:
#include<stdio.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<unistd.h>
void parent ();
void child();
main ()
{
int r;
r=fork();
{
printf(“fork empty”);
}
if(r>0)
parent(r);
else
child(r);
}
void child()
{
int i;
printf(“child starts executing”);
for(i=0;i<5;i++)
{
printf(“\n child %d is executing %d time”,getpid(),i);
sleep(i);
}
printf(“child %d exit \n”,getpid());
}
void parent(pid_t child_pid)
{
printf(“\n Parent %d watiting for child %d to start the executing “, getpid(),child_pid);
printf(“\n”);
waitpid(child_pid,NULL,0);
printf(“Parent %d exit \n”,getpid());
}

OUTPUT:
Parent 269 watiting for child 70 to start the executing
Child starts executing
Child 270 is executing 0 time
Child 270 is executing 1 time
Child 270 is executing 2 time
Child 270 is executing 3 time
Child 270 is executing 4 time
Child 270 exit.
Parent 269 exit

RESULT:
 Thus the given C Program is executed and output is verified.

Wednesday, 28 September 2011

OOAD Lab manual

Click Here 

For Downloading OOAD or Case Tool Lab manual.