#include #include #include #include #include #include //#include #include #include typedef struct CLL { char ch; struct CLL *next; } CLL; int p1[2]; int main (int argc, char **argv); char *readstring (int fd, int *len); void sendmessage (void); //Polling is a technique that Linux uses to help manage I/O devices. //"poll" lets you know whether there is input to be read from a file //descriptor without actually reading that input. If there's no //input to be read, then your program can go on about its business. //If there is input, then your program can read or not read. //This sort of thing was necessary for text-based video games. //If you read from the keyboard, the program will block until //the input is entered. If you want to have the program do stuff //while the user is deciding what to type, you need to use poll //or something similar (such as select). int main (int argc, char **argv) { srand (time (NULL)); struct pollfd fds[2]; //An array of things to poll. Maybe terminals, or pipes or sockets pipe (p1); int pid = fork (); if (pid==0) sendmessage(); close (p1[1]); fds[0].fd = 0; //I will check standard input, the keyboard. fds[0].events = POLLIN; //I will check to see if anything is waiting to be typed fds[1].fd = p1[0]; //I will check standard input, the keyboard. fds[1].events = POLLIN; //I will check to see if anything is waiting to be typed int dead = 0; while (!dead) { write (1,"What do you want to do? ",25); int ret = 0; // do { // ret = poll (fds,1,0); //I'll poll for 0 milliseconds. If there's input. This doesn't work any differently than a regular read, but I could other stuff in this loop if I wanted. If this were a video game, I might move stuff around. ret = poll (fds,2,-1); //I'll poll for infinite time. The poll will not terminate until there's something to read. This is poll until there's something on either the keyboard or the pipe to read. // } while (ret==0); if (ret == 0) { write (1,"\nWhile you were thinking, an ogre ate you!\n",43); dead = 1; } else { int len; if (fds[1].revents & POLLIN) { //This means is there something to read on the pipe char *s = readstring (p1[0],&len); write (1,s,len); write (1,"\n",1); } if (fds[0].revents & POLLIN) { char *s = readstring (0,&len); if (rand()%2==0) { write (1,"I don\'t know how to ",20); write (1,s,len); write (1,"\n",1); free (s); } else { write (1,"You got killed!\n",16); dead =1; } } } } kill (pid,SIGKILL); //this is equivalent to kill -9. A regular kill command sends a message to the process saying would you please do me the honor of killing yourself, and the other process is allowed to say no. (This is the SIGTERM signal.) sigkill forces the other process to die. You can only kill processes with the same userid as yourself, unless you are root. wait (NULL); return 0; } char *readstring (int fd, int *len) { CLL *L = NULL; int ct = 0; char buf; char *final; while (1) { read (fd,&buf,1); if (buf=='\n') break; CLL *N = malloc (sizeof (CLL)); N->ch = buf; N->next = L; L = N; ct++; } final = malloc (ct+1); *len = ct; final[ct] = '\0'; while (ct > 0) { ct--; final[ct] = L->ch; CLL *N = L; L = L->next; free (N); } return final; } void sendmessage (void) { close (p1[0]); while (1) { sleep (10); write (p1[1],"\nStill waiting...\n",18); } }