Vous êtes sur la page 1sur 16

Lecture 3:

Processes
(Section 2.1)

COP 4600 Operating Systems Lecture 3 1


From William Stalling’s “The UNIX Operating System”, 2005
COP 4600 Operating Systems Lecture 3 2
Sequential Processes (review)

COP 4600 Operating Systems Lecture 3 3


The Birth of a Process (review)

myprogram.c myprogram.o
int j; object
char* s = “hello\n”; assembler data
file
int p() {
j = write(1, s, 6); data
data
data
return(j);
}
libraries
….. linker
p: and other
store this objects
store that
push
jsr _write
compiler ret
etc.
data program
myprogram.s

myprogram
COP 4600 Operating Systems Lecture 3 (executable file) 4
A Peek Inside a Running Program (review)

0
CPU common runtime
x
your program
code library

your data address space


R0 (virtual or physical)

Rn heap
PC x
SP y
registers
y
stack
high
“memory”

COP 4600 Operating Systems Lecture 3 5


Process States

COP 4600 Operating Systems Lecture 3 6


Process Control Block (PCB)

Information associated with each process:


• Process state
• Program counter
• CPU registers
• CPU scheduling information
• Memory-management information
• Accounting information
• I/O status information

COP 4600 Operating Systems Lecture 3 7


CPU Switch From Process to Process

COP 4600 Operating Systems Lecture 3 8


Process States in UNIX

From William Stalling’s “The UNIX Operating System”, 2005


COP 4600 Operating Systems Lecture 3 9
Process States in Linux

Equivalent to “blocked” state from the simple


state transition representation:
• Uninterruptible: waiting on hardware
conditions and thus not handling signals
• Interruptible: waiting for an event, such as
end of I/O operation, available resource, or
signal from another process

From William Stalling’s “The Linux Operating System”, 2005

COP 4600 Operating Systems Lecture 3 10


A tree of processes on Solaris

COP 4600 Operating Systems Lecture 3 11


System Calls Related to Process
Management in Linux
• execve Execute program.
• exit Terminate the calling process.
• getpid Get process identification.
• setuid Set user identity of the current process.
• ptrace Provides a means by which a parent process
my observe and control the execution of another
process, and examine and change its core image and
registers.

Comprehensive list: please refer to document uploaded


into Black Board (under Course Documents)
COP 4600 Operating Systems Lecture 3 12
In-class problems with fork()

COP 4600 Operating Systems Lecture 3 13


What does this code do?
#include <unistd.h>
int main(int argc, char* args[])
{
while(1)
fork();
return 0;
}

COP 4600 Operating Systems Lecture 3 14


PCB Table and the “Fork Bomb”

• Fork fills the PCB table


– Can’t create another process to kill forked processes
– The only solution: reboot
• How to prevent the fork bomb?
– Limit number of processes per user
• (ulimit in bash; set up limits in
/etc/security/limits.conf on Linux or BSD Unix)
• To what value?
– OS detects and stops it (rarely implemented)

COP 4600 Operating Systems Lecture 3 15


#include <sys/types.h>
#include <unistd.h>

#define CHILD (pid_t) 0

int main(int argc, char * argv[] )


{
pid_t child;
int status; /* this is where the child's return status is going */
int options = 0; /* not setting WNOHANG or WUNTRACED */
int x;
printf("P: parent process id is: %d\n",getpid());

for( x = 0; x < argc; x++ )


printf("P: argv[%d]: %s\n",x,argv[x]);

if( (child = fork()) < 0 )


printf("P: VERY BAD NEWS: fork failed!\n");

if( child == CHILD )


{
printf("C: in child process, my process id is %d\n",getpid());
printf("C: my parent's process id is, hmm, let me see: %d\n",getppid());
printf("C: in child, my argv looks identical to my parent's. Wonder why?\n");
for( x = 0; x < argc; x++ )
printf("C: argv[%d]: %s\n",x,argv[x]);
printf("C: child is going to execvp the incoming command. HERE WE GO:\nC: Goodbye Cruel World:\n");
execvp(argv[1],&argv[1]);
}
else
{
printf("P: waiting for my kid, as usual...\n");
waitpid(child, &status, options);
printf("P: still in parent, my process id is still %d\n",getpid());
printf("P: and my child was %d\n",child);
COP 4600printf("P:
OperatingmySystems
child's exit status was: %d\n",status); Lecture 3 16
}

Vous aimerez peut-être aussi