OS Lab6
OS Lab6
#include <iostream>
#include <unistd.h>
using namespace std;
int main()
{
pid_t childpid=fork();
if ( childpid<0){
cerr << "Fork Failed"<<endl;
} else if (childpid==0){
cout << "Child process (PID:"<< getpid() <<")is running"<<endl;
}else {
cout<<"Parent process(PID:" << getpid() <<")is running" <<endl;
}
Task 2:
Create a C++ program that demonstrates the use of the fork() system call to create 4,7,11 child
processes in separate files and then elaborate how many child and parent will be shown as
output. You have to tell the exact number of outputs for each sub task
#include <iostream>
#include <sys/types.h>
#include <unistd.h>
using namespace std;
int main() {
cout<< "Parent process ID: " << getpid() << endl;
int num1;
cout<<"HOw many child you want= ";
cin>> num1;
for (int i = 0; i < num1; i++) {
pid_t child_pid = fork();
if (child_pid == 0) {
cout << "Child process ID: " << getpid() << " is running" << endl;
return 0;
}
}
return 0;
}
Task 3:
Write a C++ program that uses the exec() system call (e.g., execl, execvp, etc.) to replace the
current process with a new process.
#include <iostream>
#include <unistd.h>
#include <sys/types.h>
using namespace std;
int main() {
cout << "I am in execv" << endl;
cout << "PID of execv: " << getpid() << endl;
const char *args[] = {"./home", nullptr};
execv(const_cast<char*>(args[0]), const_cast<char**>(args));
perror("execv");
return 0;
}
#include<iostream>
#include<unistd.h>
#include<sys/types.h>
using namespace std;
int main()
{
cout<<"I am college"<<endl;
cout<<"PID of college: "<<getpid()<<endl;
return 0;
}
Task 4:
Create a C++ program that uses the wait() system call to wait for a child process to exit. The
child process should execute a simple task, and the parent should print a message when the child
exits.
#include <iostream>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
using namespace std;
int main(){
pid_t p;
for (int i=0; i<5; i++){
p=fork();
if (p==0){
cout<<"child is created with id : "<<getpid()<<endl;
return 0;
}
else if (p>0){
wait(NULL);
cout<<"Child has exit"<<endl;
}
}
}
Task 5:
Extend the previous program to use the exit() system call in the child process to return an exit
status. The parent process should use the WIFEXITED and WEXITSTATUS macros to retrieve
and print the child's exit status.
Please explain what is change after using WEXITSTATUS.
#include <iostream>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
using namespace std;
int main(){
pid_t p;
for (int i=0; i<5; i++){
p=fork();
if (p==0){
cout<<"child is created with id : "<<getpid()<<endl;
exit(i+1);
}
else if (p>0){
int status;
wait(&status);
if(WIFEXITED(status)){
cout<<"child process ended normally"<<" status : "<<WEXITSTATUS(status)<<endl;
}
else{
cout<<"Child process not ended normally"<<endl;
}
}
}
}
WEXITSTATUS() basically print the status of execution it happens when the program
terminates properly which we can check using WIFEXITED().
Process Management Tasks:
Task 6:
Write a C++ program that uses the getpid() system call to retrieve and print the PID of the
current process. First make a program named “test” and then find the pid of this test file in new
program named “pid”.
#include <iostream>
#include <unistd.h>
int main() {
pid_t pid = getpid();
std::cout << "PID of task6test: " << pid << std::endl;
return 0;
}
Task 7:
Create a C++ program that uses the getppid() system call to retrieve and print the PID of the
parent process (PPID) of the current process.
#include <iostream>
#include <unistd.h>
int main() {
pid_t ppid = getppid();
std::cout << "Parent process PID (PPID): " << ppid << std::endl;
return 0;
Task 8:
Implement a C++ program that demonstrates the use of the kill() system call to send a signal
(e.g., SIGTERM, SIGINT) to a target process. The target process can be another program you
create. The program should handle the signal, and you should observe the behavior of the target
process when it receives the signal.
Please explain what is change after using SIGINT.
#include <iostream>
#include <csignal>
#include <cstdlib> // for std::exit
int main() {
pid_t pid = 2994;
int signal = SIGTERM;
if (kill(pid, signal) == 0) {
std::cout << "Signal sent successfully." << std::endl;
} else {
std::cerr << "Failed to send signal." << std::endl;
std::exit(1); // Exit with an error status
}
return 0;
}