Control de trabajos - Arcos

Transcripción

Control de trabajos - Arcos
Sistemas Operativos
sesión 10: procesos
Grado en Ingeniería Informática
Universidad Carlos III de Madrid
ARCOS @ UC3M
Alejandro Calderón Mateos
Agenda
Linux
2
Servicios para
procesos
Ejercicios
ARCOS @ UC3M
Alejandro Calderón Mateos
Agenda
Linux
3
Servicios para
procesos
Ejercicios
ARCOS @ UC3M
Alejandro Calderón Mateos
Contenidos
• Control de trabajos
• Control de procesos
• Compresión y descompresión
4
ARCOS @ UC3M
Alejandro Calderón Mateos
Contenidos
• Control de trabajos
• Control de procesos
• Compresión y descompresión
5
ARCOS @ UC3M
Alejandro Calderón Mateos
Motivación
• Trabajos = aplicaciones ejecutadas
en un intérprete de mandatos
• ¿Cómo puedo gestionarlos?
acaldero@phoenix:~/work$ sleep 25 &
acaldero@phoenix:~/work$ jobs
…
6
ARCOS @ UC3M
Alejandro Calderón Mateos
sleep <número de segundos>
7
ARCOS @ UC3M
Alejandro Calderón Mateos
Control de trabajos
ejecución en primer/segundo plano
Ejecutando en
primer plano
Ejecutando en
segundo plano
8
ARCOS @ UC3M
Alejandro Calderón Mateos
Control de trabajos
de ejecución en primer plano a congelación
ctrl-z
Ejecutando en
primer plano
fg
Congelada la
ejecución
Ejecutando en
segundo plano
9
ARCOS @ UC3M
Alejandro Calderón Mateos
Control de trabajos
de congelado a ejecución en segundo plano
ctrl-z
Ejecutando en
primer plano
fg
Congelada la
ejecución
Ejecutando en
segundo plano
10
ARCOS @ UC3M
Alejandro Calderón Mateos
Control de trabajos
listado de trabajos
jobs
ctrl-z
Ejecutando en
primer plano
fg
Congelada la
ejecución
%<id>
Ejecutando en
segundo plano
11
ARCOS @ UC3M
Alejandro Calderón Mateos
Control de trabajos
finalización ejecutando en primer plano
ctrl-c
jobs
ctrl-z
Ejecutando en
primer plano
fg
Congelada la
ejecución
Provocar
fin ejecución
%<id>
Ejecutando en
segundo plano
12
ARCOS @ UC3M
Alejandro Calderón Mateos
Control de trabajos
finalización trabajo congelado
ctrl-c
jobs
ctrl-z
Ejecutando en
primer plano
fg
Congelada la
ejecución
kill -9 %<id>
Provocar
fin ejecución
%<id>
Ejecutando en
segundo plano
13
ARCOS @ UC3M
Alejandro Calderón Mateos
Control de trabajos
finalización ejecutando en segundo plano
ctrl-c
jobs
ctrl-z
Ejecutando en
primer plano
fg
Congelada la
ejecución
kill -9 %<id>
Provocar
fin ejecución
%<id>
jobs
Ejecutando en
segundo plano
14
ARCOS @ UC3M
Alejandro Calderón Mateos
Control de trabajos
ctrl-c
jobs
ctrl-z
Ejecutando en
primer plano
fg
Congelada la
ejecución
kill -9 %<id>
Provocar
fin ejecución
%<id>
jobs
Ejecutando en
segundo plano
15
ARCOS @ UC3M
Alejandro Calderón Mateos
Contenidos
• Control de trabajos
• Control de procesos
• Compresión y descompresión
16
ARCOS @ UC3M
Alejandro Calderón Mateos
Motivación
• ¿Cómo puedo gestionar los programas
que están siendo ejecutados desde
otra sesión de intérprete de mandatos?
acaldero@phoenix:~/work$ ps axu
acaldero@phoenix:~/work$ kill -1 1
…
17
ARCOS @ UC3M
Alejandro Calderón Mateos
Control de procesos
ctrl-c
ps axu
jobs
ctrl-z
Ejecutando en
primer plano
fg
Congelada la
ejecución
kill -9 %<id>
kill -9 <pid>
Provocar
fin ejecución
%<id>
ps axu jobs
Ejecutando en
segundo plano
18
ARCOS @ UC3M
Alejandro Calderón Mateos
Contenidos
• Control de trabajos
• Control de procesos
• Compresión y descompresión
19
ARCOS @ UC3M
Alejandro Calderón Mateos
Comprimir, descomprimir y listar
Extensión
zip
.zip
Tar +
gzip
Comprimir
Descomprimir
Listar
zip –r f.zip <ficheros>
zip –a f.zip
unzip –l f.zip
.tgz
.tar.gz
tar zcvf f.tgz <ficheros>
tar zxvf f.tgz
tar ztvf f.tgz
Tar +
bzip2
.tbz2
.tar.bz2
tar jcf f.tbz2 <ficheros>
tar jxf f.tbz2
tar jtvf f.tbz2
rar
.rar
rar a f.rar <ficheros>
rar x f.rar
rar l f.rar
20
ARCOS @ UC3M
Alejandro Calderón Mateos
Agenda
Linux
21
Servicios para
procesos
Ejercicios
ARCOS @ UC3M
Alejandro Calderón Mateos
Contenidos
• Entendiendo el fork, exec y wait
• fork+exec+exit simple
• fork+exec+exit múltiple
22
ARCOS @ UC3M
Alejandro Calderón Mateos
Contenidos
• Entendiendo el fork, exec, exit y wait
• fork+exec+exit simple
• fork+exec+exit múltiple
23
ARCOS @ UC3M
Alejandro Calderón Mateos
Fork
• Crea un “clon” de un proceso
+
=
(fork)
ARCOS @ UC3M
Alejandro Calderón Mateos
Exec
• Cambia la imagen de un proceso
+
=
(exec)
ARCOS @ UC3M
Alejandro Calderón Mateos
Exit
• Finalizar la ejecución de un proceso
+
=
(exit)
ARCOS @ UC3M
Alejandro Calderón Mateos
Fork
• Crea un “clon” de un proceso
– Iguales salvo pequeñas diferencias:
al padre se le devuelve el PID del hijo, y el hijo cero.
Pid 200
pid = fork( ) ;
201
Pid 201
pid = fork( ) ;
0
ARCOS @ UC3M
Alejandro Calderón Mateos
Exec
• Cambia la imagen de un proceso
– Si todo va bien no
se vuelve de esta
función (el código se
sustituye por otro)
Pid 201
execlp("ls",
"ls","-l",NULL);
Pid 201
/* código de /bin/ls */
ARCOS @ UC3M
Alejandro Calderón Mateos
Exit
• Finalizar la ejecución de un proceso
– El parámetro es un valor entero que se suele usar como
código de diagnóstico: si se ha ejecutado todo bien, ha
habido algún problema leve, algún error grave, etc.
Pid 201
exit( 10 ) ;
ARCOS @ UC3M
Alejandro Calderón Mateos
Wait
• Tiene tres efectos:
1. Bloque la ejecución del padre hasta que alguno de sus
hijos termine su ejecución.
2. Guarda en su parámetro el valor devuelto por el hijo.
3. Devuelve el pid del hijo que ha terminado.
Pid 200
pid != wait(&status) ;
Pid 201
exit( 10 );
1
3
2
pid != wait(&status) ;
201
10
ARCOS @ UC3M
Alejandro Calderón Mateos
Contenidos
• Entendiendo el fork, exec y wait
• fork+exec+exit simple
• fork+exec+exit múltiple
31
ARCOS @ UC3M
Alejandro Calderón Mateos
Repaso
fork() + exec()
/* ejecutar el mandato ls -l */
#include <sys/types.h>
#include <stdio.h>
main() {
pid_t pid;
int status;
pid = fork();
if (pid == 0)
{
execlp("ls","ls","-l",NULL);
exit(-1);
}
else
{
while (pid != wait(&status));
}
exit(0);
}
32
ARCOS @ UC3M
Alejandro Calderón Mateos
Repaso
fork() + exec()
/* ejecutar el mandato ls -l */
#include <sys/types.h>
#include <stdio.h>
main() {
pid_t pid;
int status;
pid = fork();
if (pid == 0)
{
execlp("ls","ls","-l",NULL);
exit(-1);
}
else
{
while (pid != wait(&status));
}
exit(0);
}
33
ARCOS @ UC3M
Alejandro Calderón Mateos
Repaso
fork() + exec()
/* ejecutar el mandato ls -l */
#include <sys/types.h>
#include <stdio.h>
/* ejecutar el mandato ls -l */
#include <sys/types.h>
#include <stdio.h>
main() {
pid_t pid;
int status;
main() {
pid_t pid;
int status;
}
pid = fork();
if (pid == 0)
{
execlp("ls","ls","-l",NULL);
exit(-1);
}
else
{
while (pid != wait(&status));
}
pid = fork();
if (pid == 0)
{
execlp("ls","ls","-l",NULL);
exit(-1);
}
else
{
while (pid != wait(&status));
}
exit(0);
exit(0);
}
34
ARCOS @ UC3M
Alejandro Calderón Mateos
Repaso
fork() + exec()
/* ejecutar el mandato ls -l */
#include <sys/types.h>
#include <stdio.h>
/* ejecutar el mandato ls -l */
#include <sys/types.h>
#include <stdio.h>
main() {
pid_t pid;
int status;
main() {
pid_t pid;
int status;
}
pid = fork();
if (pid == 0)
{
execlp("ls","ls","-l",NULL);
exit(-1);
}
else
{
while (pid != wait(&status));
}
pid = fork();
if (pid == 0)
{
execlp("ls","ls","-l",NULL);
exit(-1);
}
else
{
while (pid != wait(&status));
}
exit(0);
exit(0);
}
35
ARCOS @ UC3M
Alejandro Calderón Mateos
Repaso
fork() + exec()
/* ejecutar el mandato ls -l */
#include <sys/types.h>
#include <stdio.h>
/* ejecutar el mandato ls -l */
#include <sys/types.h>
#include <stdio.h>
main() {
pid_t pid;
int status;
main() {
pid_t pid;
int status;
}
pid = fork();
if (pid == 0)
{
execlp("ls","ls","-l",NULL);
exit(-1);
}
else
{
while (pid != wait(&status));
}
pid = fork();
if (pid == 0)
{
execlp("ls","ls","-l",NULL);
exit(-1);
}
else
{
while (pid != wait(&status));
}
exit(0);
exit(0);
}
36
ARCOS @ UC3M
Alejandro Calderón Mateos
Repaso
fork() + exec()
/* ejecutar el mandato ls -l */
#include <sys/types.h>
#include <stdio.h>
/* ejecutar el mandato ls -l */
#include <sys/types.h>
#include <stdio.h>
main() {
pid_t pid;
int status;
main() {
pid_t pid;
int status;
}
pid = fork();
if (pid == 0)
{
execlp("ls","ls","-l",NULL);
exit(-1);
}
else
{
while (pid != wait(&status));
}
pid = fork();
if (pid == 0)
{
execlp("ls","ls","-l",NULL);
exit(-1);
}
else
{
while (pid != wait(&status));
}
exit(0);
exit(0);
}
37
ARCOS @ UC3M
Alejandro Calderón Mateos
Repaso
fork() + exec()
/* ejecutar el mandato ls -l */
#include <sys/types.h>
#include <stdio.h>
/* ejecutar el mandato ls -l */
#include <sys/types.h>
#include <stdio.h>
main() {
pid_t pid;
int status;
main() {
pid_t pid;
int status;
}
pid = fork();
if (pid == 0)
{
execlp("ls","ls","-l",NULL);
exit(-1);
}
else
{
while (pid != wait(&status));
}
pid = fork();
if (pid == 0)
{
execlp("ls","ls","-l",NULL);
exit(-1);
}
else
{
while (pid != wait(&status));
}
exit(0);
exit(0);
}
38
ARCOS @ UC3M
Alejandro Calderón Mateos
Repaso
fork() + exec()
/* ejecutar el mandato ls -l */
#include <sys/types.h>
#include <stdio.h>
/* código del mandato ls */
#include <sys/types.h>
#include <stdio.h>
main() {
pid_t pid;
int status;
main() {
pid = fork();
if (pid == 0)
{
execlp("ls","ls","-l",NULL);
exit(-1);
}
else
{
while (pid != wait(&status));
}
/* código del ls */
exit( 0 );
}
exit(0);
}
39
ARCOS @ UC3M
Alejandro Calderón Mateos
Repaso
wait() + exit()
/* ejecutar el mandato ls -l */
#include <sys/types.h>
#include <stdio.h>
/* código del mandato ls */
#include <sys/types.h>
#include <stdio.h>
main() {
pid_t pid;
int status;
main() {
pid = fork();
if (pid == 0)
{
execlp("ls","ls","-l",NULL);
exit(-1);
}
else
{
while (pid != wait(&status));
}
/* código del ls */
exit( 0 );
}
exit(0);
}
40
ARCOS @ UC3M
Alejandro Calderón Mateos
Repaso
wait() + exit()
/* ejecutar el mandato ls -l */
#include <sys/types.h>
#include <stdio.h>
/* código del mandato ls */
#include <sys/types.h>
#include <stdio.h>
main() {
pid_t pid;
int status;
main() {
pid = fork();
if (pid == 0)
{
execlp("ls","ls","-l",NULL);
exit(-1);
}
else
{
while (pid != wait(&status));
}
/* código del ls */
exit( 0 );
}
exit(0);
}
41
ARCOS @ UC3M
Alejandro Calderón Mateos
Repaso
wait() + exit()
/* ejecutar el mandato ls -l */
#include <sys/types.h>
#include <stdio.h>
/* código del mandato ls */
#include <sys/types.h>
#include <stdio.h>
main() {
pid_t pid;
int status;
main() {
pid = fork();
if (pid == 0)
{
execlp("ls","ls","-l",NULL);
exit(-1);
}
else
{
while (pid != wait(&status));
}
/* código del ls */
exit( 0 );
}
exit(0);
}
42
ARCOS @ UC3M
Alejandro Calderón Mateos
Repaso
wait() + exit()
/* ejecutar el mandato ls -l */
#include <sys/types.h>
#include <stdio.h>
main() {
pid_t pid;
int status;
pid = fork();
if (pid == 0)
{
execlp("ls","ls","-l",NULL);
exit(-1);
}
else
{
0
while (pid != wait(&status));
}
exit(0);
}
43
ARCOS @ UC3M
Alejandro Calderón Mateos
Repaso
wait() + exit()
/* ejecutar el mandato ls -l */
#include <sys/types.h>
#include <stdio.h>
main() {
pid_t pid;
int status;
pid = fork();
if (pid == 0)
{
execlp("ls","ls","-l",NULL);
exit(-1);
}
else
{
while (pid != wait(&status));
}
exit(0);
}
44
ARCOS @ UC3M
Alejandro Calderón Mateos
Repaso
wait() + exit()
45
ARCOS @ UC3M
Alejandro Calderón Mateos
Contenidos
• Entendiendo el fork, exec y wait
• fork+exec+exit simple
• fork+exec+exit múltiple
46
ARCOS @ UC3M
Alejandro Calderón Mateos
Repaso
múltiples procesos (bloqueante)
#include <sys/types.h>
#include <stdio.h>
main() {
pid_t pid;
int status;
pid = fork();
if (pid != 0) {
while (pid != wait(&status));
wait
}
else
{
/* hacer algo */
exit(3);
}
pid = fork();
if (pid != 0) {
while (pid != wait(&status));
wait
}
else
{
/* hacer algo */
exit(3);
}
}
47
ARCOS @ UC3M
Alejandro Calderón Mateos
Repaso
múltiples procesos (bloqueante)
#include <sys/types.h>
#include <stdio.h>
main() {
pid_t pid;
int status;
pid = fork();
if (pid != 0) {
while (pid != wait(&status));
wait
}
else
{
/* hacer algo */
exit(3);
}
pid = fork();
if (pid != 0) {
while (pid != wait(&status));
wait
}
else
{
/* hacer algo */
exit(3);
}
}
48
ARCOS @ UC3M
Alejandro Calderón Mateos
Repaso
múltiples procesos (bloqueante)
#include <sys/types.h>
#include <stdio.h>
#include <sys/types.h>
#include <stdio.h>
main() {
pid_t pid;
int status;
main() {
pid_t pid;
int status;
pid = fork();
if (pid == 0) {
/* hacer algo */
exit(3);
}
else
{
while (pid != wait(&status));
wait
}
pid = fork();
if (pid != 0) {
while (pid != wait(&status));
wait
}
else
{
/* hacer algo */
exit(3);
}
pid = fork();
if (pid != 0) {
while (pid != wait(&status));
wait
}
else
{
/* hacer algo */
exit(3);
}
pid = fork();
if (pid == 0) {
/* hacer algo */
exit(3);
}
else
{
while (pid != wait(&status));
wait
}
}
}
49
ARCOS @ UC3M
Alejandro Calderón Mateos
Repaso
múltiples procesos (bloqueante)
#include <sys/types.h>
#include <stdio.h>
#include <sys/types.h>
#include <stdio.h>
main() {
pid_t pid;
int status;
main() {
pid_t pid;
int status;
pid = fork();
if (pid == 0) {
/* hacer algo */
exit(3);
}
else
{
while (pid != wait(&status));
wait
}
pid = fork();
if (pid != 0) {
while (pid != wait(&status));
wait
}
else
{
/* hacer algo */
exit(3);
}
pid = fork();
if (pid != 0) {
while (pid != wait(&status));
wait
}
else
{
/* hacer algo */
exit(3);
}
pid = fork();
if (pid == 0) {
/* hacer algo */
exit(3);
}
else
{
while (pid != wait(&status));
wait
}
}
}
50
ARCOS @ UC3M
Alejandro Calderón Mateos
Repaso
múltiples procesos (bloqueante)
#include <sys/types.h>
#include <stdio.h>
#include <sys/types.h>
#include <stdio.h>
#include <sys/types.h>
#include <stdio.h>
main() {
pid_t pid;
int status;
main() {
pid_t pid;
int status;
main() {
pid_t pid;
int status;
pid = fork();
if (pid != 0) {
while (pid != wait(&status));
wait
}
else
{
/* hacer algo */
exit(3);
}
pid = fork();
if (pid != 0) {
while (pid != wait(&status));
wait
}
else
{
/* hacer algo */
exit(3);
}
}
pid = fork();
if (pid == 0) {
/* hacer algo */
exit(3);
}
else
{
while (pid != wait(&status));
wait
}
pid = fork();
if (pid == 0) {
/* hacer algo */
exit(3);
}
else
{
while (pid != wait(&status));
wait
}
pid = fork();
if (pid == 0) {
/* hacer algo */
exit(3);
}
else
{
while (pid != wait(&status));
wait
}
pid = fork();
if (pid == 0) {
/* hacer algo */
exit(3);
}
else
{
while (pid != wait(&status));
wait
}
}
}
51
ARCOS @ UC3M
Alejandro Calderón Mateos
Repaso
múltiples procesos (bloqueante)
#include <sys/types.h>
#include <stdio.h>
#include <sys/types.h>
#include <stdio.h>
#include <sys/types.h>
#include <stdio.h>
main() {
pid_t pid;
int status;
main() {
pid_t pid;
int status;
main() {
pid_t pid;
int status;
pid = fork();
if (pid != 0) {
while (pid != wait(&status));
wait
}
else
{
/* hacer algo */
exit(3);
}
pid = fork();
if (pid != 0) {
while (pid != wait(&status));
wait
}
else
{
/* hacer algo */
exit(3);
}
}
pid = fork();
if (pid == 0) {
/* hacer algo */
exit(3);
}
else
{
while (pid != wait(&status));
wait
}
pid = fork();
if (pid == 0) {
/* hacer algo */
exit(3);
}
else
{
while (pid != wait(&status));
wait
}
pid = fork();
if (pid == 0) {
/* hacer algo */
exit(3);
}
else
{
while (pid != wait(&status));
wait
}
pid = fork();
if (pid == 0) {
/* hacer algo */
exit(3);
}
else
{
while (pid != wait(&status));
wait
}
}
}
52
ARCOS @ UC3M
Alejandro Calderón Mateos
Repaso
múltiples procesos (no bloqueante)
#include <sys/types.h>
#include <stdio.h>
main() {
pid_t pid;
int status;
pid = fork();
if (pid == 0) {
/* hacer algo */
exit(3);
}
pid = fork();
if (pid == 0) {
/* hacer algo */
exit(3);
}
while (pid != wait(&status));
wait
}
53
ARCOS @ UC3M
Alejandro Calderón Mateos
Repaso
múltiples procesos (no bloqueante)
#include <sys/types.h>
#include <stdio.h>
main() {
pid_t pid;
int status;
pid = fork();
if (pid == 0) {
/* hacer algo */
exit(3);
}
pid = fork();
if (pid == 0) {
/* hacer algo */
exit(3);
}
while (pid != wait(&status));
wait
}
54
ARCOS @ UC3M
Alejandro Calderón Mateos
Repaso
múltiples procesos (no bloqueante)
#include <sys/types.h>
#include <stdio.h>
#include <sys/types.h>
#include <stdio.h>
main() {
pid_t pid;
int status;
main() {
pid_t pid;
int status;
pid = fork();
if (pid == 0) {
/* hacer algo */
exit(3);
}
pid = fork();
if (pid == 0) {
/* hacer algo */
exit(3);
}
pid = fork();
if (pid == 0) {
/* hacer algo */
exit(3);
}
pid = fork();
if (pid == 0) {
/* hacer algo */
exit(3);
}
while (pid != wait(&status));
wait
while (pid != wait(&status));
wait
}
}
55
ARCOS @ UC3M
Alejandro Calderón Mateos
Repaso
múltiples procesos (no bloqueante)
#include <sys/types.h>
#include <stdio.h>
#include <sys/types.h>
#include <stdio.h>
#include <sys/types.h>
#include <stdio.h>
main() {
pid_t pid;
int status;
main() {
pid_t pid;
int status;
main() {
pid_t pid;
int status;
pid = fork();
if (pid == 0) {
/* hacer algo */
exit(3);
}
pid = fork();
if (pid == 0) {
/* hacer algo */
exit(3);
}
while (pid != wait(&status));
wait
}
pid = fork();
if (pid == 0) {
/* hacer algo */
exit(3);
}
pid = fork();
if (pid == 0) {
/* hacer algo */
exit(3);
}
pid = fork();
if (pid == 0) {
/* hacer algo */
exit(3);
}
pid = fork();
if (pid == 0) {
/* hacer algo */
exit(3);
}
while (pid != wait(&status));
wait
while (pid != wait(&status));
wait
}
}
56
ARCOS @ UC3M
Alejandro Calderón Mateos
Repaso
múltiples procesos (no bloqueante)
#include <sys/types.h>
#include <stdio.h>
#include <sys/types.h>
#include <stdio.h>
#include <sys/types.h>
#include <stdio.h>
main() {
pid_t pid;
int status;
main() {
pid_t pid;
int status;
main() {
pid_t pid;
int status;
pid = fork();
if (pid == 0) {
/* hacer algo */
exit(3);
}
pid = fork();
if (pid == 0) {
/* hacer algo */
exit(3);
}
while (pid != wait(&status));
wait
}
pid = fork();
if (pid == 0) {
/* hacer algo */
exit(3);
}
pid = fork();
if (pid == 0) {
/* hacer algo */
exit(3);
}
pid = fork();
if (pid == 0) {
/* hacer algo */
exit(3);
}
pid = fork();
if (pid == 0) {
/* hacer algo */
exit(3);
}
while (pid != wait(&status));
wait
while (pid != wait(&status));
wait
}
}
57
ARCOS @ UC3M
Alejandro Calderón Mateos
Repaso
múltiples procesos (no bloqueante)
#include <sys/types.h>
#include <stdio.h>
#include <sys/types.h>
#include <stdio.h>
#include <sys/types.h>
#include <stdio.h>
main() {
pid_t pid;
int status;
main() {
pid_t pid;
int status;
main() {
pid_t pid;
int status;
pid = fork();
if (pid == 0) {
/* hacer algo */
exit(3);
}
pid = fork();
if (pid == 0) {
/* hacer algo */
exit(3);
}
while (pid != wait(&status));
wait
}
pid = fork();
if (pid == 0) {
/* hacer algo */
exit(3);
}
pid = fork();
if (pid == 0) {
/* hacer algo */
exit(3);
}
pid = fork();
if (pid == 0) {
/* hacer algo */
exit(3);
}
pid = fork();
if (pid == 0) {
/* hacer algo */
exit(3);
}
while (pid != wait(&status));
wait
while (pid != wait(&status));
wait
}
}
58
ARCOS @ UC3M
Alejandro Calderón Mateos
Repaso
múltiples procesos (no bloqueante)
#include <sys/types.h>
#include <stdio.h>
#include <sys/types.h>
#include <stdio.h>
#include <sys/types.h>
#include <stdio.h>
main() {
pid_t pid;
int status;
main() {
pid_t pid;
int status;
main() {
pid_t pid;
int status;
pid = fork();
if (pid == 0) {
/* hacer algo */
exit(3);
}
pid = fork();
if (pid == 0) {
/* hacer algo */
exit(3);
}
while (pid != wait(&status));
wait
}
pid = fork();
if (pid == 0) {
/* hacer algo */
exit(3);
}
pid = fork();
if (pid == 0) {
/* hacer algo */
exit(3);
}
pid = fork();
if (pid == 0) {
/* hacer algo */
exit(3);
}
pid = fork();
if (pid == 0) {
/* hacer algo */
exit(3);
}
while (pid != wait(&status));
wait
while (pid != wait(&status));
wait
}
}
59
ARCOS @ UC3M
Alejandro Calderón Mateos
Repaso
múltiples procesos (no bloqueante)
#include <sys/types.h>
#include <stdio.h>
#include <sys/types.h>
#include <stdio.h>
#include <sys/types.h>
#include <stdio.h>
main() {
pid_t pid;
int status;
main() {
pid_t pid;
int status;
main() {
pid_t pid;
int status;
pid = fork();
if (pid == 0) {
/* hacer algo */
exit(3);
}
pid = fork();
if (pid == 0) {
/* hacer algo */
exit(3);
}
while (pid != wait(&status));
wait
}
pid = fork();
if (pid == 0) {
/* hacer algo */
exit(3);
}
pid = fork();
if (pid == 0) {
/* hacer algo */
exit(3);
}
pid = fork();
if (pid == 0) {
/* hacer algo */
exit(3);
}
pid = fork();
if (pid == 0) {
/* hacer algo */
exit(3);
}
while (pid != wait(&status));
wait
while (pid != wait(&status));
wait
}
}
60
ARCOS @ UC3M
Alejandro Calderón Mateos
Repaso
múltiples procesos (no bloqueante)
#include <sys/types.h>
#include <stdio.h>
#include <sys/types.h>
#include <stdio.h>
#include <sys/types.h>
#include <stdio.h>
main() {
pid_t pid;
int status;
main() {
pid_t pid;
int status;
main() {
pid_t pid;
int status;
pid = fork();
if (pid == 0) {
/* hacer algo */
exit(3);
}
pid = fork();
if (pid == 0) {
/* hacer algo */
exit(3);
}
while (pid != wait(&status));
wait
}
pid = fork();
if (pid == 0) {
/* hacer algo */
exit(3);
}
pid = fork();
if (pid == 0) {
/* hacer algo */
exit(3);
}
pid = fork();
if (pid == 0) {
/* hacer algo */
exit(3);
}
pid = fork();
if (pid == 0) {
/* hacer algo */
exit(3);
}
while (pid != wait(&status));
wait
while (pid != wait(&status));
wait
}
}
61
ARCOS @ UC3M
Alejandro Calderón Mateos
Agenda
Linux
62
Servicios para
procesos
Ejercicios
ARCOS @ UC3M
Alejandro Calderón Mateos
Parcial del curso 2010-2011
Ejercicio 2 (3 puntos)
• Se desea implementar un proceso que lea un
número desde el teclado y comience a escribir
una secuencia de números consecutivos a
partir del número introducido.
Simultáneamente deberá seguir leyendo del
teclado y en el momento en que se produzca
otra entrada cambiará la secuencia de
números que aparecen por pantalla.
63
ARCOS @ UC3M
Alejandro Calderón Mateos
Parcial del curso 2010-2011
Ejercicio 2 (3 puntos)
• Un ejemplo de ejecución sería el siguiente:
$> miprograma
27
28
29
30
31
32
5
6
7
8
3
4
5
0
Fin del programa
$> -
64
Número introducido por teclado
Número introducido por teclado
Número introducido por teclado
Número introducido por teclado
ARCOS @ UC3M
Alejandro Calderón Mateos
Parcial del curso 2010-2011
Ejercicio 2 (3 puntos)
• Puesto que no se pueden simultanear en un mismo
proceso la lectura teclado y escritura en la pantalla,
se decide realizar un programa con:
– un proceso padre que continuamente está leyendo por
teclado, si la entrada es un número entero lanza un
proceso hijo cuya misión consiste en escribir números
consecutivos a partir del introducido.
– Cuando el usuario introduzca otro número el padre mata al
proceso que estaba escribiendo y lanza un nuevo hijo con
la misma labor.
– El programa termina cuando el usuario introduce un 0, en
este caso el padre mata al proceso en ejecución y termina
el programa sacando por pantalla el literal
“Fin del programa”
65
ARCOS @ UC3M
Alejandro Calderón Mateos
Parcial del curso 2010-2011
Ejercicio 2 (3 puntos)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main ( void )
{
int contador;
pid_t pid;
scanf (“%d”,&contador);
while (contador)
{
pid = fork();
switch (pid)
{
case 0:
/* estoy en el hijo */
while (1) { printf(“%d”,++contador ); sleep(1); }
case –1:
perror(“error al ejecutar el fork”)
exit(-1) ;
default:
scanf (“%d”, &contador);
kill(pid,9);
}
}
printf(“Fin del programa”);
}
66
ARCOS @ UC3M
Alejandro Calderón Mateos
Final del curso 2006-2007
Ejercicio 2 (3 puntos)
• Se desea un programa que cree tres procesos y que,
independientemente del orden de ejecución de esos
tres procesos, siempre imprima estos mensajes el en
orden dado:
1.
2.
3.
4.
67
"Soy H1\n” (Ejecuta primero el Hijo 1)
"Soy H2\n” (Luego ejecuta el Hijo 2)
"Soy P\n” (Finalmente ejecuta el padre)
"Adiós\n”
ARCOS @ UC3M
Alejandro Calderón Mateos
Final del curso 2006-2007
Ejercicio 2 (3 puntos)
• Para ello se parte del siguiente fragmento de pseudocódigo:
int fd;
int pidH1;
int pidH2;
...
pidH1 = fork();
/* Padre de los dos hijos (H1 y H2) */
1
Escribir_bloqueante("Soy P\n”);
Escribir_bloqueante("Adiós\n”);
2
/* Hijo 1 */
Escribir_bloqueante("Soy H1\n”);
3
/* Hijo 2 */
Escribir_bloqueante("Soy H2\n”);
4
68
ARCOS @ UC3M
Alejandro Calderón Mateos
Final del curso 2006-2007
Ejercicio 2 (3 puntos)
• Se pide que rellene la siguiente tabla indicando qué llamadas
al sistema (fork, exec, exit, wait y getpid) que es necesario
realizar en los puntos indicados en el código anterior (1-4):
Zona del código
Llamadas a añadir
1
2
3
4
• Nota: Escribir_bloqueante queda el proceso bloqueado hasta
terminar de escribir la cadena de caracteres dada por parámetro.
69
ARCOS @ UC3M
Alejandro Calderón Mateos
Final del curso 2006-2007
Ejercicio 2 (3 puntos)
Zona del
código
Solución 1
1
wait
2
exit
3
4
70
fork
wait
exit
exit
Solución 2
wait
fork
work
exit
exit
exit
ARCOS @ UC3M
Alejandro Calderón Mateos
Sistemas Operativos
sesión 10: procesos
Grado en Ingeniería Informática
Universidad Carlos III de Madrid
ARCOS @ UC3M
Alejandro Calderón Mateos

Documentos relacionados