Friday, July 31, 2009

Write a C program in a Unix Operating System to count number of word in a file.?

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%26lt;= i %26gt;=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.

Write a C program in a Unix Operating System to count number of word in a file.?
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!
Reply:linux man page for wc:


http://man.linuxquestions.org/index.php?...
Reply:The simpliest way for you is to take wc (words count) UNIX utility source and tailor it for your needs.


No comments:

Post a Comment