Professional Documents
Culture Documents
s1 PDF
s1 PDF
1
The kernel
2
User Mode & kernel mode
User mode apps delegate to system APIs in order to access hardware
User
User space
Utilities compile / shell
Standard library
3
What is a Shell?
• A user app running in user space.
• An interface to the kernel.
• Shell ≠ kernel.
• Shell ó OS.
• Shell commands delegate to system calls.
4
Bash shell
Bash - the classical Unix shell. Probably the most widly shell in use.
5
System Calls
Definition:
Internal kernel routines that are exposed to user applications directly, through
the C library, or via any other user mode interfaces are called system calls.
System call = the programmatic way to requests a service from the kernel.
(!) System calls are implemented in the kernel and are not part of the C library.
6
System Calls
7
Linux process management
8
The process ID
• Each process is represented by a unique identifier, the process ID (pid).
9
process ID Allocation
• By default, the kernel imposes a maximum process ID value of 32768.
• The kernel does not reuse process ID values until it wraps around from the
top. That is, earlier values will not be reused until the values will allocate.
10
The type pid_t
11
System( )
System() –execute shell command.
# include <stdlib.h>
int system (const char *string);
The system function runs the command passed to it as a string and waits
for it to complete.
Example:
#include <stdio.h>
#include <string.h>
int main (){
char command [50];
strcpy(command,”ls –l”);
system(command);
return(0);
}
12
The exec family
The exec family are system calls (and not C library function), that executing
a new program.
#include <unistd.h>
13
execvp() - Definition
• replaces the program in the current process with a new program.
• return value:
– on error, returns -1.
– on success, there is no process to return values to.
14
main.c
execvp() -Example
kid.c
#include <stdio.h>
#include <stdio.h>
int main(){
char** args = {0};
int main(){
exevp("/home/ubuntu/workspace/ printf("hello! I am the same
kid", args);
process (:\n");
printf("no one will print it return 0;
\n");
}
return 0;
}
15
The fork()
16
The fork()
#include <sys/types.h>
#include <unistd.h>
Return value:
Upon success: 0 to the childand the PID ot the child to the parent.
Upon failure: no process is created , -1 returned to the parent.
17
fork() Example 1
pid_t new_pid;
new_pid = fork();
switch(new_pid){
case -1: /*Error*/
break;
case 0: /*We are child*/
break;
default: /*We are parent*/
break;
}
18
fork() Example 2
#include <sys/types.h> (Code cont.)
{
#include <unistd.h> case -1:
#include <stdio.h> perror(“fork failed”);
#include <stdlib.h> exit(1);
case 0:
int main() message = “This is the child”;
{ n = 5;
break;
pid_t pid;
default:
char *message; message = “This is the parent”;
int n; n = 3;
break;
printf(“fork program starting\n”); }
for(; n > 0; n--) {
pid = fork();
puts(message);
switch(pid) sleep(1);
}
exit(0);
}
19
fork() introduces non-determinism
example:
pid_t x = fork();
if(x){
printf("1");
}else{
printf("2");
}
20
fork() Example 3
An example:
int i = 1;
printf("my process pid is %d\n",getpid());
fork_id=fork(); Program flow:
if (fork_id==0){
i= 2;
printf(“child pid %d, i=%d
\n",getpid(),i); PID = 8864 i=1
} else
printf(“parent pid %d, i=%d
\n",getpid(),i); fork ()
return 0;
Possible Output:
fork_id=0
my process pid is 8864 fork_id = 8865
i=2
child pid 8865, i=2 i=1
parent pid 8864, i=1
PID = 8864 PID = 8865
21
getpid() vs. getppid()
int main(void)
{
pid_t parentPID;
pid_t childPID;
childPID = fork();
Possible output:
Parent Process :: childPID 4672 parentPID 2251
Child Process :: childPID 4673 parentPID 4672 22
What is a possible output? (1)
int main(void)
{
fork();
printf(“fork 1.\n”);
fork();
printf(“fork 2.\n”);
return 0;
}
a b c
23
What is a possible output? (2)
What will be printed in each code?
a b
24
What is a possible output? (3)
25
What is a possible output? (4)
How many lines of “Hello” will be printed in the following example:
a b c
26
Tree of processes (1)
Draw the tree of process that will result:
int x;
fork();
x = fork();
if(x != 0)
fork();
printf(“pid= %d”,getpid()); Solution:
1
5 4
What are possible outputs of the code?
2
6 3
27
Tree of processes(2)
Write code that creates this tree:
28
Tree of processes(2)
Solution:
1
int main(void)
{
pid_t id= = fork(); 2
if (id == 0) { // if this is process #2
fork();
}
return 0; 3
}
29
Tree of processes(3)
Write code that creates this tree:
3 4
2
5 6 7
30
Tree of processes(3)
Solution: 1
int main(void)
{ 3 4
pid_t first_id= = fork(); 2
fork();
fork();
5 6 7
if (first_id == 0)
{
fork(); Naive solution, Does not work!
fork();
fork();
}
return 0;
}
31
Tree of processes(3)
Solution:
1
int main(void)
{
first_id = fork(); // create #2
if (first_id != 0) //if you’re the father 3 4
{ 2
second_id = fork(); // create #3
if (second_id != 0) //if you’re the father
fork(); // create #4
}
5 6 7
if (first_id == 0) //if you’re #2 (a child)
{
forth_id = fork(); // create #5
32
fork() and Binary Tree
33
fork() and Binary Tree
lazy evaluation -
• if “&&” left side evaluated to be false, right side will not executed.
• if “||” left side evaluated to be true, right side will not executed.
34
fork() and Binary Tree
• first fork:
– return true for the parent
– return false for the child
• second fork:
– Lazy evaluation - the child has false, so it won’t execute the right
hand
first fork 1
second fork
2 3
35
fork() and Binary Tree
fork() || fork()
36
fork() and Binary Tree
fork() || fork()
Lazy evaluation - the parent recieved true, so it won’t execute the right hand
1
first fork
second fork 2
3
37
fork() and Binary Tree
38
fork() and Binary Tree
fork 1
true false
fork 3
fork 2
39
fork() and Binary Tree
40
fork() and Binary Tree
fork();
fork() && fork() || fork();
fork();
41
fork() & for loops (1)
int i;
for( i=0; i<3 && fork() ; i++ );
printf("line\n");
42
fork() & for loops (2)
int i;
for( i=0; i<3 || fork() ; i++ ){
printf("line %d\n", i);
}
43
fork-exec combination
main.c kid.c
#include <stdio.h>
int main(){
#include <stdio.h>
char** args = {0};
if(!fork())
int main(){
execvp("/home/ubuntu/workspace/ printf("hello! I am just a
kid", args);
child (:\n");
else
return 0;
printf("I am the parent !\n");
}
printf("only parent, will print it
\n");
return 0;
}
44
wait()
#include <sys/types.h>
#include <sys/wait.h>
• The wait() system call causes a parent process to pause until one of
its child processes is stopped.
45
wait()
• You can interpret the status information using macros defined in sys/
wait.h, shown in the following table.
Macro Definition
46
wait()- Example
#include <sys/types.h> (Code cont.)
{
#include <sys/wait.h>
case -1:
#include <unistd.h> perror(“fork failed”);
#include <stdio.h> exit(1);
#include <stdlib.h> case 0:
message = “This is the child”;
int main() n = 5;
{ exit_code = 37;
pid_t pid; break;
default:
char *message;
message = “This is the parent”;
int n; n = 3;
int exit_code; exit_code = 0;
break;
printf(“fork program starting\n”); }
pid = fork(); for(; n > 0; n--) {
puts(message);
switch(pid)
sleep(1);
}
47
wait()- Example
This section of the program waits for the child process to finish.
(Code cont.)
if (pid!=0) {
int stat_val;
pid_t child_pid;
child_pid = wait(&stat_val);
48
Zombie Process
When a child process terminates, an association with its parent
survives until the parent in turn either terminates normally or
calls wait().
49
Zombie Process- Example
#include <sys/types.h> (Code cont.)
{
#include <unistd.h> case -1:
#include <stdio.h> perror(“fork failed”);
#include <stdlib.h> exit(1);
case 0:
int main() message = “This is the child”;
{ n = 3;
break;
pid_t pid;
default:
char *message; message = “This is the parent”;
int n; n = 5;
break;
printf(“fork program starting\n”); }
for(; n > 0; n--) {
pid = fork();
puts(message);
switch(pid) sleep(1);
}
exit(0);
}
50
exit()
#include <stdlib.h>
void exit (int status);
51
waitpid()
#include <sys/types.h>
#include <sys/wait.h>
52
Daemon process
• A daemon is a process that runs in the background, not connecting to any
controlling terminal. Daemons are normally started at boot time, are run
as root or some other special user ,and handle system-level tasks.
• A daemon has two general requirements: it must run as a child of init and
it must not be connected to a terminal.
53
Session and Process Groups
• Each process is a member of a process group, which is a collection of one
or more processes generally associated with each other for the purposes
of job control.
• The primary attribute of a process group is that signals may be sent to all
processes in the group.
54
You have to learn the following functions on your own
• vfork()
• getsid()
• setsid()
• getuid()
• getgid()
55