Invoke Child And Parent Process

Print   

02 Nov 2017

Disclaimer:
This essay has been written and submitted by students and is not an example of our work. Please click this link to view samples of our professional work witten by our professional essay writers. Any opinions, findings, conclusions or recommendations expressed in this material are those of the authors and do not necessarily reflect the views of EssayCompany.

Student’s Name

Reg. No.

Date of submission

: C. W. S. Goonetilleke

: 409064899

: 04/03/2013

AIM: Write a C program using the fork () system call that generates the Fibonacci sequence in the child process in Linux.

OBJECTIVES: - Familiar with process.

- Invoke child and parent process.

- Familiar with Linux and windows system calls.

THEORY:

Process in operating systems

A running instance of a program can be identified as a process. It includes the current activities, states of the program execution and several information that belongs to program execution. When process execute, executable file is loaded into memory area that normally called a process address space.

Child process and a parent process

Parent process is the process that create another process and the created process by parent process is named as the child process. Child process inherits most of parent process’s attributes, such as file descriptors. Each process may create more than one child processes. But child process may only has single parent process. Process does not have a parent, when it was created directly by the kernel. When parent process terminated, its child process will be leaving as an orphan process. But after child process become orphan it will be adopted by the kernel process.

Structure of a process in memory after an executable file is loaded in to memory

After loading process into memory its address space is divided into three segments as follows.

Text segment: the area in which the executable or binary image instructions reside.

Data segment: the area which statically allocated and global data.

Stack segment: the area where local variables are allocated.

Process states changing

http://www.d.umn.edu/~gshute/os/images/process-diagram.png

After creation of process it admitted into the ready state. Ready state process has all of the resources that it needs for further execution. Ready state process is normally held in a ready queue until a processor becomes available. When processor become available process dispatch. Then it became to running state and execute its instructions. If any time out occurred process again back to Ready state. Anyway if required resource blocked by another process or process needs to response from resource, process become to block state. It still hold in block state until the required resource available. Afterwards process become Ready state again and waiting for processor becomes available. After completing instruction process terminated and release resources if holds any.

Process Control Block (PCB)

PCB for a process contains resource management information, administrative information and execution snapshot.

Resource management information includes information about required resource such as open files, page tables and I/O streams.

Administrative information includes a process identifier, resource usage information such as execution time, process ownership, and administrative data needed for system security.

The execution snapshot captures information such as register contents and Program Counter states that is required to restore its operation back.

PCB in a Linux operating system

http://4.bp.blogspot.com/_cga7sfO2vms/TOsAHnl174I/AAAAAAAAElE/qijm_yA6vqM/s1600/PCB.png

Pointer: to Next process and to Previous process

Process state:

Creation state such as unrunnable, runnable or terminated

Runnable state such as ready, block or running

Priority state

Process number: Error number or exit code

Program counter: Instruction pointer of loaded instruction in process

Registers: Flag registers, debug registers

Memory limits: Memory domain of process

List of open files

Those various data are struct by Linux kernel as follows,

volatile long state; /* -1 unrunnable, 0 runnable, >0 stopped */

long counter; long priority; unsigned long signal;

unsigned long blocked; /* bitmap of masked signals */

unsigned long flags; /* per process flags, defined below */

int errno; long debugreg[8]; /* Hardware debugging registers */

struct exec_domain *exec_domain;

/* various fields */

struct linux_binfmt *binfmt;

struct task_struct *next_task, *prev_task;

struct task_struct *next_run, *prev_run;

unsigned long saved_kernel_stack;

unsigned long kernel_stack_page;

int exit_code, exit_signal;

TASK 1

PROCEDURE:

Use gedit and code c program for create sub process using foke() system call and wait parent process until child process being completed using wait() system call in Linux.

Write program to find Fibonacci sequence for given number.

Save code and compile it using gcc.

Execute compiled program.

#include <stdio.h>

#include <sys/types.h>

#include <unistd.h>

void main(){

long int fib0 = 0, fib1 = 1, fibn;

int n;

printf("\nNumber of Fibonacci Sequence: ");

scanf("%d", &n);

if (n >= 0){

pid_t pid = fork();

if (pid == 0){

printf("\nChild process of PID:%d is going to work on Fibonacci Sequence\n",getppid());

long int a = fib0, b = fib1;

int i;

printf("\n%d, %d, ", fib0, fib1);

if (n > 1){

n = n - 1;

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

fibn=a+b;

printf("%d, ", fibn);

a=b;

b=fibn;

}

}

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

}else if (pid > 0){

printf("Parent process (PID:%d) is waiting for child process (PID:%d) to complete\n",getpid(), pid);

wait(pid);

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

}else{

printf("Ooops.. Something goes wrong!\n");

}

}else {

printf("Try again.. You must enter positive integer as a number\n");

}

}

OBSERVATIONS AND RESULTS:

TASK 2

PROCEDURE:

Create c++ console solution using visual studio.

Create project under solution for child process.

Code child process as follows to find Fibonacci sequence of a number.

#include <stdio.h>

#include <windows.h>

void main(){

long int fib0 = 0, fib1 = 1, fibn;

int n;

DWORD pid = GetCurrentProcessId();

printf("\nChild process of PID:%d is going to work on Fibonacci Sequence\n",pid);

printf("\nNumber of Fibonacci Sequence: ");

scanf_s("%d", &n);

if (n >= 0){

long int a = fib0, b = fib1;

int i;

printf("\n%d, %d, ", fib0, fib1);

if (n > 1){

n = n - 1;

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

fibn=a+b;

printf("%d, ", fibn);

a=b;

b=fibn;

}

}

printf("\n\nChild process (PID:%d) ends\n", pid);

}else {

printf("Try again.. You must enter positive integer as a number\n");

}

}

Compile and Run program to verify its functionality.

Create project under solution for parent process and

Set solution properties to build child first and then build parent process.

Set debugging project as parent project.

Write program for parent process as follow to get child process as input parameter of parent process and to execute it via parent process using createProcess() system call in win 32 API.

#include <windows.h>

#include <stdio.h>

#include <conio.h>

#include <tchar.h>

void _tmain( int argc, TCHAR *argv[] )

{

STARTUPINFO si;

PROCESS_INFORMATION pi;

ZeroMemory( &si, sizeof(si) );

si.cb = sizeof(si);

ZeroMemory( &pi, sizeof(pi) );

DWORD pid = GetCurrentProcessId();

if( argc != 2 )

{

printf("Usage: task2 [child process name]\n", argv[0]);

return;

}

printf( "Parent Process (PID:%d) Started.\n", pid);

// Start the child process.

if( !CreateProcess( NULL, // No module name

argv[1], // Command line

NULL, // Process handle not inheritable

NULL, // Thread handle not inheritable

TRUE, // Set handle inheritance to TRUE

0, // No creation flags

NULL, // Use parent's environment block

NULL, // Use parent's starting directory

&si, // Pointer to STARTUPINFO structure

&pi ) // Pointer to PROCESS_INFORMATION structure

)

{

printf( "CreateProcess failed (%d).\n", GetLastError() );

return;

}

// Wait until child process exits.

printf( "Parent Process (PID:%d) Wait until child process exits\n", pid);

WaitForSingleObject( pi.hProcess, INFINITE );

// Close process and thread handles.

CloseHandle( pi.hProcess );

CloseHandle( pi.hThread );

printf( "Parent Process (PID:%d) Compleated\nPress any key to exit", pid);

_getch();

}

OBSERVATIONS AND RESULTS:



rev

Our Service Portfolio

jb

Want To Place An Order Quickly?

Then shoot us a message on Whatsapp, WeChat or Gmail. We are available 24/7 to assist you.

whatsapp

Do not panic, you are at the right place

jb

Visit Our essay writting help page to get all the details and guidence on availing our assiatance service.

Get 20% Discount, Now
£19 £14/ Per Page
14 days delivery time

Our writting assistance service is undoubtedly one of the most affordable writting assistance services and we have highly qualified professionls to help you with your work. So what are you waiting for, click below to order now.

Get An Instant Quote

ORDER TODAY!

Our experts are ready to assist you, call us to get a free quote or order now to get succeed in your academics writing.

Get a Free Quote Order Now