By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,152 Members | 2,159 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,152 IT Pros & Developers. It's quick & easy.

unistd.h Problem Pls.Help me

P: 3
i'm currently using c++ on windowsXP,
unistd.h doesnt work for windows
what other substitute for it could i use to replace it
Pls. Help me Pls pls

the code goes as follows

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>

#define TRUE 1
#define FALSE 0
#define CHAIRS 3 //Sillas Disponibles
#define CANT_CUST 5 //Cantidad de Clientes
#define T_CUST 0 //Tiempo de llegada de Clientes
#define T_CORTE 3 //Tiempo de corte de pelo

typedef int semaphore;

//Prototipo de funciones
void *customer (void *);
void *barber (void *);
void up (semaphore *);
void down (semaphore *);

semaphore sem_barber=0, sem_customer=0,sem_mutex=1;
int waiting=0;

//Main function
int main (void) {
pthread_t barb_t,cust_t[CANT_CUST];
int i;

pthread_create(&barb_t,NULL,barber,NULL);
for (i=0;i<CANT_CUST;i++){
sleep(T_CUST);
pthread_create(&cust_t[i],NULL,customer,NULL);
}

pthread_join(barb_t,NULL);
return(0);
}

void *customer (void *n) {
printf ("Customer:entrando hay %d esperando\n",waiting);
down (&sem_mutex);
if (waiting < CHAIRS) {
waiting++;
up (&sem_customer);
up (&sem_mutex);
down (&sem_barber);
printf ("Customer:Me estan cortando el pelo.\n");
}
else {
up (&sem_mutex);
printf ("Customer:Me fui no hay lugar.\n");
}
}

void *barber (void *j) {
printf ("Barber:Empiezo a trabajar\n");
while (TRUE) {
down (&sem_customer);
down (&sem_mutex);
waiting--;
up (&sem_barber);
up (&sem_mutex);
printf ("Barber:Comienzo el corte de pelo de un cliente quedan %d esperand
o.\n",waiting);
sleep (T_CORTE);
printf ("Barber:Termine de cortar el pelo de un cliente quedan %d esperand
o.\n",waiting);
}
}

void up (semaphore *sem) {
*sem+=1;
}

void down (semaphore *sem) {
while (*sem<=0){};
*sem-=1;
}
Oct 2 '06 #1
Share this Question
Share on Google+
3 Replies


Banfa
Expert Mod 5K+
P: 8,916
Of course not, it's a unix header, so is pthread.h.

I think you want stdlib.h and Windows.h instread but lots of your function calls will need changing too.
Oct 2 '06 #2

P: 3
Of course not, it's a unix header, so is pthread.h.

I think you want stdlib.h and Windows.h instead but lots of your function calls will need changing too.

Thanks!!

i really am not used in using windows c++
sorry for the bad grammar
Pls help me, what functions shud i change and to change them with what??
Pls. help me pls...
Oct 2 '06 #3

Banfa
Expert Mod 5K+
P: 8,916
Well anything that is not standard C will need changing

As far as I can see the only standard C function you call is

printf

Everything else needs changing.

I suggest you look up

CreateThread
mutex
semaphore

in whatever you are using for your Windows documentation
(goto msdn.microsoft.com if all else fails)
Oct 2 '06 #4

Post your reply

Sign in to post your reply or Sign up for a free account.