Operating System
Operating System
sudo
Command: sudo
Description: Superuser Do. Allows a user to execute a command with superuser
privileges.
Example: sudo apt-get install firefox (installs Firefox browser with superuser
privileges)
2. ls
Command: ls
Description: List files and directories.
Options:
-l : displays a detailed list of files and directories
-a : displays all files and directories, including hidden ones
-d : displays only directories
Example: ls -l (displays a detailed list of files and directories)
3. pwd
Command: pwd
Description: Print Working Directory. Displays the current working directory.
Example: pwd (displays the current working directory)
4. mkdir
Command: mkdir
Description: Make Directory. Creates a new directory.
Options:
-p : creates parent directories if they don't exist
Example: mkdir myfolder (creates a new directory named "myfolder")
5. rmdir
Command: rmdir
Description: Remove Directory. Deletes an empty directory.
Example: rmdir myfolder (deletes the directory "myfolder" if it's empty)
6. rm
Command: rm
Description: Remove. Deletes files and directories.
Options:
-i : prompts for confirmation before deleting
-r : deletes directories and their contents recursively
Example: rm myfile (deletes the file "myfile")
7. cd
Command: cd
Description: Change Directory. Changes the current working directory.
Example: cd myfolder (changes the current working directory to "myfolder")
8. cp
Command: cp
Description: Copy. Copies files and directories.
Options:
-i : prompts for confirmation before overwriting
-r : copies directories and their contents recursively
Example: cp myfile myfolder (copies the file "myfile" to the directory "myfolder")
9. wc
Command: wc
Description: Word Count. Displays the number of lines, words, and characters in a
file.
Example: wc myfile (displays the number of lines, words, and characters in the file
"myfile")
10. mv
Command: mv
Description: Move or rename. Moves or renames files and directories.
Example: mv myfile myfolder (moves the file "myfile" to the directory "myfolder")
11. cmp
Command: cmp
Description: Compare. Compares the contents of two files.
Example: cmp file1 file2 (compares the contents of "file1" and "file2")
12. passwd
Command: passwd
Description: Password. Changes the password for a user account.
Example: passwd (changes the password for the current user account)
13. who
Command: who
Description: Who. Displays information about the users currently logged in to the
system.
Example: who (displays information about the users currently logged in)
14. uname
Command: uname
Description: Unix Name. Displays information about the system, including the
operating system name, version, and architecture.
Example: uname -a (displays detailed information about the system)
SYSTEM CALLS
ASSIGNMENT 4
1. Write a program to implement fork () system call.
The fork system call is used to create a new process, or child process, by duplicating an existing
process, or parent process, in Unix-based operating systems. The new process is an exact copy of the
parent process, with its own memory and address space.
Here are some things to know about the fork system call:
Return values: The fork system call returns a value to indicate the success or failure of the
operation:
o Negative value: The child process creation was unsuccessful
#include <stdio.h> #used for input and output function like printf scanf etc
#include <sys/types.h> #used for the declaration of fork
#include <unistd.h> #defines data-types used in system calls
int main() { #its entry point of the program
int pid = fork(); #fork() system call is used to create a new process
if (pid < 0) {
printf("Fork failed.\n");
} else if (pid == 0) {
printf("In Child process");
} else {
printf("In Parent process");
}
}
2. Write a program to implement wait () and exit () System Calls
The wait system call is used by a parent process to wait for the termination of a child process and
retrieve its exit status.
The exit system call is used by a process to terminate its execution and return an exit status to its
parent process.
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
main () {
int pid = fork(); #fork() creates a new child process.
if (pid < 0) {
printf("Fork failed.\n");
} else if (pid == 0) {
printf("Child process, PID: %d\n", getpid());
printf("Child is exiting.\n");
exit(0);
} else {
printf("Parent process, Child PID: %d\n", pid);
printf("Parent is waiting for the child to exit...\n");
wait(NULL); #waits for child process to terminate.
printf("Parent's wait is done.\n"); } }
The execv system call is used to execute a new program in the current process, replacing the current
program with a new one.
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h> #For process waiting functions
int main() {
pid_t pid; #Declare a variable to hold the process ID
pid = fork(); #Create a new process
if (pid < 0) {
perror("fork"); # Print an error message if fork fails
exit(1); #Exit the program with an error code
} else if (pid == 0) { #this block is executed by child process
char *argv[] = {"/bin/ls", "-l", NULL}; #Defines an array of strings argv to pass as arguments to
the execv() system call. The first element is the path to the ls command, the second element is the -
l option, and the third element is a null pointer to indicate the end of the argument list.
execv(argv[0], argv); #Replace the child process with the 'ls -l' command
perror("execv"); #Print an error message if execv fails
exit(1);
} else { #this block is executed by parent process
int status; #Variable to hold the exit status of the child
waitpid(pid, &status, 0); #Wait for the child process to finish
printf("Child process exited with status: %d\n", WEXITSTATUS(status));
#Extracts the exit status of a child process from the status argument.
}
return 0; #successful completion.
}
4. Write a program to implement the system calls open (), read (), write
() & close ().
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/watt.h>
#include<unistd.h>
int main(){
int fd;
char buffer [80];
static char message[]="hello";
fd=open("myfile.txt", O_RDWR | O_CREAT);
if(fd!=-1){
printf("myfile.txt opened with read/write access\n");
write(fd, message, sizeof(message));
lseek(fd,0,SEEK_SET);
read(fd, buffer, sizeof(message));
printf("%s was written to nyfile.txt\n", buffer);
close(fd);}
return 0; }
ping command options
The ping command has several options that can be used to customize its behaviour and
gather more information about the network connection. Here are some common options:
1. -c count: Specifies the number of packets to send.
Example:
ping -c 5 google.com
This command sends 5 packets to google.com.
2. -i interval: Specifies the interval between packets in seconds.
Example:
ping -i 2 google.com
This command sends packets to google.com with a 2-second interval between each packet.
3. -l preload: Specifies the number of packets to send initially.
Example:
ping -l 3 google.com
This command sends 3 packets to google.com initially, and then continues to send packets
until interrupted.
4. -n: Specifies that the output should only display numerical addresses, without resolving
hostnames.
ping -n google.com
This command displays the IP address of google.com instead of the hostname.
5. -q: Specifies quiet output, only displaying the summary at the end.
Example:
ping -q google.com
This command sends packets to google.com and only displays the summary at the end,
without showing each packet's response.
6. -s packetsize: Specifies the packet size in bytes.
Example:
ping -s 1024 google.com
This command sends packets of size 1024 bytes to google.com.
7. -t ttl: Specifies the time-to-live (TTL) for the packets.
Example:
ping -t 10 google.com
This command sets the TTL to 10 for the packets sent to google.com.
8. -v: Specifies verbose output, displaying more detailed information about each packet.
Example:
ping -v google.com
This command displays detailed information about each packet sent to google.com.
9. -w deadline: Specifies the deadline for the ping command to complete.
Example:
ping -w 10 google.com
This command sets a deadline of 10 seconds for the ping command to complete.
10. -4 or -6: Specifies the IP protocol version to use (IPv4 or IPv6).
Example:
ping -4 google.com
This command uses IPv4 to send packets to google.com.
Install a Package
1. Open a terminal and type sudo apt-get install
<package_name> (replace <package_name> with the name of the package you
want to install).
2. Press Enter to execute the command.
3. If prompted, enter your password to authenticate the installation.
4. The package will be downloaded and installed.
Example: sudo apt-get install firefox