Professional Documents
Culture Documents
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.