Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <semaphore.h>
- #include <unistd.h>
- #include <cstdlib>
- #include <cstring>
- #include <cerrno>
- using namespace std;
- void* cliente(void *);
- void* servidor(void *);
- int
- main(int argc, char *argv[]) {
- sem_t *sem = new sem_t;
- if (sem_init(sem, 0, 0) == -1) {
- cerr << "Error creando el semaforo: " <<
- strerror(errno) << endl;
- return EXIT_FAILURE;
- }
- pthread_t hSrv;
- pthread_t hCln;
- pthread_create(&hSrv, nullptr, servidor, (void *) sem);
- pthread_create(&hCln, nullptr, cliente, (void *) sem);
- pthread_join(hSrv, nullptr);
- pthread_join(hCln, nullptr);
- return EXIT_SUCCESS;
- }
- void* cliente(void *arg) {
- sem_t *sem = (sem_t *) arg;
- for (;;) {
- sleep(10);
- sem_post(sem);
- }
- }
- void* servidor(void *arg) {
- sem_t *sem = (sem_t *) arg;
- for (;;) {
- cout << "Esperando para imprimir" << endl;
- sem_wait(sem);
- cout << "Impresion lista" << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment