int main() { int sem_id = create_sem(1); init_sem(sem_id, 0, 1); pid_t id = fork(); if(id < 0) { perror("fork"); return -1; } else if(id == 0) {//child while(1) { sem_P(sem_id); printf("A"); fflush(stdout); usleep(20000); usleep(23456); printf("A"); sem_V(sem_id); } } else {//father while(1) { sem_P(sem_id); printf("B"); fflush(stdout); usleep(29633); usleep(23456); printf("B"); sem_V(sem_id); } } destroy_sem(sem_id); return 0; }
void SimpleThread(void* which) { int i; sem_P(*((struct s*)which)->sem); for (i = 0; i < 1000; i++) { PutChar(((struct s*)which)->c); } sem_V(*((struct s*)which)->sem); UserThreadExit(); }
void send_QR(int sleepTime) { sleep(sleepTime); t_msg_header hdr; hdr.type = RFC_QUESTIONREQUEST; printf("cr: %i \n", hdr.type); //umdrehen <=16 Bit werte hdr.length = 0; sem_P(keymng_local(KEY_GCI_SEM)); send(GCI.sock, &hdr, sizeof(hdr), 0); sem_V(keymng_local(KEY_GCI_SEM)); printf("QR was send \n"); }
void sendCR() { t_msg_header hdr; hdr.type = RFC_CATALOGREQUEST; printf("cr: %i \n", hdr.type); //umdrehen <=16 Bit werte hdr.length = 0; sem_P(keymng_local(KEY_GCI_SEM)); send(GCI.sock, &hdr, sizeof(hdr), 0); sem_V(keymng_local(KEY_GCI_SEM)); printf("CR was send \n"); }
/**=========================================== * @brief main thread, starting up the client and the other threads * @param argc:int argv**char * ==========================================*/ int main(int argc, char **argv) { char *name = "Guest"; char *server = "localhost"; char *port = "54321"; int thread; int ret, c, conn=0; while(optind < argc) { int option_index = 0; static struct option long_options[] = { {"name", required_argument, 0, 'n'}, {"port", optional_argument, 0, 'p'}, {"help", no_argument, 0, 'h'}, {0,0,0,0} }; c = getopt_long(argc, argv, "hp:n:", long_options, &option_index); if(c == -1) break; switch(c) { case '?': /* unknown parameter */ case ':': /* missing argument */ print_help(argv[0]); break; case 'n': name = strdup(optarg); if(!name) return -1; break; case 'p': port = strdup(optarg); break; case 'h': print_help(argv[0]); break; default: break; } } while(optind < argc) { server = argv[optind++]; } GCI.name = name; printf("Benutzername: %s\n", GCI.name); printf("server: %s", server); struct addrinfo *addr_info, *p; ret = getaddrinfo(server, port, NULL, &addr_info); if(ret) { printf("getaddrinfo: %s\n", gai_strerror(ret)); exit(-1); } p = addr_info; while (p) { if(p->ai_socktype != SOCK_STREAM)/* we only care about TCP */ { p = p->ai_next; continue; } int sock = socket(p->ai_family, p->ai_socktype, 0); if(sock == -1) { perror("socket"); exit(-1); } if(connect(sock, p->ai_addr, p->ai_addrlen) == 0) { signal(SIGINT, sigint_handler); // move to better positioon sem_V(keymng_local(KEY_GCI_SEM)); printf("Socket OK"); GCI.sock = sock; send_login(GCI.name); int state = wait_loginOK(); if(state !=0) { printf("Keine antwort erhalten \n"); return 0; } printf("juhu ich bin eingeloggt \n"); GCI.status = preparation; conn =1; guiInit(&argc, &argv); printf("GUI init \n"); setClientMode(); preparation_showWindow(); guiShowMessageDialog("Willkommen bei You Dont Know Rainer",0); // start the threads thread = pthread_create(&listener_thread_id, NULL, &listener_thread, NULL); if(thread) { printf("Failed to start Listener Thread\n"); exit(0); } thread = pthread_create(&fragen_thread_id, NULL, &fragen_thread, NULL); if(thread) { printf("Failed to start Fragewechsel Thread\n"); exit(0); } //katalog request sendCR(); guiMain(); guiDestroy(); } close(sock); p = p->ai_next; } if(conn==0) printf("Could not connect to server :/\n"); freeaddrinfo(addr_info); exit(0); }