Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

EX.

NO:2

TO ILLUSTRATE THE USE OF fork () AND


getpid () SYSTEM CALLS
FORK & GETPID

Aim :

To Create a process in the following hierarchy

Parent

Child1

Child2

Child3

Algorithm

1. Declare the necessary variables.


2. Parent process is the process of the program which is running.
3. Create the child1 process using fork() When parent is active.
4. Create the child2 process using fork() when child1 is active.
5. Create the child3 process using fork() when child2 is active.

FORK ( ) & GETPID ( )

#include<stdio.h>

void main()

int pid1,pid2,pid3;

printf("Parent id is %d and root id is %d\n",getpid(),getppid());


pid1=fork();

if(pid1==0)

printf("Process 1 id is %d and its parent id is %d\n",getpid(),getppid());

pid2=fork();

if(pid2==0)

printf("Process 2 id is %d and its parent id is %d\n",getpid(),getppid());

pid3=fork();

if(pid1==0&&pid2==0&&pid3==0)

printf("Process 3 id is %d and its parent id is %d\n",getpid(),getppid());

RESULT:
Thus the process id, value were printed by using the fork and getpid system calls.

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.

//#include<unistd.h>

//#include<stdio.h>

//#include<sys/types.h>

#include <stdio.h> /* printf, stderr, fprintf */

#include <unistd.h> /* _exit, fork */

#include <stdlib.h> /* exit */

#include <errno.h> /* errno */

main(int argc,char *argv[])

int pid;

pid =fork();

if(pid<0)

printf(stderr,"Fork failed \n");


exit(-1);

else if(pid==0)

execlp("/bin/ls","ls",NULL);

else

wait(NULL);

printf("Child Complete");

}}

RESULT:

Thus the execlp function were implemented

EX NO:3

TO SIMULATE FILE OPEN, READ, WRITE OPERATIONS


AIM:

To write a shell program to simulate file open, read and write operations.

ALGORITHM:

1. Start the program


2. Define the buffer size,Pmode value
3. The open function used to open the file and the creat function used to create the file
4. To read or write a file assumed to exist already.
5. The returned value fildes is a file-descriptor used to denote the file in subsequent calls that
read, write or otherwise manipulate the file.
6. The function open returns -1 if any error occurs; otherwise, it returns a valid open file-
descriptor.
7. If the file is brand new, creat creates it with the protection mode specified by the pmode
argument.
8. Stop the execution

OPEN READ WRITE

#include<stdio.h>

#define BUFSIZE 512

#define PMODE 0644

char *progname;

main(argc, argv)

int argc;

char *argv[ ];

int fd1, fd2, n;

char buf[BUFSIZE];

progname=argv[0];

if (argc != 3)

error("Usage: cp from to", progname);

if ((fd1 = open(argv[1], 0)) == -1)

error("cp: can't open %s", argv[1]);


if ((fd2 = creat(argv[2], PMODE)) == -1)

error("cp: can't create %s", argv[2]);

while ((n = read(fd1, buf, BUFSIZE)) > 0)

if (write(fd2, buf, n) != n)

error("cp: write error", (char *)0);

exit(0);

RESULT:

Thus the file operations were simulated

EX.NO. 4

SIMULATION OF LS COMMAND

AIM:

To write a program to simulate the ls command

ALGORITHM:

1. Start the program


2. To define a pointer to a structure ,dirname predefined structure DIR and another pointer to a
structure called preaddir
3. The directory is opened using the opendir() function.
4. In the while loop read each entry using the readdir() function which returns a pointer.
5. If no pointer is returned the program is exited, else the name of the entry is displayed.
6. The closedir() function closes this open directory
7. Stop the program.
LS ()

#include<stdio.h>

#include<sys/types.h>

#include<dirent.h>

main(argc,argv)

int argc;

char *argv[];

DIR *dirname;

struct dirent *rdir;

dirname=opendir(argv[1]);

while(1)

rdir=readdir(dirname);

if(rdir==NULL)

closedir(dirname);

exit(0);

printf("found entry in %s:%s\n",argv[1],rdir->d_name);

closedir(dirname);

exit(0);

}
RESULT:

Thus the ls command were simulated

GREP ()

#include<stdio.h>

#include<string.h>

void main()

FILE *fptr;

char ch;

int i;

char p[10], a[50];

fptr=fopen("input.txt","w");

printf("Enter the data to be stored in the file\n");

scanf("%c",&ch);

while(ch!='$')

fprintf(fptr,"%c",ch);

scanf("%c",&ch);

fclose(fptr);

printf("Enter the pattern to be searched");

scanf("%s",p);

fptr=fopen("input.txt","r");
i=0;

while(!feof(fptr))

ch=getc(fptr);

You might also like