| | 149 | |
| | 150 | === Slide === |
| | 151 | |
| | 152 | * '''Turno 1 (11 Marzo 2009)''': [http://homes.dico.unimi.it/sisop/lucidi0809/solab02.pdf slide] ([http://homes.dico.unimi.it/sisop/lucidi0809/solab02-handout.pdf Versione stampa]) |
| | 153 | |
| | 154 | === Esercizi === |
| | 155 | |
| | 156 | ==== {{{fork}}} ==== |
| | 157 | |
| | 158 | {{{ |
| | 159 | #!c |
| | 160 | /* un programma che "forca" un nuovo processo */ |
| | 161 | |
| | 162 | #include <unistd.h> |
| | 163 | #include <stdio.h> |
| | 164 | #include <stdlib.h> |
| | 165 | |
| | 166 | int main(void){ |
| | 167 | int x = fork(); |
| | 168 | if (x < 0){ |
| | 169 | perror("Errore nella fork:"); |
| | 170 | exit(1); |
| | 171 | } else { |
| | 172 | if (x != 0){ |
| | 173 | while(1){ |
| | 174 | sleep(1); |
| | 175 | printf("Processo padre (Figlio: %d)\n", x); |
| | 176 | } |
| | 177 | } else { |
| | 178 | while(1) { |
| | 179 | printf("Processo figlio (%d)\n", x); |
| | 180 | sleep(1); |
| | 181 | } |
| | 182 | } |
| | 183 | } |
| | 184 | return 0; |
| | 185 | } |
| | 186 | |
| | 187 | /* da compilare con un comando del tipo 'cc -o prova prova.c' */ |
| | 188 | /* Per terminare Ctrl-C */ |
| | 189 | }}} |
| | 190 | |
| | 191 | ==== Esecuzione in sequenza e parallelo ==== |
| | 192 | |
| | 193 | {{{ |
| | 194 | #!c |
| | 195 | #include <stdio.h> |
| | 196 | #include <unistd.h> |
| | 197 | |
| | 198 | int main(void){ |
| | 199 | int i = 0; |
| | 200 | for (i=0; i<10; ++i){ |
| | 201 | sleep(1); |
| | 202 | printf("%d: %d\n", getpid(), i); |
| | 203 | } |
| | 204 | return 0; |
| | 205 | } |
| | 206 | }}} |
| | 207 | |
| | 208 | {{{ |
| | 209 | #!sh |
| | 210 | # In sequenza |
| | 211 | ./hello ; ./hello |
| | 212 | # In parallelo |
| | 213 | ./hello & ./hello |
| | 214 | echo $? |
| | 215 | }}} |
| | 216 | |
| | 217 | ==== Cosa fa? ==== |
| | 218 | |
| | 219 | {{{ |
| | 220 | #!sh |
| | 221 | /usr/bin/touch piripacchio |
| | 222 | while /bin/ls piripacchio; do |
| | 223 | /usr/bin/sleep 2 |
| | 224 | /bin/echo ciao |
| | 225 | done & ( /usr/bin/sleep 10 ; /bin/rm piripacchio )}} |
| | 226 | }}} |
| | 227 | |
| | 228 | Consultate il manuale! |
| | 229 | |
| | 230 | {{{ |
| | 231 | #!sh |
| | 232 | man man |
| | 233 | man touch |
| | 234 | man ls |
| | 235 | man 1 sleep # man 3 sleep è il manuale della funzione della libreria C |
| | 236 | man echo |
| | 237 | man rm |
| | 238 | }}} |
| | 239 | |
| | 240 | ==== Uso del {{{for}}} e {{{if}}} ==== |
| | 241 | |
| | 242 | {{{ |
| | 243 | #!sh |
| | 244 | for i in dog cat fish; do if ls /bin/$i; then echo Trovato $i; else echo $i non trovato; fi; done |
| | 245 | }}} |
| | 246 | |
| | 247 | {{{ |
| | 248 | #!sh |
| | 249 | man test |
| | 250 | for i in dog cat fish; do if test -f /bin/$i; then echo Trovato $i; else echo $i non trovato; fi; done |
| | 251 | }}} |
| | 252 | |
| | 253 | === Link === |
| | 254 | |
| | 255 | * [http://jrmiii.com/attachments/Vim.pdf Vi cheat sheet] |
| | 256 | |
| | 257 | |