-
Notifications
You must be signed in to change notification settings - Fork 1
/
Messages_Between_Processes.c
101 lines (85 loc) · 2.85 KB
/
Messages_Between_Processes.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
/***
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];
pid_t pidA, ppidA; // define PID storage
pid_t pidB, ppidB;
pid_t pidC, pid;
main( )
{
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);
}
//execl("./hello","hello",'\0'); // replace this process with another
printf(" ...we never print this line...\n");
} else
{
int fcoid;
puts("friend has been called");
puts("friend connecting to channel. \n");
fcoid = ConnectAttach(0, pidB, 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);
}
return(0);
} // end main