/* * Program args: num_threads, num_pthread_exit. */ int main(int argc, char **argv) { pthread_t td; long i; if (argc < 2 || sscanf(argv[1], "%d", &num_threads) < 1) num_threads = NUM_THREADS; if (argc < 3 || sscanf(argv[2], "%d", &num_pthread_exit) < 1) num_pthread_exit = NUM_PTHREAD_EXIT; printf("num_threads = %d, num_pthread_exit = %d\n\n", num_threads, num_pthread_exit); gettimeofday(&start, NULL); for (i = 1; i <= num_threads; i++) { if (pthread_create(&td, NULL, my_thread, (void *)i) != 0) errx(1, "pthread_create failed"); } wait_for_time(program_time - 1); printf("----------------------------------------\n"); wait_for_time(program_time); printf("main exit (return)\n"); return (0); }
/* * Threads 1..k call pthread_exit() and k+1..n call exit(), * where k = num_pthread_exit. */ void * my_thread(void *v) { long num = (long)v; int is_exit = (num > num_pthread_exit); printf("start thread: %ld\n", num); wait_for_time(program_time); printf("end thread: %ld (%s)\n", num, is_exit ? "exit" : "pthread_exit"); if (is_exit) exit(0); else pthread_exit(NULL); return (NULL); }
int main() { //allocDealloc(); //The function for allocation and deallocation is there but not used srand(time(NULL)); pid_t pd1,pd2,pd; char choice[10]; int variable=0,status; pd1=fork(); //First child Process if (pd1<0) printf("Forking process failed\n"); else if (pd1==0) childProcess(1); pd2=fork(); //Second child Process if (pd2<0) printf("Forking process failed\n"); else if (pd2==0) childProcess(2); parentProcess(); pd=wait(&status); //Waits for any of the two child process to finish printf("Parent detects child process %d was done first\n",pd); pd=wait(&status); //Waits for second process to finish printf("Parent detects child process %d is done \n",pd); printf("Parent exits\n"); wait_for_time(1); //Wait for a given time printf("Do you want to execute any file yes or no\n"); scanf("%s",choice); if (strcmp(choice,"yes")==0) { printf("The program is moving over to executing another binary\n"); //executes another file execute(); } else { printf("That's all"); exit(0); } return 0; }
/*! * Hauptprogramm des Bots. Diese Schleife kuemmert sich um seine Steuerung. */ int main (void){ #endif #ifdef PC /*! * Hauptprogramm des Bots. Diese Schleife kuemmert sich um seine Steuerung. */ int main (int argc, char *argv[]){ int ch; int start_server = 0; /*!< Wird auf 1 gesetzt, falls -s angegeben wurde */ char *hostname = NULL; /*!< Speichert den per -t uebergebenen Hostnamen zwischen */ // Die Kommandozeilenargumente komplett verarbeiten while ((ch = getopt(argc, argv, "hst:")) != -1) { switch (ch) { case 's': // Servermodus [-s] wird verlangt start_server = 1; break; case 't': // Hostname, auf dem ct-Sim laeuft wurde // uebergeben. Der String wird in hostname // gesichert. { const int len = strlen(optarg); hostname = malloc(len + 1); if (NULL == hostname) exit(1); strcpy(hostname, optarg); } break; case 'h': default: // -h oder falscher Parameter, Usage anzeigen usage(); } } argc -= optind; argv += optind; if (start_server != 0) // Soll der TCP-Server gestartet werden? { printf("ARGV[0]= %s\n",argv[1]); tcp_server_init(); tcp_server_run(); } else { printf("c't-Bot\n"); if (hostname) // Hostname wurde per Kommandozeile uebergeben tcp_hostname = hostname; else { // Der Zielhost wird per default durch das Macro IP definiert und // tcp_hostname mit einer Kopie des Strings initialisiert. tcp_hostname = malloc(strlen(IP) + 1); if (NULL == tcp_hostname) exit(1); strcpy(tcp_hostname, IP); } } #endif #ifdef TEST_AVAILABLE_MOTOR uint16 calls=0; /*!< Im Testfall zaehle die Durchlaeufe */ #endif #ifdef LOG_AVAILABLE printf("Logging is on ("); #ifdef LOG_UART_AVAILABLE printf("UART"); #endif #ifdef LOG_CTSIM_AVAILABLE printf("CTSIM"); #endif #ifdef LOG_DISPLAY_AVAILABLE printf("DISPLAY"); #endif #ifdef LOG_STDOUT_AVAILABLE printf("STDOUT"); #endif printf(")\n"); #else printf("Logging is off!\n "); #endif init(); #ifdef WELCOME_AVAILABLE display_cursor(1,1); display_printf("c't-Roboter"); LED_set(0x00); #ifdef LOG_AVAILABLE LOG_DEBUG(("Hallo Welt!")); #endif #endif #ifdef TEST_AVAILABLE_COUNTER display_screen=2; resets=eeprom_read_byte(&resetsEEPROM)+1; eeprom_write_byte(&resetsEEPROM,resets); /* Lege den Grund für jeden Reset im EEPROM ab */ eeprom_write_byte(&resetInfoEEPROM+resets,reset_flag); #endif /*! Hauptschleife des Bot */ for(;;){ #ifdef MCU bot_sens_isr(); #endif #ifdef TEST_AVAILABLE show_sensors(); #endif // Testprogramm, dass den Bot erst links, dann rechtsrum dreht #ifdef TEST_AVAILABLE_MOTOR calls++; if (calls == 1) motor_set(BOT_SPEED_SLOW,-BOT_SPEED_SLOW); else if (calls == 501) motor_set(-BOT_SPEED_SLOW,BOT_SPEED_SLOW); else if (calls== 1001) motor_set(BOT_SPEED_STOP,BOT_SPEED_STOP); else #endif // hier drin steckt der Verhaltenscode #ifdef BEHAVIOUR_AVAILABLE if (sensors_initialized ==1 ) bot_behave(); else printf("sensors not initialized\n"); #endif #ifdef MCU #ifdef BOT_2_PC_AVAILABLE // static int16 lastTimeCom =0; bot_2_pc_inform(); // Den PC ueber Sensorern und aktuatoren informieren bot_2_pc_listen(); // Kommandos vom PC empfangen // if (timer_get_s() != lastTimeCom) { // sollte genau 1x pro Sekunde zutreffen // lastTimeCom = timer_get_s(); // } #endif #endif #ifdef LOG_AVAILABLE //LOG_DEBUG(("LOG TIME %d s", timer_get_s())); #endif // Alles Anzeigen #ifdef DISPLAY_AVAILABLE display(); #endif #ifdef PC wait_for_time(100000); #endif #ifdef MCU // delay(10); #endif } /*! Falls wir das je erreichen sollten ;-) */ return 1; }