227CSS 3 - Lab 4
227CSS 3 - Lab 4
1.1 OBJECTIVE
The objective of this lab exercise is to give the general overview system calls, and how to find
the process identifier (PID) for newly created child and parent using getpid( ) and getppid ( )
respectively. C language will be used as an example to demonstrate practically how to write
program to check PID for parent and child by using the VMware in Linux environment.
In computing, a system call is how a program requests a service from an operating system's
kernel. This may include hardware related services (e.g. accessing the hard disk), creating and
executing new processes, and communicating with integral kernel services (like scheduling).
System calls provide an essential interface between a process and the operating system.
3. PROCESS IDENTIFIER
In computing, the process identifier (normally referred to as the process ID or just PID) is a
number used by most operating system kernels, —such as that of UNIX, Mac OS X or Microsoft
Windows— to temporarily uniquely identify a process. This number may be used as a parameter
Page 1 of 6
in various function calls allowing processes to be manipulated, such as adjusting the process's
priority or killing it altogether.
In Unix-like operating systems, new processes are created by the fork() system call. The PID is
returned to the parent enabling it to refer to the child in further function calls. The parent may,
for example, wait for the child to terminate with the waitpid() function, or terminate the process
with kill( ).
There are two tasks with especially distinguished process IDs: swapper or sched has process ID 0
and is responsible for paging, and is actually part of the kernel rather than a normal user-mode
process. Process ID 1 is usually the init process primarily responsible for starting and shutting
down the system. Originally, process ID 1 was not specifically reserved for init by any technical
measures: it simply had this ID as a natural consequence of being the first process invoked by the
kernel. More recent Unix systems typically have additional kernel components visible as
'processes', in which case PID 1 is actively reserved for the init process to maintain consistency
with older systems.
Process IDs are usually allocated on a sequential basis, beginning at 0 and rising to a maximum
value which varies from system to system. Once this limit is reached, allocation restarts at 300
and again increases. In Mac OS X and HP-UX, allocation restarts at 100. However, for this and
subsequent passes any PIDs still assigned to processes are skipped. Some consider this to be
potential security vulnerability in that it allows information about the system to be extracted, or
messages to be covertly passed between processes. As such, implementations that are
particularly concerned about security may choose a different method of PID assignment On
some systems, like MPE/iX, the lowest available PID is used, sometimes in an effort to minimize
the number of process information kernel pages in memory.
The current process ID is provided by a getpid() system call and the process ID of a parent
process is obtainable by a getppid() system call.
Page 2 of 6
4. LAB EXERCISE
Experiment : Actually, there are three distinct operations involved: creating a new child
process, causing the new process to execute a program, and coordinating the completion of the
child process with the original program.
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
main()
{
int pid;
printf("%d: I am the parent. Remember my number!\n", getpid());
printf("%d: I am now going to fork ... \n", getpid());
pid = fork( );
if (pid != 0)
{
/* the parent will execute this code */
printf(“Hello! I am the parent process %d\n", getpid());
}
else /* forkresult == 0 */
Page 3 of 6
Lab Question 4.2:
Following program demonstrates the use of getppid( ) and getpid( ). First display some message
with parent process ID, than create a child process and print some message with Process ID in
parent block and some message with process ID in child block.
Write this program in VMware, compile it and run it according to lab instructor instructions and
study the output. And write the description of each step.
#include <stdio.h>
main()
{
int pid ;
printf("I'am the original process with PID %d and PPID %d.\n",
getpid(), getppid()) ;
pid = fork ( ) ; /* Duplicate. Child and parent continue from here */
if ( pid != 0 ) /* pid is non-zero,so I must be the parent*/
{
printf("I'am the parent with PID %d and PPID %d.\n",
getpid(), getppid()) ;
printf("My child's PID is %d\n", pid ) ;
}
else /* pid is zero, so I must be the child */
{
printf("I'm the child with PID %d and PPID %d.\n",
getpid(), getppid()) ;
}
}
Page 4 of 6
Lab Question 4.3:
Following program demonstrates the use of exit ( ) and sleep ( ). Write this program in VMware,
compile it and run it according to lab instructor instructions and study the output. Write brief
explanation of each step.
#include <stdio.h>
main ( )
{
int pid ;
pid = fork();
if ( pid != 0 ) /* pid is non-zero, so I must be the parent */
{
sleep (5) ; /* stop executing for 5 seconds */
printf (“I am in parent block after sleep \n”);
exit (42); /*exit with any number*/
printf (“I am in parent block after executing exit ( ) command ”);
}
else /* pid is zero, so I must be the child */
{
printf ( “Helloooo! I am in child block \n”);
printf (“I am the child process \n”);
}
}
Explanation of the Problem 4.3
Page 5 of 6
Comments by the Student:
Signature: Date: / /
Page 6 of 6