/* Execute the given command in the array. Supports arguments */ void execute(char *input){ pid_t pid; char command[BUF_SIZE]; trim(input, command); if(strcmp("exit", command) == 0){ printf("exiting...\n"); exit(0); } else if(strcmp("", command) != 0){ pid = fork(); if(pid < 0){ perror("fork"); exit(-1); } else if(pid == 0){ /*child*/ child_code(command); } else{ /* parent */ parent_code(); } } }
main(int ac, char *av[]) { void child_code(int), parent_code(int); pid_t pid; int i,n; int delay; /* * setup */ if ( ac == 1 ){ fprintf(stderr,"usage: sol08.8b numberofchildren\n"); exit(1); } n = atoi(av[1]); srand(getpid()); printf("before: mypid is %d\n", getpid()); /* * main loop */ for(i=0; i<n ; i++){ delay = 1 + (rand() % MAXDELAY); pid = fork(); if ( pid == -1 ) oops("fork", 2); if ( pid == 0 ) child_code(delay); } parent_code(n); return 0; }
void *entry (void *arg) { Shared *shared = (Shared *) arg; child_code (shared); printf ("Child done.\n"); pthread_exit (NULL); }
int main(int argc, char const *argv[]) { /* basically the same key used for the semaphore programs */ key_t key; pid_t childPid; int msqid; /* create a token */ if((key = ftok("/tmp", SECRET_KEY)) == -1) { printf("[-] Error on ftok\n"); return 1; } /* create a set of msg */ if((msqid = msgget(key, 0666|IPC_CREAT|IPC_EXCL)) == -1) { printf("[-] Error on msgid\n"); return 1; } /* now use the fork */ switch (childPid = fork()){ case -1: //fork fallita return 1; break; case 0: // eseguo il codice del figlio child_code(msqid); break; default: // eseguo il codice del padre parent_code(msqid); break; } return 0; }
// main takes two parameters: argc is the number of command-line // arguments; argv is an array of strings containing the command // line arguments int main (int argc, char *argv[]) { int status; pid_t pid; double start, stop; int i, num_children; // the first command-line argument is the name of the executable. // if there is a second, it is the number of children to create. if (argc == 2) { num_children = atoi (argv[1]); } else { num_children = 1; } // get the start time start = get_seconds (); for (i=0; i<num_children; i++) { // create a child process printf ("Creating child %d.\n", i); pid = fork (); /* check for an error */ if (pid == -1) { fprintf (stderr, "fork failed: %s\n", strerror(errno)); perror (argv[0]); exit (1); } /* see if we're the parent or the child */ if (pid == 0) { child_code(i); } } /* parent continues */ printf ("Hello from the parent.\n"); for (i=0; i<num_children; i++) { pid = wait (&status); if (pid == -1) { fprintf (stderr, "wait failed: %s\n", strerror(errno)); perror (argv[0]); exit (1); } // check the exit status of the child status = WEXITSTATUS (status); printf ("Child %d exited with error code %d.\n", pid, status); } // compute the elapsed time stop = get_seconds (); printf ("Elapsed time = %f seconds.\n", stop - start); exit (0); }
int main(void) { int newpid; printf("Before: my pid is %d\n", getpid()); if ((newpid = fork()) == -1) perror("fork"); else if (newpid == 0) child_code(DELAY); else parent_code(newpid); return EXIT_SUCCESS; }
int main() { int newpid; void child_code(), parent_code(); printf("before: my pid is %d \n", getpid()); if ((newpid = fork()) == -1) perror("fork"); else if (newpid == 0) child_code(DELAY); else parent_code(newpid); }
int main(int argc, char *argv[]) { process_num = atoi(argv[1]); int parent_pid = getpid(); printf("Before: my pid is %d\n", parent_pid); for (int i = 0; i < process_num; ++i) child_code(parent_pid, DELAY); signal(SIGCHLD, parent_code); while (true) { printf("waiting...\n"); sleep(1); } return EXIT_SUCCESS; }
int main(void) { int newpid; void child_code(), parent_code(); printf("before: my pid is %d\n", getpid()); if ((newpid = fork()) == -1) { perror("fork"); } else if (newpid == 0) { child_code(DELAY); } else { parent_code(newpid); } return 0; }
int main(int argc, char **argv) { int newpid; int i; void child_code(int); void parent_code(int); if(argc != 2){ printf("Usage: waitdemo_l number_of_fork\n"); exit(0); } for(i = 0; i < atoi(argv[1]); i++){ if((newpid = fork()) == -1){ perror("fork"); exit(0); }else if(newpid == 0) child_code(DELAY); else parent_code(newpid); } return 0; }
int main(int argc, char *argv[]) { int i; int n_children; int father_pid; int sec; /*int pid[MAX];*/ // Print all argument (debug purpose) printf("Arguments:\n"); for (i = 0; i < argc; i++) printf("[%d]:%s\n", i,argv[i]); //Check number of arguments if(argc != 4){ printf("Illegal number of parameters\n"); exit(1); } //Check argument is digit if(!isdigit(argv[1][0])){ printf("%d must be digit\n",argv[1][0]); exit(1); } //Check argument is positive if (argv[1][0]<=0) { printf("%d must be >0\n",argv[1][0]); exit(1); } //Check files exist and executable for (i = 2; i < argc-1; ++i) { if (access(argv[i],F_OK)!= -1 && access (argv[i],X_OK)!=-1) { printf("file: %s exist and executable\n", argv[i]); } else { printf("file: %s incorrect\n", argv[i]); } } sec = atoi(argv[1]); n_children = 2; printf("n_children = %d\n", n_children); signal(SIGUSR1,&signal_handler); signal(SIGTERM,&signal_handler); printf("----- start program -----\n"); //Pid of the father father_pid = getpid(); for (i = 0; i < n_children; ++i) { //printf("about to create child [%d]\n", i); pid[i] = fork(); if (pid[i] == 0) //child { printf("child:pid - [%d][%d]\n",i,getpid()); child_code(argv[i+2],i,sec); } else if (pid[i] > 0)//father {} else //Errors { perror("Unable to fork()"); exit(2); } } //father code for (i = 0; i < n_children; ++i) { wait_child(); } return 0; }