如何在C语言中练习Unix编程?

时间:2021-07-03 15:04:15

After five years of professional Java (and to a lesser extent, Python) programming and slowly feeling my computer science education slip away, I decided I wanted to broaden my horizons / general usefulness to the world and do something that feels more (to me) like I really have an influence over the machine. I chose to learn C and Unix programming since I feel like that is where many of the most interesting problems are.

经过五年的专业Java(在较小程度上,Python)编程,慢慢感觉我的计算机科学教育溜走,我决定我想扩大我的视野/一般对世界有用和做一些感觉更(对我来说)就像我真的对机器有影响。我选择学习C和Unix编程,因为我觉得这是许多最有趣的问题所在。

My end goal is to be able to do this professionally, if for no other reason than the fact that I have to spend 40-50 hours per week on work that pays the bills, so it may as well also be the type of coding I want to get better at. Of course, you don't get hired to do things you haven't done before, so for now I am ramping up on my own.

我的最终目标是能够做这个专业,如果没有其他原因,我必须花每周40小时的工作,支付账单,所以这可能也是编码的类型,我想变得更好。当然,你不会被雇佣去做你以前没做过的事情,所以现在我要自己努力了。

To this end, I started with K&R, which was a great resource in part due to the exercises spread throughout each chapter. After that I moved on to Computer Systems: A Programmer's Perspective, followed by ten chapters of Advanced Programming in the Unix Environment. When I am done with this book, I will read Unix Network Programming.

为此,我从K&R开始,这是一个很大的资源,因为每个章节都有练习。之后,我转向了计算机系统:一个程序员的视角,接着是Unix环境中10个高级编程的章节。读完这本书,我将阅读Unix网络编程。

What I'm missing in the Stevens books is the lack of programming problems; they mainly document functionality and provide examples, with a few end-of-chapter questions following. I feel that I would benefit much more from being challenged to use the knowledge in each chapter a la K&R. I could write some test program for each function, but this is a less desirable method as (1) I would probably be less motivated than if I were rising to some external challenge, and (2) I will naturally only think to use the function in the ways that have already occurred to me.

我在史蒂文斯书中漏掉的是缺少编程问题;它们主要记录功能并提供示例,后面还有一些章节结尾的问题。我觉得如果我能在每一章里都运用知识,我将会受益更多。我可以写一些测试程序为每个函数,但这是一个不太理想的方法(1)我的动机可能会低于如果我是一些外部的挑战,和(2)我只会自然而然地认为使用函数的方式已经发生给我。

So, I'd like to get some recommendations on how to practice. Obviously, my first choice would be to find some resource that has Unix programming challenges. I have also considered finding and attempting to contribute to some open source C project, but this is a bit daunting as there would be some overhead in learning to use the software, then learning the codebase. The only open-source C project I can think of that I use regularly is Python, and I'm not sure how easy that would be to get started on.

所以,我想就如何练习提出一些建议。显然,我的首选是找到一些具有Unix编程挑战的资源。我也考虑过寻找并尝试为某个开放源码C项目做出贡献,但这有点令人气馁,因为学习使用软件,然后学习代码基会带来一些开销。我能想到的唯一一个我经常使用的开源C项目是Python,我不确定这有多容易开始。

That said, I'm open to all kinds of suggestions as there are likely things I haven't even thought of.

话虽如此,我对各种各样的建议都持开放态度,因为我可能还没想过。

8 个解决方案

#1


26  

Reinvent a lot of the core Unix utilities. Most of these were (and still are) written in C, so they are a good way to start off learning. Depending on your skill, pick harder or easier utilities to copy.

重新创建许多核心Unix实用程序。其中大部分都是用C语言写的,所以它们是开始学习的好方法。根据你的技能,选择更硬或更容易的工具来复制。

Try writing your own malloc. You'll learn a lot about Unix and a lot of C programming as well.

试着写你自己的malloc。您将学习许多Unix知识和许多C编程。

Google for computer science operating system courses and do the projects there. Many schools have these projects on public websites so you could get everything you need. Here is a link to Purdue's site. Give the shell project a shot; it was difficult, but really educational.

计算机科学操作系统课程的谷歌,在那里做项目。许多学校在公共网站上都有这样的项目,所以你可以得到你需要的一切。这里是Purdue网站的链接。给shell项目一个机会;这很困难,但很有教育意义。

#2


5  

Here are a few * postings discussing C/Unix programming books. Their main claim to fame is extensive fan-out to other resources.

这里有一些讨论C/Unix编程书籍的*帖子。他们出名的主要原因是对其他资源的广泛依赖。

Practice some of the idioms (understand the ins and outs of pointers etc) and pick projects that help with that. The third item in the list has an answer (disclaimer, I wrote it) with a bunch of high-level C idioms where an idiomatic C program would differ from Java.

练习一些习语(了解指针的来历),并挑选出一些有助于解决这些问题的项目。列表中的第三项有一个答案(免责声明,我写了它),其中有一些高级的C语言,其中一个惯用的C程序与Java不同。

Learning XLib or Win32 GUI programming with C is probably less useful as almost anything is better than C for GUI programming and wrapping a core C engine with another language is generally much easier - unless you're really concerned with speed. As a starting point, concentrate on 'systems' programming applications, which is where you are most likely to get real mileage from C. Learn how the C interface of one of the scripting languages like Tcl or Python works.

使用C语言学习XLib或Win32 GUI编程可能没有什么用处,因为几乎任何东西都比C更好,而用另一种语言封装核心C引擎通常要容易得多——除非你真正关心的是速度。作为一个起点,专注于“系统”编程应用程序,这是您最有可能从C获得真正的好处的地方。了解像Tcl或Python这样的脚本语言的C接口是如何工作的。

Writing some bit-twiddly and pointer heavy code (e.g. picking apart network packets or interpreting a protocol), or server-side items using sockets will take you right into C's core competencies. If you've got some of the WRS books, try making pthreads work (IIRC UNP2 has a big section about pThreads). For a contrast, write something that uses non-blocking I/O. This stuff is C's home turf and you'll get a good working knowledge of C by doing this. Troll through the RFCs for some ideas of network protocols to implement.

编写一些字节和指针重的代码(例如,分离网络数据包或解释协议)或使用套接字的服务器端项目将使您直接进入C的核心竞争力。如果您有一些WRS书籍,请尝试让pthreads工作(IIRC UNP2有很大一部分关于pthreads)。相反,编写使用非阻塞I/O的代码。这个东西是C的地盘,通过这样做,你会对C有很好的了解。通过RFCs来实现网络协议的一些想法。

#3


1  

Are you open to book suggestions? Although it is a bit dated (where "a bit" perhaps is a huge understatement), Maurice Bach's "The Design of the Unix Operating System" is a great book. It belongs next to K&R on your bookshelf.

你愿意接受别人的建议吗?尽管这本书有点过时(在这本书中,“一点点”可能被大大低估),但莫里斯•巴赫(Maurice Bach)的《Unix操作系统的设计》(The Design of The Unix操作系统)是一本好书。它在你书架上K&R的旁边。

#4


1  

You might try working your way through all the examples in the book Software Tools (Amazon). Much of it is pretty pedestrian (right-justify text, de-tabify, etc.), but it's a great introduction to the Unix philosophy and basic C programming.

您可以尝试使用书籍软件工具(Amazon)中的所有示例。它的大部分都是相当普通的(右对齐的文本,去tabify等等),但是它是对Unix哲学和基本C编程的一个很好的介绍。

#5


1  

This is pretty interesting. As we know that Unix is designed by using the C language, it may not difficult to learn. If you are using the Windows OS, you can use a "Windows services for Unix" application to practice your programs. If you are using Unix, then we can use editors like vi.

这是很有趣的。正如我们所知道的,Unix是用C语言设计的,学习起来可能并不困难。如果您正在使用Windows操作系统,您可以使用“Unix的Windows服务”应用程序来实践您的程序。如果您正在使用Unix,那么我们可以使用像vi这样的编辑器。

#6


0  

I would recommend one thing highly.

我强烈推荐一件事。

Try and re-write all usual Linux command lines tools like ls, mkdir, cd etc.

尝试重新编写所有常用的Linux命令行工具,如ls、mkdir、cd等。

You'll gain a lot of knowlege about programming and Linux both at the same time.

您将同时获得许多编程和Linux方面的知识。

Pick the commands from the easiest, say "time" and work all the way up to the more complicated ones.

选择最简单的命令,说“时间”,然后一直工作到更复杂的。

#7


0  

The best way to consolidate your learnings it to practise. So just choose a kind of application that interest you and start developing it (for example, a network client/server simple application).

巩固所学的最好方法就是练习。因此,只需选择一种您感兴趣的应用程序并开始开发它(例如,网络客户机/服务器简单应用程序)。

Try to test most of the Unix APIs (files, sockets, etc.) to see how they work. You could for example get an Unix programming book, follow its chapters and test on your application everything you read, by creating your own functions. On that way, you can start to develop your own function library to be used in further projects.

尝试测试大多数Unix api(文件、套接字等),看看它们是如何工作的。例如,您可以通过创建自己的函数来获得一本Unix编程书籍,遵循它的章节并对您的应用程序进行测试。这样,您就可以开始开发自己的函数库,以便在以后的项目中使用。

#8


0  

Write a webserver.

编写一个网络服务器。

  • Make it multi-threaded.

    多线程。

  • Have it support a new scripting language you develop (a la PHP, etc.)

    让它支持你开发的一种新的脚本语言(一个la PHP,等等)

  • Allow uploads from authenticated users.

    允许通过身份验证的用户进行上传。

  • Write a plugin for your favorite tool (i.e. integrate with SVN to give a webview).

    为您喜欢的工具编写一个插件(例如,与SVN集成以提供webview)。

#1


26  

Reinvent a lot of the core Unix utilities. Most of these were (and still are) written in C, so they are a good way to start off learning. Depending on your skill, pick harder or easier utilities to copy.

重新创建许多核心Unix实用程序。其中大部分都是用C语言写的,所以它们是开始学习的好方法。根据你的技能,选择更硬或更容易的工具来复制。

Try writing your own malloc. You'll learn a lot about Unix and a lot of C programming as well.

试着写你自己的malloc。您将学习许多Unix知识和许多C编程。

Google for computer science operating system courses and do the projects there. Many schools have these projects on public websites so you could get everything you need. Here is a link to Purdue's site. Give the shell project a shot; it was difficult, but really educational.

计算机科学操作系统课程的谷歌,在那里做项目。许多学校在公共网站上都有这样的项目,所以你可以得到你需要的一切。这里是Purdue网站的链接。给shell项目一个机会;这很困难,但很有教育意义。

#2


5  

Here are a few * postings discussing C/Unix programming books. Their main claim to fame is extensive fan-out to other resources.

这里有一些讨论C/Unix编程书籍的*帖子。他们出名的主要原因是对其他资源的广泛依赖。

Practice some of the idioms (understand the ins and outs of pointers etc) and pick projects that help with that. The third item in the list has an answer (disclaimer, I wrote it) with a bunch of high-level C idioms where an idiomatic C program would differ from Java.

练习一些习语(了解指针的来历),并挑选出一些有助于解决这些问题的项目。列表中的第三项有一个答案(免责声明,我写了它),其中有一些高级的C语言,其中一个惯用的C程序与Java不同。

Learning XLib or Win32 GUI programming with C is probably less useful as almost anything is better than C for GUI programming and wrapping a core C engine with another language is generally much easier - unless you're really concerned with speed. As a starting point, concentrate on 'systems' programming applications, which is where you are most likely to get real mileage from C. Learn how the C interface of one of the scripting languages like Tcl or Python works.

使用C语言学习XLib或Win32 GUI编程可能没有什么用处,因为几乎任何东西都比C更好,而用另一种语言封装核心C引擎通常要容易得多——除非你真正关心的是速度。作为一个起点,专注于“系统”编程应用程序,这是您最有可能从C获得真正的好处的地方。了解像Tcl或Python这样的脚本语言的C接口是如何工作的。

Writing some bit-twiddly and pointer heavy code (e.g. picking apart network packets or interpreting a protocol), or server-side items using sockets will take you right into C's core competencies. If you've got some of the WRS books, try making pthreads work (IIRC UNP2 has a big section about pThreads). For a contrast, write something that uses non-blocking I/O. This stuff is C's home turf and you'll get a good working knowledge of C by doing this. Troll through the RFCs for some ideas of network protocols to implement.

编写一些字节和指针重的代码(例如,分离网络数据包或解释协议)或使用套接字的服务器端项目将使您直接进入C的核心竞争力。如果您有一些WRS书籍,请尝试让pthreads工作(IIRC UNP2有很大一部分关于pthreads)。相反,编写使用非阻塞I/O的代码。这个东西是C的地盘,通过这样做,你会对C有很好的了解。通过RFCs来实现网络协议的一些想法。

#3


1  

Are you open to book suggestions? Although it is a bit dated (where "a bit" perhaps is a huge understatement), Maurice Bach's "The Design of the Unix Operating System" is a great book. It belongs next to K&R on your bookshelf.

你愿意接受别人的建议吗?尽管这本书有点过时(在这本书中,“一点点”可能被大大低估),但莫里斯•巴赫(Maurice Bach)的《Unix操作系统的设计》(The Design of The Unix操作系统)是一本好书。它在你书架上K&R的旁边。

#4


1  

You might try working your way through all the examples in the book Software Tools (Amazon). Much of it is pretty pedestrian (right-justify text, de-tabify, etc.), but it's a great introduction to the Unix philosophy and basic C programming.

您可以尝试使用书籍软件工具(Amazon)中的所有示例。它的大部分都是相当普通的(右对齐的文本,去tabify等等),但是它是对Unix哲学和基本C编程的一个很好的介绍。

#5


1  

This is pretty interesting. As we know that Unix is designed by using the C language, it may not difficult to learn. If you are using the Windows OS, you can use a "Windows services for Unix" application to practice your programs. If you are using Unix, then we can use editors like vi.

这是很有趣的。正如我们所知道的,Unix是用C语言设计的,学习起来可能并不困难。如果您正在使用Windows操作系统,您可以使用“Unix的Windows服务”应用程序来实践您的程序。如果您正在使用Unix,那么我们可以使用像vi这样的编辑器。

#6


0  

I would recommend one thing highly.

我强烈推荐一件事。

Try and re-write all usual Linux command lines tools like ls, mkdir, cd etc.

尝试重新编写所有常用的Linux命令行工具,如ls、mkdir、cd等。

You'll gain a lot of knowlege about programming and Linux both at the same time.

您将同时获得许多编程和Linux方面的知识。

Pick the commands from the easiest, say "time" and work all the way up to the more complicated ones.

选择最简单的命令,说“时间”,然后一直工作到更复杂的。

#7


0  

The best way to consolidate your learnings it to practise. So just choose a kind of application that interest you and start developing it (for example, a network client/server simple application).

巩固所学的最好方法就是练习。因此,只需选择一种您感兴趣的应用程序并开始开发它(例如,网络客户机/服务器简单应用程序)。

Try to test most of the Unix APIs (files, sockets, etc.) to see how they work. You could for example get an Unix programming book, follow its chapters and test on your application everything you read, by creating your own functions. On that way, you can start to develop your own function library to be used in further projects.

尝试测试大多数Unix api(文件、套接字等),看看它们是如何工作的。例如,您可以通过创建自己的函数来获得一本Unix编程书籍,遵循它的章节并对您的应用程序进行测试。这样,您就可以开始开发自己的函数库,以便在以后的项目中使用。

#8


0  

Write a webserver.

编写一个网络服务器。

  • Make it multi-threaded.

    多线程。

  • Have it support a new scripting language you develop (a la PHP, etc.)

    让它支持你开发的一种新的脚本语言(一个la PHP,等等)

  • Allow uploads from authenticated users.

    允许通过身份验证的用户进行上传。

  • Write a plugin for your favorite tool (i.e. integrate with SVN to give a webview).

    为您喜欢的工具编写一个插件(例如,与SVN集成以提供webview)。