-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtwo_prod_one_cons.c
92 lines (82 loc) · 2.34 KB
/
two_prod_one_cons.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
scriptname: two_prod_one_cons.c
author: Richard Tervo
purpose: to be a part of lab
description: todoloo
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <sys/neutrino.h>
#define MSGSIZE 80
int chid;
int rcvid;
unsigned flags;
char send_message[MSGSIZE]; //send message
char receive_message[MSGSIZE]; //receive message
char reader[MSGSIZE]; //
char readerreply[MSGSIZE];
// Thread Definitions
void * user()
{
// Sends the data from command line to the friend.
int ucoid;
puts("user has been called");
puts("user connecting to channel. \n");
ucoid = ConnectAttach(0, 0, chid, 0, NULL);
while(1) {
puts("user:go ahead, enter a string");
gets(send_message);
printf("user:waiting to send: = %s =.\n",send_message);
MsgSend(ucoid, &send_message, sizeof(send_message), &receive_message, sizeof(receive_message));
printf("user: received a reply from friend: = %s =\n", receive_message);
sleep(1);
}
}
void * friend()
{
int fcoid;
puts("friend has been called");
puts("friend connecting to channel. \n");
fcoid = ConnectAttach(0, 0, chid, 0, NULL);
strcpy(readerreply, "Got it!");
while(1) {
printf("friend: I am waiting to receive message from user.\n");
rcvid = MsgReceive(chid, &reader, sizeof(reader), NULL);
printf("friend: received = %s = from user.\n", reader);
MsgReply(rcvid, NULL, &readerreply, sizeof(readerreply));
sleep(1);
}
}
void * add_producer()
{
int prod_coid;
char temp[MSGSIZE];
temp[0] = 'D';//(char[MSGSIZE])
puts("Producer has been called");
puts("Producer connecting to channel");
prod_coid=ConnectAttach(0,0,chid,0,NULL);
while(1){
printf("user wasnts to send %s",temp);
MsgSend(prod_coid, &temp, sizeof(temp), &receive_message, sizeof(receive_message));
printf("producer: reply from friend %s", receive_message);
}
}
// MAIN______________________________________________________________
int main(void)
{
puts("Embedded Message Passing Example Program");
puts("creating channel");
flags = NULL;
chid = ChannelCreate(flags);
if (chid==1){
printf("could not create channel \n");
//exit(1);
}
pthread_create(NULL, NULL, &user,NULL);
pthread_create(NULL, NULL, &friend,NULL);
pthread_create(NULL,NULL, &add_producer,NULL);
sleep(120);
return(0);
}