-
Notifications
You must be signed in to change notification settings - Fork 1
/
Multi_Process_Prod.c
134 lines (114 loc) · 3.89 KB
/
Multi_Process_Prod.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/***
forker.c R.Tervo Jan 2013
-------------------------------------
Exploring fork() and exec()
This program monitors PID and PPID in
while spawning a second program.
-------------------------------------
***/
#include <unistd.h>
#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];
main( )
{
pid_t pidA, ppidA; // define PID storage
pid_t pidB, ppidB;
pid_t pidC, pid;
pid_t pidD, pidE;
pid_t ppidE;
int status; // status of wait()
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);
}
pidA = getpid(); // establish main program PID
ppidA = getppid();
printf("PID=%i, PPID=%i I am the main program.\n", pidA, ppidA);
pid = wait(&status); // wait for child process to end (none)
printf("I am done waiting for PID[%i] with status %i\n",pid, status);
printf("Fork now..\n");
fflush(stdout); // complete any pending prints
pidC = fork(); // returns child PID (or 0 if child)
pidB = getpid();
ppidB = getppid();
if(pidA != pidB)
{
printf("PID=%i, PPID=%i I am the child. \n", pidB, ppidB);
printf("Exec now..\n");
fflush(stdout); // complete any pending prints
int ucoid;
puts("user has been called");
puts("user connecting to channel. \n");
ucoid = ConnectAttach(0, pidA, 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);
}
}
else
{
pidD= fork();
pidE=getpid();
ppidE=getppid();
if(pidA != pidE){
printf("PID=%i, PPID=%i I am the parent. \n", pidA, ppidA);
printf("Exec now..\n");
fflush(stdout); // complete any pending prints
int prodcoid;
puts("prod has been called");
puts("prod connecting to channel. \n");
prodcoid = ConnectAttach(0, pidA, chid, 0, NULL);
while(1) {
puts("prod:go ahead, enter a string");
//gets(send_message);
strcpy(send_message,"hello");
printf("prod:waiting to send: = %s =.\n",send_message);
MsgSend(prodcoid, &send_message, sizeof(send_message), &receive_message, sizeof(receive_message));
printf("prod: received a reply from friend: = %s =\n", receive_message);
sleep(1);
}
}
else{
//--
printf("PID=%i, PPID=%i I am the second child. \n", pidE, ppidE);
int fcoid;
puts("friend has been called");
puts("friend connecting to channel. \n");
fcoid = ConnectAttach(0, pidA, 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);
printf("PID=%i, PPID=%i I am the parent. \n", pidB, ppidB);
}
pid = wait(&status); // wait for child process to end (none)
printf("I am done waiting for PID[%i] with status %i\n",pid, status);
//--
//execl("./hello","hello",'\0'); // replace this process with another
printf(" ...we never print this line2...\n");
}
}
return(0);
} // end main