Sie sind auf Seite 1von 11

BITS Pilani BITS Pilani

Pilani | Dubai | Goa | Hyderabad

2.1.4. Linux Processes


In the previous segment
BITS Pilani

In the last lecture, we have discussed briefly about

Components of an Operating System.


In this segment
BITS Pilani

We will discuss about

Linux Processes.
Processes in Linux
BITS Pilani
A Process is a program in execution.

A Linux Process has by default one Thread and a memory address space.

All Process begin with one thread of execution that runs main() function.

You can create additional POSIX threads using pthread_create().

Each process is associated with a Process Id (pid)

After booting the first process created is the ‘init’ process, with pid = 1.
Linux Process Creation
BITS Pilani

 A Linux process is created using ‘fork()’.

 ‘fork’ creates a child process exactly identical to the parent, including stack,
heap, file descriptors, variables etc, but the child runs in another address space.

 So after ‘fork’ both parent and child processes run.

 ‘fork’ returns two PIDs:


o ‘0’ to the child process
o ‘positive number’ to the parent process.
o ‘-1’ for failure
Linux Process Creation - Example
BITS Pilani
#include <stdio.h>
#include <stdlib.h>  Save this file as ‘process.c’
#include <unistd.h>  Compile it using command:
#include <sys/types.h> $ cc process.c -o process
 Then run it by
int main(int argc, char* argv[]) $ ./process
{
int pid, child_status;
pid = fork();
if(pid == 0) {
printf("I am the child process \n");
}
else if(pid == -1) {
printf("Error in process creation! \n\n");
return 1;
}
else {
printf("I am the parent process, child pid = %d \n", pid);
/* Wait until the child terminates */
wait(&child_status);
}
return 0;
}
Linux Process Creation
BITS Pilani

fork()

Parent Process Child Process


int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
Control will
… …
come here
pid = fork(); pid = fork();
if(pid == 0) { if(pid == 0) {
Control will … …
come here } }
else if(pid == -1) { else if(pid == -1) {
… …
} }
else { else {
… …
} }
return 0; return 0;
} }
Linux Process Creation –
Running a different program in the child process
BITS Pilani
After fork(), the child process will be running the parent’s code.
We need to run a different program in the child process (otherwise why did we
created it in first place ?)
It will be done by ‘exec’ family of system calls.
There are many in this family, we will use

int execl(const char *path, const char *arg, …)


1st argument = path of the program
2nd argument = 1st argument of the program,
i.e. the executable file name itself
next arguments = You can pass the argument required by the program,
pass NULL if the program doesn’t require any arguments
Linux Process Creation –
Running a different program in the child process
BITS Pilani
 We will execute ‘/bin/ls’ in the child process.
 In the parent process we will wait for the child process to terminate.
 It can be achieved by
o wait(NULL) : suspends the current process until one of its child terminates
o waitpid(pid, &child_status, 0) :suspends the process until the
child process specified by the pid changes its state. Here change of state
means termination.

 Since the parent has only one child in our case, both calls result in same
behaviour.
Linux Process Creation –
Running a different program in the child process
BITS Pilani
#include <stdio.h> else if(pid == -1) {
#include <stdlib.h> /* Process creation failed */
#include <unistd.h> printf("Error in process creation! \n\n");
return 1;
#include <sys/types.h>
}
#include <sys/wait.h> else {
/* It is the parent process */
int main(int argc, char* argv[]) printf("I am the parrent process, child pid = %d
{ \n", pid);
char cmd_str[128]; /* Wait until the child terminates */
int pid; wait(&child_status);
int child_status; printf("-------------------------\n");
printf("Done, status = %d\n", child_status);
}
printf("sh> ");
return 0;
scanf("%s", cmd_str); }
pid = fork();
if(pid == 0) { Compile it using command:
/*child process*/ $ cc process.c -o process
printf("I am the child process \n"); Then run it by
printf("cmd = '%s'\n", cmd_str); $ ./process
printf("-------------------------\n");
execl(cmd_str, cmd_str, (char*)NULL);
/* We should not return from 'execl'.
We will go to next line, if error occurs */
perror("execl() failure!\n\n");
printf("This print should not come if execl were successful! \n\n");
exit(1);
}
Summary
BITS Pilani

In this lecture, we have discussed about

Linux Processes.

In the next lecture, we will discuss about

POSIX Threads in Linux.

Das könnte Ihnen auch gefallen