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

Surya 23cs8077-1

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)
36 views10 pages

Surya 23cs8077-1

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

Potnuru Surya Teja 23CS8077 SEC - X

Assignment – 2

1. Write a C program that demonstrates the use of fork(), wait(), and sleep() system calls, incorporating a
menu-driven approach using a switch case. The program should: Display a menu to the user with the
following options:

• Create a single child process.

• Create multiple child processes (based on user input).

• Exit the program.

• Based on the selected option:

Option 1: • Create one child process using fork().

• The child process should: • Print its PID and its parent's PID.

• Sleep for 3 seconds and then print a completion message.

• The parent process should wait for the child to finish and then print a message indicating it has
terminated.

Option 2: • Ask the user how many child processes to create.

• Create that many child processes in a loop. • Each child process should:

• Print its PID and its parent's PID.

• Randomly determine a sleep time (between 1 and 5 seconds), simulate work using sleep(), and then exit
with a status based on its index.

• The parent process should:

• Wait for each child to finish and print its PID and exit status.

Option 3: • Exit the program with a goodbye message.

Code –

#include<stdio.h>

#include<stdlib.h>

#include<unistd.h>

#include<sys/wait.h>

void cprocess(){

printf("Child process :\n");

printf("PID : %d \n",getpid());

printf("PPID : %d \n",getppid()); }
Potnuru Surya Teja 23CS8077 SEC - X

void pprocess(){

wait(NULL);

printf("Parent process :\n");

printf("PID : %d \n",getpid()); }

int main(){

int c ;

int n;

printf("Enter \n 1 To create single child process \n 2 To create multiple child processes (based on user input)
\n 3 To exit the program \n ");

scanf("%d",&c);

pid_t k ;

switch(c){

case 1 : {

k = fork() ;

if(k < 0)

printf("Process failed!!\n");

else if(k == 0){

cprocess() ;

sleep(3) ;

printf("Child process is completed!!\n");

return 0;}

else{

pprocess();

printf("Parent process is terminated!!\n"); }

break; }

case 2 :{

printf("Enter the no. of child process to be created : \n ");

scanf("%d",&n);

for(int i =1;i<=n;i++){

k = fork() ;
Potnuru Surya Teja 23CS8077 SEC - X

if(k < 0)

printf("Process failed!!\n");

else if(k == 0){

cprocess() ;

sleep((rand()%5 + 1)) ;

printf("child process is completed!!\n");

return 0;}

else{

pprocess();

printf("Parent process is terminated!!\n"); } }

break; }

case 3 :{

printf("Goodbye!!\n");

exit(0);

break; }

default :

printf("Command not found!!\n"); }

return 0; }

Output –
Potnuru Surya Teja 23CS8077 SEC - X

2. Write a C program that demonstrates the use of fork(), getpid(), getppid(), sleep(), and wait() system calls
for creating and managing multiple child processes. The program should: a. b. Create a specified number of
child processes (from user input). Each child process should: • Print its own process ID (PID) and its parent's
process ID (PPID). • Randomly determine a work time (between 1 and 10 seconds), then simulate a task by
sleeping for that amount of time. • Print a message indicating that its work is done and exit with a status
corresponding to its loop index (child number). c. The parent process should: • Print its own PID and PPID at
the beginning. • Wait for all the child processes to terminate using wait(), and print which child has
terminated based on the exit status. d. After all child processes have terminated, the parent should print a
message indicating that it.

Code –

#include<stdio.h>

#include<stdlib.h>

#include<unistd.h>

#include<sys/wait.h>

void cprocess(){

printf("Child process :\n");

printf("PID : %d \n",getpid());

printf("PPID : %d \n",getppid()); }

void pprocess(){
Potnuru Surya Teja 23CS8077 SEC - X

printf("Parent process :\n");

printf("PID : %d \n",getpid());

wait(NULL); }

int main(){

pid_t k;

int n;

printf("Enter the no. of child process to be created : \n ");

scanf("%d",&n);

for(int i =1;i<=n;i++){

k = fork() ;

if(k < 0)

printf("Process failed!!\n");

else if(k == 0){

cprocess() ;

sleep((rand()%10 + 1)) ;

printf("child process - %d is completed!!\n",i);

return 0;}

else{

pprocess();

if(i==n){

printf("Parent process is terminated!!\n"); } } }

return 0; }

Output –
Potnuru Surya Teja 23CS8077 SEC - X

3. Write a C program that demonstrates the use of fork(), malloc(), and wait() system calls to create a
parent-child process interaction. The program should: • Allocate a dynamically created array of integers
using malloc(). • Initialize the array in both the parent and child processes with different values. • Print the
memory addresses of both statically and dynamically allocated arrays in both processes. • Ensure that the
child process and parent process sleep for a random number of seconds, and display their corresponding
sleep times. • After waking up, each process should print the contents of both the statically and dynamically
allocated arrays. • Use wait() to make sure the parent process waits for the child to finish before it exits. •
Free the dynamically allocated memory before exiting.

Code –

#include<stdio.h>

#include<stdlib.h>

#include<unistd.h>

#include<sys/wait.h>

int main(){

pid_t k;

int n;

k = fork();

int i;

int arr[5];

int *arr1;

if(k < 0)

printf("Process failed!!\n");

else if(k == 0){

printf("Child process :\n");

printf("Enter the elements of arr (static array) :\n");

for(i = 0; i < 5; i++){

scanf("%d", &arr[i]); }

printf("Enter the number of elements to be inserted in dynamically created array : \n");

scanf("%d", &n);

arr1 = (int*)malloc(n * sizeof(int));

for(i = 0; i < n; i++){

scanf("%d", &arr1[i]); }
Potnuru Surya Teja 23CS8077 SEC - X

printf("Elements of static array :\n");

for(i = 0; i < 5; i++){

printf(" %d ", arr[i]); }

printf("\n");

printf("Address of static array : %p \n", arr);

printf("Elements of dynamic array :\n");

for(i = 0; i < n; i++){

printf(" %d ", arr1[i]); }

printf("\n");

printf("Address of dynamic array : %p \n", arr1);

free(arr1); // Free dynamically allocated memory }

else{

wait(NULL);

printf("Parent process : \n");

printf("Enter the elements of arr (static array) :\n");

for(i = 0; i < 5; i++){

scanf("%d", &arr[i]); }

printf("Enter the number of elements to be inserted in dynamically created array : \n");

scanf("%d", &n);

arr1 = (int*)malloc(n * sizeof(int));

for(i = 0; i < n; i++){

scanf("%d", &arr1[i]); }

printf("Elements of static array :\n");

for(i = 0; i < 5; i++){

printf(" %d ", arr[i]); }

printf("\n");

printf("Address of static array : %p \n", arr);

printf("Elements of dynamic array :\n");

for(i = 0; i < n; i++){

printf(" %d ", arr1[i]); }


Potnuru Surya Teja 23CS8077 SEC - X

printf("\n");

printf("Address of dynamic array : %p \n", arr1);

free(arr1); // Free dynamically allocated memory }

return 0;}

Output -

4. Write a suitable code to speedup finding all prime numbers in a given range [1, N], creating M child
processes by the parent process. First try to run it for two processes then generalize it for M child processes.
Check the time of execution by recording the clock time before and after the computation is over. Display
the difference in time.

Code -

#include <stdio.h>

#include <stdlib.h>
Potnuru Surya Teja 23CS8077 SEC - X

#include <math.h>

#include <unistd.h>

#include <sys/wait.h>

#include <time.h>

#define MAX_PRIME_RANGE 1000000

int is_prime(int num) {

if (num <= 1) {

return 0; }

for (int i = 2; i <= sqrt(num); i++) {

if (num % i == 0) {

return 0; } }

return 1;}

void find_primes(int start, int end) {

for (int i = start; i < end; i++) {

if (is_prime(i)) {

printf("%d ", i); } }}

int main() {

int N, M;

printf("Enter the value of N: ");

scanf("%d", &N);

printf("Enter the number of child processes M: ");

scanf("%d", &M);

clock_t start_time = clock();

int step = N / M;

pid_t pid;

for (int i = 0; i < M; i++) {

int start = i * step + 1;

int end = (i == M - 1) ? N + 1 : (i + 1) * step + 1; // Last process takes the remainder of the range

pid = fork();

if (pid < 0) {
Potnuru Surya Teja 23CS8077 SEC - X
perror("Fork failed");

exit(1); }

if (pid == 0) {

find_primes(start, end);

exit(0); } }

for (int i = 0; i < M; i++) {

wait(NULL);

clock_t end_time = clock();

double time_taken = ((double)(end_time - start_time)) / CLOCKS_PER_SEC;

printf("\nExecution time with %d processes: %f seconds\n", M, time_taken);

return 0;

Output -

You might also like