int main () { pid_t childId = fork(); if (childId == 0) { doChild(); exit(EXIT_SUCCESS); } int i; char line[MAX_LINE]; printf("The PID of the child is %d\n",childId); for (i = 0; i < 4; i++) { printf("Press enter to tickle child:\n"); fgets(line,MAX_LINE,stdin); kill(childId,SIGINT); } kill(childId,SIGKILL); return(EXIT_SUCCESS); }
static void spk_say (volatile SpeechSynthesizer *spk, const unsigned char *buffer, size_t length, size_t count, const unsigned char *attributes) { if (voice) { if (child != -1) goto ready; if (pipe(pipeDescriptors) != -1) { if ((child = fork()) == -1) { logSystemError("fork"); } else if (child == 0) { _exit(doChild()); } else ready: { unsigned char text[length + 1]; memcpy(text, buffer, length); text[length] = '\n'; write(*pipeInput, text, sizeof(text)); return; } close(*pipeInput); close(*pipeOutput); } else { logSystemError("pipe"); } } }
void SoSwitch::doAction(SoAction *action) // //////////////////////////////////////////////////////////////////////// { int numIndices; const int *indices; if (action->getPathCode(numIndices, indices) == SoAction::IN_PATH) { int i; for (i = 0; i < numIndices; i++) doChild(action, indices[i]); } else doChild(action); }
int main() { pid_t pid = fork(); switch (pid) { case -1: perror("fork"); exit(-1); case 0: doChild(); exit(0); default: doParent(); } }
int main(int argc, char *argv[]) { int pid; dataIO_t data; int start_msgs, done_msgs; FILE *fd_pipes, *fd_events; if(argc < 2) usage(); int c; opterr=0; int mutexfl = 0; const char* short_options = "p:"; const struct option long_options[] = { {"mutexl", 0, &mutexfl, 1}, {NULL,0,NULL,0} }; while(1){ c = getopt_long(argc, argv, short_options, long_options, NULL); if (c == -1) break; switch (c) { case 0: break; case 'p': data.processes = atoi(optarg)+1; break; case '?': default: usage(); } } data.lid = 0; if ((fd_pipes = fopen(pipes_log, "w")) == NULL) { fprintf(stderr, "Error opening the file %s\n", pipes_log); exit(1); } for(int i = 0; i < data.processes; i++) { for(int j = 0; j < data.processes; j++) { if(j==i) { data.pipes[i][j].rdwr[0] = -1; data.pipes[i][j].rdwr[1] = -1; } else { if(pipe(data.pipes[i][j].rdwr) < 0) { fprintf(stderr, "Error creating the pipe\n"); exit(1); } int mode; mode = fcntl(data.pipes[i][j].rdwr[0], F_GETFL); fcntl(data.pipes[i][j].rdwr[0], F_SETFL, mode | O_NONBLOCK); mode = fcntl(data.pipes[i][j].rdwr[1], F_GETFL); fcntl(data.pipes[i][j].rdwr[1], F_SETFL, mode | O_NONBLOCK); fprintf(fd_pipes, "The pipe %d ===> %d was created\n", j, i); } } } fclose(fd_pipes); Message msg, resMsg; msg.s_header.s_magic = MESSAGE_MAGIC; msg.s_header.s_local_time = 0; if ((fd_events = fopen(events_log, "a")) == NULL) { fprintf(stderr, "Error opening the file %s\n", events_log); exit(1); } for(int i = 1; i < data.processes; i++) { pid = fork(); if(pid < 0) { fprintf(stderr, "Error creating the child\n"); exit(1); } else if (pid == 0) { doChild(&data, fd_events, i, 0, mutexfl); exit(0); } } closeUnusedPipes(&data); timestamp_t tm = get_lamport_time(); start_msgs = data.processes - 1; done_msgs = data.processes - 1; while(start_msgs) { if(receive_any(&data, &resMsg) > -1) { if(resMsg.s_header.s_type == STARTED) { start_msgs--; lamportStamp = max(lamportStamp, resMsg.s_header.s_local_time); tm = get_lamport_time(); } if(resMsg.s_header.s_type == DONE) { done_msgs--; lamportStamp = max(lamportStamp, resMsg.s_header.s_local_time); tm = get_lamport_time(); } } } fprintf(fd_events, log_received_all_started_fmt, tm, data.lid); fflush(fd_events); printf(log_received_all_started_fmt, tm, data.lid); while(done_msgs) { if(receive_any(&data, &resMsg) > -1) { if(resMsg.s_header.s_type == DONE){ done_msgs--; lamportStamp = max(lamportStamp, resMsg.s_header.s_local_time); tm = get_lamport_time(); } } } fprintf(fd_events, log_received_all_done_fmt, tm, data.lid); fflush(fd_events); printf(log_received_all_done_fmt, tm, data.lid); fclose(fd_events); for(int i = 0; i < data.processes; i++) { wait(&i); } return 0; }