HW12
12.16
void* thread(void * vargp) {
printf("12.16 homework threadId: %ld!\n", pthread_self());
return NULL;
}
int main(int argc, char **argv) {
if (argc != 2) {
fprintf(stderr, "usage: %s <n>\n", argv[0]);
exit(0);
}
int n = atoi(argv[1]);
pthread_t tid[n];
for (size_t i = 0; i < n; i++) {
pthread_create(&tid[i], NULL, thread, NULL);
}
for (size_t i = 0; i < n; i++) {
pthread_join(tid[i], NULL); // 等待线程结束
}
exit(0);
}12.17
void* thread(void * vargp) {
sleep(1);
printf("Hello, world!\n");
return NULL;
}
int main(int argc, char **argv) {
pthread_t tid;
pthread_create(&tid, NULL, thread, NULL);
pthread_join(tid, NULL);
exit(0);
}12.35
static int cnt = 0;
void signchld_handler(int sig) {
// WNOHANG: 如果等待集中的子进程均未终止,则立即返回(返回值为 0)。
// 默认行为是暂停调用进程,直到子进程终止
while (waitpid(-1, NULL, WNOHANG) > 0) {
cnt--;
}
}
int main(int argc, char **argv)
{
int listenfd, connfd;
char hostname[MAXLINE], port[MAXLINE];
socklen_t clientlen;
struct sockaddr_storage clientaddr;
if (argc != 2) {
fprintf(stderr, "usage: %s <port>\n", argv[0]);
exit(1);
}
listenfd = Open_listenfd(argv[1]);
while (1) {
clientlen = sizeof(clientaddr);
connfd = Accept(listenfd, (SA *)&clientaddr, &clientlen);
cnt+=1;
if (cnt >= 16) {
// 回收子线程,避免内存泄漏
while (waitpid(-1, NULL, WNOHANG) > 0)
{
cnt -= 1;
}
}
Getnameinfo((SA *) &clientaddr, clientlen, hostname, MAXLINE,
port, MAXLINE, 0);
printf("Accepted connection from (%s, %s)\n", hostname, port);
if (Fork() == 0) {
doit(connfd);
Close(connfd);
exit(0);
}
Close(connfd);
}
}12.37
🐝 利用生产消费者 sbuf.c
#define SBUFSIZE 64;
#define NTHREADS 16
static sbuf_t sbuf;
void* thread(void *vargp) {
Pthread_detach(pthread_self());
while (1) {
// 此时若是有还没处理的connfd,则继续,否则阻塞
int connfd = sbuf_remove(&sbuf);
doit(connfd);
Close(connfd);
}
}
int main(int argc, char **argv) {
// 初始化Sbuf
sbuf_init(&sbuf, SBUFFSIZE);
// 创建多个线程去处理客户端请求
pthread tid[NTHREADS];
for (size_t i = 0; i < NTHREADS; i++) {
Pthread_create(&tid[i], NULL, thread, NULL);
}
while (1) {
// 若是有空余插槽则插入,否则阻塞
sbuf_insert(&sbuf, connfd);
}
}
转载请注明出处