int main(int argc, char *argv[]) { pid_t pid; TELL_WAIT(); pid = Fork(); if (pid == 0) { while ( n < 1000) { WAIT_PARENT(); printf(" c%d ", n); n += 2; TELL_PARENT(getppid()); //sleep(1); } } else if(pid > 0) { while (n < 1000) { printf(" p%d ", n); n += 2; TELL_CHILD(pid); // sleep(1); WAIT_CHILD(); } } printf("Hello, world\n"); return 0; }
/* gcc apue.h apue_err.c figure-14.5.c figure-10.24.c figure-14.7.c */ int main(void) { int fd; pid_t pid; /* * Create a file and write two bytes to it. */ if ((fd = creat("templock", FILE_MODE)) < 0) err_sys("creat error"); if (write(fd, "ab", 2) != 2) err_sys("write error"); TELL_WAIT(); /* figure-10.24.c */ if ((pid = fork()) < 0) { err_sys("fork error"); } else if (pid == 0) { /* child */ lockabyte("child", fd, 0); TELL_PARENT(getppid()); WAIT_PARENT(); lockabyte("child", fd, 1); } else { /* parent */ lockabyte("parent", fd, 1); TELL_CHILD(pid); WAIT_CHILD(); lockabyte("parent", fd, 0); } exit(0); }
/* increment counter in shared memory segment; mmap /dev/zero version */ int main(void) { int fd, i, counter; pid_t pid; void *area; if ((fd = open("/dev/zero", O_RDWR)) < 0) err_sys("open error"); if ((area = mmap(0, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) == MAP_FAILED) err_sys("mmap error"); close(fd); /* can close /dev/zero now that it is mapped */ TELL_WAIT(); if ((pid = Fork()) > 0) { /* parent */ for (i = 0; i < NLOOPS; i += 2) { if ((counter = update((long *)area)) != i) err_quit("parent: expected %d, got %d", i, counter); TELL_CHILD(pid); WAIT_CHILD(); } } else { /* child */ for (i = 1; i < NLOOPS + 1; i += 2) { WAIT_PARENT(); if ((counter = update((long *)area)) != i) err_quit("child: expected %d, got %d", i, counter); TELL_PARENT(); } } exit(0); }
int main(int argc, char *argv[]) { int fd; pid_t pid; /*Create a file and write two bytes to it*/ fd = Open("templock", O_RDWR | O_CREAT | O_TRUNC, FILE_MODE) ; if(write(fd, "ab", 2) != 2) err_sys("write error"); TELL_WAIT(); pid = Fork(); if(pid == 0) { lockabyte("child", fd, 0); TELL_PARENT(getppid()); WAIT_PARENT(); lockabyte("child", fd, 1); }else { lockabyte("parent", fd, 1); TELL_CHILD(pid); WAIT_CHILD(); lockabyte("parent", fd, 0); } return 0; }
int main (void) { pid_t pid = 0, ppid = 0, childpid = 0; int i = 0; init (); if ((pid = fork ()) < 0) { err_sys ("fork error"); } else if (pid == 0) { /* child block */ pid = getpid (); ppid = getppid (); for (i = 0; i < 100; i ++) { WAIT_PARENT (); increment (pid); TELL_PARENT (ppid); } } else { /* parent block */ childpid = pid; pid = getpid (); for (i = 0; i < 100; i ++) { increment (pid); TELL_CHILD (childpid); WAIT_CHILD (); } } exit (0); }
int main(void) { int fd; pid_t pid; if ((fd = creat("templock", FILE_MODE)) < 0) err_sys("creat error"); if (write(fd, "ab", 2) != 2) err_sys("write error"); TELL_WAIT(); if ((pid = fork()) < 0){ err_sys("fork error"); }else if(pid == 0){/*child*/ lockabyte("child", fd, 0); TELL_PARENT(getppid()); WAIT_PARENT(); lockabyte("child", fd, 1); }else{ lockabyte("parent", fd, 1); TELL_CHILD(pid); WAIT_CHILD(); lockabyte("parent", fd, 0); } exit(0); }
int main(int argc, char* argv[]) { int fd; pid_t pid; if((fd = creat("templock", FILEMODE)) < 0) printf("creat error"); if(write(fd, "ab", 2) != 2) printf("write error"); TELL_WAIT(); if((pid = fork()) < 0) printf("fork error"); else if(pid == 0) { lockabyte("child:", fd, 0); TELL_PARENT(getpid()); WAIT_PARENT(); lockabyte("child:", fd, 1); } else { lockabyte("parent:", fd, 1); TELL_CHILD(pid); WAIT_CHILD(); lockabyte("parent:", fd, 0); } exit(0); }
int main(void) { pid_t pid, mypid; if((pid = fork()) < 0) err_sys("fork error"); else if(pid > 0) { // parent mypid = getpid(); TELL_WAIT(); printf("%d: WAIT_CHILD()\n", mypid); WAIT_CHILD(); printf("%d: wake up\n", mypid); sleep(2); printf("%d: TELL_CHILD()\n", mypid); TELL_CHILD(pid); } else { // child mypid = getpid(); TELL_WAIT(); sleep(2); printf("%d: TELL_PARENT()\n", mypid); TELL_PARENT(getppid()); printf("%d: WAIT_PARENT()\n", mypid); WAIT_PARENT(); printf("%d: wake up\n", mypid); } return 0; }
int main(void) { pid_t pid; FILE *fp; int fd; int i = 0; char *child = "in child\n"; char *parent = "in parent\n"; if ((fp = fopen("tmp", "a")) == NULL) { perror("fopen error"); exit(1); } fd = fileno(fp); /* if (write(fd, pi, 1) != 1) { */ /* perror("write error"); */ /* exit(1); */ /* } */ fprintf(fp, "%d\n", i); fflush(fp); TELL_WAIT(); if ((pid = fork()) < 0) { /* pid = fork()要用括号括起来 */ perror("fork error"); exit(1); } else if (pid == 0) { while (i < 100) { WAIT_PARENT(); /* if (write(fd, pi, 1) != 1) { */ /* perror("write error"); */ /* exit(1); */ /* } */ i += 2; fprintf(fp, "%d\t", i); fflush(fp); write(fd, child, strlen(child)); TELL_PARENT(getppid()); } } else { i++; while (i < 100) { /* if (write(fd, pi, 1) != 1) { */ /* perror("write error"); */ /* exit(1); */ /* } */ fprintf(fp, "%d\t", i); fflush(fp); i += 2; write(fd, parent, strlen(parent)); TELL_CHILD(pid); WAIT_CHILD(); } } return 0; }
int main(int argc, char *argv[]) { int fd; pid_t pid; char buf[5]; struct stat statbuf; if (argc != 2) { fprintf(stderr, "usage: %s filename\n", argv[0]); exit(1); } if ((fd = open(argv[1], O_RDWR | O_CREAT | O_TRUNC, FILE_MODE)) < 0) err_sys("open error"); if (write(fd, "abcdef", 6) != 6) err_sys("write error"); /* turn on set-group-ID and turn off group-execute */ if (fstat(fd, &statbuf) < 0) err_sys("fstat error"); if (fchmod(fd, (statbuf.st_mode & ~S_IXGRP) | S_ISGID) < 0) err_sys("fchmod error"); TELL_WAIT(); if ((pid = fork()) < 0) { err_sys("fork error"); } else if (pid > 0) { /* parent */ /* write lock entire file */ if (write_lock(fd, 0, SEEK_SET, 0) < 0) err_sys("write_lock error"); TELL_CHILD(pid); if (waitpid(pid, NULL, 0) < 0) err_sys("waitpid error"); } else { /* child */ WAIT_PARENT(); /* wait for parent to set lock */ set_fl(fd, O_NONBLOCK); /* first let's see what error we get if region is locked */ if (read_lock(fd, 0, SEEK_SET, 0) != -1) /* no wait */ err_sys("child: read_lock succeeded"); printf("read_lock of already-locked region returns %d\n", errno); /* now try to read the mandatory locked file */ if (lseek(fd, 0, SEEK_SET) == -1) err_sys("lseek error"); if (read(fd, buf, 2) < 0) err_ret("read failed (mandatory locking works)"); else printf("read OK (no mandatory locking), buf = %2.2s\n", buf); } exit(0); }
int main(int argc,char ** argv) { int fd; pid_t pid; char buf[5]; struct stat statbuf; if(argc!=2) { fprintf(stderr,"usage:%s filename \n",argv[0]); exit(1); } if((fd=open(argv[1],O_RDWR|O_CREAT|O_TRUNC,FILE_MODE))<0) err_sys("open error"); if(write(fd,"abcdef",6)<0) err_sys("write error"); if(fstat(fd,&statbuf)<0) err_sys("fstat error"); if(fchmod(fd,(statbuf.st_mode & ~S_IXGRP)|S_ISGID)<0) err_sys("fchmod error"); TELL_WAIT(); if((pid=fork())<0) err_sys("fork error"); else if(pid>0) { if(write_lock(fd,0,SEEK_SET,0)<0) err_sys("write_lock error"); TELL_CHILD(pid); if(waitpid(pid,NULL,0)<0) err_sys("waitpid error"); } else { WAIT_PARENT(); set_fl(fd,O_NONBLOCK); if(read_lock(fd,0,SEEK_SET,0)!=-1) err_sys("child read_lock successed "); printf("read_lock of arleady-locked region return %d \n",errno); if(lseek(fd,0,SEEK_SET)==-1) err_sys("lseek error"); if(read(fd,buf,2)<0) err_ret("read failed (mandatory locking works )"); else printf("read OK(no mandatory locking ,buf=%2.2s\n)",buf); exit(0); } }
int main(int argc, char** argv) { int n; int fd[2]; pid_t pid; int MAXLINE = 1024; char *line = malloc(MAXLINE * sizeof(char)); INIT(); if(pipe(fd) < 0){ perror("pipe error\n"); exit(1); } if((pid = fork()) < 0){ perror("fork error\n"); exit(1); } else if(pid > 0){/* parent */ close(fd[0]); printf("child pid = %d\n", pid); for(;;){ printf("parent > "); gets(line); write(fd[1], line, strlen(line) + 1); TELL_CHILD(pid); WAIT_FOR_CHILD(); if(strcmp(line, "STOP") == 0) break; } } else{/* child process */ close(fd[1]); for(;;){ WAIT_FOR_PARENT(); n = read(fd[0], line, MAXLINE); printf("child: %s\n", line); TELL_PARENT(getppid()); if(strcmp(line, "STOP") == 0) break; } } return 0; }
int main() { pid_t pid; TELL_WAIT(); if ((pid = fork()) < 0) { err_sys("fork error"); } else if (pid == 0) { WAIT_PARENT(); /* parent goes first */ charatatime("output from child\n"); } else { charatatime("output from parent\n"); TELL_CHILD(pid); } exit(0); }
int main() { int fd = open(path, O_CREAT|O_TRUNC|O_RDWR, 0644); write(fd, "0", 1); char inbuf[20]; char outbuf[20]; int num; TELL_WAIT(); pid_t pid; pid = fork(); if(pid<0) { perror("fork error"); return 1; } else if(pid == 0) { //child for(int i=0;i<10;++i) { lseek(fd, 0, SEEK_SET); ssize_t len = read(fd, inbuf, 20); if(len<0) break; inbuf[len] = 0; fprintf(stderr, "CHILD read %s\n",inbuf);//debug sscanf(inbuf,"%d",&num); ++num; lseek(fd, 0, SEEK_SET); sprintf(outbuf, "%d", num); write(fd, outbuf, strlen(outbuf)); fprintf(stderr, "CHILD write %s\n",outbuf);//debug TELL_PARENT(getppid()); WAIT_PARENT(); } } else { //parent for(int i=0;i<10;++i) { WAIT_CHILD(); lseek(fd, 0, SEEK_SET); ssize_t len = read(fd, inbuf, 20); if(len<0) break; inbuf[len] = 0; fprintf(stderr, "PARENT read %s\n",inbuf);//debug sscanf(inbuf,"%d",&num); ++num; lseek(fd, 0, SEEK_SET); sprintf(outbuf, "%d", num); write(fd, outbuf, strlen(outbuf)); fprintf(stderr, "PARENT write %s\n",outbuf);//debug TELL_CHILD(pid); } } }
int main(void){ pid_t pid; TELL_WAIT(); if((pid=fork())<0){ err_sys("fork error"); }else if(pid==0){/*child*/ WAIT_PARENT();/*parent goes next*/ char_at_a_time("output from child\n"); /*TELL_PARENT(getppid());*//*child goes first*/ }else{/*parent*/ /*sleep(2);*/ /*WAIT_CHILD();*//*parent goes first*/ char_at_a_time("output from parent\n"); TELL_CHILD(pid);/*child goest next*/ } exit(0); }
int main(int argc,char *argv[]) { pid_t pid; TELL_WAIT(); if ((pid = fork()) < 0){ perror("fork"); }else if (pid == 0){ TELL_PARENT(getppid()); WAIT_PARENT(); chara("output from child\n"); }else{ chara("output from parent\n"); TELL_CHILD(pid); } exit(0); }
int main() { int fid; pid_t pid; if((fid = open("number.txt", O_WRONLY|O_CREAT|O_TRUNC))<0) err_sys("create file error...\n"); sprintf(str,"%d",number); write(fid,str,strlen(str)); close(fid); TELL_WAIT(); if((pid = fork())<0) err_sys("fork error"); else if(pid==0) { while((getNumber("number.txt")+1)<1000) { WAIT_PARENT(); number = getNumber("number.txt"); setNumber("number.txt",number+1); printf("Pid = %d,Number = %d\n",getpid(),number+1); TELL_PARENT(getppid()); } } else { while((getNumber("number.txt")+1)<1000) { number = getNumber("number.txt"); setNumber("number.txt",number+1); printf("Pid = %d,Number = %d\n",getpid(),number+1); TELL_CHILD(pid); WAIT_CHILD(); } } close(fid); return 0; }
int main(void) { pid_t pid; TELL_WAIT(); if ((pid = fork()) < 0) err_sys("fork error"); else if (pid == 0) { WAIT_PARENT(); charatatime("child 12345678901234567890123456\n"); } else { charatatime("parent abcdefghigklmnopqrstuvwxyz\n"); TELL_CHILD(pid); } exit(0); }
int main(void) { pid_t pid; TELL_WAIT(); if((pid = fork()) < 0) err_sys("fork error"); else if (pid == 0) { WAIT_PARENT(); /* parent goes first */ charactatime("output from child\n"); TELL_PARENT(getppid()); } else { charactatime("output from parent\n"); TELL_CHILD(pid); WAIT_CHILD(); } return 0; }
/* parent goes first program */ int main() { pid_t pid; TELL_WAIT(); pid = fork(); if(pid < 0) { printf("fork error\n"); } else if(pid == 0) { WAIT_PARENT(); do_task("child task\n"); } else { do_task("parent task\n"); TELL_CHILD(pid); } return 0; }
int main(int argc, char *argv[]) { char line[MAXLINE]; int n; pid_t pid; int fd; if (argc != 2) err_sys("usage: a.out <pathname>"); if ((fd = open(argv[1], O_RDWR)) < 0) err_sys("open %s file failed", argv[1]); memset(line, '\0', MAXLINE); TELL_WAIT(); pid = fork(); switch (pid) { case -1://error err_sys("fork error"); case 0://child WAIT_PARENT(); lseek(fd, SEEK_SET, 0); if ((n = read(fd, line, MAXLINE)) < 0) err_sys("read error form %s", argv[1]); printf("read %d bytes:", n); fflush(stdout); write(STDOUT_FILENO, line, n); TELL_PARENT(getppid()); break; default://parent sleep(2); n = write(fd, "hello child\n", 13); printf("write %d bytes\n", n); fflush(stdout); TELL_CHILD(pid); WAIT_CHILD(); break; } close(fd); exit(0); }
int test_record_lock() { int ret; int fd; pid_t pid; char *name; name = tmpnam(NULL); if((fd = creat(name, FILE_MODE)) < 0) { err_sys("create"); } if (write(fd, "ab", 2) != 2) err_sys("write"); TELL_WAIT(); if ((pid = fork()) < 0) err_sys("fork"); else if (pid == 0) { lockbyte("child", fd, 0); TELL_PARENT(getpid()); WAIT_PARENT(); lockbyte("child", fd, 1); sleep(2); exit(0); } else { lockbyte("parent", fd, 1); TELL_CHILD(getpid()); WAIT_CHILD(); lockbyte("parent", fd, 0); close(fd); sleep(1); unlink(name); } return 0; }
int main(int argc, char *argv[]) { int fd; pid_t pid; void *addr = NULL; fd = open("/dev/zero", O_RDWR); if (fd < 0) { perror("open failure: "); return EXIT_FAILURE; } addr = mmap(NULL, sizeof(int), PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_SHARED, fd, 0); if (addr == NULL) { perror("mmap failure: "); return EXIT_FAILURE; } close(fd); // mmap success and close fd TELL_WAIT(); if ((pid = fork()) < 0) { perror("fork failure: "); return EXIT_FAILURE; } else if (pid == 0) { int i = 0; pid = getpid(); for (i = 0; i < 1000; i+=2) { if (update((int *)addr) != i) { fprintf(stderr, "child process sync failure, i = %d\n", i); return EXIT_FAILURE; } printf("child start ##############\n"); TELL_PARENT(pid); WAIT_PARENT(); } } else { int i = 0; pid = getpid(); for (i = 1; i < 1000; i+=2) { WAIT_CHILD(); printf("parent start @@@@@@@@@@@@@@\n"); if (update((int *)addr) != i) { fprintf(stderr, "parent process sync failure, i = %d\n", i); return EXIT_FAILURE; } TELL_CHILD(pid); } } munmap(addr, sizeof(int)); return EXIT_SUCCESS; }
/** * main() * * This program comprises two processes. First, the "brain" which * communicates with a supervisor-client. The brain receives * high-level control commands from the supervisor-client and then * conveys any resulting sensor data back. The second process is the * "nerves"; this process translates high-level commands into * low-level iRobot SCI commands, executes them on the iRobot, and * then obtains any resulting sensor data. * */ int main(int argc, char* argv[]) { /* Create a message queue using O_CREAT so that if the queue doesn't * already exist, it will be created. When using mq_open with * O_CREAT, one must supply four arguments. The first "name" * argument must begin with a slash. The third "mode" argument is * derived from the symbolic constants is <sys/stat.h>. */ mqd_t mqd_cmd = mq_open("/q_cmd", O_RDWR | O_CREAT , S_IRWXU | S_IRWXG | S_IRWXO, NULL); mqd_t mqd_sns = mq_open("/q_sns", O_RDWR | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO, NULL); printf("The message queue id is: %d\n", mqd_cmd); /* Determine the size of messages for this message queue */ struct mq_attr a; mq_getattr(mqd_cmd,&a); printf("The default message size is: %d\n", a.mq_msgsize); if( mqd_cmd == -1) { perror("mq_open():"); return -1; } if( mqd_sns == -1) { perror("mq_open():"); return -1; } int check = 0; char addresses[3][13]; if ((check = checkArgName(argc, argv, addresses)) == -1) { printf("Not correct name entered. Exiting.\n"); exit(0); } int i, counter, serverID, clientSock, numBytes; pid_t pid, pid2; // Arrays to hold most recent command sent by the // supervisor-client and the most recent command sent to the iRobot. char commandFromSupervisor[MAXDATASIZE] = {'\0'}; char commandToRobot[MAXDATASIZE] = {'\0'}; // An array to hold sensor data sent to the supervisor-client and // obtained from the iRobot. char sensDataToSupervisor[MAXDATASIZE] = {'\0'}; char sensDataFromRobot[MAXDATASIZE] = {'\0'}; char emptyDataToSupervisor[MAXDATASIZE] = "0000000000 "; char rawTimeString[12] = {'\0'}; // An array to hold the timestamp. char currTime[100]; // Create pipe to communicate between parent and child int fd[2]; if(pipe(fd) < 0) perror("pipe error"); //------------------------------------------------------------------------ // Added Code to implement client //------------------------------------------------------------------------ int sockfd, numbytes; //buffer to store sensor information char sensorBuf[MAXDATASIZE]; char msgBuf[MAXDATASIZE]; struct addrinfo hints, *servinfo, *p; int rv; char s[INET6_ADDRSTRLEN]; //array to hold the command sent char cmd[1]; //initialize the input to NULL char char input = '\0'; //------------------------------------------------------------------------ // End of added section //------------------------------------------------------------------------ // Create a socket-based server if((serverID = createServer()) == -1) { perror("createServer()"); return -1; } printf("brainstem: waiting for connections...\n"); // Establish a client-connection for the socket-based server if((clientSock = establishConnection(serverID)) == -1) { perror("establishConnection(serverID)"); return -1; } // Set up signal-based communication between the child // (brain) and parent (nervous system) to avoid possible // race conditions. TELL_WAIT(); // Fork a child process. This process will handle the "brain" // activities: communicating sensor data to the client // and receiving control commands from the client. if(( pid = fork()) < 0) { perror("fork error"); } //------------------------------------------------------------------------ // Code for parent (nerves) //------------------------------------------------------------------------ else if (pid > 0) { // Close the client socket since this parent won't be using it. close(clientSock); //TELL_CHILD(pid); // Open the serial port to the robot. If this is unsuccessful, // there is no point in continuing. if(openPort() == 0) { printf("Port failed to open \n"); exit(-1); } // Initialize the robot, prepare it to receive commands. Wait // for a second to give this some time to take effect. initialize(); sleep(1); #ifdef TARGET_WEBBY // Initialize the compass device. initializeCompass(); // This is temporary until turn() works. // Turn Left 10 degrees turn(TURN_LEFT, 10); #endif // Turn on the LED to indicate the robot is ready and listening. // It's a nice sanity check. setLED(RED, PLAY_ON, ADVANCE_ON); //------------------------------------------------------------------------ // Added Code to implement client //------------------------------------------------------------------------ if(check == 1) { #ifdef DEBUG printf("%s %d \n", __FILE__, __LINE__); #endif memset(&hints, 0, sizeof hints); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; if ((rv = getaddrinfo(addresses[1], PORT, &hints, &servinfo)) != 0) { fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv)); return 1; } // loop through all the results and connect to the first we can for(p = servinfo; p != NULL; p = p->ai_next) { if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) { perror("client: socket"); continue; } if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) { close(sockfd); perror("client: connect"); continue; } break; } if (p == NULL) { fprintf(stderr, "client: failed to connect\n"); return 2; } inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr), s, sizeof s); printf("client: connecting to %s\n", s); freeaddrinfo(servinfo); // all done with this structure //if the client does not recieve anything from server then exit if ((numbytes = recv(sockfd, msgBuf, MAXDATASIZE-1, 0)) == -1) { perror("recv"); exit(1); } } //------------------------------------------------------------------------ // End of added section //------------------------------------------------------------------------ while(commandToRobot[0] != ssQuit) { commandToRobot[0] = 'z'; // Wait until a valid command is received. while(commandToRobot[0] == 'z') { commandToRobot[0] = readFromMessageQueueAndExecute(mqd_cmd); } printf("commandToRobot: %d\n", commandToRobot[0]); //------------------------------------------------------------------------ // Added Code to implement client //------------------------------------------------------------------------ if(check == 1) { if(send(sockfd, &commandToRobot[0], 1, 0) == -1) perror("send"); printf(" the command code sent was: %d\n", commandToRobot[0]); } //------------------------------------------------------------------------ // End of added section //------------------------------------------------------------------------ receiveGroupOneSensorData(sensDataFromRobot); // check if any of the sensor data indicates a // sensor has been activated. If so, react be // driving backwards briefly and then stopping. int sensData = 0; // Check sensor data first to stop ramming into wall. sensData = checkSensorData(sensDataFromRobot); #ifdef DEBUG printf("%s %d \n", __FILE__, __LINE__); #endif // Wait until child has sent previous sensor data. //WAIT_CHILD(); #ifdef DEBUG printf("%s %d \n", __FILE__, __LINE__); printf("%d \n", sensData); #endif if(sensData) { #ifdef DEBUG printf("%s %d \n", __FILE__, __LINE__); #endif // Drive backwards and then stop. driveBackwardsUntil(EIGHTH_SECOND, MED); STOP_MACRO; // Convey sensorData back to the child. writeSensorDataToMessageQueue(sensDataFromRobot, mqd_sns, getTime(), getRawTime()); } // Done writing sensor data, tell child to proceed reading sensor data. TELL_CHILD(pid); // Reset the array; fill it again in the next loop. for(i = 0; i <= 6; i++) { sensDataFromRobot[i]= FALSE; } } if (closePort() == -1) { perror("Port failed to close \n"); exit(-1); } send(sockfd, &input, 1, 0); close(sockfd); kill(0, SIGTERM); mq_close(mqd_cmd); exit(0); } //------------------------------------------------------------------------ // Code for child (brain) //------------------------------------------------------------------------ else { // Child process doesn't need the listener. close(serverID); // Initially tell the parent to proceed and write sensor data. //TELL_PARENT(getppid()); // Send the client the initial connection message. if(send(clientSock, MSG, sizeof(MSG), 0) == -1) perror("send"); // At the request of the supervisor implementation team, The // brain follows a strict alternation of 'receive control // command' then 'send sensor data'. If the control command // sent by the supervisor-client is 'turn left' and the iRobot // happens to bump into something, then the subsequent message // to the supervisor-client will indicate which bumper was activated. // Similarly, if the control command sent by the supervisor is // 'turn left' and the iRobot experiences no sensory input, the // subsequent message to the supervisor will indicate that no sensors // were activated. // As long as the quit command 'q' has not been sent by the // supervisor-client, receive a control command and send the // subsequent sensor data. while(commandFromSupervisor[0] != ssQuit) { // Wait to receive command from supervisor-client; read the command // into cmdBuf. if ((numBytes = recv(clientSock, commandFromSupervisor, MAXDATASIZE-1, 0)) == -1) { perror("recv"); return -1; } // Write the read command into shared memory so that the // parent (nerves) may read and execute it. //writeCommandToSharedMemory(commandFromSupervisor, cmdArea,mqd_cmd); writeCommandToMessageQueue(commandFromSupervisor,mqd_cmd); // Wait until parent has written sensor data. WAIT_PARENT(); #ifdef DEBUG printf("%s %d \n", __FILE__, __LINE__); #endif // If there is sensor data available, send it to the // supervisor-client. if(readSensorDataFromMessageQueue(sensDataToSupervisor, mqd_sns)) { printf("\nsensDataToSupervisor: %s \n", sensDataToSupervisor); if(send(clientSock, sensDataToSupervisor, strlen(sensDataToSupervisor)-1 , 0) == -1) perror("send"); } // Otherwise, assume no sensor was activated. Send an empty // sensor message to the supervisor-client. else { itoa(getRawTime(), rawTimeString); // Construct an empty sensor data message and send it to // the supervisor-client. strncat(emptyDataToSupervisor, rawTimeString, MAXDATASIZE-SIZE_OF_EMPTY_DATA); strncat(emptyDataToSupervisor, " ", 1); strncat(emptyDataToSupervisor, getTime(), MAXDATASIZE-SIZE_OF_EMPTY_DATA); printf("\nemptySensDataToSupervisor: %s \n", emptyDataToSupervisor); if(send(clientSock, emptyDataToSupervisor, MAXDATASIZE-1, 0) == -1) perror("send"); emptyDataToSupervisor[SIZE_OF_EMPTY_DATA] = '\0'; } // Done reading sensor data, allow parent to write more sensor data. TELL_PARENT(getppid()); } // All done. Clean up the toys and go home. printf("Closing socket and terminating processes.\nHave a nice day!\n"); kill(pid, SIGTERM); close(clientSock); mq_close(mqd_cmd); exit(0); } // Added following code to implement communication between roomba's }