Mario Charest wrote:
Please post it.
This is the output:
ivanova(dana):~/assign1$ ./main
Creating channels
Spawing threads…
Server starting
Server finished
Producer starting
Producer finishes
Consumer starting
Consumer starting
Consumer finished
This is the source. I don’t understand why the “Consumer starting” string
is printed twice. If I take out every \n in the code, it gets printed only
once.
Thanks!
Dana
#include <stdio.h>
#include <sys/neutrino.h>
#include <sys/netmgr.h>
#include <pthread.h>
struct two_channels {
int consumer_chid;
int producer_chid;
};
void *server (void *two_channels);
void *consumer (void *consumer_channel_id);
void *producer (void producer_channel_id);
int main (int argc, char argv[])
{
int consumer_channel_id;
int producer_channel_id;
struct two_channels producer_consumer_channels;
pthread_attr_t attr;
/* create two channels for the threads to use /
printf (“Creating channels\n”);
consumer_channel_id = ChannelCreate (0); / consumer <–> server /
producer_channel_id = ChannelCreate (0); / producer <–> server */
producer_consumer_channels.consumer_chid = consumer_channel_id;
producer_consumer_channels.producer_chid = producer_channel_id;
/* do thread init stuff */
pthread_attr_init ( &attr );
pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
/* spawn the threads */
printf(“Spawing threads…\n”);
pthread_create(NULL,&attr,server,(void *)&producer_consumer_channels);
pthread_create(NULL,&attr,producer,(void *)&producer_channel_id);
pthread_create(NULL,&attr,consumer,(void *)&consumer_channel_id);
/* printf (“Parent process finished\n”); */
return (NULL);
}
void *server (void *producer_consumer_channels)
{
int consumer_coid;
int producer_coid;
struct two_channels *channels;
printf (“Server starting\n”);
channels = (struct two_channels *) producer_consumer_channels;
/* attach to both channels since we’re going to be talking to both
threads */
consumer_coid =
ConnectAttach(ND_LOCAL_NODE,0,channels->consumer_chid,0,0);
producer_coid =
ConnectAttach(ND_LOCAL_NODE,0,channels->producer_chid,0,0);
printf (“Server finished\n”);
return(NULL);
}
void *consumer (void *consumer_channel_id)
{
int server_coid;
int consumer_chid;
consumer_chid = *(int )consumer_channel_id;
printf (“Consumer starting\n”);
/ attach to consumer<–>server channel */
server_coid = ConnectAttach(ND_LOCAL_NODE,0,consumer_chid,0,0);
printf (“Consumer finished\n”);
return(NULL);
}
void *producer (void *producer_channel_id)
{
int server_coid;
int producer_chid;
producer_chid = *(int )producer_channel_id;
/ attach to producer<–>server channel */
printf (“Producer starting\n”);
server_coid = ConnectAttach(ND_LOCAL_NODE,0,producer_chid,0,0);
printf (“Producer finishes\n”);
return(NULL);
}