Final
Final
You are tasked to write a C program that demonstrates the use of the fork()
system call in Linux.
The program should create multiple child processes and manage them correctly.
Follow the
instructions below:
1. Program Structure:
user).
user).
user).
2. Process Management:
o The parent process should wait for all three child processes to complete before
it
exits.
o Each child process should print its process ID (PID) and the result of its task.
o The parent process should print a message indicating it has successfully waited
for
4. Error Handling:
o Ensure that the fork() system call is handled appropriately, and the program
5. Bonus (Optional):
1. Open a text editor (e.g., nano) to create your C++ program. In the terminal, type the
following command to create a new C++ file:
bash
Copy code
nano fork_example.cpp
2. Paste the following C++ code into the editor (this code demonstrates the use of fork() to
create child processes that perform different tasks):
cpp
Copy code
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main() {
int N, M, X;
// Prompt user for inputs
printf("Enter a number N for sum calculation: ");
scanf("%d", &N);
return 0;
}
3. After pasting the code, press Ctrl + O to save the file, then press Enter to confirm.
Finally, press Ctrl + X to exit the nano editor.
Now, you need to compile the C++ program using G++ (the GNU C++ compiler).
bash
Copy code
g++ fork_example.cpp -o fork_example
bash
Copy code
./fork_example
mathematica
Copy code
Enter a number N for sum calculation: 5
Enter a number M for factorial calculation: 4
Enter a number X for Fibonacci series: 10
3. The program will create three child processes, and each will perform the respective tasks.
You will see output similar to this:
less
Copy code
Child 1 (PID: 12345): Sum from 1 to 5 is 15
Child 2 (PID: 12346): Factorial of 4 is 24
Child 3 (PID: 12347): Fibonacci series up to 10: 0 1 1 2 3 5 8
Parent (PID: 12344): All child processes have completed.
Error Handling
The program includes basic error handling to check for invalid inputs (like negative
numbers for the factorial and Fibonacci series).
If fork() fails, the program will output an error message and terminate gracefully.
cpp
Copy code
// welcome.cpp
#include <iostream>
using namespace std;
int main() {
cout << "Welcome to the C++ Program!" << endl;
return 0;
}
Save the file: Press Ctrl + O, then Enter, then Ctrl + X to exit.
Compile: g++ welcome.cpp -o welcome
Run: ./welcome
cpp
Copy code
// five_values.cpp
#include <iostream>
using namespace std;
int main() {
int values[5];
cout << "Enter 5 values:" << endl;
for (int i = 0; i < 5; i++) {
cin >> values[i];
}
return 0;
}
cpp
Copy code
// swap_floats.cpp
#include <iostream>
using namespace std;
int main() {
float a, b;
cout << "Enter two floating-point numbers: ";
cin >> a >> b;
cout << "Before swap: a = " << a << ", b = " << b << endl;
// Swapping
float temp = a;
a = b;
b = temp;
cout << "After swap: a = " << a << ", b = " << b << endl;
return 0;
}
cpp
Copy code
// area_of_circle.cpp
#include <iostream>
#include <cmath>
using namespace std;
int main() {
float radius;
cout << "Enter the radius of the circle: ";
cin >> radius;
return 0;
}
cpp
Copy code
// circumference_of_circle.cpp
#include <iostream>
#include <cmath>
using namespace std;
int main() {
float radius;
cout << "Enter the radius of the circle: ";
cin >> radius;
float circumference = 2 * M_PI * radius;
cout << "The circumference of the circle is: " << circumference << endl;
return 0;
}
This program calculates the area of a sector based on the radius and angle.
cpp
Copy code
// area_of_sector.cpp
#include <iostream>
#include <cmath>
using namespace std;
int main() {
float radius, angle;
cout << "Enter the radius of the circle: ";
cin >> radius;
cout << "Enter the angle of the sector in degrees: ";
cin >> angle;
return 0;
}
This program displays the multiplication table for a number entered by the user.
cpp
Copy code
// multiplication_table.cpp
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter a number to display its multiplication table: ";
cin >> num;
return 0;
}
This program collects the user's name, age, and address and displays it.
cpp
Copy code
// user_info.cpp
#include <iostream>
#include <string>
using namespace std;
int main() {
string name, address;
int age;
return 0;
}
This program finds the greatest of three values entered by the user.
cpp
Copy code
// greatest_of_three.cpp
#include <iostream>
using namespace std;
int main() {
int a, b, c;
cout << "Enter three numbers: ";
cin >> a >> b >> c;
int greatest = a;
if (b > greatest) greatest = b;
if (c > greatest) greatest = c;
cout << "The greatest number is: " << greatest << endl;
return 0;
}
10. Display the Maximum and Minimum Value from the Input
This program calculates the maximum and minimum values from three numbers.
cpp
Copy code
// max_min_values.cpp
#include <iostream>
using namespace std;
int main() {
int a, b, c;
cout << "Enter three numbers: ";
cin >> a >> b >> c;
int maximum = a;
int minimum = a;
return 0;
}
Description:
Linux uses configuration files to manage user accounts, their default settings, and permissions.
Important user-related configuration files include:
Commands:
bash
Copy code
sudo cat /etc/shadow
bash
Copy code
ls -la /etc/skel
Description:
Each user has a customizable environment stored in hidden configuration files in their home
directory, such as .bashrc, .profile, and .bash_profile.
Commands:
bash
Copy code
nano ~/.bashrc
bash
Copy code
echo "Welcome, $USER!" >> ~/.bashrc
2. Apply changes:
bash
Copy code
source ~/.bashrc
bash
Copy code
export PATH=$PATH:/new/path
3. Adding and Removing Users
Description:
The system administrator can add, modify, and delete users using various commands.
Commands:
bash
Copy code
sudo adduser irfan
bash
Copy code
sudo usermod -c "Irfan Karim, Administrator" irfan
3. Delete a user:
bash
Copy code
sudo deluser irfan
bash
Copy code
sudo deluser --remove-home irfan
bash
Copy code
sudo passwd irfan
Description:
LDAP is used to centralize authentication and user account management in large environments.
Commands:
bash
Copy code
ldapsearch -x -LLL -H ldap://ldap.example.com -b "dc=example,dc=com"
Description:
Commands:
bash
Copy code
ls /etc/pam.d/
2. Enable password complexity: Edit the PAM file for password quality:
bash
Copy code
sudo nano /etc/pam.d/common-password
Add:
bash
Copy code
password requisite pam_pwquality.so retry=3 minlen=8
To demonstrate a program that interacts with the Linux system, you can create a C++ program to
display user details.
cpp
Copy code
#include <iostream>
#include <cstdlib>
int main() {
// Execute the "whoami" command to get the current user
std::cout << "Current user: ";
system("whoami");
return 0;
}
bash
Copy code
g++ user_info.cpp -o user_info
3. Run:
bash
Copy code
./user_info
bash
Copy code
pwd
Output: Prints the present working directory.
bash
Copy code
ls -la ~
Output: Lists all files, including hidden files (those starting with .).
bash
Copy code
cd /tmp
bash
Copy code
mkdir ~/linux_practice
bash
Copy code
cd ~/linux_practice
bash
Copy code
touch example.txt
bash
Copy code
mv backup.txt archive.txt
9. Delete example.txt
Command:
bash
Copy code
rm example.txt
bash
Copy code
cd ~
File Viewing
csharp
Copy code
Linux is a powerful operating system.
The terminal gives you direct control.
Command:
bash
Copy code
echo -e "Linux is a powerful operating system.\nThe terminal gives you
direct control." > notes.txt
bash
Copy code
cat notes.txt
bash
Copy code
less notes.txt
bash
Copy code
head -n 2 notes.txt
bash
Copy code
tail -n 2 notes.txt
To integrate file handling in C++ and simulate viewing a file, here is an example program:
cpp
Copy code
#include <iostream>
#include <fstream>
#include <string>
int main() {
std::ofstream outfile("notes.txt");
outfile << "Linux is a powerful operating system.\n";
outfile << "The terminal gives you direct control.\n";
outfile.close();
std::ifstream infile("notes.txt");
std::string line;
return 0;
}
bash
Copy code
g++ file_viewing.cpp -o file_viewing
3. Run:
bash
Copy code
./file_viewing
This will display the content of notes.txt in the terminal. Let me know if you'd like assistance
with any specific task!
Step-by-Step Guide to Install Ubuntu
This guide will help you install Ubuntu, a popular Linux operating system, on your computer.
Follow the steps below to successfully complete the installation.
Prerequisites
1. System Requirements:
o 2 GHz dual-core processor or better.
o 4 GB of RAM or more.
o At least 25 GB of free storage space.
2. Bootable Media:
o A USB flash drive with at least 8 GB capacity or a DVD.
o Ubuntu ISO file (download from the official Ubuntu website).
3. Backup Your Data:
o Back up your important files, as installing Ubuntu may erase your existing
operating system and data.
4. BIOS/UEFI Access:
o Familiarize yourself with accessing BIOS/UEFI to modify boot order.
1. Keyboard Layout:
o Select your preferred keyboard layout and click "Continue."
2. Updates and Software:
o Choose whether to install updates and third-party software for graphics, Wi-Fi,
and additional media formats.
3. Installation Type:
o Erase Disk and Install Ubuntu: This will remove all existing data and operating
systems.
o Dual Boot (Install Alongside Existing OS): Install Ubuntu alongside Windows
or another OS.
o Custom Partitioning: Advanced users can create custom partitions.
4. Partition Configuration (If Needed):
o Recommended partitions:
/ (root): At least 20 GB.
swap: Equal to your RAM size (optional for SSDs).
/home: For personal files.
5. User Information:
o Set your name, computer name, username, and password.
bash
Copy code
sudo apt update && sudo apt upgrade
2. Install Additional Drivers: Open "Software & Updates," go to the "Additional Drivers"
tab, and install any proprietary drivers if needed.
3. Install Common Software:
bash
Copy code
sudo apt install build-essential git curl vim
4. Explore Ubuntu: Familiarize yourself with the desktop environment and available
applications.