aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--print_foobar_alternately.c81
-rw-r--r--print_foobar_alternately.py68
2 files changed, 149 insertions, 0 deletions
diff --git a/print_foobar_alternately.c b/print_foobar_alternately.c
new file mode 100644
index 0000000..6892b2c
--- /dev/null
+++ b/print_foobar_alternately.c
@@ -0,0 +1,81 @@
+// 1115. Print FooBar Alternately
+#include "leetcode.h"
+#include <semaphore.h>
+
+/*
+ * suppose you are given the following code:
+ class FooBar {
+ public void foo() {
+ for (int i = 0; i < n; i++) {
+ print("foo");
+ }
+ }
+
+ public void bar() {
+ for (int i = 0; i < n; i++) {
+ print("bar");
+ }
+ }
+ }
+ * the same instance of 'FooBar' will be passed to two different threads: thread
+ * 'a' will call 'foo()' and thread 'b' will call 'bar()'. modify the given
+ * program to output "foobar" 'n' times.
+ */
+
+typedef struct {
+ int n;
+ sem_t s_foo;
+ sem_t s_bar;
+} FooBar;
+
+// Function declarations. Do not change or remove this line
+void printFoo();
+void printBar();
+
+FooBar *fooBarCreate(int n) {
+ FooBar *obj = (FooBar *)malloc(sizeof(FooBar));
+ obj->n = n;
+ sem_init(&obj->s_foo, 0, 0);
+ sem_init(&obj->s_bar, 0, 1);
+ return obj;
+}
+
+void foo(FooBar *obj) {
+ for (int i = 0; i < obj->n; i++) {
+ sem_wait(&obj->s_bar);
+ // printFoo() outputs "foo". Do not change or remove this line.
+ printFoo();
+ sem_post(&obj->s_foo);
+ }
+}
+
+void bar(FooBar *obj) {
+ for (int i = 0; i < obj->n; i++) {
+ sem_wait(&obj->s_foo);
+ // printBar() outputs "bar". Do not change or remove this line.
+ printBar();
+ sem_post(&obj->s_bar);
+ }
+}
+
+void fooBarFree(FooBar *obj) {
+ sem_destroy(&obj->s_foo);
+ sem_destroy(&obj->s_bar);
+ if (obj)
+ free(obj);
+}
+
+int main() {
+ FooBar *f1 = fooBarCreate(1);
+ FooBar *f2 = fooBarCreate(2);
+ foo(f1);
+ bar(f1);
+ foo(f2);
+ bar(f2);
+ fooBarFree(f1);
+ fooBarFree(f2);
+}
+
+void printFoo() { printf("foo"); }
+
+void printBar() { printf("bar"); }
diff --git a/print_foobar_alternately.py b/print_foobar_alternately.py
new file mode 100644
index 0000000..d8b121a
--- /dev/null
+++ b/print_foobar_alternately.py
@@ -0,0 +1,68 @@
+# 1115. Print FooBar Alternately
+from threading import Semaphore
+
+"""
+suppose you are given the following code:
+class FooBar {
+public void foo() {
+ for (int i = 0; i < n; i++) {
+ print("foo");
+ }
+}
+
+public void bar() {
+ for (int i = 0; i < n; i++) {
+ print("bar");
+ }
+}
+}
+the same instance of 'FooBar' will be passed to two different threads: thread
+'a' will call 'foo()' and thread 'b' will call 'bar()'. modify the given
+program to output "foobar" 'n' times.
+"""
+
+
+def printFoo():
+ print("foo")
+
+
+def printBar():
+ print("bar")
+
+
+class FooBar(object):
+ def __init__(self, n):
+ self.n = n
+ self.s_foo = Semaphore(1)
+ self.s_bar = Semaphore(0)
+
+ def foo(self, printFoo):
+ """
+ :type printFoo: method
+ :rtype: void
+ """
+ for i in range(self.n):
+ self.s_foo.acquire()
+ # printFoo() outputs "foo". Do not change or remove this line.
+ printFoo()
+ self.s_bar.release()
+
+ def bar(self, printBar):
+ """
+ :type printBar: method
+ :rtype: void
+ """
+ for i in range(self.n):
+ self.s_bar.acquire()
+ # printBar() outputs "bar". Do not change or remove this line.
+ printBar()
+ self.s_foo.release()
+
+
+if __name__ == "__main__":
+ f1 = FooBar(1)
+ f2 = FooBar(2)
+ f1.foo()
+ f1.bar()
+ f2.foo()
+ f2.bar()