Está en la página 1de 7

UNIVERSIDAD TCNICA DE MANAB

FACULTAD DE CIENCIAS INFORMATICAS CARRERA DE INGENIERIA EN SISTEMAS INFORMTICOS

INFORME DE SISTEMAS OPERATIVOS

TEMA Programas en C++ y en Java sobre Hilos y Procesos

PERTENECE A

Castro Pin Miguel ngel

CURSO Y PARALELO

Quinto Nivel Paralelo B

DOCENTE

Ing. Pablo Flores

FECHA DE ENTREGA

Martes, 06 de Agosto del 2013.

1. Programa en java que ejemplifique las llamadas al sistema copiando el contenido de un archivo en otro.
package llamada.al.sistema;

import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class LlamadaAlSistema { @SuppressWarnings("CallToThreadDumpStack") public static void main(String[] args) { File origen = new File("D:\\miguel_archivos\\archivo1.txt"); File destino = new File("D:\\miguel_archivos\\archivo2.txt");

try { InputStream in = new FileInputStream(origen); OutputStream out = new FileOutputStream(destino) byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } in.close(); out.close(); } catch (IOException ioe){ ioe.printStackTrace(); } }}

2. Programa en que ejemplifique la creacin de hilos en C.


#include <stdio.h> #include <pthread.h> #define N 8 #define M 16 const char *mensaje[2] = {"Hola Mundo", "Mundo Hola"}; const int cantidad[2] = {N, M}; void imprime_mensaje(void *ptr); int main(void) { pthread_t hilo0, hilo1; int id0=0, id1=1; pthread_create(&hilo0, NULL, (void *) &imprime_mensaje, (void *) &id0); pthread_create(&hilo1, NULL, (void *) &imprime_mensaje, (void *) &id1); pthread_join(hilo0, NULL); pthread_join(hilo1, NULL); return 0; }

void imprime_mensaje(void *ptr) { int i=0, id=0; id = *(int *) ptr; for(i=0; i <cantidad[id]; i++) printf("%s\n",mensaje[id]); return; }

3. Crear un programa donde cree 2 procesos donde se enve mensaje. El segundo proceso recibe el mensaje y lo muestra en pantalla.
/* fork.c - Ejecucin conjunta de procesos padre e hijo */ #include <stdio.h> #include <unistd.h> main () { printf ("Ejemplo de fork.\n"); printf ("Inicio del proceso padre. PID=%d\n", getpid ()); if (fork() == 0) { /* Proceso hijo */ printf ("Inicio proceso hijo. PID=%d, PPID=%d\n", getpid (), getppid ()); sleep (1); } else { /* Proceso padre */ printf ("Continuacin del padre. PID=%d\n", getpid ()); sleep (1); } printf ("Fin del proceso %d\n", getpid ()); return 0; }

4. Crear un programa en java que ejemplifique el uso de la llamada while y esperando la terminacin de un hijo mostrar un mensaje

package hilos; /** * * @author Miguel */ public class Hilos { public static void main(String[] args) {

Proceso hilopadre = new Proceso("Hilo padre"); hilopadre.crear(); } }

package hilos; /** * * @author Miguel */ public class Proceso extends Thread{ String mensaje; public Proceso(String msg) { super(msg); } @Override public void run() { for(int i =0;i<15;i++) { System.out.println(mensaje); } System.out.println("Este proceso ha terminado:"+this.getName()); }

public void setMensaje(String msj) { this.mensaje = msj; } public void crear() { Proceso hijo = new Proceso("Hilo hijo"); hijo.setMensaje("Soy el hilo hijo"); hijo.start(); }

También podría gustarte