CS162 操作系统HW2(使用Liunx内核链表以及多线程实现WordCounter)

本文介绍了C/C++开发中IDE自动补全提高开发效率的重要性,以及如何利用GDB进行调试。展示了如何使用条件编译在单线程和多线程环境中实现wordCount程序,通过宏定义简化代码。同时,详细说明了线程安全的插入操作,并给出了主函数的多线程处理方式。
摘要由CSDN通过智能技术生成

心得体会

IDE自动提示补全真的特别重要,大大提高开发效率。通过IDE自动搜索库函数API。

GDB调试能力要加强。

 

使用前面提供的list.h来改写wordCount程序,头文件的实现相当有技巧,将使用外部list库,多线程都用宏定义到同一份代码中,这是C/C++开发的常用技巧。

这里Linux内核里面的链表实现和传统的有一些区别

word_count.h

#ifdef PINTOS_LIST
#include "list.h"
typedef struct word_count {
  char* word;
  int count;
  struct list_elem elem;
} word_count_t;

#ifdef PTHREADS
#include <pthread.h>
typedef struct word_count_list {
  struct list lst;
  pthread_mutex_t lock;
} word_count_list_t;
#else  /* PTHREADS */
typedef struct list word_count_list_t;
#endif /* PTHREADS */

#else  /* PINTOS_LIST */

typedef struct word_count {
  char* word;
  int count;
  struct word_count* next;
} word_count_t;

typedef word_count_t* word_count_list_t;
#endif /* PINTOS_LIST */

/* Initialize a word count list. */
void init_words(word_count_list_t* wclist);

/* Get length of a word count list. */
size_t len_words(word_count_list_t* wclist);

/* Find a word in a word_count list. */
word_count_t* find_word(word_count_list_t* wclist, char* word);

/*
 * Insert word with count=1, if not already present; increment count if
 * present. Takes ownership of word.
 */
word_count_t* add_word(word_count_list_t* wclist, char* word);

/* Print word counts to a file. */
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值