Browse Source

queue_thread_model: worker model start

cmte 5 years ago
parent
commit
f51af2b46b
4 changed files with 59 additions and 23 deletions
  1. 8 3
      queue_thread_model/Makefile
  2. 40 11
      queue_thread_model/model.c
  3. 6 6
      queue_thread_model/queue.c
  4. 5 3
      queue_thread_model/queue.h

+ 8 - 3
queue_thread_model/Makefile

@@ -2,16 +2,21 @@ CC=/usr/bin/gcc
 CFLAGS=-g -Werror -pthread -pedantic -pedantic-errors -fno-fast-math -fno-builtin -m64 -std=iso9899:1999 -I${PWD}
 OUT=bin
 OBJ=$(OUT)/obj
-OBJS=$(OBJ)/queue.o
+OBJS=$(OBJ)/queue.o $(OBJ)/worker.o
 
 
-all: $(OBJS)
+all: dir $(OBJS)
 	$(CC) $(CFLAGS) model.c $(OBJS) -o $(OUT)/model
 
 $(OBJ)/queue.o:
 	$(CC) $(CFLAGS) -c queue.c -o $(OBJ)/queue.o
 
+$(OBJ)/worker.o:
+	$(CC) $(CFLAGS) -c worker.c -o $(OBJ)/worker.o
+
+dir:
+	mkdir -p bin/obj
 
 clean:
-	rm -rf *.o model
+	rm -rf bin
 

+ 40 - 11
queue_thread_model/model.c

@@ -1,27 +1,56 @@
 #include <stdio.h>
+#include <stdlib.h>
+
 #include "queue.h"
+#include "worker.h"
+
+void sum(int argc, char **values, char *buf_return)
+{
+
+	if (argc != 2) {
+		buf_return[0] = 0;
+		return;
+	} else {
+		long int a = strtol(values[0], NULL, 10);
+		long int b = strtol(values[1], NULL, 10);
+		long int sum = a + b;
+		sprintf(buf_return, "%d", sum);
+	}
+}
 
 int main(int argc, char **argv)
 {
+	worker *w, *x;
+
 
 	queue *q;
-	int i = -1;
 
 	q = initialize_queue();
+	char *buf1, *buf2;
 
-	enqueue(q, 3);
-	enqueue(q, 4);
-	enqueue(q, 5);
+	char **params1 = malloc(sizeof(char *) * 2);
+	params1[0] = "1";
+	params1[1] = "2";
 
-	i = dequeue(q);
-	printf("\nvalue: %d", i);
+	w = new_worker(sum, 2, params1, 10, buf1);
+	enqueue(q, w);
 
-	i = dequeue(q);
-	printf("\nvalue: %d", i);
+	char **params2 = malloc(sizeof(char *) * 2);
+	params2[0] = "3";
+	params2[1] = "4";
 
-	i = dequeue(q);
-	printf("\nvalue: %d", i);
+	w = new_worker(sum, 2, params2, 10, buf2);
+	enqueue(q, w);
 
-	return (0);
+	x = dequeue(q);
+	execute(x);
+
+	printf("\nValue 1: %s", x->buf_return);
+
+	x = dequeue(q);
+	execute(x);
+	printf("\nValue 2: %s", x->buf_return);
 
+
+	return (0);
 }

+ 6 - 6
queue_thread_model/queue.c

@@ -11,28 +11,28 @@ queue *initialize_queue()
 	return q;
 }
 
-void enqueue(queue * q, int value)
+void enqueue(queue * q, worker * w)
 {
-	struct node* aux;
+	struct node *aux;
 	if (q->head == NULL) {
 		q->head = (struct node *) malloc(sizeof(struct node *));
 		q->head->next = NULL;
-		q->head->value = value;
+		q->head->value = w;
 		q->tail = q->head;
 		return;
 	} else {
 		aux = (struct node *) malloc(sizeof(struct node *));
 		aux->next = NULL;
-		aux->value = value;
+		aux->value = w;
 		q->tail->next = aux;
 		q->tail = aux;
 	}
 }
 
-int dequeue(queue * q)
+worker *dequeue(queue * q)
 {
 	struct node *aux;
-	int ret_val;
+	worker *ret_val;
 
 	ret_val = q->head->value;
 	aux = q->head;

+ 5 - 3
queue_thread_model/queue.h

@@ -1,8 +1,10 @@
 #ifndef QUEUE_H
 #define QUEUE_H
 
+#include "worker.h"
+
 struct node {
-	int value;
+	worker *value;
 	struct node *next;
 };
 
@@ -12,8 +14,8 @@ typedef struct queue {
 } queue;
 
 queue *initialize_queue();
-void enqueue(queue * q, int value);
-int dequeue(queue * q);
+void enqueue(queue * q, worker * w);
+worker *dequeue(queue * q);
 void destroy_queue(queue * q);
 
 #endif