unit-1 programms
unit-1 programms
Once the background process completes its execution, the parent process
should print a message to indicate that the background process has
finished. If the program encounters any issues while creating the
background process, it should print an appropriate error message.
Charlie needs to ensure that the parent process correctly waits for the
child process to finish before displaying the final message. If the fork ()
system call fails, the program should report this failure.
Note:
Use the fork() method.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <string.h>
int main() {
char message[MAX_MESSAGE_LENGTH];
return 0;
}
2 Problem Statement
Leka is studying process management in her Operating Systems course and needs to create a
program to understand process creation and communication. She needs to implement a
program that demonstrates the use of the fork () system call to manage processes.
Read two messages from the user: one for the parent process and one for the child process.
Create a child process using fork().
The child process should print the message intended for it.
The parent process should wait for the child process to finish and then print its own message.
if (p < 0) {
perror("fork failed");
exit(EXIT_FAILURE);
} else if (p == 0) {
printf("Child Process Message: %s\n", childMessage);
exit(EXIT_SUCCESS);
} else {
wait(NULL);
printf("Parent Process Message: %s\n", parentMessage);
}
}
int main() {
char parentMessage[MAX_MESSAGE_LENGTH],
childMessage[MAX_MESSAGE_LENGTH];
len = strlen(childMessage);
if (len > 0 && childMessage[len - 1] == '\n') {
childMessage[len - 1] = '\0';
}
forkexample(parentMessage, childMessage);
return 0;
}
1 Problem Statement
Neka, a junior software developer, is learning about process management. For her project,
she needs to simulate a parent-child process interaction using the fork () system call and
display fixed process IDs.
The program will prompt the user to enter an integer. It will then create a child process using
fork () that simulates its own process ID and the parent's process ID with predefined values.
The child process will output the integer received from the user, and after the child
completes, the parent process will show a message indicating that the child process has
finished.
"Child process:"
"Child Process ID: X" where X is the fixed child process ID (5678).
"Parent Process ID: Y" where Y is the fixed parent process ID (1234).
"Received integer: Z" where Z is the integer input value provided by the user.
Parent process output:
"Parent process:"
"Parent Process ID: X"
"Child process finished executing."
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main() {
int userInput;
if (pid < 0) {
// Fork failed
perror("fork");
exit(EXIT_FAILURE);
} else if (pid == 0) {
// Simulate child process with fixed PID
pid_t child_pid = FIXED_CHILD_PID;
pid_t parent_pid = FIXED_PARENT_PID;
printf("Child process:\n");
printf("Child Process ID: %d\n", child_pid);
printf("Parent Process ID: %d\n", parent_pid);
printf("Received integer: %d\n", userInput);
exit(EXIT_SUCCESS);
} else {
// Simulate parent process with fixed PID
pid_t parent_pid = FIXED_PARENT_PID;
wait(NULL); // Wait for the child process to complete
printf("Parent process:\n");
printf("Parent Process ID: %d\n", parent_pid);
printf("Child process finished executing.\n");
}
return 0;
}
2. Problem Statement
The child process should display the calculated product and factorial along
with simulated fixed process IDs. After the child process completes, the
parent process will output a message indicating the completion of the
child process. The process IDs used in the simulation are fixed values.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
long factorial(int n) {
if (n == 0 || n == 1)
return 1;
long result = 1;
for (int i = 2; i <= n; i++)
result *= i;
return result;
}
int main() {
int num1, num2;
if (pid < 0) {
perror("fork");
exit(EXIT_FAILURE);
} else if (pid == 0) {
long factorial_num1 = factorial(num1);
int product = num1 * num2;
pid_t child_pid = FIXED_CHILD_PID;
pid_t parent_pid = FIXED_PARENT_PID;
printf("Child process:\n");
printf("Child Process ID: %d\n", child_pid);
printf("Parent Process ID: %d\n", parent_pid);
printf("Product of %d and %d: %d\n", num1, num2, product);
printf("Factorial of %d: %ld\n", num1, factorial_num1);
exit(EXIT_SUCCESS);
} else {
pid_t parent_pid = FIXED_PARENT_PID;
wait(NULL);
printf("Parent process:\n");
printf("Parent Process ID: %d\n", parent_pid);
printf("Child process finished executing.");
}
return 0;
}