vytvoril jsem toto
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <sys/un.h>
#include <unistd.h>
#include <netinet/in.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
#define SIZE 1024
int main()
{
pid_t pid_1, pid_2;
int pfd[2];
int nread;
int buf[SIZE];
// Deklarace
// spustit game
printf("pid pred forkem %d\n", getpid());
printf("rodic11 %d\n", getppid());
if (pipe(pfd) == -1){
perror("pipe failed");
exit(1);
}
pid_1 = fork();
if (pid_1 == 0){
printf("PID1 %d\n", getpid());
printf("PPID1 %d\n", getppid());
int server_sockfd, client_sockfd;
int server_len, client_len;
struct sockaddr_in server_address;
struct sockaddr_in client_address;
fd_set clients, tests;
int result;
server_sockfd = socket( AF_INET, SOCK_STREAM, 0 );
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = inet_addr( "127.0.0.1" );
server_address.sin_port = htons( 10000 );
server_len = sizeof( server_address );
if( bind( server_sockfd, ( struct sockaddr *)&server_address, server_len ) != 0 )
{
perror("oops: server-tcp-single");
exit( 1 );
}
listen( server_sockfd, 5 );
signal( SIGCHLD, SIG_IGN );
while( 1 ){
char ch;
printf( "server ceka...\n" );
client_len = sizeof( client_address );
client_sockfd = accept( server_sockfd, ( struct sockaddr *)&client_address, &client_len );
printf( "Pripojil se klient z %c\n", inet_ntoa( client_address.sin_addr) );
pid_2 = fork();
if( pid_2 == 0 ) {
printf("PID2 %d\n", getpid());
printf("PPID2 %d\n", getppid());
read( client_sockfd, &ch, 1 );
printf( "1 Klient poslal = %c\n", ch );
close(pfd[0]);
buf[0] = getpid();
//strcpy(buf,"a");
//write(pfd[1],buf,strlen(buf)+1);
write(pfd[1],buf,2);
//close(pfd[1]);
ch++;
sleep( 1 );
printf( "1 Server odesila = %c\n", ch );
write( client_sockfd, &ch, 1 );
close( client_sockfd );
while(1){
close(pfd[1]);
open(pfd[0]);
if(nread = read(pfd[0],buf,SIZE) != 0){
printf("child %d got: %d \n",getpid(),buf[0]);
break;
}
}
exit (0 );
}
else
close( client_sockfd );
}
}
if(pid_1 > 0){
while(1){
close(pfd[1]);
if(nread = read(pfd[0],buf,SIZE) != 0) {
printf("parent got: %d \n",buf[0]);
break;
}
}
sleep(3);
close(pfd[0]);
open(pfd[1]);
buf[0] = 33;
write(pfd[1],buf,2);
}
}
[\code]
komunikace smerem od potomku k hlavnimu procesu hry je ok. Jak ale udelat komunikace opacnym smerem tzn. od rodice k potomkum resp. od procesu hry k procesu hrace.
Potomci posilaji svuj pid, rodic by jim na oplatku mel poslat cislo 33. Vysledek ale je, ze potomci dostanou resp. z roury prectou zase svuj pid. Co delam spatne ???
btw. mam to chapat, ze kdyz bude mit rodic vic potomku a hodi neco do roury, tak to dostanou vsichni potomci ?