int main(int argc, char ** argv, char ** envp) { while(1) { fprintf(stderr, "Sleep...\n"); slepp(1); } return 0; }
int main(int argc, char ** argv, char ** envp) { int i, j; char buf[16]; char * args[3]; struct sigaction action; action.sa_flags = SA_SIGINFO; action.sa_sigaction = sig_handler; sigemptyset(&action.sa_mask); sigaction(SIGCHLD, &action, NULL); sigaction(SIGTERM, &action, NULL); srand(time(NULL)); fprintf(stderr, "hello, pid=%d, argc=%d, argv0=[%s], argv1=[%s]\n", getpid(), argc, argv[0], argv[1]); while(1) { slepp(1); switch(rand() & 0xf) { case 0: // Fork... if (fork() == 0) { srand(getpid()); fprintf(stderr, "fork, pid=%d\n", getpid()); } break; case 1: // Exit... fprintf(stderr, "exit, pid=%d\n", getpid()); exit(0); case 2: case 3: // Exec... j = 0; if (argc > 1) sscanf(argv[1], "%d", &j); sprintf(buf, "%d", ++j); args[0] = argv[0]; args[1] = buf; args[2] = NULL; fprintf(stderr, "exec, argv0=[%s], argv1=[%s]\n", argv[0], buf); execve(argv[0], args, envp); break; default: // Do nothing by default... fprintf(stderr, "sleep, zzzz...\n"); slepp(2 + (rand() % 5)); } } return 0; }
int main() { int server_sockfd, client_sockfd; int server_len, client_len; struct sockaddr_in server_address; struct sockaddr_in client_address; server_sockfd = socket(AF_INET,SOCK_STREAM,0); server_address.sin_family = AF_INET; server_address.sin_addr.s_addr = htonl(INADDR_ANY); server_address.sin_port = htons(9734); server_len = sizeof(server_address); bind(server_sockfd, (struct sockaddr *)&server_address, server_len); listen(server_sockfd, 5); signal(SIGCHLD,SIG_INT); while(1) { char ch; printf("server waititng \n"); client_len = sizeof(client_address); client_sockfd = accept(server_sockfd,(struct sockaddr *)&client_address,&client_len); if (fork() == 0) { read(client_sockfd, &ch, 1); slepp(5); ch++; write(client_sockfd, &ch, 1); close(client_sockfd); exit(EXIT_SUCCESS); } else { close(client_sockfd); } } }