0% found this document useful (0 votes)
27 views8 pages

Solutions Slips To Execute

The document contains several C programming exercises demonstrating process management concepts such as creating child processes with fork(), using exec() to execute commands, managing process priorities with nice(), and handling orphan processes. Each exercise includes code snippets, expected outputs, and explanations of the behavior of parent and child processes. Additionally, it covers resource management topics like calculating need matrices and determining minimum resources to avoid deadlock.

Uploaded by

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

Solutions Slips To Execute

The document contains several C programming exercises demonstrating process management concepts such as creating child processes with fork(), using exec() to execute commands, managing process priorities with nice(), and handling orphan processes. Each exercise includes code snippets, expected outputs, and explanations of the behavior of parent and child processes. Additionally, it covers resource management topics like calculating need matrices and determining minimum resources to avoid deadlock.

Uploaded by

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

1. Create a child process using fork(), display parent and child process id.

Child process will display the message “Hello World” and the parent process should
display “Hi”.

Program:

#include <unistd.h> // POSIX functions: fork(), getpid(), getppid(),sleep(),


write(), and many low-level system calls
#include <sys/types.h> // Defines pid_t and other system types
#include <stdio.h> // Standard I/O functions like printf().
#include <sys/wait.h> // Process waiting functions like wait()

int main()
{
pid_t p;
printf("\n before fork\n");
p = fork();

if (p == 0) // child
{
printf("\nHello World");
printf("\n I am child having id %d\n", getpid());
printf("\n My parent's id is %d\n", getppid());
}
else // parent
{
printf("\n Hi");
printf("\n My child's id is %d\n", p);
printf("\n I am parent having id %d\n", getpid());
}
}

output:
before fork

Hello World
I am child having id 4575

My parent's id is 4574

Hi
My child's id is 4575

I am parent having id 4574

-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
----------------------------

Q.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.
->
• Create a child process using fork().
• Use the nice() system call to give the child process higher priority

Program:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h> // For exit(), general utilities

int main()
{
int pid, retnice; //stores return value from nice()

printf("press ^Z to stop process \n");

pid = fork();

for (;;) //forever loop. It will never exit unless: (Ctrl + C or Ctrl + Z)
{
if (pid == 0) // Child process
{
retnice = nice(-5); // Try to increase priority
printf("Child gets higher CPU priority %d \n", retnice);
sleep(1); // pauses the loop for 1 second.
}
else // Parent process
{
retnice = nice(4); // Lower priority
printf("Parent gets lower CPU priority %d \n", retnice);
sleep(1);
}
}
}

output:
press ^Z to stop process
Child gets higher CPU priority -5
Parent gets lower CPU priority 4
Child gets higher CPU priority -10
Parent gets lower CPU priority 8
Child gets higher CPU priority -15
Parent gets lower CPU priority 12
Child gets higher CPU priority -20
Parent gets lower CPU priority 16
Child gets higher CPU priority -20
Parent gets lower CPU priority 19

-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
----------------------------

Q. 3 Creating a child process using the command exec(). Note down process ids of
the parent and the child processes, check whether the control is given back to the
parent after the child process terminates.

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

int main() {
char* command = "ls";
char* argument_list[] = {"ls", "-l", NULL}; // command itself (1st
argument),option for long listing,signals the end of the argument list (required)

printf("Before calling execvp()\n");

// Calling the execvp() system call


int status_code = execvp(command, argument_list); // replaces the current
process image with a new process image.

if (status_code == -1) {
printf("Process did not terminate correctly\n");
exit(1);
}

printf("This line will not be printed if execvp() runs correctly\n");

return 0;
}

output:
Before calling execvp()
total 94000
-rw------- 1 root root 4258 Sep 29 2015 anaconda-ks.cfg
-rwxr-xr-x 1 root root 5104 Jul 3 11:36 a.out
-rwxr-xr-x 1 root root 5924214 Jul 8 2008 apache-tomcat-5.5.12.tar.gz
drwxr-xr-x 2 root root 4096 Oct 5 2019 Desktop
-rw-r--r-- 1 root root 1038 Jul 2 14:59 first.c
-rwxr-xr-x 1 root root 109217 Dec 15 2015 glut-3.7-8.i386.rpm
-rwxr-xr-x 1 root root 183093 Dec 15 2015 glut-devel-3.7-8.i386.rpm
-rw-r--r-- 1 root root 58603 Sep 29 2015 install.log
-rw-r--r-- 1 root root 8740 Sep 29 2015 install.log.syslog
-rw-r--r-- 1 root root 44855533 Sep 15 2004 jdk-1_5_0-linux-i586.rpm
-rwxr-xr-x 1 root root 44566787 Jul 8 2008 jdk-1_5_0-linux-i586-rpm.bin
drwxrwxrwx 4 root root 4096 Feb 14 2022 Linux Package
-rwxr-xr-x 1 root root 124786 Dec 15 2010 mm.mysql-2.0.13-bin.jar
-rw-r--r-- 1 root root 531 Jul 3 11:27 p1.c
-rw-r--r-- 1 root root 642 Jul 3 11:28 p2.c
-rw-r--r-- 1 root root 506 Jul 3 11:36 p3.c
-rwxr-xr-x 1 root root 210371 Jul 8 2008 pg74.216.jdbc3.jar
-rw-r--r-- 1 root root 531 Jul 3 10:54 prog.c
-rwxr-xr-x 1 root root 236 Dec 31 2004 ss

-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
----------------------------

Q. 4 Write a program to illustrate the concept of orphan process ( Using fork() and
sleep())

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>

int main()
{
pid_t p;
p = fork();

if (p == 0)
{
sleep(5); // child goes to sleep and in the meantime parent terminates
printf("I am child having PID %d\n", getpid());
printf("My parent PID is %d\n", getppid());
}
else
{
printf("I am parent having PID %d\n", getpid());
printf("My child PID is %d\n", p);
}

return 0;
}

output:
I am parent having PID 4639
My child PID is 4640
I am child having PID 4640
My parent PID is 1

-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
----------------------------

Q.5 Write a program to create a child process using fork().The parent should goto
sleep state and child process should begin its execution. In the child process, use
execl() to execute the “ls” command.

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

int main() {
char* command = "ls";
char* argument_list[] = {"ls", "-l", NULL};

printf("Before calling execvp()\n");


printf("Creating another process using fork()...\n");

if (fork() == 0) {
// Newly spawned child process. This will be taken over by "ls -l"
int status_code = execvp(command, argument_list);
printf("ls -l has taken control of this child process. This won't execute
unless it terminates abnormally!\n");

if (status_code == -1) {
printf("Terminated Incorrectly\n");
return 1;
}
} else {
// Old parent process. The C program will come here
sleep(10);
printf("This line will be printed\n");
}

return 0;
}

output:
Before calling execvp()
Creating another process using fork()...
total 94008
-rw------- 1 root root 4258 Sep 29 2015 anaconda-ks.cfg
-rwxr-xr-x 1 root root 5333 Jul 3 11:41 a.out
-rwxr-xr-x 1 root root 5924214 Jul 8 2008 apache-tomcat-5.5.12.tar.gz
drwxr-xr-x 2 root root 4096 Oct 5 2019 Desktop
-rw-r--r-- 1 root root 1038 Jul 2 14:59 first.c
-rwxr-xr-x 1 root root 109217 Dec 15 2015 glut-3.7-8.i386.rpm
-rwxr-xr-x 1 root root 183093 Dec 15 2015 glut-devel-3.7-8.i386.rpm
-rw-r--r-- 1 root root 58603 Sep 29 2015 install.log
-rw-r--r-- 1 root root 8740 Sep 29 2015 install.log.syslog
-rw-r--r-- 1 root root 44855533 Sep 15 2004 jdk-1_5_0-linux-i586.rpm
-rwxr-xr-x 1 root root 44566787 Jul 8 2008 jdk-1_5_0-linux-i586-rpm.bin
drwxrwxrwx 4 root root 4096 Feb 14 2022 Linux Package
-rwxr-xr-x 1 root root 124786 Dec 15 2010 mm.mysql-2.0.13-bin.jar
-rw-r--r-- 1 root root 531 Jul 3 11:27 p1.c
-rw-r--r-- 1 root root 642 Jul 3 11:28 p2.c
-rw-r--r-- 1 root root 506 Jul 3 11:36 p3.c
-rw-r--r-- 1 root root 483 Jul 3 11:39 p4.c
-rw-r--r-- 1 root root 806 Jul 3 11:41 p5.c
-rwxr-xr-x 1 root root 210371 Jul 8 2008 pg74.216.jdbc3.jar
-rw-r--r-- 1 root root 531 Jul 3 10:54 prog.c
-rwxr-xr-x 1 root root 236 Dec 31 2004 ss
This line will be printed

-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
----------------------------

Q. 6 Write a program to find the execution time taken for execution of a given set
of instructions (use clock() function)

#include <stdio.h>
#include <time.h>

int main() {
clock_t start, end;
double cpu_time_used;

start = clock(); // Start time

// Sample instructions (e.g., a simple loop)


int sum = 0;
int i;
for (i = 0; i < 1000000; i++) {
sum += i;
}

end = clock(); // End time

cpu_time_used = ((double)(end - start)) / CLOCKS_PER_SEC;

printf("Sum = %d\n", sum);


printf("Execution time = %f seconds\n", cpu_time_used);

return 0;
}
output:
Sum = 1783293664
Execution time = 0.000000 seconds

-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
----------------------------

Q.7 Write a C program to accept the number of process and resources and find the
need matrix content and display it.

#include <stdio.h>

int main() {
int process, resource,i,j;

printf("Enter number of processes: ");


scanf("%d", &process);
printf("Enter number of resources: ");
scanf("%d", &resource);

int max[10][10], alloc[10][10], need[10][10];

printf("\nEnter Max Matrix:\n");


for (i = 0; i < process; i++) {
for (j = 0; j < resource; j++) {
scanf("%d", &max[i][j]);
}
}

printf("\nEnter Allocation Matrix:\n");


for (i = 0; i < process; i++) {
for (j = 0; j < resource; j++) {
scanf("%d", &alloc[i][j]);
}
}

// Calculate Need Matrix


for (i = 0; i < process; i++) {
for (j = 0; j < resource; j++) {
need[i][j] = max[i][j] - alloc[i][j];
}
}

printf("\nNeed Matrix:\n");
for (i = 0; i < process; i++) {
for (j = 0; j < resource; j++) {
printf("%d ", need[i][j]);
}
printf("\n");
}

return 0;
}

output:
Enter number of processes: 2
Enter number of resources: 2
Enter Max Matrix:
7
7
7
8

Enter Allocation Matrix:


3
4
2
3

Need Matrix:
4 3
5 5

-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
----------------------------

Q.8 Write the program to calculate minimum number of resources needed to avoid
deadlock.
#include <stdio.h>

int main() {
int n,i, max[10];

printf("Enter number of processes: ");


scanf("%d", &n);

printf("Enter maximum resource need for each process:\n");


for ( i = 0; i < n; i++) {
printf("Process %d: ", i);
scanf("%d", &max[i]);
}

int total = 0;
for ( i = 0; i < n; i++) {
total += (max[i] - 1);
}

int min_resources = total + 1;

printf("Minimum number of resources to avoid deadlock: %d\n", min_resources);

return 0;
}

output:
Enter number of processes: 2
Enter maximum resource need for each process:
Process 0: 3
Process 1: 4
Minimum number of resources to avoid deadlock: 6

-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
----------------------------

You might also like