1 / 9

Universidad Tecnológica Nacional Facultad Regional Buenos Aires

Universidad Tecnológica Nacional Facultad Regional Buenos Aires Ingeniería en Sistemas de Información. Sistemas Operativos. Funciones Útiles fork(). Fork() – Caso base. #include &lt;stdio.h&gt; int main (void) { char c; printf(&quot;Ingrese un caracter<br>&quot;); c = getchar();

Télécharger la présentation

Universidad Tecnológica Nacional Facultad Regional Buenos Aires

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Universidad Tecnológica Nacional Facultad Regional Buenos Aires Ingeniería en Sistemas de Información Sistemas Operativos Funciones Útiles fork()

  2. Fork() – Caso base #include <stdio.h> int main (void) { char c; printf("Ingrese un caracter\n"); c = getchar(); printf(“Se ingresó: %c\n", c); } ¿Cómo puedo hacer si quiero que si el usuario no presiona ninguna tecla imprima en pantalla el mensaje nuevamente cada un segundo?

  3. Fork() – Introducción • Identificador único (PID) • Padre único • Tres estructuras de memoria - Código (solo lectura) - Datos (memoria dinámica) - Stack (asignaciones estáticas) Proceso ejecutando

  4. Fork() – Introducción UID PID PPID C STIME TTY TIME CMD root 1 0 0 12:41 ? 00:00:00 init [2] 100 2173 1 0 12:41 ? 00:00:00 /usr/sbin/exim4 -bd -q30m root 2184 1 0 12:41 ? 00:00:00 /usr/sbin/inetd root 2206 1 0 12:41 ? 00:00:00 /usr/sbin/sshd statd 2210 1 0 12:41 ? 00:00:00 /sbin/rpc.statd daemon 2217 1 0 12:41 ? 00:00:00 /usr/sbin/atd root 2224 1 0 12:41 ? 00:00:00 /usr/sbin/cron root 2239 1 0 12:41 tty1 00:00:00 /bin/login -- root 2242 1 0 12:41 tty2 00:00:00 /sbin/getty 38400 tty2 root 2243 1 0 12:41 tty3 00:00:00 /sbin/getty 38400 tty3 root 2244 1 0 12:41 tty4 00:00:00 /sbin/getty 38400 tty4 root 2245 1 0 12:41 tty5 00:00:00 /sbin/getty 38400 tty5 root 2246 1 0 12:41 tty6 00:00:00 /sbin/getty 38400 tty6 root 2262 2239 0 12:42 tty1 00:00:00 -bash

  5. Fork() – Introducción Proceso en ejecución (PID x) fork() Proceso Padre (PID x) Proceso Hijo (PID Y / PPID X)

  6. Fork() – ¿Para Qué? Respuesta 1: Que los dos procesos sean iguales no quiere decir que tengan que seguir haciendo lo mismo. Leer_de_teclado() Proceso en ejecución (PID x). ahora padre de Y fork() Proceso en ejecución (PID x) Escribir_en_archivo() Proceso en ejecución (PID Y). Hijo de X

  7. Fork() – ¿Para Qué? Respuesta 1: Que los dos procesos sean iguales no quiere decir que tengan que seguir haciendo lo mismo. int main() { int pid = 0, status = 0; pid = fork() if (pid == 0) { /* SOLO Proceso Hijo */ printf(``El PID de mi proceso padre es %d\n'', getppid()); exit(1); } else { /* SOLO Proceso Padre */ printf(``Mi PID es el %d y el de mi hijo es %d\n'', getpid(), pid); exit(0); } }

  8. Fork() – Notas • El proceso hijo comparte el entorno con el proceso padre. (Descriptores abiertos) • El proceso padre debe esperar la muerte del proceso hijo para que este no sea un zombie. funcion wait() • Todo proceso puede a su vez ser padre • Al morir un hijo se recibe la señal SIGCHLD • Al morir un padre deben morir sus hijos

  9. Fork() – Exec*() La familia de funciones exec() reemplaza la imagen de proceso actual con la imagen de un proceso nuevo. if (pid == 0) { // hijo execv(“/bin/ls”, NULL); // nunca se ejecuta exit(0); } else { // padre wait (pid); exit(0); } fork() Proceso en ejecución (PID x)

More Related