Last active
August 29, 2015 14:16
-
-
Save IuryAlves/cc0209e9fe166d2af9d8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# include <stdio.h> | |
#define BUFFER_SIZE 5 | |
struct _buffer{ | |
int count; // numero de itens no buffer | |
int in; // próxima posiçãolivre | |
int out; // proxima posição cheia | |
int itens[BUFFER_SIZE]; | |
}; | |
typedef struct _buffer buffer; // define um tipo buffer | |
/* insere um elemento no buffer | |
se o buffer não estiver cheio | |
*/ | |
void insert(int item, buffer *b){ | |
while (b->count == BUFFER_SIZE){}; | |
b->count++; | |
b->itens[b->in] = item; | |
b->in = (b->in +1) % BUFFER_SIZE; | |
} | |
/* remove um elemento do buffer | |
e retorna ele, se o buffer não estiver vazio | |
caso contrário fica esperando até que o buffer não esteja cheio | |
*/ | |
int _remove(buffer *b){ | |
while(b->count == 0){}; | |
b->count--; | |
int item = b->itens[b->out]; | |
b->itens[b->out] = 0; | |
b->out = (b->out + 1) % BUFFER_SIZE; | |
return item; | |
} | |
int main(void){ | |
// cria um buffer | |
buffer b; | |
b.count = 0; | |
b.in = 0; | |
b.out = 0; | |
// insere um elemento no buffer | |
insert(2, &b); | |
printf("%i\n", b.itens[0]); | |
// remove o elemento inserido no buffer | |
int removed_item = _remove(&b); | |
printf("%i\n", removed_item); | |
printf("%i\n", b.itens[0]); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# include <stdio.h> | |
# include <stdlib.h> | |
# include <sys/types.h> | |
void division(int x, int y){ | |
/* uma função simples pra demonstrar | |
a execução de um processo filho | |
*/ | |
printf("%i / %i = %i \n", x, y, x / y); | |
} | |
int main(void){ | |
/* cria um fork do processo | |
pid_t é o tipo de dado pra | |
armazenar pid de processos */ | |
pid_t pid = fork(); | |
/* pra armazenar o status do processo filho */ | |
int status; | |
/* se o pid é 0 | |
significa que esse é o processo | |
filho */ | |
if (pid == 0){ | |
/* apenas para demonstrar um processo | |
filho dando pau */ | |
printf("Processo filho \n"); | |
printf("Executando método de divisão com as variaveis: %i e %i \n", 2, 0); | |
division(2, 0); | |
} | |
// caso contrario é o processo pai | |
else{ | |
/* espera pelo processo filho | |
morrer */ | |
wait(&status); | |
printf("Processo filho foi pro saco! com o status: %i \n", status); | |
printf("Processo pai indo pro saco também com o mesmo status ... \n"); | |
/* sai do programa com o mesmo status | |
do processo filho */ | |
exit(status); | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
import os | |
from multiprocessing import Process, Queue | |
def process(queue): | |
# pid do processo | |
pid = os.getpid() | |
# poe o pid do processo na fila | |
queue.put(pid) | |
print("Processo com o pid %i" % pid) | |
print(u"Listando diretórios") | |
# lista todos os arquivos no diretório atual | |
files_in_cur_dir = os.listdir(os.curdir) | |
# poe os arquivos do diretorio atual na fila | |
queue.put(files_in_cur_dir) | |
exit(0) | |
if __name__ == '__main__': | |
# cria uma fila | |
queue = Queue() | |
# cria um processo | |
# target eh a funcao a ser executada no processo | |
# args são os argumentos do proceso | |
process_1 = Process(target=process, args=(queue, )); | |
# inicia o processo | |
process_1.start() | |
pid = queue.get() | |
print "Arquivos listados no processo com o pid: %i \n" % pid | |
# imprime tudo que está na fila do processo | |
for f in queue.get(): | |
print("%s \n " % f) | |
exit(0); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment