0% found this document useful (0 votes)
7 views10 pages

Assignment 1 OS

Uploaded by

rayanshinde206
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views10 pages

Assignment 1 OS

Uploaded by

rayanshinde206
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Assignment No.

1
Set A.1) Implement the C Program to create a child process using fork(), display parent and
child process id. Child process will display the message “I am Child Process” and the parent
process should display “I am Parent Process”
CODE:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main() {
pid_t pid;
// Create a child process
pid = fork();
if (pid < 0) {
// fork() failed
printf("Fork failed!\n");
return 1;
}
else if (pid == 0) {
// Child process
printf("I am Child Process\n");
printf("Child PID: %d\n", getpid());
printf("Parent PID (from child): %d\n", getppid());
}
else {
// Parent process
printf("I am Parent Process\n");
printf("Parent PID: %d\n", getpid());
printf("Child PID (from parent): %d\n", pid);
}
return 0;
}
Output:
Set A.2) Write a program that demonstrates the use of nice() system call. After a child process is
started using fork(), assign higher priority to the child using nice() system call.

CODE:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
int main() {
pid_t pid;
pid = fork();
if (pid < 0) {
perror("fork failed");
exit(1);
}
if (pid == 0) {
// Child process
printf("Child process before nice, PID = %d\n", getpid());
// Assign higher priority (lower nice value)
int ret = nice(-5); // decrease nice value by 5
if (ret == -1 && errno != 0) {
perror("nice failed");
} else {
printf("Child process new nice value = %d\n", ret);
}
for (int i = 0; i < 5; i++) {
printf("Child running iteration %d\n", i + 1);
sleep(1);
}
exit(0);
} else {
// Parent process
printf("Parent process, PID = %d\n", getpid());
for (int i = 0; i < 5; i++) {
printf("Parent running iteration %d\n", i + 1);
sleep(1);
}
wait(NULL);
printf("Parent: Child finished execution\n");
}
return 0;
}
Output:
Set B.1) Implement the C program to accept n integers to be sorted. Main function creates child
process using fork system call. Parent process sorts the integers using bubble sort and waits for
child process using wait system call. Child process sorts the integers using insertion sort.

CODE:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
// Bubble Sort
void bubbleSort(int arr[], int n) {
int i, j, temp;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
// Insertion Sort
void insertionSort(int arr[], int n) {
int i, key, j;
for (i = 1; i < n; i++) {
key = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
// Print Array
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int n, i;
pid_t pid;
printf("Enter number of integers: ");
scanf("%d", &n);
int arr[n], arr_copy[n];
printf("Enter %d integers:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
arr_copy[i] = arr[i]; // copy for child process
}
pid = fork();
if (pid < 0) {
perror("Fork failed");
exit(1);
}
else if (pid == 0) {
// Child Process
printf("\nChild Process (PID = %d) - Insertion Sort:\n", getpid());
insertionSort(arr_copy, n);
printArray(arr_copy, n);
}
else {
// Parent Process
wait(NULL); // wait for child to finish
printf("\nParent Process (PID = %d) - Bubble Sort:\n", getpid());
bubbleSort(arr, n);
printArray(arr, n);
}
return 0;
}
Output:
Set B.2) Write a C program to illustrate the concept of orphan process. Parent process creates a
child and terminates before child has finished its task. So child process becomes orphan process.
(Use fork(), sleep(), getpid(), getppid()).

CODE:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
int main() {
pid_t pid;
pid = fork(); // create child
if (pid < 0) {
perror("fork failed");
exit(1);
}
else if (pid == 0) {
// Child process
printf("Child process started. PID = %d, Parent PID = %d\n", getpid(), getppid());
// Sleep so that parent terminates first
sleep(5);
// After parent dies, child's parent becomes 'init' (PID = 1)
printf("Child process still running. PID = %d, New Parent PID = %d\n", getpid(), getppid());
}
else {
// Parent process
printf("Parent process (PID = %d) is terminating before child.\n", getpid());
exit(0); // Parent terminates early }
return 0;}
Output:
Set C.1) Implement the C program that accepts an integer array. Main function forks child
process. Parent process sorts an integer array and passes the sorted array to child process
through the command line arguments of execve() system call. The child process uses execve()
system call to load new program that uses this sorted array for performing the binary search to
search the particular item in the array.

CODE:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
// Bubble sort
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
// Binary search
int binarySearch(int arr[], int n, int key) {
int low = 0, high = n - 1, mid;
while (low <= high) {
mid = (low + high) / 2;
if (arr[mid] == key) return mid;
else if (arr[mid] < key) low = mid + 1;
else high = mid - 1;
}
return -1;
}
// Function for child process (invoked via execve)
int child_main(int argc, char *argv[]) {
int n = atoi(argv[1]);
int arr[n];
for (int i = 0; i < n; i++) {
arr[i] = atoi(argv[i + 2]);
}
printf("\n[Child] Received sorted array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
int key;
printf("[Child] Enter element to search: ");
scanf("%d", &key);
int result = binarySearch(arr, n, key);

if (result != -1)
printf("[Child] Element %d found at index %d\n", key, result);
else
printf("[Child] Element %d not found in array\n", key);
return 0;
}
int main(int argc, char *argv[]) {
// Check if this is child re-execution
if (argc > 1 && strcmp(argv[0], "./self") == 0 && strcmp(argv[1], "child") == 0) {
// Adjust argv to pass only array args to child_main
return child_main(argc - 1, &argv[1]);
}
int n;
printf("Enter number of integers: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d integers:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Sort array
bubbleSort(arr, n);
printf("[Parent] Sorted Array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
pid_t pid = fork();
if (pid < 0) {
perror("fork failed");
exit(1);
}
else if (pid == 0) {
// Child process will re-exec same program
char *args[n + 4];
args[0] = "./self"; // program name
args[1] = "child"; // flag so we know we're in child_main
char n_str[10];
sprintf(n_str, "%d", n);
args[2] = n_str;
for (int i = 0; i < n; i++) {
char *num = malloc(20);
sprintf(num, "%d", arr[i]);
args[i + 3] = num;
}
args[n + 3] = NULL;
execve("./self", args, NULL);
perror("execve failed");
exit(1);
}
else {
// Parent waits for child
wait(NULL);
printf("[Parent] Child process finished.\n");
}
return 0;
}

Output:
Set C.2) Implement the C Program to create a child process using fork(), Using exec() system
call, child process will execute the program specified in Set A(1) and parent will continue by
printing message “I am parent”.

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]) {
pid_t pid;
// If run with argument "childmode", act as the child program
if (argc > 1 && strcmp(argv[1], "childmode") == 0) {
printf("I am Child Process\n");
printf("Child PID: %d\n", getpid());
printf("Parent PID: %d\n", getppid());
return 0;}
// Otherwise, this is the parent starting point
pid = fork();
if (pid < 0) {
perror("fork failed");
exit(1);
}
else if (pid == 0) {
// Child: replace itself with the same program in "childmode"
execl("./combined", "combined", "childmode", NULL);
// If exec fails
perror("execl failed");
exit(1);}
else {
// Parent continues here
printf("I am parent (PID = %d)\n", getpid());}
return 0;}
Output:

You might also like