E { 服务器=gethostbyname(地址); 端口号=下一个_端口; } 如果(服务器==NULL) { fprintf(stderr,“找不到匹配的主机名\n”); 出口(0); } bzero((char*)&serv_addr,sizeof(serv_addr))//函数bzero()将缓冲区内的所有值设置为零 serv_addr.sin_family=AF_INET//这包含地址族的代码 b复制((char*)服务器->h_地址,(char*)和服务地址sin_地址s_地址,服务器->h_长度); serv_addr.sin_port=htons(端口号); if(connect(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr))
G.cE { 服务器=gethostbyname(地址); 端口号=下一个_端口; } 如果(服务器==NULL) { fprintf(stderr,“找不到匹配的主机名\n”); 出口(0); } bzero((char*)&serv_addr,sizeof(serv_addr))//函数bzero()将缓冲区内的所有值设置为零 serv_addr.sin_family=AF_INET//这包含地址族的代码 b复制((char*)服务器->h_地址,(char*)和服务地址sin_地址s_地址,服务器->h_长度); serv_addr.sin_port=htons(端口号); if(connect(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr)),c,sockets,select,pipe,posix,C,Sockets,Select,Pipe,Posix,G.c #include <stdlib.h> #include <stdio.h> #include <unistd.h> #include <string.h> #include <time.h> #include <errno.h> #include <netdb.h> #include <sys/wait.h> #include <sys/types.h> #include <
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <netdb.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <signal.h>
#include <syslog.h>
#include <fcntl.h>
#include <math.h>
#include "config.h"
// This is the main, you have to only execute this command: ./main (you can also run sudo netstat -tulpn for socket troubleshooting)
// The duty of this piece of code is to load config data and to launch all the needed processes (S, P, G and L)
int main(int argc, char *argv[])
{
int P, G, S, L;
int pfd1[2]; //file descriptors for pipe 1
int pfd2[2]; //file descriptors for pipe 2
int pfd3[2]; //file descriptors for pipe 3
int wait_status = 0;
if (pipe(pfd1) < 0) //error condition on pipe 1
{
perror("Pipe 1 creation error");
return -1;
}
if (pipe(pfd2) < 0) //error condition on pipe 2
{
perror("Pipe 2 creation error");
return -1;
}
if (pipe(pfd3) < 0) //error condition on pipe 2
{
perror("Pipe 3 creation error");
return -1;
}
char read1[SIZE];
char write1[SIZE];
char read2[SIZE];
char write2[SIZE];
char read3[SIZE];
char write3[SIZE];
sprintf(read1, "%d", pfd1[0]); //load the fd input/output (3rd arg.) into the char array (1st arg.),
sprintf(write1, "%d", pfd1[1]); //while formatting it as stated in 2nd arg.
sprintf(read2, "%d", pfd2[0]);
sprintf(write2, "%d", pfd2[1]);
sprintf(read3, "%d", pfd3[0]);
sprintf(write3, "%d", pfd3[1]);
argv[0] = read1; //pipe1: read
argv[1] = write1; //pipe1: write
argv[2] = read2; //pipe2: read
argv[3] = write2; //pipe2: write
argv[4] = read3; //pipe3: read
argv[5] = write3; //pipe3: write
//I will be passing to each node all the pipe ends, by transforming their fd in char and then reverting them to integers
G = fork();
if (G < 0) //error condition on fork
{
perror("Fork G");
return -1;
}
if (G == 0) //G process
{
char *node_name = "./G";
if (execvp(node_name, argv) < 0) //error handling for file G
{
perror("Exec failed for G");
return -1;
}
}
else if (G > 0)
{
P = fork();
if (P < 0) //error condition on fork
{
perror("Fork P");
return -1;
}
if (P == 0) //P process
{
char *node_name = "./P";
if (execvp(node_name, argv) < 0) //error handling for file P
{
perror("Exec failed for P");
return -1;
}
}
printf("[all processes setup and running]\n");
wait(&wait_status);
close(pfd1[0]);
close(pfd1[1]);
close(pfd2[0]);
close(pfd2[1]);
close(pfd3[0]);
close(pfd3[1]);
return 0;
}
}
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <netdb.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <signal.h>
#include <syslog.h>
#include <fcntl.h>
#include <math.h>
#include "config.h"
//This node is the computational core. It is also the nevralgic waypoint of communications: all other nodes involved are
//in some way or another bond to P
/*
Instructions for compiling this node:
gcc P.c -o P -lm
*/
void error(const char *m) //Display a message about the error on stderr and then aborts the program
{
perror(m);
exit(0);
}
int main(int argc, char *argv[])
{
close(atoi(argv[1]));
close(atoi(argv[3]));
close(atoi(argv[4]));
int state = 1;
token_struct token;
token.token_value = 1;
token.token_timestamp = time(NULL);
struct timeval tv;
int retval;
float dt = 0; //time delay between reception and delivery time instants of the token
clock_t t = 0;
pid_t Ppid;
Ppid = getpid();
printf("P: my PID is %d\n", Ppid);
//new token = received token + DT x (1. - (received token)^2/2) x 2 pi x RF
struct message msg;
char address[13] = "192.168.1.233";
int sockfd; //socket file descriptor
int portno; //stores the port number on which the server accepts connections
int n;
struct sockaddr_in serv_addr;
struct hostent *server;
portno = 5000; //port number definition
sockfd = socket(AF_INET, SOCK_STREAM, 0); //create a new socket
if (sockfd < 0)
{
error("Error creating a new socket\n");
}
if (!run_mode)
{
server = gethostbyname("ZenBook");
}
else
{
server = gethostbyname(address);
portno = NEXT_PORT;
}
if (server == NULL)
{
fprintf(stderr, "Could not find matching host name\n");
exit(0);
}
bzero((char *)&serv_addr, sizeof(serv_addr)); //the function bzero() sets all values inside a buffer to zero
serv_addr.sin_family = AF_INET; //this contains the code for the family of the address
bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);
serv_addr.sin_port = htons(portno);
if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
error("Connection failed");
printf("[P node sending FIRST message]\n");
n = write(sockfd, &(token), sizeof(token_struct));
if (n < 0)
error("Error writing to socket\n");
while (1)
{
tv.tv_sec = 2; //amount of seconds the select listens for incoming data from either pipe 1 and 2
tv.tv_usec = 0; //same as the previous line, but with microseconds
fd_set readfds; //set of involved pipes from which P needs to read through the select
FD_ZERO(&readfds); //inizialization of the set
FD_SET(atoi(argv[0]), &readfds); //addition of the desired pipe ends to the set
FD_SET(atoi(argv[2]), &readfds);
if (state == 1) //running situation
{
retval = select(atoi(argv[2]) + 1, &readfds, NULL, NULL, &tv);
printf("retval: %d\n", retval);
if (retval == -1)
{
perror("Select failed\n");
}
else if (retval > 0)
{
if (FD_ISSET(atoi(argv[0]), &readfds)) //read of first pipe (data coming from S) is ready
{
//code
}
if (FD_ISSET(atoi(argv[2]), &readfds)) //read of second pipe (data coming from G) is ready
{
//code
msg.timestamp = time(NULL); //get the current time
//This section is related to the communication with G, as the one with L is all set
t = clock() - t;
dt = ((float)t) / ((float)CLOCKS_PER_SEC); //by doing like this, the first cycle (and only that one) has a meaningless dt value
token.token_value = msg.value + dt * (((float)1) - (powf(msg.value, ((float)2)) / ((float)2))) * ((float)2) * ((float)M_PI) * rf;
t = clock();
printf("[P node sending message]\n");
usleep(waiting_time);
token.token_timestamp = msg.timestamp;
n = write(sockfd, &token, sizeof(token_struct));
if (n < 0)
error("Error writing to socket\n");
}
}
else if (retval == 0)
printf("No data written sent to pipes in the last 2 seconds\n");
}
else if (state == 0) //pausing sitation
{
//code
}
}
close(atoi(argv[0]));
close(atoi(argv[2]));
close(atoi(argv[5]));
close(sockfd);
return 0;
}
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <netdb.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <signal.h>
#include <syslog.h>
#include <fcntl.h>
#include <math.h>
#include "config.h"
//This node can be run in 2 modes: debug mode (single machine) or normal mode (communicating with other PCs);
//in the first case it receives tokens from P and then sends them back to it, in the other scenario it still
//receives data from P, but the token is sent to another machine
void error(const char *m) //display a message about the error on stderr and then abort the program
{
perror(m);
exit(1);
}
int main(int argc, char *argv[])
{
close(atoi(argv[0]));
close(atoi(argv[1]));
close(atoi(argv[2]));
close(atoi(argv[4]));
close(atoi(argv[5]));
int sockfd; //socket file descriptor
int newsockfd;
int portno; //port of the server for the client connection
socklen_t clilen;
struct sockaddr_in serv_addr, cli_addr;
int n;
token_struct token;
token.token_value = 5;
token.token_timestamp = time(NULL);
pid_t Gpid;
Gpid = getpid();
printf("G: my PID is %d\n", Gpid);
sockfd = socket(AF_INET, SOCK_STREAM, 0); //create a new socket
if (sockfd < 0)
error("Error creating a new socket\n");
bzero((char *)&serv_addr, sizeof(serv_addr)); //the function bzero() sets all values inside a buffer to zero
portno = 5000;
serv_addr.sin_family = AF_INET; //this contains the code for the family of the address
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) //the bind() system call binds a socket to an address
error("Error on binding\n");
listen(sockfd, 5); //system call that allows the process to listen for connections over the socket
printf("[G node waiting for messages]\n");
if (!run_mode)
{
while (1)
{
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, (socklen_t *)&clilen);
//The accept() system call causes the process to block until a client connects to the server
if (newsockfd < 0)
{
perror("'accept()' system call failed\n");
return 1;
}
else
{
puts("Connection accepted\n");
n = read(newsockfd, &(token), sizeof(token_struct));
if (n < 0)
error("Error reading from socket\n");
printf("Here is the message: %f | received at: %li\n", token.token_value, token.token_timestamp);
write(atoi(argv[3]), &(token), sizeof(token_struct));
printf("G: I tried to write on the pipe\n");
}
}
}
else //This is the code relative to the multiple machine case
{
while (1)
{
//code
}
}
close(sockfd);
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括“config.h”
//该节点可以在两种模式下运行:调试模式(单机)或正常模式(与其他PC机通信);
//在第一种情况下,它从P接收令牌,然后将它们发送回P,在另一种情况下,它仍然
//从P接收数据,但令牌被发送到另一台机器
void error(const char*m)//在stderr上显示有关错误的消息,然后中止程序
{
佩罗尔(m);
出口(1);
}
int main(int argc,char*argv[])
{
关闭(atoi(argv[0]);
关闭(atoi(argv[1]);
关闭(atoi(argv[2]);
关闭(atoi(argv[4]);
关闭(atoi(argv[5]);
int sockfd;//套接字文件描述符
int newsockfd;
int portno;//客户端连接的服务器端口
socklen_t clilen;
服务地址中的结构sockaddr\u,cli\u addr;
int n;
令牌结构令牌;
token.token_值=5;
token.token_timestamp=时间(空);
pid_t Gpid;
Gpid=getpid();
printf(“G:我的PID是%d\n”,Gpid);
sockfd=socket(AF_INET,SOCK_STREAM,0);//创建一个新的套接字
if(sockfd<0)
错误(“创建新套接字时出错\n”);
bzero((char*)&serv_addr,sizeof(serv_addr));//函数bzero()将缓冲区内的所有值设置为零
端口号=5000;
serv\u addr.sin\u family=AF\u INET;//这包含地址族的代码
serv_addr.sin_addr.s_addr=INADDR_ANY;
serv_addr.sin_port=htons(端口号);
if(bind(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr))<0)//bind()系统调用将套接字绑定到地址
错误(“绑定错误”\n);
listen(sockfd,5);//允许进程侦听套接字上的连接的系统调用
printf(“[G节点等待消息]\n”);
如果(!运行模式)
{
而(1)
{
clilen=sizeof(cli_addr);
newsockfd=accept(sockfd,(struct sockaddr*)和cli_addr,(socklen_t*)和clilen);
//accept()系统调用会导致进程阻塞,直到客户端连接到服务器
if(newsockfd<0)
{
perror(“'accept()'系统调用失败\n”);
返回1;
}
其他的
{
puts(“已接受连接”);
n=读取(newsockfd,&(令牌),sizeof(令牌结构));
if(n<0)
错误(“从套接字读取错误\n”);
printf(“这是消息:%f |接收地址:%li\n”,token.token_值,token.token_时间戳);
写入(atoi(argv[3]),&(令牌),sizeof
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <netdb.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <signal.h>
#include <syslog.h>
#include <fcntl.h>
#include <math.h>
#include "config.h"
//This node can be run in 2 modes: debug mode (single machine) or normal mode (communicating with other PCs);
//in the first case it receives tokens from P and then sends them back to it, in the other scenario it still
//receives data from P, but the token is sent to another machine
void error(const char *m) //display a message about the error on stderr and then abort the program
{
perror(m);
exit(1);
}
int main(int argc, char *argv[])
{
close(atoi(argv[0]));
close(atoi(argv[1]));
close(atoi(argv[2]));
close(atoi(argv[4]));
close(atoi(argv[5]));
int sockfd; //socket file descriptor
int newsockfd;
int portno; //port of the server for the client connection
socklen_t clilen;
struct sockaddr_in serv_addr, cli_addr;
int n;
token_struct token;
token.token_value = 5;
token.token_timestamp = time(NULL);
pid_t Gpid;
Gpid = getpid();
printf("G: my PID is %d\n", Gpid);
sockfd = socket(AF_INET, SOCK_STREAM, 0); //create a new socket
if (sockfd < 0)
error("Error creating a new socket\n");
bzero((char *)&serv_addr, sizeof(serv_addr)); //the function bzero() sets all values inside a buffer to zero
portno = 5000;
serv_addr.sin_family = AF_INET; //this contains the code for the family of the address
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) //the bind() system call binds a socket to an address
error("Error on binding\n");
listen(sockfd, 5); //system call that allows the process to listen for connections over the socket
printf("[G node waiting for messages]\n");
if (!run_mode)
{
while (1)
{
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, (socklen_t *)&clilen);
//The accept() system call causes the process to block until a client connects to the server
if (newsockfd < 0)
{
perror("'accept()' system call failed\n");
return 1;
}
else
{
puts("Connection accepted\n");
n = read(newsockfd, &(token), sizeof(token_struct));
if (n < 0)
error("Error reading from socket\n");
printf("Here is the message: %f | received at: %li\n", token.token_value, token.token_timestamp);
write(atoi(argv[3]), &(token), sizeof(token_struct));
printf("G: I tried to write on the pipe\n");
}
}
}
else //This is the code relative to the multiple machine case
{
while (1)
{
//code
}
}
close(sockfd);
return 0;
}
{
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, (socklen_t *)&clilen);
//The accept() system call causes the process to block until a client connects to the server
if (newsockfd < 0)
{
perror("'accept()' system call failed\n");
return 1;
}
else {
puts("Connection accepted\n");
}
while (1)
{
{
n = read(newsockfd, &(token), sizeof(token_struct));
if (n < 0)
error("Error reading from socket\n");
printf("Here is the message: %f | received at: %li\n", token.token_value, token.token_timestamp);
write(atoi(argv[3]), &(token), sizeof(token_struct));
printf("G: I tried to write on the pipe\n");
}
}
}