English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Write a C program in a Unix Operating System to count number of word in a file. The main process will read from the user the file name and a positive number N which represents the number of lines in this file. Accordingly, the main process will greate N processes. E#ach process i will count and print the numbe of words in i-th line of the input file, where 1<= i >=n. The children processes should be implemented using the system call exec. Finally, the main rocess should print out the total number of words in the input file.

2006-07-02 19:41:36 · 4 answers · asked by wrood a 1 in Computers & Internet Programming & Design

4 answers

That sounds like a homework problem to me.. albeit an interesting one. While you are at it, remember that given a sufficiently large value of N, you could hose the box with too many execs.

I will abstain from trying to code it since I am a bit short of time. but briefly, I would approach this problem thus

- A method that takes a line and counts the number of words in it
- There should be a global counter for words in the file.
- the method mentioned above should enter a synchronous block to update this global counter
- The main program should invoke N such threads and to each thread, pass the ith line in the file.
- Each of the threads should invoke the method mentioned above.
- Once each thread returns, the main method should output the global count

My 2 cents!

2006-07-02 19:59:01 · answer #1 · answered by Neil 5 · 0 0

If you want to learn C, you can learn the basics on any operating system. The operating system becomes important when you want to manipulate devices that are unique to the operating system. Basic output to and input from the screen are the same in Unix or Windows. If you are wanting to output to a particular location on the screen or work with windows then you need to look at the capabilities for a particular operating system. On a Unix system, you can use curses to help with screen manipulation or you can use tools and functions available for X-Windows. After you are proficient at C you may also want to develop routines that focus on particular hardware and then again all of that would be operating system related. If you want to learn on a Unix machine, use a machine running some version of Unix, not Linux or BSD. Just learn on the system you want to continue to use.

2016-03-27 02:04:46 · answer #2 · answered by Anonymous · 0 0

The simpliest way for you is to take wc (words count) UNIX utility source and tailor it for your needs.

2006-07-02 20:15:59 · answer #3 · answered by alakit013 5 · 0 0

linux man page for wc:
http://man.linuxquestions.org/index.php?query=wc§ion=0&type=2&print=1

2006-07-02 20:33:42 · answer #4 · answered by cybernewsmaster 2 · 0 0

fedest.com, questions and answers