From d8486dab000de228fdc861d6a42d1491beeee337 Mon Sep 17 00:00:00 2001 From: Leandro Lucarella Date: Wed, 29 Mar 2006 07:06:32 +0000 Subject: [PATCH] Concurrentes I. --- ej1.6.py | 20 ++++++++++++++++++++ ej1.9.py | 23 +++++++++++++++++++++++ minimo.py | 37 +++++++++++++++++++++++++++++++++++++ thread.java | 24 ++++++++++++++++++++++++ thread2.java | 26 ++++++++++++++++++++++++++ 5 files changed, 130 insertions(+) create mode 100755 ej1.6.py create mode 100755 ej1.9.py create mode 100644 minimo.py create mode 100644 thread.java create mode 100644 thread2.java diff --git a/ej1.6.py b/ej1.6.py new file mode 100755 index 0000000..d531082 --- /dev/null +++ b/ej1.6.py @@ -0,0 +1,20 @@ +#!/usr/bin/env python +# vim: set et sw=4 sts=4 ts=4 + +import os, time, sys + +if __name__ == '__main__': + pid = os.fork() + if pid: + for i in xrange(100): + print "Soy el padre de", pid + time.sleep(0.011) + (pid, status) = os.waitpid(pid, 0) + print "Termino mi hijito", pid, "con estado", status + sys.exit(0) + else: + for i in xrange(100): + print "Soy el hijo!" + time.sleep(0.01) + sys.exit(1) + diff --git a/ej1.9.py b/ej1.9.py new file mode 100755 index 0000000..c4eeee8 --- /dev/null +++ b/ej1.9.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python +# vim: set et sw=4 sts=4 ts=4 + +import os, time, sys, random + +def fork_child(): + pid = os.fork() + if not pid: + random.seed(time.time()) + timeout = int(random.random() * 30) + # Hijo + print "HIJO: Voy a esperar", timeout, "segundos" + time.sleep(timeout) + sys.exit(timeout) + +if __name__ == '__main__': + for i in xrange(int(sys.argv[1])): + fork_child() + for i in xrange(int(sys.argv[1])): + (pid, status) = os.wait() + timeout = os.WEXITSTATUS(status) + print "PADRE: Termino", pid, "despues de esperar", timeout, "segundos" + diff --git a/minimo.py b/minimo.py new file mode 100644 index 0000000..0685c8f --- /dev/null +++ b/minimo.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python + +from threading import Thread, Lock +from sys import argv + +minimo = 0 +minimo_lock = Lock() + +class Worker(Thread): + def __init__(self, lista): + Thread.__init__(self) + self.lista = lista + def run(self): + for num in self.lista: + minimo_lock.acquire() + if num < minimo: + minimo = num + minimo_lock.release() + +cant = int(argv[1]) +lista = argv[2:] +total = len(lista) +size = total/cant +workers = [] + +print "Lista a ordenar:", lista + +for n in xrange(0, total-2): + w = Worker(lista[n*size:(n+1)*size]) + workers.append(w) + +for w in workers: + w.start() + +for w in workers: + w.join() + diff --git a/thread.java b/thread.java new file mode 100644 index 0000000..05ef25c --- /dev/null +++ b/thread.java @@ -0,0 +1,24 @@ + +// Programacion concurrente - Palma Mendez, Garrido Carrera, et al + +class ThreadHilo extends Thread +{ + String palabra; + public ThreadHilo(String frase) + { + palabra = frase; + } + public void run() + { + System.out.println(palabra); + } + static public void main(String[] args) + { + Thread a = new ThreadHilo("Pepe"); + Thread b = new ThreadHilo("Juan"); + a.start(); + b.start(); + System.out.println("Listo"); + } +} + diff --git a/thread2.java b/thread2.java new file mode 100644 index 0000000..bee8e1c --- /dev/null +++ b/thread2.java @@ -0,0 +1,26 @@ + +public class ThreadCorrer +{ + String palabra; + public ThreadCorrer(String frase) + { + palabra = frase; + } + public void run() + { + for (int i = 0; i < 10; i++) + System.out.println(palabra); + } + public static void main(String args[]) + { + ThreadCorrer a = new ThreadCorrer("Pepe"); + ThreadCorrer b = new ThreadCorrer("Luis"); + Thread t1 = new Thread(a); + Thread t2 = new Thread(b); + t1.start(); + t2.start(); + System.out.println("Listo!"); + } +} + + -- 2.43.0