Sei sulla pagina 1di 2

Examen de Final de Programación de Sistemas

1. (3 puntos) Dado el siguiente código: 7 i f ( f o r k ( ) == 0 ) { e x i t ( 0 ) ; }


8 }
1 int c o u n t e r = 0 ;
9 while ( w a i t (NULL) != −1);
2 int main ( ) {
10 p r i n t f ( ” c o u n t e r = % d\n” , c o u n t e r ) ;
3 int i ;
11 return 0 ;
4 f o r ( i = 0 ; i < 2 ; i ++){
12 }
5 fork ( ) ;
6 c o u n t e r ++;
7 p r i n t f ( ” c o u n t e r = % d\n” , c o u n t e r ) ; a) Diga si el programa siempre imprime el mismo valor
8 } para counter, en cada ejecución:
9 p r i n t f ( ” c o u n t e r = % d\n” , c o u n t e r ) ;
b) Si la respuesta es SI, muestre el valor de counter,
10 return 0 ;
si la respuesta es NO, muestre todos los posibles
11 }
valores de counter.

a) Número de veces que se imprime el valor de 4. (3 puntos) Dado el siguiente código:


counter:
1 int v a l = 1 0 ;
b) Primer valor de counter impreso: 2 void h a n d l e r ( s i g ) {
c) Último valor counter impreso: 3 v a l += 5 ;
4 return ;
2. (4 puntos) Dado el siguiente código: 5 }
6 int main ( ) {
1 p i d t pid ;
7 int p i d ;
2 int c o u n t e r = 0 ;
8 s i g n a l (SIGCHLD, h a n d l e r ) ;
3 void h a n d l e r 1 ( int s i g ) {
9 i f ( ( p i d = f o r k ( ) ) == 0 ) {
4 c o u n t e r ++;
10 v a l −= 3 ;
5 p r i n t f ( ” c o u n t e r = % d\n” , c o u n t e r ) ;
11 exit (0);
6 f f l u s h ( stdout ) ;
12 }
7 k i l l ( pid , SIGUSR1 ) ;
13 w a i t p i d ( pid , NULL, 0 ) ;
8 }
14 p r i n t f ( ” v a l = % d\n” , v a l ) ;
9 void h a n d l e r 2 ( int s i g ) {
15 exit (0);
10 c o u n t e r += 3 ;
16 }
11 p r i n t f ( ” c o u n t e r = % d\n” , c o u n t e r ) ;
12 exit (0);
13 } Diga el valor que se imprime.
14 main ( ) {
5. (4 puntos) Considere M y N fueron declaradas usando
15 s i g n a l ( SIGUSR1 , h a n d l e r 1 ) ;
#define, dado el siguiente código:
16 i f ( ( p i d = f o r k ( ) ) == 0 ) {
17 s i g n a l ( SIGUSR1 , h a n d l e r 2 ) ; 1 int mat1 [M] [ N ] ; int mat2 [N ] [M] ;
18 k i l l ( g e t p p i d ( ) , SIGUSR1 ) ; 2 int sum element ( int i , int j ) {
19 while ( 1 ) { } ; 3 return mat1 [ i ] [ j ] + mat2 [ i ] [ j ] ;
20 } else { 4 }
21 p i d t p ; int s t a t u s ;
22 i f ( ( p = w a i t (& s t a t u s ) ) > 0 ) { Suponga que el código asembler generado fué:
23 c o u n t e r += 2 ;
24 p r i n t f ( ” c o u n t e r = % d\n” , c o u n t e r ) ; sum_element:
25 } pushl %ebp
26 } movl %esp,%ebp
27 } movl 8(%ebp),%eax
movl 12(%ebp),%ecx
Diga qué imprime el programa. sall $2,%ecx
leal 0(,%eax,8),%edx
3. (3 puntos) Dado el siguiente código:
subl %eax,%edx
1 int c o u n t e r = 0 ; leal (%eax,%eax,4),%eax
2 void h a n d l e r ( int s i g ) { c o u n t e r ++; } movl mat2(%ecx,%eax,4),%eax
3 int main ( ) { addl mat1(%ecx,%edx,4),%eax
4 int i ; movl %ebp,%esp
5 s i g n a l (SIGCHLD, h a n d l e r ) ; popl %ebp
6 f o r ( i = 0 ; i < 5 ; i ++){ ret

1
Diga el valor de M y N.
6. (3 puntos) Diga qué función le corresponde al código
asembler generado
1 int fun7 ( int a ) { return a ∗ 3 0 ; }
2 int fun8 ( int a ) { return a ∗ 3 4 ; }
3 int fun9 ( int a ) { return a ∗ 1 8 ; }

pushl %ebp
movl %esp,%ebp
movl 8(%ebp),%eax
sall $4,%eax
addl 8(%ebp),%eax
addl %eax,%eax
movl %ebp,%esp
popl %ebp
ret

Potrebbero piacerti anche