소스 검색

initial pthread and q_thread_model

cmte 5 년 전
부모
커밋
a44e3560ae
5개의 변경된 파일175개의 추가작업 그리고 0개의 파일을 삭제
  1. 59 0
      pthread/example1.c
  2. 13 0
      queue_thread_model/Makefile
  3. 27 0
      queue_thread_model/model.c
  4. 57 0
      queue_thread_model/queue.c
  5. 19 0
      queue_thread_model/queue.h

+ 59 - 0
pthread/example1.c

@@ -0,0 +1,59 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <pthread.h>
+
+struct fibo {
+	int limit;
+	int answer;
+};
+
+int 
+fib(int n)
+{
+	int i=0;
+	if (n==1 || n==0) return 1;
+	for(i=0;i<n;i++){
+		return (fib(n-1) + fib(n-2));
+	}
+	return (-1);
+}
+
+void*
+runner1(void* arg)
+{
+	struct fibo* fibl = (struct fibo*) arg;
+	fibl->answer = fib(fibl->limit);
+	pthread_exit(0);
+}
+
+int
+main (int argc, char** argv)
+{
+	
+	int i;
+	pthread_t id[argc];
+	pthread_attr_t attr;
+	struct fibo d[argc];
+
+	if (argc < 2) {
+		printf("usage: %s num1 num2 num3... numN", argv[0]);
+		return (1);
+	}
+
+	
+
+	pthread_attr_init(&attr);
+	for (i=1; i<argc;i++){
+		d[i-1].limit = atoi(argv[i]);
+		d[i-1].answer = 0;
+		pthread_create(&id[i-1], &attr, runner1, &d[i-1]);
+	}
+
+	for (i=0;i<argc-1;i++){
+		pthread_join(id[i], NULL);
+		printf("\n\nFib of limit: %d is %d", d[i].limit, d[i].answer);
+	}
+
+	return (0);
+
+}

+ 13 - 0
queue_thread_model/Makefile

@@ -0,0 +1,13 @@
+CC=/opt/gnu/bin/gcc
+CFLAGS=-g -Werror -pthread -pedantic -pedantic-errors -fno-fast-math -fno-builtin -m64 -std=iso9899:1999
+
+all: clean queue.o
+	$(CC) $(CFLAGS) model.c queue.o -o model
+
+queue.o:
+	$(CC) $(CFLAGS) -c queue.c -o queue.o
+
+
+clean:
+	rm -rf *.o model
+

+ 27 - 0
queue_thread_model/model.c

@@ -0,0 +1,27 @@
+#include <stdio.h>
+#include "queue.h"
+
+int main(int argc, char **argv)
+{
+
+	queue *q;
+	int i = -1;
+
+	q = initialize_queue();
+
+	enqueue(q, 3);
+	enqueue(q, 4);
+	enqueue(q, 5);
+
+	i = dequeue(q);
+	printf("\nvalue: %d", i);
+
+	i = dequeue(q);
+	printf("\nvalue: %d", i);
+
+	i = dequeue(q);
+	printf("\nvalue: %d", i);
+
+	return (0);
+
+}

+ 57 - 0
queue_thread_model/queue.c

@@ -0,0 +1,57 @@
+#include "queue.h"
+#include <stdlib.h>
+#include <stdio.h>
+
+queue *initialize_queue()
+{
+	queue *q;
+
+	q = (queue *) malloc(sizeof(queue *));
+
+	return q;
+}
+
+void enqueue(queue * q, int value)
+{
+	struct node* aux;
+	if (q->head == NULL) {
+		q->head = (struct node *) malloc(sizeof(struct node *));
+		q->head->next = NULL;
+		q->head->value = value;
+		q->tail = q->head;
+		return;
+	} else {
+		aux = (struct node *) malloc(sizeof(struct node *));
+		aux->next = NULL;
+		aux->value = value;
+		q->tail->next = aux;
+		q->tail = aux;
+	}
+}
+
+int dequeue(queue * q)
+{
+	struct node *aux;
+	int ret_val;
+
+	ret_val = q->head->value;
+	aux = q->head;
+	q->head = q->head->next;
+	free(aux);
+	return (ret_val);
+}
+
+void destroy_queue(queue * q)
+{
+	struct node *aux, *next;
+
+	aux = q->head;
+
+	while (aux != NULL) {
+		next = aux->next;
+		free(aux);
+		if (next != NULL)
+			aux = next;
+	}
+	free(q);
+}

+ 19 - 0
queue_thread_model/queue.h

@@ -0,0 +1,19 @@
+#ifndef QUEUE_H
+#define QUEUE_H
+
+struct node {
+	int value;
+	struct node *next;
+};
+
+typedef struct queue {
+	struct node *head;
+	struct node *tail;
+} queue;
+
+queue *initialize_queue();
+void enqueue(queue * q, int value);
+int dequeue(queue * q);
+void destroy_queue(queue * q);
+
+#endif