You are on page 1of 35

OPERATING

SYSTEM
(CSL 320)
TO O B A K H A N
D E PA RT ME N T O F C O M P U T E R E N G I N E E R I N G
B A H R I A U N I V E R S I T Y, I S L A M A B A D
LAB NO.6
System Calls (Process Creation) I
Objectives
To learn how to create process in Linux using system calls,
 fork()
 getpid()
 getppid()
 wait()
 exit()
 exec()
 sleep()
Running a C program in Linux Text
Editor
1. Create .c file e.g. text.c
2. Double click and write a program in C as shown below and save it.

3. Go to terminal
4. Compile and run your program as
 gcc -o filename filename.c
 ./filename
Running a C program in Linux Text
Editor
Running a C++ program in Linux Text
Editor
1. Create .cpp file e.g. tst.cpp
2. Double click and write a program in C++ as shown below and save it.
Running a C++ program in Linux Text
Editor
3. Go to terminal
4. Compile and run your program as
 g++ -o filename filename.cpp
 ./filename

Note
 Use gedit filename.c to edit .c file
 Use gedit filename.cpp to edit .cpp file
Process
Process States
Process IDs (pids)
 Each process in Linux is represented by a unique ID called Process ID or pid.
 Process IDs are 16-bit numbers that are assigned sequentially when a new
process is created.
 Every process has the parent process except the init process (whose pid=1 and
it’s a process started when system boots up).
getpid() and getppid() system calls
 getpid() is a system call used to get process ID of a process a
program is running in.
 getppid() is used to obtain process ID of a parent process.
 In C/C++ we use
 pid_t as datatype
 It is defined in headerfile <sys/types.h>
Example 1
Write a program to get the process ID of running process and its parent process.
Viewing Active Processes…ps
command
 ps command is used to display the processes that are running on your system.

 invocation of ps shows two processes, bash and ps.


 First, bash is shell running on this terminal.
 second is the running ps itself.
 First column shows PID the process IDs of each running process.
ps command
 ps –e –o pid, ppid, command
 -e option instructs ps to display all processes running on the system.
 -o pid, ppid, command tells ps what information to display about each process.
 information to display must be given with –o separated with comma.
Task 2: Running ps command
Show the output of ps command running on your terminal.
System Call
 It’s a medium or way to request services from kernel.
 Kernel is core of OS and it manages all services.
Creating Process in Linux…fork()
 System call fork() is used to create processes. It has no arguments and returns a
process ID. The purpose of using fork() is to create new process which becomes the
child process of the caller. After a new process is created, both processes will execute
the next instruction following the fork() system call. Therefore, we have to distinguish
the parent process from the child, this can be done by testing the return value,
i. If fork() returns a negative value, creation of child process is unsuccessful.
ii. If fork() returns zero, child process is created.
iii. If fork() returns positive value, means it returns child pid to parent process.
Use getpid() system call to retrieve process ID.
fork() system call
 fork() function provides different return values to the parent and child processes.
 One process goes in to the fork call, and two processes come out with different return
values.
 Return value in parent is PID of child.
 Return value in child is zero. Because there is no ever process with zero PID so this
makes it easy for the program whether its running as parent process or child process.
Example 2: Program to create process using fork().
After call to fork()…
Process Creation using fork()

 Parent and child executes concurrently.


 Parent waits until child terminates.
Process Creation using fork()
wait() and exit() system calls
 Parent process usually needs to synchronize its actions by waiting until child process has either
stopped or terminated its actions.
 The wait() call allows the parent process to suspend its activities until one of these actions has
occurred.
 The wait() system call accepts a single argument, which is a pointer to an integer and returns a value
defines as type pid_t with the header file <sys/wait.h>
 If calling process does not have any child associated with it, wait will return immediately with the
value of -1.
 If any child processes is still active, the calling process will suspends its activities until a child process
terminates.
 Processes may request their own termination by making exit() system call, typically returning an int.
Example 3: (using fork(), exit() and wait()
together)
Write a program to use
fork(), exit() and wait()
system calls together.
Note:
Add #include<unistd.h> to avoid warnings while compiling
your code.

You might also like