Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

K. J.

Somaiya College of Engineering, Mumbai-77


(An Autonomous College Affiliated to University of Mumbai)

Batch: B4 Roll No.: 1913121


Experiment / assignment / tutorial No.8
Grade: AA / AB / BB / BC / CC / CD /DD

Signature of the Staff In-charge with date

TITLE: Process system call


AIM: Shell scripts programs on Process system call ()

OUTCOME: Student can Compare different algorithms used for management and
scheduling of processes.

Program for System Call:

1. Write a Program for creating process using System call (E.g., fork ())
Create a child process. Display the details about that process using
getpid and getppid functions. In a child process, Open the file using file
system calls and read the contents and display.

• Use any online C editor/IDE for the above program. click

• Explore the working of the fork () system call and document it with examples

Fork system call is used for creating a new process, which is called child process, which
runs concurrently with the process that makes the fork () call (parent process). After a
new child process is created, both processes will execute the next instruction following
the fork () system call. A child process uses the same pc(program counter), same CPU
registers, same open files which use in the parent process.
It takes no parameters and returns an integer value. Below are different values
returned by fork ().
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
// make two process which run same
// program after this instruction
fork();

printf("Hello world!\n");
return 0;
}

Department of Electronics and Telecommunication Engineering

OS/Sem V/ Aug- Dec 20 Page No


K. J. Somaiya College of Engineering, Mumbai-77
(An Autonomous College Affiliated to University of Mumbai)

OUTPUT:
CODE:
#include <stdio.h>
#include <sys/types.h>
int main()
{
fork();
fork();
fork();
printf("hello\n");
return 0;
}
OUTPUT:

• Explore the working of the functions getpid (), getppid ()

1. getppid() : returns the process ID of the parent of the calling process. If the calling
process was created by the fork() function and the parent process still exists at the
time of the getppid function call, this function returns the process ID of the parent
process. Otherwise, this function returns a value of 1 which is the process id for
init process.

Code:
// C++ Code to demonstrate getppid()
#include <iostream>
#include <unistd.h>
using namespace std;

// Driver Code
int main()
{
Department of Electronics and Telecommunication Engineering

OS/Sem V/ Aug- Dec 20 Page No


K. J. Somaiya College of Engineering, Mumbai-77
(An Autonomous College Affiliated to University of Mumbai)
int pid;
pid = fork();
if (pid == 0)
{

Department of Electronics and Telecommunication Engineering

OS/Sem V/ Aug- Dec 20 Page No


K. J. Somaiya College of Engineering, Mumbai-77
(An Autonomous College Affiliated to University of Mumbai)
cout << "\nParent Process id : "
<< getpid() << endl;
cout << "\nChild Process with parent id : "
<< getppid() << endl;
}
return 0;
}
Output:

2. getpid() : returns the process ID of the calling process. This is often used by
routines that generate unique temporary filenames.

CODE:
// C++ Code to demonstrate getpid()
#include <iostream>
#include <unistd.h>
using namespace std;

// Driver Code
int main()
{
int pid = fork();
if (pid == 0)
cout << "\nCurrent process id of Process : "
<< getpid() << endl;
return 0;
}

OUTPUT:

• Corresponding Header files needed.


1. #include <sys/types.h>
2. #include <unistd.h>
3. #include <sys/wait.h>

• Use the Value returned by the fork system call, to create if, elseif and else block, If for
system call failure, elseif for child block, else for parent process block. Further perform
assigned task as per question.

CODE:
Department of Electronics and Telecommunication Engineering

OS/Sem V/ Aug- Dec 20 Page No


K. J. Somaiya College of Engineering, Mumbai-77
(An Autonomous College Affiliated to University of Mumbai)
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>

Department of Electronics and Telecommunication Engineering

OS/Sem V/ Aug- Dec 20 Page No


K. J. Somaiya College of Engineering, Mumbai-77
(An Autonomous College Affiliated to University of Mumbai)
#include <sys/wait.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
printf("I am: %d\n", (int) getpid());
pid_t pid = fork();
printf ("fork returned: %d\n", (int) pid);
if (pid < 0) { /* error occurred */
perror("Fork failed");
}
if (pid == 0) { /* child process */
printf("I am the child with pid %d\n", (int) getpid());
printf("Child process is exiting\n");
exit(0);
}
/* parent process */
printf("I am the parent waiting for the child process to
end\n");
wait(NULL);
printf("parent process is exiting\n");
return(0);
}

OUTPUT:

CODE:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
pid_t retval;
retval= fork();
if (retval == 0)
printf("This is the child process. My pid is %d and my

Department of Electronics and Telecommunication Engineering

OS/Sem V/ Aug- Dec 20 Page No


K. J. Somaiya College of Engineering, Mumbai-77
(An Autonomous College Affiliated to University of Mumbai)
parent's id is %d.\n", getpid(),getppid());
else

Department of Electronics and Telecommunication Engineering

OS/Sem V/ Aug- Dec 20 Page No


K. J. Somaiya College of Engineering, Mumbai-77
(An Autonomous College Affiliated to University of Mumbai)
printf("This is the parent process. My pid is %d and my
parent's id is %d.\n", getpid(), getppid() );
printf("Hello world!\n");
return 0;
}
OUTPUT:

• Save the screenshot of the code and output of the above program.

Signature of faculty in-charge

Department of Electronics and Telecommunication Engineering

OS/Sem V/ Aug- Dec 20 Page No

You might also like