Browse Source

pthread: merge conflict

Douglas Andreani 5 years ago
parent
commit
7d6e0fae05
6 changed files with 184 additions and 54 deletions
  1. 13 5
      pthread/Makefile
  2. 9 49
      pthread/example.c
  3. 12 0
      pthread/example.h
  4. 60 0
      pthread/example2.c
  5. 21 0
      pthread/example2.h
  6. 69 0
      pthread/foo.c

+ 13 - 5
pthread/Makefile

@@ -1,10 +1,18 @@
 CC=/usr/bin/gcc
-CFLAGS=-Wall -Werror -fno-builtin -O0 -g -pthread -m64 -std=iso9899:1990
+CFLAGS=-Wall -Werror -fno-builtin -O0 -g -pthread -m64 -std=iso9899:1999
 OBJ=bin/obj
-OBJS=$(OBJ)/example.o
+OBJS=$(OBJ)/example.o $(OBJ)/example2.o $(OBJ)/foo.o
 
-all: $(OBJS)
+all: bin/example
+
+bin/example: $(OBJS)
 	$(CC) $(CFLAGS) -o bin/example $(OBJS)
 
-bin/obj/example.o:
-	$(CC) $(CFLAGS) -c -o bin/obj/example.o example.c
+bin/obj/foo.o: example.h example2.h
+	$(CC) $(CFLAGS) -c -o $(OBJ)/foo.o foo.c
+
+bin/obj/example.o: example.c
+	$(CC) $(CFLAGS) -c -o $(OBJ)/example.o example.c
+
+bin/obj/example2.o: example2.c
+	$(CC) $(CFLAGS) -c -o $(OBJ)/example2.o example2.c

+ 9 - 49
pthread/example.c

@@ -1,59 +1,19 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <pthread.h>
+#include "example.h"
 
-struct fibo {
-	int limit;
-	int answer;
-};
-
-int 
-fib(int n)
+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));
+	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)
+void *runner1(void *arg)
 {
-	struct fibo* fibl = (struct fibo*) 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);
-
-}

+ 12 - 0
pthread/example.h

@@ -0,0 +1,12 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <pthread.h>
+
+struct fibo {
+	int limit;
+	int answer;
+};
+
+int fib(int n);
+
+void *runner1(void *arg);

+ 60 - 0
pthread/example2.c

@@ -0,0 +1,60 @@
+#include "example2.h"
+
+void *produce(void *buf)
+{
+	buffer *buff;
+	buff = (buffer *) buf;
+	printf("\nproducer started");
+
+	for (int i = 0; i < 20; i++) {
+		sem_wait(&buff->full);
+		pthread_mutex_lock(&buff->mutex);
+
+		buff->data[buff->pos] = 0;
+		buff->pos = buff->pos + 1;
+		printf("\nproduced");
+
+		sem_post(&buff->empty);
+		pthread_mutex_unlock(&buff->mutex);
+
+		sleep(1);
+	}
+
+	pthread_exit(0);
+}
+
+void *consume(void *buf)
+{
+
+	buffer *buff;
+	buff = (buffer *) buf;
+	printf("\nconsumer started");
+	for (int i = 0; i < 20; i++) {
+		sem_wait(&buff->empty);
+		pthread_mutex_lock(&buff->mutex);
+
+		buff->pos = buff->pos - 1;
+		printf("\ngot value %d", buff->data[buff->pos]);
+
+		printf("\nconsumed");
+
+		sem_post(&buff->full);
+
+		pthread_mutex_unlock(&buff->mutex);
+		sleep(2);
+	}
+	pthread_exit(0);
+}
+
+void init_buffer(buffer * buf)
+{
+
+	sem_init(&buf->empty, 0, 0);
+	sem_init(&buf->full, 0, BUFFER_SIZE);
+	pthread_mutex_init(&buf->mutex, NULL);
+	buf->pos = 0;
+
+	for (int i = 0; i < BUFFER_SIZE; i++) {
+		buf->data[i] = -1;
+	}
+}

+ 21 - 0
pthread/example2.h

@@ -0,0 +1,21 @@
+#include <pthread.h>
+#include <stdlib.h>
+#include <semaphore.h>
+#include <stdio.h>
+#include <stdio.h>
+#include <unistd.h>
+
+#define BUFFER_SIZE 10
+
+typedef struct buffer {
+	sem_t empty;
+	sem_t full;
+	pthread_mutex_t mutex;
+	int pos;
+	int data[BUFFER_SIZE];
+
+} buffer;
+
+void *produce(void *buf);
+void *consume(void *buf);
+void init_buffer(buffer * buf);

+ 69 - 0
pthread/foo.c

@@ -0,0 +1,69 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <pthread.h>
+
+#include "example.h"
+#include "example2.h"
+
+int example1(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);
+
+}
+
+
+int example2(int argc, char **argv)
+{
+
+	pthread_t id[2];
+	pthread_attr_t attr;
+	buffer buf;
+
+	init_buffer(&buf);
+
+	pthread_attr_init(&attr);
+	pthread_create(&id[0], &attr, produce, &buf);
+	pthread_create(&id[1], &attr, consume, &buf);
+
+	pthread_join(id[0], NULL);
+	pthread_join(id[1], NULL);
+
+	sem_destroy(&buf.empty);
+	pthread_mutex_destroy(&buf.mutex);
+
+	printf("\nProgram finished");
+	return (EXIT_SUCCESS);
+}
+
+int main(int argc, char **argv)
+{
+	example2(argc, argv);
+}