Beruflich Dokumente
Kultur Dokumente
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.
After booting the first process created is the ‘init’ process, with pid = 1.
Linux Process Creation
BITS Pilani
‘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.
fork()
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
Linux Processes.