You are on page 1of 2

CC3301 - Programación de Software de Sistemas

Auxiliar 6
Profesor: Luis Mateu
Auxiliar: Alexandra Ibarra

23 de Noviembre del 2018

1. Input / Output
1.1. Parte I

1 #i n c l u d e <s t d i o . h>
2 #i n c l u d e <d i r e n t . h> /∗ Manejo de d i r e c t o r i o s ∗/
3 #i n c l u d e <s y s / s t a t . h> /∗ Funcion s t a t y l s t a t ∗/
4 #i n c l u d e <s t r i n g . h>
5 #i n c l u d e <u n i s t d . h> /∗ Tipos y c o n s t a n t e s e s t a n d a r ∗/
6
7 void f i n d ( ) {
8 DIR ∗ c u r r e n t d i r ;
9 struct dirent ∗ this entry ;
10 struct stat status ;
11
12 i f ( ( c u r r e n t d i r=o p e n d i r ( ” . ” ) )==NULL)
13 r e t u r n ; /∗ e r r o r ∗/
14
15 f o r ( t h i s e n t r y = r e a d d i r ( c u r r e n t d i r ) ; t h i s e n t r y != NULL; t h i s e n t r y = r e a d d i r (
current dir ) ) {
16 i f ( strcmp ( t h i s e n t r y −>d name , ” . . ” ) == 0 | | strcmp ( t h i s e n t r y −>d name , ” . ” ) == 0 )
17 c o n t i n u e ; /∗ ignoramos l a c a r p e t a a c t u a l y su padre ∗/
18
19 i f ( l s t a t ( t h i s e n t r y −>d name , &s t a t u s ) != 0 )
20 c o n t i n u e ; /∗ e r r o r ∗/
21
22 if ( ! S ISDIR ( s t a t u s . st mode ) )
23 continue ;
24
25 p r i n t f ( ” %s \n” , t h i s e n t r y −>d name ) ;
26
27 /∗ cambiamos e l d i r e c t o r i o a l r e c i e n i m p r e s o ∗/
28 c h d i r ( t h i s e n t r y −>d name ) ;
29 find () ;
30 chdir (” . . ”) ;
31 }
32 closedir ( current dir ) ;
33 }
34
35 i n t main ( i n t argc , c h a r c o n s t ∗ argv [ ] ) {
36 find () ;
37 return 0;
38 }

1
2. Procesos
2.1. Parte I

1 i n t main ( i n t argc , c h a r c o n s t ∗ argv [ ] ) {


2 int i ;
3 pid t children [ argc ] ;
4
5 f o r ( i = 1 ; i < a r g c ; i ++) {
6 i f ( ( c h i l d r e n [ i ] = f o r k ( ) ) == 0 ) {
7 e x e c l p ( ” g c c ” , ” g c c ” , ”−c ” , argv [ i ] , NULL) ;
8 }
9 }
10
11 f o r ( i = 1 ; i < a r g c ; i ++) {
12 w a i t p i d ( c h i l d r e n [ i ] , NULL, 0 ) ;
13 }
14
15 return 0;
16 }

2.2. Parte II

1 #d e f i n e BUFF SIZE 8192


2
3 i n t main ( i n t argc , c h a r c o n s t ∗ argv [ ] ) {
4 int i ;
5 pid t children [ argc ] ;
6 int fds [ argc ] ;
7 c h a r buf [ BUFF SIZE + 1 ] ;
8
9 f o r ( i = 1 ; i < a r g c ; i ++) {
10 int fd [ 2 ] ;
11 i f ( p i p e ( f d ) != 0 ) {
12 p e r r o r ( ”Can ’ t p i p e ” ) ;
13 exit (1) ;
14 }
15 f d s [ i ] = f d [ 0 ] ; /∗ guardo c a n a l de l e c t u r a para l e e r d e s p u e s ∗/
16
17 i f ( ( c h i l d r e n [ i ] = f o r k ( ) ) == 0 ) {
18 c l o s e (STDERR FILENO) ; /∗ cambia s a l i d a d e l c a n a l , a s t d e r r ∗/
19 dup ( f d [ 1 ] ) ;
20 c l o s e ( f d [ 0 ] ) ; /∗ c i e r r o c a n a l de l e c t u r a ∗/
21 c l o s e ( f d [ 1 ] ) ; /∗ c i e r r o un c a n a l de e s c r i t u r a ∗/
22 e x e c l p ( ” g c c ” , ” g c c ” , ”−c ” , argv [ i ] , NULL) ;
23 }
24 else {
25 c l o s e ( f d [ 1 ] ) ; /∗ c i e r r o e l o t r o c a n a l de e s c r i t u r a ∗/
26 }
27 }
28 f o r ( i = 1 ; i < a r g c ; i ++) {
29 int n;
30 f p r i n t f ( s t d e r r , ”=== %s ===\n” , argv [ i ] ) ;
31 w h i l e ( ( n = r e a d ( f d s [ i ] , buf , BUFF SIZE ) ) > 0 ) {
32 buf [ n ] = 0 ;
33 f p r i n t f ( s t d e r r , ” %s ” , buf ) ;
34 }
35 close ( fds [ i ] ) ;
36 }
37 f o r ( i = 1 ; i < a r g c ; i ++) {
38 w a i t p i d ( c h i l d r e n [ i ] , NULL, 0 ) ;
39 }
40 return 0;
41 }

You might also like