Exercise Number : 3.
a
Title: Imitate the action of “ls” command
Aim:
To execute a program for system calls which imitate the action of “ls” command in c program.
Algorithm:
Step1: Start the program
Step2: Declare the necessary header files,
Step3: Declare the structure
Step4: Open the file in read mode
Step5: Print the contents of the root directory
Step6: Close the file
Pseudocode:
struct dirent **namelist;
Declare int n,i,char pathname[100];
Use Function getcwd(pathname);
Calculate n = scandir(pathname, &namelist, 0, alphasort);
Check if(n < 0)
Print Error
else
Loop for(i=0; i<n; i++) if(namelist[i]->d_name[0] != '.')
namelist[i]->d_name
Exit
Output:
apache2 lock log mount node_modules programiz-oc [Link] secrets [Link].d shm swift-
[Link] systemd user
RESULT:
Thus, the C program to read the contents of the root directory is executed successfully.
Exercise Number : 3.b
Title: Use system calls to imitate the action of UNIX command "grep"
Aim:
To implement system calls to imitate the action of UNIX command "grep".
Algorithm:
Step1: Start the program.
Step2: Create struct dirent.
Step3: declare the variable buff and pointer dptr.
Step4: Get the directory name.
Step5: Open the directory.
Step6: Read the contents in directory and print it.
Step7: Close the directory.
Pseudocode:
Declare char fn[10], pat[10], temp[200];
Declare FILE *fp;
Assign fp = fopen(fn, "r")
Check while (!feof(fp))
Use Function fgets(temp, sizeof(fp), fp)
Check if (strcmp(temp, pat))
Print temp
Use Function fclose(fp);
Return 1
Output:
Enter file name : program.c
Enter the pattern: while
while (!feof(fp))
{
fgets(temp, sizeof(fp), fp);
if (strcmp(temp, pat))
printf("%s", temp);
}
Result:
Thus, the system call have been implemented successfully.
Exercise Number : 3.c
Title: Implement Process Life Cycle
Aim:
To implement Process Life Cycle in C Programming.
Algorithm:
Step1: Start the program.
Step2: Read the input from the command line.
Step3: Use fork() system call to create process, getppid() system call used to get the parent process
ID and getpid() system call used to get the current process ID
Step4: execvp() system call used to execute that command given on that command line argument
Step5: execlp() system call used to execute specified command.
Step6: Open the directory at specified in command line input.
Step7: Display the directory contents.
Step8: Stop the program.
Pseudocode:
Initiate Process pid_t pid;
Create Process pid = fork();
Check if(pid==0)
Invoke Function getpid()
Initialize int i=0;
Loop for(i=0;i<8;i++)
Print i
exit(0)
Check else if(pid > 0)
getpid()
Declare int status;
wait(&status);
Check else
exit(EXIT_FAILURE)
Return 0
Result:
Thus, the program for process life cycle is implemented successfully.