0% found this document useful (0 votes)
9 views7 pages

Ex2 and 3

The document illustrates basic UNIX commands and shell programming, detailing commands like ls, cd, and mkdir for file management. It also explains process management using system calls such as fork, exec, and wait, providing a sample C program that demonstrates these concepts. The output of the program shows the interaction between parent and child processes during execution.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views7 pages

Ex2 and 3

The document illustrates basic UNIX commands and shell programming, detailing commands like ls, cd, and mkdir for file management. It also explains process management using system calls such as fork, exec, and wait, providing a sample C program that demonstrates these concepts. The output of the program shows the interaction between parent and child processes during execution.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Illustrate UNIX commands and Shell Programming

Exp.no: 2

Date:

Aim:

To Illustrate UNIX commands and Shell Programming.

Study :

UNIX commands are a set of basic instructions that can be executed in a terminal window
to perform various tasks in a UNIX-based operating system, such as Linux and macOS.
Here are some common UNIX commands:

1. ls: Lists the files and directories in the current directory.


2. cd: Changes the current directory.
3. mkdir: Creates a new directory.
4. rmdir: Deletes an empty directory.
5. rm: Deletes a file.
6. touch: Creates a new empty file.
7. cp: Copies a file or directory.
8. mv: Moves or renames a file or directory.
9. cat: Displays the contents of a file.
10. echo: Writes text to the terminal or a file.
This script starts with a shebang line that specifies the path to the bash
interpreter, ech
followed by a comment that explains what the script o command is
does. The to print a message to the console. used

Shell programming is a way to automate tasks and perform operations on a


computer using a shell, which is a command-line interface that allows you to
interact with the operating system. The shell reads commands from the user,
executes them, and returns the results. Some common shell programs
include the Bourne shell (sh), the C shell (csh), and the Bourne-Again shell
(bash).
Result:
Process Management using System Calls: Fork, Exec, Getpid, Exit, Wait, Close

Exp.no: 3

Date:

Aim:

To Process Management using System Calls : Fork, Exec, Getpid, Exit, Wait, Close.

Algorithm:

Step 1: Declare a pid_t variable to store the process ID

Step 2: Call fork() to create a child process

Step 3: If fork() returns a negative value, print an error message and exit with an error code

Step 4: If fork() returns 0, the process is the child:

Step 4.1: Get the process ID of the child using getpid()

Step 4.2: Execute a command using execlp()

Step 5: If fork() returns a positive value, the process is the parent:

Step 5.1: Get the process ID of the parent using getpid()

Step 5.2: Call wait() to wait for the child process to complete

Step 5.3: Print a message to the console indicating that the child process has completed

Step 6: Call exit() to exit the program


Program :

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

int main() {
pid_t pid;

// Fork a child process


pid = fork();

if (pid < 0) { // Error handling


fprintf(stderr, "Fork failed\n");
exit(1);
}
else if (pid == 0) { // Child process
printf("I am the child process, pid=%d\n", getpid());

// Execute a command using exec


execlp("/bin/ls", "ls", NULL);
}
else { // Parent process
printf("I am the parent process, pid=%d\n", getpid());

// Wait for the child process to complete


wait(NULL);

printf("Child process has completed\n");


}

// Close the program


printf("Exiting program\n");
exit(0);
}
Output:

I am the parent process, pid=12345


I am the child process, pid=12346
file1.txt file2.txt program.c
Child process has completed
Exiting program

Result :

You might also like