Professional Documents
Culture Documents
To Illustrate The Use of Fork and Getpid SYSTEM CALLS: EX - NO:2
To Illustrate The Use of Fork and Getpid SYSTEM CALLS: EX - NO:2
NO:2
Aim :
Parent
Child1
Child2
Child3
Algorithm
#include<stdio.h>
void main()
int pid1,pid2,pid3;
if(pid1==0)
pid2=fork();
if(pid2==0)
pid3=fork();
if(pid1==0&&pid2==0&&pid3==0)
RESULT:
Thus the process id, value were printed by using the fork and getpid system calls.
AIM:
ALGORITHM:
1. Start the program.
//#include<unistd.h>
//#include<stdio.h>
//#include<sys/types.h>
int pid;
pid =fork();
if(pid<0)
else if(pid==0)
execlp("/bin/ls","ls",NULL);
else
wait(NULL);
printf("Child Complete");
}}
RESULT:
EX NO:3
To write a shell program to simulate file open, read and write operations.
ALGORITHM:
#include<stdio.h>
char *progname;
main(argc, argv)
int argc;
char *argv[ ];
char buf[BUFSIZE];
progname=argv[0];
if (argc != 3)
if (write(fd2, buf, n) != n)
exit(0);
RESULT:
EX.NO. 4
SIMULATION OF LS COMMAND
AIM:
ALGORITHM:
#include<stdio.h>
#include<sys/types.h>
#include<dirent.h>
main(argc,argv)
int argc;
char *argv[];
DIR *dirname;
dirname=opendir(argv[1]);
while(1)
rdir=readdir(dirname);
if(rdir==NULL)
closedir(dirname);
exit(0);
closedir(dirname);
exit(0);
}
RESULT:
GREP ()
#include<stdio.h>
#include<string.h>
void main()
FILE *fptr;
char ch;
int i;
fptr=fopen("input.txt","w");
scanf("%c",&ch);
while(ch!='$')
fprintf(fptr,"%c",ch);
scanf("%c",&ch);
fclose(fptr);
scanf("%s",p);
fptr=fopen("input.txt","r");
i=0;
while(!feof(fptr))
ch=getc(fptr);