0% found this document useful (0 votes)
2 views5 pages

Sample Programs

The document contains several C and C++ programs demonstrating process creation and management using the fork() system call. It illustrates various scenarios such as creating child processes, handling orphan and zombie processes, executing commands with execvp, and performing calculations in parent and child processes. Each program includes print statements to show the process IDs and results of operations performed by the parent and child processes.

Uploaded by

by9880104690
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)
2 views5 pages

Sample Programs

The document contains several C and C++ programs demonstrating process creation and management using the fork() system call. It illustrates various scenarios such as creating child processes, handling orphan and zombie processes, executing commands with execvp, and performing calculations in parent and child processes. Each program includes print statements to show the process IDs and results of operations performed by the parent and child processes.

Uploaded by

by9880104690
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

#include <stdio.

h>

#include <unistd.h>

int main() {

pid_t pid;

pid = fork(); // create child process

if (pid < 0) {

printf("Fork failed!\n");

else if (pid == 0) {

printf("Child process created! PID = %d\n", getpid());

else {

printf("Parent process. PID = %d, Child PID = %d\n", getpid(), pid);

return 0;

#include <stdio.h>

#include <unistd.h>

int main() {

pid_t pid1, pid2;

pid1 = fork();

if (pid1 == 0) {

printf("I am first child, PID = %d\n", getpid());

return 0;

else {
pid2 = fork();

if (pid2 == 0) {

printf("I am second child, PID = %d\n", getpid());

return 0;

else {

printf("I am parent, PID = %d\n", getpid());

return 0;

#include <stdio.h>

#include <unistd.h>

#include <sys/wait.h>

int main() {

pid_t pid = fork();

if (pid == 0) {

printf("Child process is running. PID = %d\n", getpid());

sleep(3); // simulate some work

printf("Child process finished.\n");

else {

wait(NULL); // parent waits for child

printf("Parent process resumes after child finishes.\n");

return 0;

#include <stdio.h>
#include <unistd.h>

int main() {

pid_t pid = fork();

if (pid == 0) {

char *args[] = {"ls", "-l", NULL};

execvp(args[0], args);

printf("This will not be printed if execvp is successful.\n");

else {

wait(NULL);

printf("Parent: Child finished executing ls -l.\n");

return 0;

#include <stdio.h>

#include <unistd.h>

int main() {

pid_t pid = fork();

if (pid > 0) {

printf("Parent process exiting. PID = %d\n", getpid());

else {

sleep(5); // parent will exit first

printf("Child process becomes orphan. PID = %d, New Parent = %d\n", getpid(), getppid());

return 0;
}

#include <stdio.h>

#include <unistd.h>

int main() {

pid_t pid = fork();

if (pid == 0) {

printf("Child process finishing. PID = %d\n", getpid());

else {

sleep(20); // Parent does not call wait

printf("Parent is still alive, child is zombie.\n");

return 0;

// C++ program to demonstrate calculation in parent and


// child processes using fork()
#include <iostream>
#include <unistd.h>
using namespace std;

// Driver code
int main()
{
int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int sumOdd = 0, sumEven = 0, n, i;
n = fork();

// Checking if n is not 0
if (n > 0) {
for (i = 0; i < 10; i++) {
if (a[i] % 2 == 0)
sumEven = sumEven + a[i];
}
cout << "Parent process \n";
cout << "Sum of even no. is " << sumEven << endl;
}

// If n is 0 i.e. we are in child process


else {
for (i = 0; i < 10; i++) {
if (a[i] % 2 != 0)
sumOdd = sumOdd + a[i];
}
cout << "Child process \n";
cout << "\nSum of odd no. is " << sumOdd << endl;
}
return 0;
}
//result
Parent process
Sum of even no. is 30
Child process
Sum of odd no. is 25

You might also like