C Solution using 2 semaphores
\ntypedef struct \n{\n    int n;\n    sem_t foo_;\n    sem_t bar_;\n    \n}FooBar;\n\n\n\nFooBar* fooBarCreate(int n) \n{\n    \n\n    FooBar* obj = (FooBar*) m
```\ntypedef struct \n{\n    int n;\n    sem_t foo_;\n    sem_t bar_;\n    \n}FooBar;\n\n\n\nFooBar* fooBarCreate(int n) \n{\n    \n\n    FooBar* obj = (FooBar*) malloc(sizeof(FooBar));\n    obj->n = n;\n\t//initialize the semaphores\n    sem_init(&obj->foo_, 0, 1);\n    sem_init(&obj->bar_, 0, 0);\n    return obj;\n}\n\n\n\nvoid foo(FooBar* obj) \n{\n    \n    for (int i = 0; i < obj->n; i++) \n    {\n\t\t//lock\n        sem_wait(&obj->foo_);\n           \n        // printFoo() outputs "foo". Do not change or remove this line.\n        printFoo();\n        \n\t\t//unlock\n        sem_post(&obj->bar_); \n    }\n}\n\nvoid bar(FooBar* obj) \n{\n    \n    for (int i = 0; i < obj->n; i++) \n    {\n\t\t//lock\n        sem_wait(&obj->bar_);\n\t\t\n        // printBar() outputs "bar". Do not change or remove this line.\n        printBar();\n\t\t\n\t\t//unlock\n        sem_post(&obj->foo_);\n    }\n}\n\nvoid fooBarFree(FooBar* obj) \n{\n\t//destroy the semaphores\n    sem_destroy(&obj->foo_);\n    sem_destroy(&obj->bar_);\n\t//free allocated memory\n    free(obj);\n    obj = NULL;\n}\n```
[]