Kaynağa Gözat

pthread: added semaphore example

cmte 5 yıl önce
ebeveyn
işleme
6395fbbb21
6 değiştirilmiş dosya ile 181 ekleme ve 44 silme
  1. 13 5
      pthread/Makefile
  2. 1 39
      pthread/example.c
  3. 14 0
      pthread/example.h
  4. 63 0
      pthread/example2.c
  5. 22 0
      pthread/example2.h
  6. 68 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

+ 1 - 39
pthread/example.c

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

+ 14 - 0
pthread/example.h

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

+ 63 - 0
pthread/example2.c

@@ -0,0 +1,63 @@
+#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;
+    }
+}
+

+ 22 - 0
pthread/example2.h

@@ -0,0 +1,22 @@
+#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);

+ 68 - 0
pthread/foo.c

@@ -0,0 +1,68 @@
+#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);
+}