|
@@ -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);
|
|
|
+}
|